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

Last change on this file since 2 was 2, checked in by sherbold, 10 years ago
  • initial commit
  • Property svn:mime-type set to text/plain
File size: 17.4 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         * Constructor. Creates a new configuration from a given file.
124         * @param filename name of the file from the configuration is loaded.
125         * @throws ExperimentConfigurationException thrown if there is an error creating the configuration
126         */
127        public ExperimentConfiguration(String filename) throws ExperimentConfigurationException {
128                this(new File(filename));
129        }
130       
131        /**
132         * Constructor. Creates a new configuration from a given file.
133         * @param filename handle of the file from the configuration is loaded.
134         * @throws ExperimentConfigurationException thrown if there is an error creating the configuration
135         */
136        public ExperimentConfiguration(File file) throws ExperimentConfigurationException {
137                loaders = new LinkedList<>();
138                versionFilters = new LinkedList<>();
139                testVersionFilters = new LinkedList<>();
140                trainingVersionFilters = new LinkedList<>();
141                setwisepreprocessors = new LinkedList<>();
142                setwiseselectors = new LinkedList<>();
143                setwisepostprocessors = new LinkedList<>();
144                setwiseTrainers = new LinkedList<>();
145                preprocessors = new LinkedList<>();
146                pointwiseselectors = new LinkedList<>();
147                postprocessors = new LinkedList<>();           
148                trainers = new LinkedList<>();
149                evaluators = new LinkedList<>();
150               
151                if (file == null) {
152            throw new IllegalArgumentException("file must not be null");
153        }
154                if (file.isDirectory()) {
155                        throw new IllegalArgumentException("file must not be a directory");
156                }
157                configFile = file;
158               
159                experimentName = file.getName().split("\\.")[0];
160
161        final SAXParserFactory spf = SAXParserFactory.newInstance();
162        spf.setValidating(true);
163
164        SAXParser saxParser = null;
165        InputSource inputSource = null;
166        try {
167                        saxParser = spf.newSAXParser();
168                } catch (ParserConfigurationException | SAXException e) {
169                        throw new ExperimentConfigurationException(e);
170                }
171               
172        InputStreamReader reader = null;
173                try {
174                        reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
175                        inputSource = new InputSource(reader);
176                } catch (UnsupportedEncodingException | FileNotFoundException e) {
177                        throw new ExperimentConfigurationException("Could not open configuration file.", e);
178                }
179               
180        if (inputSource != null) {
181            inputSource.setSystemId("file://" + file.getAbsolutePath());
182                        try {
183                                saxParser.parse(inputSource, this);
184                        } catch (SAXException | IOException e) {
185                                throw new ExperimentConfigurationException("Error parsing configuration.", e);
186                        }
187                }
188        if( reader!=null ) {
189                try {
190                                reader.close();
191                        } catch (IOException e) {
192                                throw new ExperimentConfigurationException("Error closing reader.", e);
193                        }
194        }
195        }
196       
197        /**
198         * returns the name of the experiment
199         * @return name of the experiment
200         */
201        public String getExperimentName() {
202                return experimentName;
203        }
204       
205        /**
206         * returns the loaders for instances
207         * @return data loaders
208         */
209        public List<IVersionLoader> getLoaders() {
210                return loaders;
211        }
212       
213        /**
214         * returns the results path
215         * @return results path
216         */
217        public String getResultsPath() {
218                return resultsPath;
219        }
220       
221        /**
222         * returns the data set filters of the experiment
223         * @return data set filters of the experiment
224         */
225        public List<IVersionFilter> getVersionFilters() {
226                return versionFilters;
227        }
228       
229        /**
230         * returns the test set filters of the experiment
231         * @return test set filters of the experiment
232         */
233        public List<IVersionFilter> getTestVersionFilters() {
234                return testVersionFilters;
235        }
236       
237        /**
238         * returns the candidate training version filters of the experiment
239         * @return candidate training version filters of the experiment
240         */
241        public List<IVersionFilter> getTrainingVersionFilters() {
242                return trainingVersionFilters;
243        }
244       
245        /**
246         * returns the setwise processors applied before the setwise data selection
247         * @return setwise processors applied before the setwise data selection
248         */
249        public List<ISetWiseProcessingStrategy> getSetWisePreprocessors() {
250                return setwisepreprocessors;
251        }
252       
253        /**
254         * returns the setwise data selection strategies
255         * @return setwise data selection strategies
256         */
257        public List<ISetWiseDataselectionStrategy> getSetWiseSelectors() {
258                return setwiseselectors;
259        }
260       
261        /**
262         * returns the setwise processors applied after the setwise data selection
263         * @return setwise processors applied after the setwise data selection
264         */
265        public List<ISetWiseProcessingStrategy> getSetWisePostprocessors() {
266                return setwisepostprocessors;
267        }
268       
269        /**
270         * returns the setwise training algorithms
271         * @return setwise training algorithms
272         */
273        public List<ISetWiseTrainingStrategy> getSetWiseTrainers() {
274                return setwiseTrainers;
275        }
276       
277        /**
278         * returns the processors applied before the pointwise data selection
279         * @return processors applied before the pointwise data selection
280         */
281        public List<IProcessesingStrategy> getPreProcessors() {
282                return preprocessors;
283        }
284       
285        /**
286         * returns the pointwise data selection strategies
287         * @return pointwise data selection strategies
288         */
289        public List<IPointWiseDataselectionStrategy> getPointWiseSelectors() {
290                return pointwiseselectors;
291        }
292       
293        /**
294         * returns the processors applied after the pointwise data selection
295         * @return processors applied after the pointwise data selection
296         */
297        public List<IProcessesingStrategy> getPostProcessors() {
298                return postprocessors;
299        }
300       
301        /**
302         * returns the normal training algorithm
303         * @return normal training algorithms
304         */
305        public List<ITrainingStrategy> getTrainers() {
306                return trainers;
307        }
308       
309        /**
310         * returns the evaluation strategies
311         * @return evaluation strategies
312         */
313        public List<IEvaluationStrategy> getEvaluators() {
314                return evaluators;
315        }
316       
317        /* (non-Javadoc)
318         * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
319         */
320        @Override
321        public void startElement(String uri, String localName, String qName,
322                        Attributes attributes) throws SAXException {
323                try {
324                        if( qName.equals("config") ) {
325                                // ingore
326                        }
327                        else if( qName.equals("loader") ) {
328                                final IVersionLoader loader = (IVersionLoader) Class.forName("de.ugoe.cs.cpdp.loader." + attributes.getValue("name")).newInstance();
329                                loader.setLocation(attributes.getValue("datalocation"));
330                                loaders.add(loader);
331                               
332                                // TODO location as relative
333                        }
334                        else if( qName.equals("resultspath") ) {
335                                resultsPath = attributes.getValue("path");
336                        }
337                        else if( qName.equals("versionfilter") ) {
338                                final IVersionFilter filter = (IVersionFilter) Class.forName("de.ugoe.cs.cpdp.versions." + attributes.getValue("name")).newInstance();
339                                filter.setParameter(attributes.getValue("param"));
340                                versionFilters.add(filter);
341                        }
342                        else if( qName.equals("testVersionfilter") ) {
343                                final IVersionFilter filter = (IVersionFilter) Class.forName("de.ugoe.cs.cpdp.versions." + attributes.getValue("name")).newInstance();
344                                filter.setParameter(attributes.getValue("param"));
345                                testVersionFilters.add(filter);
346                        }
347                        else if( qName.equals("trainVersionfilter") ) {
348                                final IVersionFilter filter = (IVersionFilter) Class.forName("de.ugoe.cs.cpdp.versions." + attributes.getValue("name")).newInstance();
349                                filter.setParameter(attributes.getValue("param"));
350                                trainingVersionFilters.add(filter);
351                        }
352                        else if( qName.equals("setwisepreprocessor") ) {
353                                final ISetWiseProcessingStrategy processor = (ISetWiseProcessingStrategy) Class.forName("de.ugoe.cs.cpdp.dataprocessing." + attributes.getValue("name")).newInstance();
354                                processor.setParameter(attributes.getValue("param"));
355                                setwisepreprocessors.add(processor);
356                        }
357                        else if( qName.equals("setwiseselector") ) {
358                                final ISetWiseDataselectionStrategy selection = (ISetWiseDataselectionStrategy) Class.forName("de.ugoe.cs.cpdp.dataselection." +  attributes.getValue("name")).newInstance();
359                                selection.setParameter(attributes.getValue("param"));
360                                setwiseselectors.add(selection);
361                        }
362                        else if( qName.equals("setwisepostprocessor") ) {
363                                final ISetWiseProcessingStrategy processor = (ISetWiseProcessingStrategy) Class.forName("de.ugoe.cs.cpdp.dataprocessing." + attributes.getValue("name")).newInstance();
364                                processor.setParameter(attributes.getValue("param"));
365                                setwisepostprocessors.add(processor);
366                        }
367                        else if( qName.equals("setwisetrainer") ) {
368                                final ISetWiseTrainingStrategy trainer = (ISetWiseTrainingStrategy) Class.forName("de.ugoe.cs.cpdp.training." +  attributes.getValue("name")).newInstance();
369                                trainer.setParameter(attributes.getValue("param"));
370                                setwiseTrainers.add(trainer);
371                        }
372                        else if( qName.equals("preprocessor") ) {
373                                final IProcessesingStrategy processor = (IProcessesingStrategy) Class.forName("de.ugoe.cs.cpdp.dataprocessing." +  attributes.getValue("name")).newInstance();
374                                processor.setParameter( attributes.getValue("param"));
375                                preprocessors.add(processor);
376                        }
377                        else if( qName.equals("pointwiseselector") ) {
378                                final IPointWiseDataselectionStrategy selection = (IPointWiseDataselectionStrategy) Class.forName("de.ugoe.cs.cpdp.dataselection." +  attributes.getValue("name")).newInstance();
379                                selection.setParameter( attributes.getValue("param"));
380                                pointwiseselectors.add(selection);
381                        }
382                        else if( qName.equals("postprocessor") ) {
383                                final IProcessesingStrategy processor = (IProcessesingStrategy) Class.forName("de.ugoe.cs.cpdp.dataprocessing." +  attributes.getValue("name")).newInstance();
384                                processor.setParameter( attributes.getValue("param"));
385                                postprocessors.add(processor);
386                        }
387                        else if( qName.equals("trainer") ) {
388                                final ITrainingStrategy trainer = (ITrainingStrategy) Class.forName("de.ugoe.cs.cpdp.training." +  attributes.getValue("name")).newInstance();
389                                trainer.setParameter(attributes.getValue("param"));
390                                trainers.add(trainer);
391                        }
392                        else if( qName.equals("eval") ) {
393                                final IEvaluationStrategy evaluator = (IEvaluationStrategy) Class.forName("de.ugoe.cs.cpdp.eval." + attributes.getValue("name")).newInstance();
394                                evaluators.add(evaluator);
395                        }
396                        else if( qName.equals("partialconfig") ) {
397                                String path = attributes.getValue("path");
398                                try {
399                                        boolean relative = true;
400                                        if( attributes.getValue("relative")!=null ) {
401                                                relative = Boolean.parseBoolean(attributes.getValue("relative"));
402                                        }
403                                       
404                                        if( relative ) {
405                                                path = configFile.getParentFile().getPath() + "/" + path;
406                                        }
407                                        addConfigurations(new ExperimentConfiguration(path));
408                                } catch (ExperimentConfigurationException e) {
409                                        throw new SAXException("Could not load partial configuration: " + path, e);
410                                }       
411                        } else {
412                                Console.traceln(Level.WARNING, "element in config-file " +  configFile.getName() + " ignored: " + qName);
413                        }
414                }
415        catch (NoClassDefFoundError | ClassNotFoundException | IllegalAccessException | InstantiationException | ClassCastException e) {
416                throw new SAXException("Could not initialize class correctly", (Exception) e);
417        }
418        }
419       
420        /**
421         * 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.
422         * <br><br>
423         * 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.
424         * @param other experiment whose information is added
425         */
426        private void addConfigurations(ExperimentConfiguration other) {
427                if( "results".equals(resultsPath) ) {
428                        resultsPath = other.resultsPath;
429                }
430                loaders.addAll(other.loaders);
431                versionFilters.addAll(other.versionFilters);
432                testVersionFilters.addAll(other.testVersionFilters);
433                trainingVersionFilters.addAll(other.trainingVersionFilters);
434                setwisepreprocessors.addAll(other.setwisepreprocessors);
435                setwiseselectors.addAll(other.setwiseselectors);
436                setwisepostprocessors.addAll(other.setwisepostprocessors);
437                setwiseTrainers.addAll(other.setwiseTrainers);
438                preprocessors.addAll(other.preprocessors);
439                pointwiseselectors.addAll(other.pointwiseselectors);
440                postprocessors.addAll(other.postprocessors);
441                trainers.addAll(other.trainers);
442                evaluators.addAll(other.evaluators);
443        }
444       
445        /* (non-Javadoc)
446         * @see java.lang.Object#toString()
447         */
448        @Override
449        public String toString() {
450                final StringBuilder builder = new StringBuilder();
451                builder.append("Experiment name: " + experimentName + StringTools.ENDLINE);
452                builder.append("Loaders: " + loaders + StringTools.ENDLINE);
453                builder.append("Results path: " + resultsPath + StringTools.ENDLINE);
454                builder.append("Version filters: " + versionFilters.toString() + StringTools.ENDLINE);
455                builder.append("Test version filters: " + testVersionFilters.toString() + StringTools.ENDLINE);
456                builder.append("Training version filters: " + trainingVersionFilters.toString() + StringTools.ENDLINE);
457                builder.append("Setwise preprocessors: " + setwisepreprocessors.toString() + StringTools.ENDLINE);
458                builder.append("Setwise selectors: " + setwiseselectors.toString() + StringTools.ENDLINE);
459                builder.append("Setwise postprocessors: " + setwisepostprocessors.toString() + StringTools.ENDLINE);
460                builder.append("Setwise trainers: " + setwiseTrainers.toString() + StringTools.ENDLINE);
461                builder.append("Pointwise preprocessors: " + preprocessors.toString() + StringTools.ENDLINE);
462                builder.append("Pointwise selectors: " + pointwiseselectors.toString() + StringTools.ENDLINE);
463                builder.append("Pointwise postprocessors: " + postprocessors.toString() + StringTools.ENDLINE);
464                builder.append("Pointwise trainers: " + trainers.toString() + StringTools.ENDLINE);
465                builder.append("Evaluators: " + evaluators.toString() + StringTools.ENDLINE);
466               
467                return builder.toString();
468        }
469}
Note: See TracBrowser for help on using the repository browser.