Changeset 41 for trunk/CrossPare/src/de/ugoe/cs/cpdp/Runner.java
- Timestamp:
- 09/24/15 10:59:05 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CrossPare/src/de/ugoe/cs/cpdp/Runner.java
r38 r41 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 1 15 package de.ugoe.cs.cpdp; 2 16 … … 15 29 /** 16 30 * Executable that can be used to run experiments. 31 * 17 32 * @author Steffen Herbold 18 * 33 * 19 34 */ 20 35 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 files25 */26 public static void main(String[] args) {27 new TextConsole(Level.FINE);28 final int concurrentThreads = Runtime.getRuntime().availableProcessors()-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 51 /**52 * Creates the config and starts the corresponding experiment53 * @param threadPool54 * @param configFile location of the config file55 */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 }64 36 65 if( config!=null ) { 66 Console.trace(Level.FINE, config.toString()); 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 100 } 101 102 } 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); 47 final int concurrentThreads = Runtime.getRuntime().availableProcessors() - 2; 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 } 54 else if (file.isDirectory()) { 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) { 91 Console.trace(Level.FINE, config.toString()); 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 } 103 141 }
Note: See TracChangeset
for help on using the changeset viewer.