source: trunk/CrossPare/src/de/ugoe/cs/cpdp/ExperimentConfiguration.java @ 32

Last change on this file since 32 was 32, checked in by ftrautsch, 9 years ago

integrating decent into crosspare

  • Property svn:mime-type set to text/plain
File size: 18.9 KB
Line 
1package de.ugoe.cs.cpdp;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.InputStreamReader;
8import java.io.UnsupportedEncodingException;
9import java.util.LinkedList;
10import java.util.List;
11import java.util.logging.Level;
12
13import javax.xml.parsers.ParserConfigurationException;
14import javax.xml.parsers.SAXParser;
15import javax.xml.parsers.SAXParserFactory;
16
17import org.xml.sax.Attributes;
18import org.xml.sax.InputSource;
19import org.xml.sax.SAXException;
20import org.xml.sax.helpers.DefaultHandler;
21
22import de.ugoe.cs.cpdp.dataprocessing.IProcessesingStrategy;
23import de.ugoe.cs.cpdp.dataprocessing.ISetWiseProcessingStrategy;
24import de.ugoe.cs.cpdp.dataselection.IPointWiseDataselectionStrategy;
25import de.ugoe.cs.cpdp.dataselection.ISetWiseDataselectionStrategy;
26import de.ugoe.cs.cpdp.eval.IEvaluationStrategy;
27import de.ugoe.cs.cpdp.loader.IVersionLoader;
28import de.ugoe.cs.cpdp.training.ISetWiseTrainingStrategy;
29import de.ugoe.cs.cpdp.training.ITrainingStrategy;
30import de.ugoe.cs.cpdp.versions.IVersionFilter;
31import de.ugoe.cs.util.StringTools;
32import de.ugoe.cs.util.console.Console;
33
34/**
35 * Class that contains all meta information about an experiment, i.e., its configuration. The configuration is loaded from an XML file.
36 * <br><br>
37 * In the current implementation, the experiment configuration can only be created using an XML file. Programmatic creation of experiment configurations is currently not possibly.
38 * @author Steffen Herbold
39 */
40public class ExperimentConfiguration  extends DefaultHandler {
41
42        /**
43         * handle of the file that contains the configuration
44         */
45        private final File configFile;
46       
47        /**
48         * name of the experiment (automatically set to the file name without the .xml ending)
49         */
50        private String experimentName = "exp";
51       
52        /**
53         * loads instances
54         */
55        private List<IVersionLoader> loaders;
56       
57        /**
58         * path were the results of the experiments are stored
59         */
60        private String resultsPath = "results";
61       
62        /**
63         * data set filters applied to all data
64         */
65        private List<IVersionFilter> versionFilters;
66       
67        /**
68         * data set filters that decide if a data set is used as test data
69         */
70        private List<IVersionFilter> testVersionFilters;
71       
72        /**
73         * data set filters that decide if a data is used as candidate training data
74         */
75        private List<IVersionFilter> trainingVersionFilters;
76       
77        /**
78         * setwise data processors that are applied before the setwise data selection
79         */
80        private List<ISetWiseProcessingStrategy> setwisepreprocessors;
81       
82        /**
83         * setwise data selection strategies
84         */
85        private List<ISetWiseDataselectionStrategy> setwiseselectors;
86       
87        /**
88         * setwise data processors that are applied after the setwise data selection
89         */
90        private List<ISetWiseProcessingStrategy> setwisepostprocessors;
91       
92        /**
93         * setwise trainers, i.e., trainers that require the selected training data to be separate from each other
94         */
95        private List<ISetWiseTrainingStrategy> setwiseTrainers;
96       
97        /**
98         * data processors that are applied before the pointwise data selection
99         */
100        private List<IProcessesingStrategy> preprocessors;
101       
102        /**
103         * pointwise data selection strategies
104         */
105        private List<IPointWiseDataselectionStrategy> pointwiseselectors;
106       
107        /**
108         * data processors that are applied before the pointwise data selection
109         */
110        private List<IProcessesingStrategy> postprocessors;
111       
112        /**
113         * normal trainers, i.e., trainers that require the selected training data in a single data set
114         */
115        private List<ITrainingStrategy> trainers;
116       
117        /**
118         * evaluators used for the the experiment results
119         */
120        private List<IEvaluationStrategy> evaluators;
121       
122        /**
123         * indicates, if the classifier should be saved
124         */
125        private Boolean saveClassifier = null;
126       
127        /**
128         * indicates, which execution strategy to choose
129         * (e.g. CrossProjectExperiment, ClassifierCreationExecution).
130         * Default is CrossProjectExperiment.
131         */
132        private String executionStrategy = "CrossProjectExperiment";
133       
134        /**
135         * Constructor. Creates a new configuration from a given file.
136         * @param filename name of the file from the configuration is loaded.
137         * @throws ExperimentConfigurationException thrown if there is an error creating the configuration
138         */
139        public ExperimentConfiguration(String filename) throws ExperimentConfigurationException {
140                this(new File(filename));
141        }
142       
143        /**
144         * Constructor. Creates a new configuration from a given file.
145         * @param filename handle of the file from the configuration is loaded.
146         * @throws ExperimentConfigurationException thrown if there is an error creating the configuration
147         */
148        public ExperimentConfiguration(File file) throws ExperimentConfigurationException {
149                loaders = new LinkedList<>();
150                versionFilters = new LinkedList<>();
151                testVersionFilters = new LinkedList<>();
152                trainingVersionFilters = new LinkedList<>();
153                setwisepreprocessors = new LinkedList<>();
154                setwiseselectors = new LinkedList<>();
155                setwisepostprocessors = new LinkedList<>();
156                setwiseTrainers = new LinkedList<>();
157                preprocessors = new LinkedList<>();
158                pointwiseselectors = new LinkedList<>();
159                postprocessors = new LinkedList<>();           
160                trainers = new LinkedList<>();
161                evaluators = new LinkedList<>();
162               
163                if (file == null) {
164            throw new IllegalArgumentException("file must not be null");
165        }
166                if (file.isDirectory()) {
167                        throw new IllegalArgumentException("file must not be a directory");
168                }
169                configFile = file;
170               
171                experimentName = file.getName().split("\\.")[0];
172
173        final SAXParserFactory spf = SAXParserFactory.newInstance();
174        spf.setValidating(true);
175
176        SAXParser saxParser = null;
177        InputSource inputSource = null;
178        try {
179                        saxParser = spf.newSAXParser();
180                } catch (ParserConfigurationException | SAXException e) {
181                        throw new ExperimentConfigurationException(e);
182                }
183               
184        InputStreamReader reader = null;
185                try {
186                        reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
187                        inputSource = new InputSource(reader);
188                } catch (UnsupportedEncodingException | FileNotFoundException e) {
189                        throw new ExperimentConfigurationException("Could not open configuration file.", e);
190                }
191               
192        if (inputSource != null) {
193            inputSource.setSystemId("file://" + file.getAbsolutePath());
194                        try {
195                                saxParser.parse(inputSource, this);
196                        } catch (SAXException | IOException e) {
197                                throw new ExperimentConfigurationException("Error parsing configuration.", e);
198                        }
199                }
200        if( reader!=null ) {
201                try {
202                                reader.close();
203                        } catch (IOException e) {
204                                throw new ExperimentConfigurationException("Error closing reader.", e);
205                        }
206        }
207        }
208       
209        /**
210         * returns the name of the experiment
211         * @return name of the experiment
212         */
213        public String getExperimentName() {
214                return experimentName;
215        }
216       
217        /**
218         * returns the loaders for instances
219         * @return data loaders
220         */
221        public List<IVersionLoader> getLoaders() {
222                return loaders;
223        }
224       
225        /**
226         * returns the results path
227         * @return results path
228         */
229        public String getResultsPath() {
230                return resultsPath;
231        }
232       
233        /**
234         * returns the data set filters of the experiment
235         * @return data set filters of the experiment
236         */
237        public List<IVersionFilter> getVersionFilters() {
238                return versionFilters;
239        }
240       
241        /**
242         * returns the test set filters of the experiment
243         * @return test set filters of the experiment
244         */
245        public List<IVersionFilter> getTestVersionFilters() {
246                return testVersionFilters;
247        }
248       
249        /**
250         * returns the candidate training version filters of the experiment
251         * @return candidate training version filters of the experiment
252         */
253        public List<IVersionFilter> getTrainingVersionFilters() {
254                return trainingVersionFilters;
255        }
256       
257        /**
258         * returns the setwise processors applied before the setwise data selection
259         * @return setwise processors applied before the setwise data selection
260         */
261        public List<ISetWiseProcessingStrategy> getSetWisePreprocessors() {
262                return setwisepreprocessors;
263        }
264       
265        /**
266         * returns the setwise data selection strategies
267         * @return setwise data selection strategies
268         */
269        public List<ISetWiseDataselectionStrategy> getSetWiseSelectors() {
270                return setwiseselectors;
271        }
272       
273        /**
274         * returns the setwise processors applied after the setwise data selection
275         * @return setwise processors applied after the setwise data selection
276         */
277        public List<ISetWiseProcessingStrategy> getSetWisePostprocessors() {
278                return setwisepostprocessors;
279        }
280       
281        /**
282         * returns the setwise training algorithms
283         * @return setwise training algorithms
284         */
285        public List<ISetWiseTrainingStrategy> getSetWiseTrainers() {
286                return setwiseTrainers;
287        }
288       
289        /**
290         * returns the processors applied before the pointwise data selection
291         * @return processors applied before the pointwise data selection
292         */
293        public List<IProcessesingStrategy> getPreProcessors() {
294                return preprocessors;
295        }
296       
297        /**
298         * returns the pointwise data selection strategies
299         * @return pointwise data selection strategies
300         */
301        public List<IPointWiseDataselectionStrategy> getPointWiseSelectors() {
302                return pointwiseselectors;
303        }
304       
305        /**
306         * returns the processors applied after the pointwise data selection
307         * @return processors applied after the pointwise data selection
308         */
309        public List<IProcessesingStrategy> getPostProcessors() {
310                return postprocessors;
311        }
312       
313        /**
314         * returns the normal training algorithm
315         * @return normal training algorithms
316         */
317        public List<ITrainingStrategy> getTrainers() {
318                return trainers;
319        }
320       
321        /**
322         * returns the evaluation strategies
323         * @return evaluation strategies
324         */
325        public List<IEvaluationStrategy> getEvaluators() {
326                return evaluators;
327        }
328       
329        /**
330         * returns boolean, if classifier should be saved
331         * @return boolean
332         */
333        public boolean getSaveClassifier() {
334                return saveClassifier;
335        }
336       
337        /**
338         * returns the execution strategy
339         * @return String execution strategy
340         */
341        public String getExecutionStrategy() {
342                return executionStrategy;
343        }
344       
345        /* (non-Javadoc)
346         * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
347         */
348        @Override
349        public void startElement(String uri, String localName, String qName,
350                        Attributes attributes) throws SAXException {
351                try {
352                        if( qName.equals("config") ) {
353                                // ingore
354                        }
355                        else if( qName.equals("loader") ) {
356                                final IVersionLoader loader = (IVersionLoader) Class.forName("de.ugoe.cs.cpdp.loader." + attributes.getValue("name")).newInstance();
357                                loader.setLocation(attributes.getValue("datalocation"));
358                                loaders.add(loader);
359                               
360                                // TODO location as relative
361                        }
362                        else if( qName.equals("resultspath") ) {
363                                resultsPath = attributes.getValue("path");
364                        }
365                        else if( qName.equals("versionfilter") ) {
366                                final IVersionFilter filter = (IVersionFilter) Class.forName("de.ugoe.cs.cpdp.versions." + attributes.getValue("name")).newInstance();
367                                filter.setParameter(attributes.getValue("param"));
368                                versionFilters.add(filter);
369                        }
370                        else if( qName.equals("testVersionfilter") ) {
371                                final IVersionFilter filter = (IVersionFilter) Class.forName("de.ugoe.cs.cpdp.versions." + attributes.getValue("name")).newInstance();
372                                filter.setParameter(attributes.getValue("param"));
373                                testVersionFilters.add(filter);
374                        }
375                        else if( qName.equals("trainVersionfilter") ) {
376                                final IVersionFilter filter = (IVersionFilter) Class.forName("de.ugoe.cs.cpdp.versions." + attributes.getValue("name")).newInstance();
377                                filter.setParameter(attributes.getValue("param"));
378                                trainingVersionFilters.add(filter);
379                        }
380                        else if( qName.equals("setwisepreprocessor") ) {
381                                final ISetWiseProcessingStrategy processor = (ISetWiseProcessingStrategy) Class.forName("de.ugoe.cs.cpdp.dataprocessing." + attributes.getValue("name")).newInstance();
382                                processor.setParameter(attributes.getValue("param"));
383                                setwisepreprocessors.add(processor);
384                        }
385                        else if( qName.equals("setwiseselector") ) {
386                                final ISetWiseDataselectionStrategy selection = (ISetWiseDataselectionStrategy) Class.forName("de.ugoe.cs.cpdp.dataselection." +  attributes.getValue("name")).newInstance();
387                                selection.setParameter(attributes.getValue("param"));
388                                setwiseselectors.add(selection);
389                        }
390                        else if( qName.equals("setwisepostprocessor") ) {
391                                final ISetWiseProcessingStrategy processor = (ISetWiseProcessingStrategy) Class.forName("de.ugoe.cs.cpdp.dataprocessing." + attributes.getValue("name")).newInstance();
392                                processor.setParameter(attributes.getValue("param"));
393                                setwisepostprocessors.add(processor);
394                        }
395                        else if( qName.equals("setwisetrainer") ) {
396                                final ISetWiseTrainingStrategy trainer = (ISetWiseTrainingStrategy) Class.forName("de.ugoe.cs.cpdp.training." +  attributes.getValue("name")).newInstance();
397                                trainer.setParameter(attributes.getValue("param"));
398                                setwiseTrainers.add(trainer);
399                        }
400                        else if( qName.equals("preprocessor") ) {
401                                final IProcessesingStrategy processor = (IProcessesingStrategy) Class.forName("de.ugoe.cs.cpdp.dataprocessing." +  attributes.getValue("name")).newInstance();
402                                processor.setParameter( attributes.getValue("param"));
403                                preprocessors.add(processor);
404                        }
405                        else if( qName.equals("pointwiseselector") ) {
406                                final IPointWiseDataselectionStrategy selection = (IPointWiseDataselectionStrategy) Class.forName("de.ugoe.cs.cpdp.dataselection." +  attributes.getValue("name")).newInstance();
407                                selection.setParameter( attributes.getValue("param"));
408                                pointwiseselectors.add(selection);
409                        }
410                        else if( qName.equals("postprocessor") ) {
411                                final IProcessesingStrategy processor = (IProcessesingStrategy) Class.forName("de.ugoe.cs.cpdp.dataprocessing." +  attributes.getValue("name")).newInstance();
412                                processor.setParameter( attributes.getValue("param"));
413                                postprocessors.add(processor);
414                        }
415                        else if( qName.equals("trainer") ) {
416                                final ITrainingStrategy trainer = (ITrainingStrategy) Class.forName("de.ugoe.cs.cpdp.training." +  attributes.getValue("name")).newInstance();
417                                trainer.setParameter(attributes.getValue("param"));
418                                trainers.add(trainer);
419                        }
420                        else if( qName.equals("eval") ) {
421                                final IEvaluationStrategy evaluator = (IEvaluationStrategy) Class.forName("de.ugoe.cs.cpdp.eval." + attributes.getValue("name")).newInstance();
422                                evaluators.add(evaluator);
423                        }
424                        else if( qName.equals("saveClassifier")) {
425                                saveClassifier = true;
426                        }
427                        else if( qName.equals("executionStrategy")) {
428                                executionStrategy = attributes.getValue("name");
429                        }
430                        else if( qName.equals("partialconfig") ) {
431                                String path = attributes.getValue("path");
432                                try {
433                                        boolean relative = true;
434                                        if( attributes.getValue("relative")!=null ) {
435                                                relative = Boolean.parseBoolean(attributes.getValue("relative"));
436                                        }
437                                       
438                                        if( relative ) {
439                                                path = configFile.getParentFile().getPath() + "/" + path;
440                                        }
441                                        addConfigurations(new ExperimentConfiguration(path));
442                                } catch (ExperimentConfigurationException e) {
443                                        throw new SAXException("Could not load partial configuration: " + path, e);
444                                }       
445                        } else {
446                                Console.traceln(Level.WARNING, "element in config-file " +  configFile.getName() + " ignored: " + qName);
447                        }
448                }
449        catch (NoClassDefFoundError | ClassNotFoundException | IllegalAccessException | InstantiationException | ClassCastException e) {
450                throw new SAXException("Could not initialize class correctly", (Exception) e);
451        }
452        }
453       
454        /**
455         * Adds the information of another experiment configuration to this configuration. This mechanism allows the usage of partial configuration files. The name of the other configuration is lost.
456         * <br><br>
457         * If the current data path is the empty string (&quot;&quot;), it is override by the datapath of the other configuration. Otherwise, the current data path is kept.
458         * @param other experiment whose information is added
459         * @throws ExperimentConfigurationException
460         */
461        private void addConfigurations(ExperimentConfiguration other) throws ExperimentConfigurationException {
462                if( "results".equals(resultsPath) ) {
463                        resultsPath = other.resultsPath;
464                }
465                loaders.addAll(other.loaders);
466                versionFilters.addAll(other.versionFilters);
467                testVersionFilters.addAll(other.testVersionFilters);
468                trainingVersionFilters.addAll(other.trainingVersionFilters);
469                setwisepreprocessors.addAll(other.setwisepreprocessors);
470                setwiseselectors.addAll(other.setwiseselectors);
471                setwisepostprocessors.addAll(other.setwisepostprocessors);
472                setwiseTrainers.addAll(other.setwiseTrainers);
473                preprocessors.addAll(other.preprocessors);
474                pointwiseselectors.addAll(other.pointwiseselectors);
475                postprocessors.addAll(other.postprocessors);
476                trainers.addAll(other.trainers);
477                evaluators.addAll(other.evaluators);
478               
479                if(!executionStrategy.equals(other.executionStrategy)) {
480                        throw new ExperimentConfigurationException("Executionstrategies must be the same, if config files should be added.");
481                }
482               
483                /* Only if saveClassifier is not set in the main config and
484                 * the other configs saveClassifier is true, it must be set.
485                 */
486                if(saveClassifier == null && other.saveClassifier == true) {
487                        saveClassifier = other.saveClassifier;
488                }
489
490        }
491       
492        /* (non-Javadoc)
493         * @see java.lang.Object#toString()
494         */
495        @Override
496        public String toString() {
497                final StringBuilder builder = new StringBuilder();
498                builder.append("Experiment name: " + experimentName + StringTools.ENDLINE);
499                builder.append("Loaders: " + loaders + StringTools.ENDLINE);
500                builder.append("Results path: " + resultsPath + StringTools.ENDLINE);
501                builder.append("Version filters: " + versionFilters.toString() + StringTools.ENDLINE);
502                builder.append("Test version filters: " + testVersionFilters.toString() + StringTools.ENDLINE);
503                builder.append("Training version filters: " + trainingVersionFilters.toString() + StringTools.ENDLINE);
504                builder.append("Setwise preprocessors: " + setwisepreprocessors.toString() + StringTools.ENDLINE);
505                builder.append("Setwise selectors: " + setwiseselectors.toString() + StringTools.ENDLINE);
506                builder.append("Setwise postprocessors: " + setwisepostprocessors.toString() + StringTools.ENDLINE);
507                builder.append("Setwise trainers: " + setwiseTrainers.toString() + StringTools.ENDLINE);
508                builder.append("Pointwise preprocessors: " + preprocessors.toString() + StringTools.ENDLINE);
509                builder.append("Pointwise selectors: " + pointwiseselectors.toString() + StringTools.ENDLINE);
510                builder.append("Pointwise postprocessors: " + postprocessors.toString() + StringTools.ENDLINE);
511                builder.append("Pointwise trainers: " + trainers.toString() + StringTools.ENDLINE);
512                builder.append("Evaluators: " + evaluators.toString() + StringTools.ENDLINE);
513                builder.append("Save Classifier?: " + saveClassifier + StringTools.ENDLINE);
514                builder.append("Execution Strategy: " + executionStrategy + StringTools.ENDLINE);
515                               
516                return builder.toString();
517        }
518}
Note: See TracBrowser for help on using the repository browser.