$darkmode
DENOPTIM
DescriptorForFitness.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.fitness;
20
21import java.lang.reflect.Constructor;
22import java.util.ArrayList;
23import java.util.List;
24
25import org.openscience.cdk.DefaultChemObjectBuilder;
26import org.openscience.cdk.IImplementationSpecification;
27import org.openscience.cdk.interfaces.IChemObjectBuilder;
28import org.openscience.cdk.qsar.IDescriptor;
29
30import denoptim.exception.DENOPTIMException;
31
38{
42 protected String className;
43
47 protected IDescriptor implementation;
48
52 protected String shortName;
53
57 protected List<Variable> variables = new ArrayList<Variable>();
58
64 protected int resultId = 0;
65
69 protected String[] dictClasses;
70
74 protected String dictDefinition;
75
79 protected String dictTitle;
80
84 private static IChemObjectBuilder cdkBuilder =
85 DefaultChemObjectBuilder.getInstance();
86
87//------------------------------------------------------------------------------
88
90 IDescriptor implementation, int resultId)
91 {
92 this.shortName = shortName;
93 this.className = className;
94 this.implementation = implementation;
95 this.resultId = resultId;
96 }
97
98//------------------------------------------------------------------------------
99
101 IDescriptor implementation, int resultId,
102 String[] dictClasses, String dictDefinition, String dictTitle)
103 {
105 this.dictClasses = dictClasses;
106 this.dictDefinition = dictDefinition;
107 this.dictTitle = dictTitle;
108 }
109
110//------------------------------------------------------------------------------
111
112 public String getShortName()
113 {
114 return shortName;
115 }
116
117//------------------------------------------------------------------------------
118
123 public void setVariables(List<Variable> variables)
124 {
125 this.variables = variables;
126 }
127
128//------------------------------------------------------------------------------
129
134 public List<Variable> getVariables()
135 {
136 return variables;
137 }
138
139//------------------------------------------------------------------------------
140
141 public String getClassName()
142 {
143 return className;
144 }
145
146//------------------------------------------------------------------------------
147
148 public IDescriptor getImplementation()
149 {
150 return implementation;
151 }
152
153//------------------------------------------------------------------------------
154
155 public String getDictTitle()
156 {
157 return dictTitle;
158 }
159
160//------------------------------------------------------------------------------
161
162 public String getDictDefinition()
163 {
164 return dictDefinition;
165 }
166
167//------------------------------------------------------------------------------
168
169 public String[] getDictClasses()
170 {
171 return dictClasses;
172 }
173
174//------------------------------------------------------------------------------
175
176 @Override
177 public String toString()
178 {
179 StringBuilder sb = new StringBuilder();
180 sb.append("DescriptorForFitness [shortName:").append(shortName);
181 sb.append(", variables:[");
182 for (Variable v : variables)
183 {
184 sb.append(v.getName() + ", ");
185 }
186 sb.append("]");
187 sb.append(", className:").append(className);
188 sb.append(", resultId:").append(resultId);
189 IImplementationSpecification specs = implementation.getSpecification();
190 sb.append(", specReference:").append(
191 specs.getSpecificationReference());
192 sb.append(", implTitle:").append(
193 specs.getImplementationTitle());
194 sb.append(", implId:").append(
195 specs.getImplementationIdentifier());
196 sb.append(", implVendor:").append(
197 specs.getImplementationVendor()).append("]");
198 return sb.toString();
199 }
200
201//------------------------------------------------------------------------------
202
206 public String getDictString()
207 {
208 String NL = System.getProperty("line.separator");
209 StringBuilder sb = new StringBuilder();
210 sb.append("Titile: ").append(dictTitle).append(NL);
211 sb.append("Definition: ").append(dictDefinition).append(NL);
212 sb.append("Classes: ");
213 if (dictClasses != null)
214 {
215 for (String c : dictClasses)
216 {
217 sb.append(c).append(" ");
218 }
219 }
220 sb.append(NL);
221
222 return sb.toString();
223 }
224
225//------------------------------------------------------------------------------
226
236 {
240 dff.setVariables(new ArrayList<Variable>(variables));
241 return dff;
242 }
243
244//------------------------------------------------------------------------------
245
252 {
254 clone.implementation = newDescriptorImplementation(this);
255 return clone;
256 }
257
258//------------------------------------------------------------------------------
259
260 private static IDescriptor newDescriptorImplementation(
262 {
263 String className = oldParent.getImplementation().getClass().getName();
264 IDescriptor descriptor = null;
265 try
266 {
267 Class<?> cl = Class.forName(className);
268 for (Constructor<?> constructor : cl.getConstructors())
269 {
270 Class<?>[] params = constructor.getParameterTypes();
271 if (params.length == 0)
272 {
273 descriptor = (IDescriptor) constructor.newInstance();
274 } else if (params[0].equals(IChemObjectBuilder.class))
275 {
276 //NB potential source of ambiguity on the builder class
277 descriptor = (IDescriptor) constructor.newInstance(cdkBuilder);
278 }
279 }
280 } catch (Throwable t)
281 {
282 throw new DENOPTIMException("Could not make new instance of '"
283 + className + "'.", t);
284 }
285 if (descriptor == null)
286 {
287 throw new DENOPTIMException("Could not make new instance of '"
288 + className + "'. No suitable constructor found.");
289 }
290 descriptor.initialise(cdkBuilder);
291 return descriptor;
292 }
293
294//------------------------------------------------------------------------------
295
302 {
303 variables.add(v);
304 }
305
306//------------------------------------------------------------------------------
307
308}
This is a reference to a specific descriptor value.
IDescriptor implementation
Implementation of the descriptor's calculator.
DescriptorForFitness makeCopy()
Copy this descriptor and created an independent instance of the underlying descriptor implementation.
String dictTitle
The title of descriptor as define in the descriptor dictionary.
List< Variable > variables
Variables that use values calculated by this descriptor.
int resultId
Pointer to a specific results among those that are produced by the calculation of this descriptor,...
void setVariables(List< Variable > variables)
Overwrites the list of variables using this descriptor.
DescriptorForFitness(String shortName, String className, IDescriptor implementation, int resultId, String[] dictClasses, String dictDefinition, String dictTitle)
String className
ClassName pointing to the implementation of this descriptor's calculator.
static IDescriptor newDescriptorImplementation(DescriptorForFitness oldParent)
static IChemObjectBuilder cdkBuilder
Utility for constructing CDK objects.
String getDictString()
Utility only meant to print some info.
DescriptorForFitness(String shortName, String className, IDescriptor implementation, int resultId)
List< Variable > getVariables()
Get the variables that make use of values produced by this descriptor.
DescriptorForFitness cloneAllButImpl()
This is a sort of cloning that returns a new DescriptorForFitness with the same field content of this...
String[] dictClasses
The class(es) of descriptor as define in the descriptor dictionary.
void addDependentVariable(Variable v)
Append the reference to a variable that used data produced by the calculation of this descriptor.
String dictDefinition
The Definition of descriptor as define in the descriptor dictionary.
String shortName
Descriptor short name.
A variable in the expression defining the fitness.
Definition: Variable.java:42