[2] | 1 | package de.ugoe.cs.cpdp;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
[32] | 4 | import java.lang.reflect.Constructor;
|
---|
| 5 | import java.lang.reflect.InvocationTargetException;
|
---|
[2] | 6 | import java.util.concurrent.ExecutorService;
|
---|
| 7 | import java.util.concurrent.Executors;
|
---|
| 8 | import java.util.concurrent.TimeUnit;
|
---|
| 9 | import java.util.logging.Level;
|
---|
| 10 |
|
---|
[32] | 11 | import de.ugoe.cs.cpdp.execution.IExecutionStrategy;
|
---|
[2] | 12 | import de.ugoe.cs.util.console.Console;
|
---|
| 13 | import de.ugoe.cs.util.console.TextConsole;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * Executable that can be used to run experiments.
|
---|
| 17 | * @author Steffen Herbold
|
---|
| 18 | *
|
---|
| 19 | */
|
---|
| 20 | public class Runner {
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * Main class. The arguments are {@link ExperimentConfiguration} files. Each experiment is started in a separate thread. The number of concurrently running threads is the number of logical processors of the host system.
|
---|
| 24 | * @param args experiment configuration files
|
---|
| 25 | */
|
---|
| 26 | public static void main(String[] args) {
|
---|
| 27 | new TextConsole(Level.FINE);
|
---|
[38] | 28 | final int concurrentThreads = Runtime.getRuntime().availableProcessors()-2;
|
---|
[2] | 29 | final ExecutorService threadPool = Executors.newFixedThreadPool(concurrentThreads);
|
---|
| 30 | for( String arg : args ) {
|
---|
| 31 | File file = new File(arg);
|
---|
| 32 | if( file.isFile() ) {
|
---|
| 33 | createConfig(threadPool, file.getAbsolutePath());
|
---|
| 34 | }
|
---|
| 35 | else if( file.isDirectory() ) {
|
---|
| 36 | for( File subfile : file.listFiles() ) {
|
---|
| 37 | if( subfile.isFile() ) {
|
---|
| 38 | createConfig(threadPool, subfile.getAbsolutePath());
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 | threadPool.shutdown();
|
---|
| 44 | try {
|
---|
| 45 | threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
|
---|
| 46 | } catch (InterruptedException e) {
|
---|
| 47 | e.printStackTrace();
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[32] | 51 | /**
|
---|
| 52 | * Creates the config and starts the corresponding experiment
|
---|
| 53 | * @param threadPool
|
---|
| 54 | * @param configFile location of the config file
|
---|
| 55 | */
|
---|
[2] | 56 | public static void createConfig(ExecutorService threadPool, String configFile) {
|
---|
| 57 | ExperimentConfiguration config = null;
|
---|
| 58 | try {
|
---|
| 59 | config = new ExperimentConfiguration(configFile);
|
---|
| 60 | } catch (Exception e) {
|
---|
| 61 | Console.printerrln("Failure initializing the experiment configuration for configuration file " + configFile);
|
---|
| 62 | e.printStackTrace();
|
---|
| 63 | }
|
---|
[32] | 64 |
|
---|
[2] | 65 | if( config!=null ) {
|
---|
| 66 | Console.trace(Level.FINE, config.toString());
|
---|
[32] | 67 | // Instantiate the class like it was given as parameter in the config file and cast it to the interface
|
---|
| 68 | try {
|
---|
| 69 | // Because we need to pass a parameter, a normal new Instance call is not possible
|
---|
| 70 | Class<?> executionStrategyClass = Class.forName("de.ugoe.cs.cpdp.execution."+config.getExecutionStrategy());
|
---|
| 71 | Constructor<?> executionStrategyConstructor =
|
---|
| 72 | executionStrategyClass.getConstructor(ExperimentConfiguration.class);
|
---|
| 73 |
|
---|
| 74 | IExecutionStrategy experiment = (IExecutionStrategy) executionStrategyConstructor.newInstance(config);
|
---|
| 75 | threadPool.execute(experiment);
|
---|
| 76 | } catch (NoSuchMethodException e) {
|
---|
| 77 | Console.printerrln("Class \"" + config.getExecutionStrategy()+ "\" does not have the right Constructor");
|
---|
| 78 | e.printStackTrace();
|
---|
| 79 | } catch (SecurityException e) {
|
---|
| 80 | Console.printerrln("Security manager prevents reflection");
|
---|
| 81 | e.printStackTrace();
|
---|
| 82 | } catch (IllegalArgumentException e) {
|
---|
| 83 | Console.printerrln("Class \"" + config.getExecutionStrategy()+ "\" does not have a Constructor, which"
|
---|
| 84 | + "matches the given arguments");
|
---|
| 85 | e.printStackTrace();
|
---|
| 86 | } catch (InvocationTargetException e) {
|
---|
| 87 | Console.printerrln("Constructor in Class \"" + config.getExecutionStrategy()+ "\" is not public");
|
---|
| 88 | e.printStackTrace();
|
---|
| 89 | } catch (InstantiationException e) {
|
---|
| 90 | Console.printerrln("Cannot instantiate Class \"" + config.getExecutionStrategy()+"\"");
|
---|
| 91 | e.printStackTrace();
|
---|
| 92 | } catch (IllegalAccessException e) {
|
---|
| 93 | Console.printerrln("Cannot access Class \"" + config.getExecutionStrategy()+"\"");
|
---|
| 94 | e.printStackTrace();
|
---|
| 95 | } catch (ClassNotFoundException e) {
|
---|
| 96 | Console.printerrln("Class \"" + config.getExecutionStrategy()+ "\" was not found");
|
---|
| 97 | e.printStackTrace();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[2] | 100 | }
|
---|
[32] | 101 |
|
---|
[2] | 102 | }
|
---|
| 103 | }
|
---|