source: trunk/CrossPare/src/de/ugoe/cs/cpdp/Runner.java @ 74

Last change on this file since 74 was 74, checked in by sherbold, 9 years ago
  • fixed some minor problems found by FindBugs?
  • Property svn:mime-type set to text/plain
File size: 5.5 KB
RevLine 
[41]1// Copyright 2015 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
[2]15package de.ugoe.cs.cpdp;
16
17import java.io.File;
[32]18import java.lang.reflect.Constructor;
19import java.lang.reflect.InvocationTargetException;
[2]20import java.util.concurrent.ExecutorService;
21import java.util.concurrent.Executors;
22import java.util.concurrent.TimeUnit;
23import java.util.logging.Level;
24
[32]25import de.ugoe.cs.cpdp.execution.IExecutionStrategy;
[2]26import de.ugoe.cs.util.console.Console;
27import de.ugoe.cs.util.console.TextConsole;
28
29/**
30 * Executable that can be used to run experiments.
[41]31 *
[2]32 * @author Steffen Herbold
[41]33 *
[2]34 */
35public class Runner {
[32]36
[41]37    /**
38     * Main class. The arguments are {@link ExperimentConfiguration} files. Each experiment is
39     * started in a separate thread. The number of concurrently running threads is the number of
40     * logical processors of the host system.
41     *
42     * @param args
43     *            experiment configuration files
44     */
45    public static void main(String[] args) {
46        new TextConsole(Level.FINE);
[69]47        final int concurrentThreads = Runtime.getRuntime().availableProcessors();
[41]48        final ExecutorService threadPool = Executors.newFixedThreadPool(concurrentThreads);
49        for (String arg : args) {
50            File file = new File(arg);
51            if (file.isFile()) {
52                createConfig(threadPool, file.getAbsolutePath());
53            }
[74]54            else if (file.isDirectory() && file.listFiles()!=null ) {
[41]55                for (File subfile : file.listFiles()) {
56                    if (subfile.isFile()) {
57                        createConfig(threadPool, subfile.getAbsolutePath());
58                    }
59                }
60            }
61        }
62        threadPool.shutdown();
63        try {
64            threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
65        }
66        catch (InterruptedException e) {
67            e.printStackTrace();
68        }
69    }
70
71    /**
72     * Creates the config and starts the corresponding experiment
73     *
74     * @param threadPool
75     * @param configFile
76     *            location of the config file
77     */
78    public static void createConfig(ExecutorService threadPool, String configFile) {
79        ExperimentConfiguration config = null;
80        try {
81            config = new ExperimentConfiguration(configFile);
82        }
83        catch (Exception e) {
84            Console
85                .printerrln("Failure initializing the experiment configuration for configuration file " +
86                    configFile);
87            e.printStackTrace();
88        }
89
90        if (config != null) {
[69]91            Console.trace(Level.FINEST, config.toString());
[41]92            // Instantiate the class like it was given as parameter in the config file and cast it
93            // to the interface
94            try {
95                // Because we need to pass a parameter, a normal new Instance call is not possible
96                Class<?> executionStrategyClass =
97                    Class.forName("de.ugoe.cs.cpdp.execution." + config.getExecutionStrategy());
98                Constructor<?> executionStrategyConstructor =
99                    executionStrategyClass.getConstructor(ExperimentConfiguration.class);
100
101                IExecutionStrategy experiment =
102                    (IExecutionStrategy) executionStrategyConstructor.newInstance(config);
103                threadPool.execute(experiment);
104            }
105            catch (NoSuchMethodException e) {
106                Console.printerrln("Class \"" + config.getExecutionStrategy() +
107                    "\" does not have the right Constructor");
108                e.printStackTrace();
109            }
110            catch (SecurityException e) {
111                Console.printerrln("Security manager prevents reflection");
112                e.printStackTrace();
113            }
114            catch (IllegalArgumentException e) {
115                Console.printerrln("Class \"" + config.getExecutionStrategy() +
116                    "\" does not have a Constructor, which" + "matches the given arguments");
117                e.printStackTrace();
118            }
119            catch (InvocationTargetException e) {
120                Console.printerrln("Constructor in Class \"" + config.getExecutionStrategy() +
121                    "\" is not public");
122                e.printStackTrace();
123            }
124            catch (InstantiationException e) {
125                Console.printerrln("Cannot instantiate Class \"" + config.getExecutionStrategy() +
126                    "\"");
127                e.printStackTrace();
128            }
129            catch (IllegalAccessException e) {
130                Console.printerrln("Cannot access Class \"" + config.getExecutionStrategy() + "\"");
131                e.printStackTrace();
132            }
133            catch (ClassNotFoundException e) {
134                Console.printerrln("Class \"" + config.getExecutionStrategy() + "\" was not found");
135                e.printStackTrace();
136            }
137
138        }
139
140    }
[2]141}
Note: See TracBrowser for help on using the repository browser.