Source code: joelib/process/filter/NativeValueFilter.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Filename: $RCSfile: NativeValueFilter.java,v $
3 // Purpose: Interface definition for calling external programs from JOELib.
4 // Language: Java
5 // Compiler: JDK 1.4
6 // Authors: Joerg K. Wegner
7 // Version: $Revision: 1.7 $
8 // $Date: 2003/08/22 15:56:20 $
9 // $Author: wegner $
10 //
11 // Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation version 2 of the License.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 ///////////////////////////////////////////////////////////////////////////////
22 package joelib.process.filter;
23
24
25 /*==========================================================================*
26 * IMPORTS
27 *========================================================================== */
28 import joelib.data.JOEDataType;
29 import joelib.data.JOEGenericData;
30 import joelib.data.JOEPairData;
31
32 import joelib.desc.DescResult;
33 import joelib.desc.DescriptorException;
34 import joelib.desc.DescriptorHelper;
35 import joelib.desc.NativeValue;
36
37 import joelib.molecule.JOEMol;
38
39 import joelib.util.JOEHelper;
40
41 import org.apache.log4j.Category;
42
43
44 /*==========================================================================*
45 * INTERFACE DECLARATION
46 *========================================================================== */
47
48 /**
49 * Molecule process filter for native value descriptor entries.
50 *
51 * <p>
52 * Example:
53 * <blockquote><pre>
54 * // accept all values with: Kier_shape_1<=5.0
55 * NativeValueFilter filter =new NativeValueFilter(
56 * "Kier_shape_1",
57 * NativeValueFilter.SMALLER_EQUAL,
58 * 5.0);
59 *
60 * // create molecule
61 * JOEMol mol=new JOEMol();
62 * String smiles="c1cc(OH)cc1";
63 * if (!JOESmilesParser.smiToMol(mol, smiles, setTitle.toString()));
64 *
65 * // should we accept this molecule, what do you mean ?
66 * System.out.println("Accept: ("+filter.toString()+")="+filter.accept(mol));
67 * </pre></blockquote>
68 *
69 * @author wegnerj
70 * @license GPL
71 * @cvsversion $Revision: 1.7 $, $Date: 2003/08/22 15:56:20 $
72 */
73 public class NativeValueFilter implements Filter
74 {
75 //~ Static fields/initializers /////////////////////////////////////////////
76
77 public static final int SMALLER_EQUAL = 0;
78 public static final int SMALLER = 1;
79 public static final int EQUAL = 2;
80 public static final int GREATER_EQUAL = 3;
81 public static final int GREATER = 4;
82 public static final int NOT_EQUAL = 5;
83 private static final String[] allowedRules = new String[]
84 {
85 "<=", "<", "==", ">=", ">", "!="
86 };
87
88 /*-------------------------------------------------------------------------*
89 * private static member variables
90 *------------------------------------------------------------------------- */
91
92 // Obtain a suitable logger.
93 private static Category logger = Category.getInstance(
94 "joelib.process.filter.NativeValueFilter");
95
96 //~ Instance fields ////////////////////////////////////////////////////////
97
98 private FilterInfo info;
99
100 /*-------------------------------------------------------------------------*
101 * private static member variables
102 *------------------------------------------------------------------------- */
103 private String attribute;
104 private double value;
105 private int relation;
106
107 //~ Constructors ///////////////////////////////////////////////////////////
108
109 /*-------------------------------------------------------------------------*
110 * constructor
111 *------------------------------------------------------------------------- */
112
113 /**
114 * Constructor for the DescriptorFilter object
115 */
116 public NativeValueFilter()
117 {
118 }
119
120 /**
121 * Constructor for the DescriptorFilter object
122 *
123 * @param descNamesURL Description of the Parameter
124 */
125 public NativeValueFilter(String _attribute, int _relation, double _value)
126 {
127 init(_attribute, _relation, _value);
128 }
129
130 //~ Methods ////////////////////////////////////////////////////////////////
131
132 /**
133 * Sets the filterInfo attribute of the DescriptorFilter object
134 *
135 * @param _info The new filterInfo value
136 */
137 public void setFilterInfo(FilterInfo _info)
138 {
139 info = _info;
140 }
141
142 /*-------------------------------------------------------------------------*
143 * public methods
144 *------------------------------------------------------------------------- */
145
146 /**
147 * Gets the processInfo attribute of the DescriptorFilter object
148 *
149 * @return The processInfo value
150 */
151 public FilterInfo getFilterInfo()
152 {
153 return info;
154 }
155
156 /**
157 * Description of the Method
158 *
159 * @param mol Description of the Parameter
160 * @return Description of the Return Value
161 */
162 public boolean accept(JOEMol mol)
163 {
164 if (attribute == null)
165 {
166 logger.warn("No data attribute defined in " +
167 this.getClass().getName() + ".");
168
169 return false;
170 }
171
172 double tmpDbl = 0.0;
173
174 // get parsed descriptor from molecule
175 JOEGenericData genericData = mol.getData(attribute, true);
176
177 // try to calculate if descriptor entry is not available
178 if (genericData == null)
179 {
180 DescResult result;
181
182 try
183 {
184 result = DescriptorHelper.instance().descFromMol(mol, attribute);
185 }
186 catch (DescriptorException ex)
187 {
188 // don't accept if descriptor can not be calculated
189 return false;
190 }
191
192 if (JOEHelper.hasInterface(result, "NativeValue"))
193 {
194 // get native descriptor value
195 tmpDbl = ((NativeValue) result).getDoubleNV();
196 }
197 else
198 {
199 logger.warn("Descriptor '" + attribute +
200 "' must be a native descriptor value.");
201
202 return false;
203 }
204 }
205
206 // use descriptor in molecule
207 else
208 {
209 //check for native value descriptors
210 if (genericData.getDataType() == JOEDataType.JOE_PAIR_DATA)
211 {
212 JOEPairData pairData = (JOEPairData) genericData;
213
214 if (JOEHelper.hasInterface(pairData, "NativeValue"))
215 {
216 // get native descriptor value
217 tmpDbl = ((NativeValue) pairData).getDoubleNV();
218 }
219 else
220 {
221 logger.warn("Descriptor '" + attribute +
222 "' must be a native descriptor value.");
223
224 return false;
225 }
226 }
227 else
228 {
229 logger.warn("Descriptor '" + attribute +
230 "' must be of type JOE_PAIR_DATA.");
231
232 return false;
233 }
234 }
235
236 boolean result = false;
237
238 switch (relation)
239 {
240 case SMALLER:
241 result = (tmpDbl < value);
242
243 break;
244
245 case SMALLER_EQUAL:
246 result = (tmpDbl <= value);
247
248 break;
249
250 case EQUAL:
251 result = (tmpDbl == value);
252
253 break;
254
255 case GREATER_EQUAL:
256 result = (tmpDbl >= value);
257
258 break;
259
260 case GREATER:
261 result = (tmpDbl > value);
262
263 break;
264
265 case NOT_EQUAL:
266 result = (tmpDbl != value);
267
268 break;
269 }
270
271 return result;
272 }
273
274 public boolean fromString(String rule)
275 {
276 int index;
277 boolean parsed = false;
278
279 for (int i = 0; i < allowedRules.length; i++)
280 {
281 if ((index = rule.indexOf(allowedRules[i])) != -1)
282 {
283 relation = i;
284
285 //System.out.println("relation: "+allowedRules[i]);
286 attribute = rule.substring(0, index);
287
288 String tmp = rule.substring(index + allowedRules[i].length());
289
290 try
291 {
292 value = Double.parseDouble(tmp);
293 }
294 catch (NumberFormatException ex)
295 {
296 logger.error("Invalid number: " + tmp);
297 logger.error("in rule: " + rule);
298
299 return false;
300 }
301
302 parsed = true;
303
304 break;
305 }
306 }
307
308 if (!parsed)
309 {
310 StringBuffer sb = new StringBuffer(30);
311 sb.append("Allowed rules must contain: ");
312
313 for (int i = 0; i < allowedRules.length; i++)
314 {
315 sb.append("'");
316 sb.append(allowedRules[i]);
317 sb.append("'");
318
319 if (i < (allowedRules.length - 2))
320 {
321 sb.append(", ");
322 }
323
324 if (i < (allowedRules.length - 1))
325 {
326 sb.append(" or ");
327 }
328 }
329
330 logger.error(sb.toString());
331 }
332
333 return true;
334 }
335
336 /**
337 * Description of the Method
338 *
339 * @param _descNames Description of the Parameter
340 */
341 public void init(String _attribute, int _relation, double _value)
342 {
343 attribute = _attribute;
344 relation = _relation;
345 value = _value;
346 }
347
348 public void invertRelation()
349 {
350 switch (relation)
351 {
352 case SMALLER:
353 relation = GREATER_EQUAL;
354
355 break;
356
357 case SMALLER_EQUAL:
358 relation = GREATER;
359
360 break;
361
362 case EQUAL:
363 relation = NOT_EQUAL;
364
365 break;
366
367 case GREATER_EQUAL:
368 relation = SMALLER;
369
370 break;
371
372 case GREATER:
373 relation = SMALLER_EQUAL;
374
375 break;
376
377 case NOT_EQUAL:
378 relation = EQUAL;
379
380 break;
381 }
382 }
383
384 public String toString()
385 {
386 StringBuffer sb = new StringBuffer(30);
387 sb.append(attribute);
388 sb.append(allowedRules[relation]);
389
390 /* switch (relation)
391 {
392 case SMALLER :
393 sb.append("<");
394 break;
395 case SMALLER_EQUAL :
396 sb.append("<=");
397 break;
398 case EQUAL :
399 sb.append("==");
400 break;
401 case GREATER_EQUAL :
402 sb.append(">=");
403 break;
404 case GREATER :
405 sb.append(">");
406 break;
407 case NOT_EQUAL :
408 sb.append("!=");
409 break;
410 }
411 */
412 sb.append(value);
413
414 return sb.toString();
415 }
416 }
417 ///////////////////////////////////////////////////////////////////////////////
418 // END OF FILE.
419 ///////////////////////////////////////////////////////////////////////////////