Ignore:
Timestamp:
05/05/15 16:29:40 (9 years ago)
Author:
ftrautsch
Message:

integrating decent into crosspare

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrossPare/src/de/ugoe/cs/cpdp/Runner.java

    r2 r32  
    22 
    33import java.io.File; 
     4import java.lang.reflect.Constructor; 
     5import java.lang.reflect.InvocationTargetException; 
    46import java.util.concurrent.ExecutorService; 
    57import java.util.concurrent.Executors; 
     
    79import java.util.logging.Level; 
    810 
     11import de.ugoe.cs.cpdp.execution.IExecutionStrategy; 
    912import de.ugoe.cs.util.console.Console; 
    1013import de.ugoe.cs.util.console.TextConsole; 
     
    2326        public static void main(String[] args) { 
    2427                new TextConsole(Level.FINE); 
    25                  
    2628                final int concurrentThreads = Runtime.getRuntime().availableProcessors(); 
    2729                final ExecutorService threadPool = Executors.newFixedThreadPool(concurrentThreads); 
     
    4749        } 
    4850         
     51        /** 
     52         * Creates the config and starts the corresponding experiment 
     53         * @param threadPool  
     54         * @param configFile location of the config file 
     55         */ 
    4956        public static void createConfig(ExecutorService threadPool, String configFile) { 
    5057                ExperimentConfiguration config = null; 
     
    5562                        e.printStackTrace(); 
    5663                } 
     64 
    5765                if( config!=null ) { 
    5866                        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                         
    61100                } 
     101                 
    62102        } 
    63103} 
Note: See TracChangeset for help on using the changeset viewer.