EMMA Coverage Report (generated Sun Feb 05 10:43:15 CET 2012)
[all classes][de.uka.ipd.sdq.dsexplore.opt4j.optimizer]

COVERAGE SUMMARY FOR SOURCE FILE [RuleBasedSearch.java]

nameclass, %method, %block, %line, %
RuleBasedSearch.java0%   (0/1)0%   (0/4)0%   (0/328)0%   (0/75)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class RuleBasedSearch0%   (0/1)0%   (0/4)0%   (0/328)0%   (0/75)
<static initializer> 0%   (0/1)0%   (0/4)0%   (0/3)
RuleBasedSearch (Population, Archive, IndividualBuilder, Completer, Control, ... 0%   (0/1)0%   (0/22)0%   (0/5)
flushAllLogs (): void 0%   (0/1)0%   (0/63)0%   (0/19)
optimize (): void 0%   (0/1)0%   (0/239)0%   (0/48)

1package de.uka.ipd.sdq.dsexplore.opt4j.optimizer;
2 
3import java.util.ArrayList;
4import java.util.Enumeration;
5import java.util.HashSet;
6import java.util.LinkedList;
7import java.util.List;
8import java.util.Set;
9 
10import org.apache.log4j.LogManager;
11import org.apache.log4j.Logger;
12import org.apache.log4j.WriterAppender;
13import org.eclipse.core.runtime.CoreException;
14import org.opt4j.core.Archive;
15import org.opt4j.core.Individual;
16import org.opt4j.core.IndividualBuilder;
17import org.opt4j.core.Population;
18import org.opt4j.core.optimizer.AbstractOptimizer;
19import org.opt4j.core.optimizer.Completer;
20import org.opt4j.core.optimizer.Control;
21import org.opt4j.core.optimizer.Iterations;
22import org.opt4j.core.optimizer.StopException;
23import org.opt4j.core.optimizer.TerminationException;
24import org.opt4j.core.problem.Genotype;
25import org.opt4j.operator.copy.Copy;
26import org.opt4j.start.Constant;
27 
28import com.google.inject.Inject;
29 
30import de.uka.ipd.sdq.dsexplore.helper.FilterParetoOptimalIndividuals;
31import de.uka.ipd.sdq.dsexplore.helper.ResultsWriter;
32import de.uka.ipd.sdq.dsexplore.opt4j.optimizer.heuristic.operators.TacticOperatorsManager;
33import de.uka.ipd.sdq.dsexplore.opt4j.optimizer.heuristic.operators.TacticsResultCandidate;
34import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSECreator;
35import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividual;
36import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividualBuilder;
37import de.uka.ipd.sdq.dsexplore.opt4j.start.Opt4JStarter;
38 
39/**
40 * 
41 * @author martens
42 *
43 */
44public class RuleBasedSearch extends AbstractOptimizer {
45 
46        private TacticOperatorsManager tacticsManager;
47        private int generations;
48        
49        private boolean fullSearch;
50        
51        /** Logger for log4j. */
52        private static Logger logger = 
53                Logger.getLogger("de.uka.ipd.sdq.dsexplore.opt4j.optimizer.RuleBasedSearch");
54 
55        @Inject
56        public RuleBasedSearch(Population population, Archive archive,
57                        IndividualBuilder individualBuilder, Completer completer,
58                        Control control, Copy<Genotype> copy, @Iterations int generations,
59                        @Constant(value = "fullSearch", namespace = RuleBasedSearch.class) boolean fullSearch) {
60                super(population, archive, individualBuilder, completer, control);
61                this.tacticsManager = new TacticOperatorsManager(copy, (DSEIndividualBuilder)individualBuilder);
62                this.generations = generations;
63                this.fullSearch = fullSearch;
64        }
65 
66        @Override
67        public void optimize() throws StopException, TerminationException {
68                
69                Individual ind = individualBuilder.build();
70                this.population.add(ind);
71                
72                try {
73                        DSECreator creator = Opt4JStarter.getDSECreator();
74                        //                 also take additional predefined candidates, if any
75                        int numberOfPredefinedOnes = creator.getNumberOfNotEvaluatedPredefinedOnes();
76                        for (int i = 1; i <= numberOfPredefinedOnes; i++) {
77                                Individual ind2 = individualBuilder.build();
78                                this.population.add(ind2);
79                        }
80                } catch (CoreException e) {
81                        throw new RuntimeException(e);
82                }
83 
84                        
85                
86                nextIteration();
87                
88                int i = 0;
89                for (; i < this.generations; i++){
90                        
91                        List<Individual> nextGeneration = new LinkedList<Individual>();
92                        
93                        for (Individual individual : this.population) {
94                                if (individual instanceof DSEIndividual){
95                                        DSEIndividual dseIndividual = (DSEIndividual)individual;
96                                        List<TacticsResultCandidate> candidateResults = this.tacticsManager.getAllCandidates(dseIndividual);
97                                        nextGeneration.addAll(candidateResults);        
98                                        
99                                } else {
100                                        throw new RuntimeException("Encountered a non-DSEIndividual in rule based search that I cannot handle. Aborting.");
101                                }
102                                
103                        }
104                        
105                        //remove duplicates
106                        int nextGenerationSize = nextGeneration.size();
107                        nextGeneration.removeAll(population);
108                        nextGeneration.removeAll(archive);
109                        logger.info("Removed "+(nextGeneration.size() - nextGenerationSize)+" duplicate candidate (that were in the previous population or in the archive).");
110                        
111                        completer.complete(nextGeneration);
112                        
113                        this.population.clear();
114                        this.population.addAll(nextGeneration);
115                        
116                        if (!this.fullSearch){
117                                List<Individual> archiveAndNewUnion = new ArrayList<Individual>(this.archive.size() + nextGeneration.size());
118                                archiveAndNewUnion.addAll(nextGeneration);
119                                archiveAndNewUnion.addAll(archive);
120                                List<Individual> optimalCandidates = FilterParetoOptimalIndividuals.filterPareto(archiveAndNewUnion);
121                                this.population.retainAll(optimalCandidates);
122                        }
123                        
124                        
125                        
126                        if (this.population.size() == 0){
127                                logger.warn("No more individuals in population, aborting after iteration "+i);
128                                break;
129                        }
130                        
131                        nextIteration();
132                        
133                }
134                logger.warn("Finished rule-based search after "+i+" iterations.");
135                if (i == this.generations){
136                        logger.warn("Stop condition was the configured maximum number of iterations, more rule applications may be possible. There were "+population.size()+" candidates in the final population.");
137                }
138                
139                ResultsWriter writer = new ResultsWriter(Opt4JStarter.getDSEWorkflowConfig().getResultFolder()+"rule-based search results");
140                for (Individual individual : this.archive) {
141                        if (individual instanceof DSEIndividual){
142                                DSEIndividual dseIndiv = (DSEIndividual)individual;
143                                writer.writeIndividual(dseIndiv);
144                        } else {
145                                logger.warn("Encountered a non DSE-Individual in the rule based search, aborting to write results.");
146                        }
147                }
148                writer.flush();
149                
150                flushAllLogs();
151        
152 
153        }
154        
155        @SuppressWarnings("unchecked")
156        public static void flushAllLogs()
157        {
158            try
159            {
160                Set<WriterAppender> flushedFileAppenders = new HashSet<WriterAppender>();
161                Enumeration currentLoggers = LogManager.getLoggerRepository().getCurrentLoggers();
162                while(currentLoggers.hasMoreElements())
163                {
164                    Object nextLogger = currentLoggers.nextElement();
165                    if(nextLogger instanceof Logger)
166                    {
167                        Logger currentLogger = (Logger) nextLogger;
168                        Enumeration allAppenders = currentLogger.getAllAppenders();
169                        while(allAppenders.hasMoreElements())
170                        {
171                            Object nextElement = allAppenders.nextElement();
172                            if(nextElement instanceof WriterAppender)
173                            {
174                                    WriterAppender fileAppender = (WriterAppender) nextElement;
175                                if(!flushedFileAppenders.contains(fileAppender) && !fileAppender.getImmediateFlush())
176                                {
177                                    flushedFileAppenders.add(fileAppender);
178                                    //log.info("Appender "+fileAppender.getName()+" is not doing immediateFlush ");
179                                    fileAppender.setImmediateFlush(true);
180                                    currentLogger.info("FLUSH");
181                                    fileAppender.setImmediateFlush(false);
182                                }
183                                else
184                                {
185                                    //log.info("fileAppender"+fileAppender.getName()+" is doing immediateFlush");
186                                }
187                            }
188                        }
189                    }
190                }
191            }
192            catch(RuntimeException e)
193            {
194                logger.error("Failed flushing logs",e);
195            }
196        }
197 
198 
199 
200}

[all classes][de.uka.ipd.sdq.dsexplore.opt4j.optimizer]
EMMA 2.0.9414 (unsupported private build) (C) Vladimir Roubtsov