1 | package de.uka.ipd.sdq.dsexplore.opt4j.representation; |
2 | |
3 | import org.apache.log4j.Logger; |
4 | import org.opt4j.core.Individual; |
5 | import org.opt4j.core.IndividualStateListener; |
6 | |
7 | import de.uka.ipd.sdq.dsexplore.helper.ResultsWriter; |
8 | import de.uka.ipd.sdq.dsexplore.launch.DSEWorkflowConfiguration; |
9 | import de.uka.ipd.sdq.dsexplore.opt4j.start.Opt4JStarter; |
10 | |
11 | public class DSEIndividualStateListener implements IndividualStateListener { |
12 | |
13 | ResultsWriter writer; |
14 | |
15 | /** Logger for log4j. */ |
16 | private static Logger logger = |
17 | Logger.getLogger("de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividualStateListener"); |
18 | |
19 | public DSEIndividualStateListener(DSEWorkflowConfiguration dseConfig){ |
20 | this.writer = new ResultsWriter(dseConfig.getResultFolder()+"allIndividualsDirectly"); |
21 | Opt4JStarter.registerWriter(this.writer); |
22 | } |
23 | |
24 | @Override |
25 | public void inidividualStateChanged(Individual individual) { |
26 | if (individual.getState() == Individual.State.EVALUATED){ |
27 | if (individual instanceof DSEIndividual){ |
28 | this.writer.writeIndividual((DSEIndividual)individual); |
29 | } else { |
30 | logger.error("There was an illegal individual that is not instance of DSEIndividual. Ignoring it. "+individual.toString()); |
31 | } |
32 | } |
33 | |
34 | } |
35 | |
36 | @Override |
37 | protected void finalize() throws Throwable { |
38 | this.writer.close(); |
39 | super.finalize(); |
40 | } |
41 | |
42 | |
43 | |
44 | } |