Changeset 32 for trunk/CrossPare/src/de/ugoe/cs/cpdp/Runner.java
- Timestamp:
- 05/05/15 16:29:40 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CrossPare/src/de/ugoe/cs/cpdp/Runner.java
r2 r32 2 2 3 3 import java.io.File; 4 import java.lang.reflect.Constructor; 5 import java.lang.reflect.InvocationTargetException; 4 6 import java.util.concurrent.ExecutorService; 5 7 import java.util.concurrent.Executors; … … 7 9 import java.util.logging.Level; 8 10 11 import de.ugoe.cs.cpdp.execution.IExecutionStrategy; 9 12 import de.ugoe.cs.util.console.Console; 10 13 import de.ugoe.cs.util.console.TextConsole; … … 23 26 public static void main(String[] args) { 24 27 new TextConsole(Level.FINE); 25 26 28 final int concurrentThreads = Runtime.getRuntime().availableProcessors(); 27 29 final ExecutorService threadPool = Executors.newFixedThreadPool(concurrentThreads); … … 47 49 } 48 50 51 /** 52 * Creates the config and starts the corresponding experiment 53 * @param threadPool 54 * @param configFile location of the config file 55 */ 49 56 public static void createConfig(ExecutorService threadPool, String configFile) { 50 57 ExperimentConfiguration config = null; … … 55 62 e.printStackTrace(); 56 63 } 64 57 65 if( config!=null ) { 58 66 Console.trace(Level.FINE, config.toString()); 59 Experiment experiment = new Experiment(config); 60 threadPool.execute(experiment); 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 61 100 } 101 62 102 } 63 103 }
Note: See TracChangeset
for help on using the changeset viewer.