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

Last change on this file was 135, checked in by sherbold, 8 years ago
  • code documentation and formatting
  • Property svn:mime-type set to text/plain
File size: 5.6 KB
RevLine 
[86]1// Copyright 2015 Georg-August-Universität Göttingen, Germany
[41]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();
[100]48        Console.traceln(Level.FINE, "exuection max " + concurrentThreads + " at the same time");
[41]49        final ExecutorService threadPool = Executors.newFixedThreadPool(concurrentThreads);
50        for (String arg : args) {
51            File file = new File(arg);
52            if (file.isFile()) {
53                createConfig(threadPool, file.getAbsolutePath());
54            }
[135]55            else if (file.isDirectory() && file.listFiles() != null) {
[41]56                for (File subfile : file.listFiles()) {
57                    if (subfile.isFile()) {
58                        createConfig(threadPool, subfile.getAbsolutePath());
59                    }
60                }
61            }
62        }
63        threadPool.shutdown();
64        try {
65            threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
66        }
67        catch (InterruptedException e) {
68            e.printStackTrace();
69        }
70    }
71
72    /**
73     * Creates the config and starts the corresponding experiment
74     *
75     * @param threadPool
76     * @param configFile
77     *            location of the config file
78     */
79    public static void createConfig(ExecutorService threadPool, String configFile) {
80        ExperimentConfiguration config = null;
81        try {
82            config = new ExperimentConfiguration(configFile);
83        }
84        catch (Exception e) {
85            Console
86                .printerrln("Failure initializing the experiment configuration for configuration file " +
87                    configFile);
88            e.printStackTrace();
89        }
90
91        if (config != null) {
[69]92            Console.trace(Level.FINEST, config.toString());
[41]93            // Instantiate the class like it was given as parameter in the config file and cast it
94            // to the interface
95            try {
96                // Because we need to pass a parameter, a normal new Instance call is not possible
97                Class<?> executionStrategyClass =
98                    Class.forName("de.ugoe.cs.cpdp.execution." + config.getExecutionStrategy());
99                Constructor<?> executionStrategyConstructor =
100                    executionStrategyClass.getConstructor(ExperimentConfiguration.class);
101
102                IExecutionStrategy experiment =
103                    (IExecutionStrategy) executionStrategyConstructor.newInstance(config);
104                threadPool.execute(experiment);
105            }
106            catch (NoSuchMethodException e) {
107                Console.printerrln("Class \"" + config.getExecutionStrategy() +
108                    "\" does not have the right Constructor");
109                e.printStackTrace();
110            }
111            catch (SecurityException e) {
112                Console.printerrln("Security manager prevents reflection");
113                e.printStackTrace();
114            }
115            catch (IllegalArgumentException e) {
116                Console.printerrln("Class \"" + config.getExecutionStrategy() +
117                    "\" does not have a Constructor, which" + "matches the given arguments");
118                e.printStackTrace();
119            }
120            catch (InvocationTargetException e) {
121                Console.printerrln("Constructor in Class \"" + config.getExecutionStrategy() +
122                    "\" is not public");
123                e.printStackTrace();
124            }
125            catch (InstantiationException e) {
126                Console.printerrln("Cannot instantiate Class \"" + config.getExecutionStrategy() +
127                    "\"");
128                e.printStackTrace();
129            }
130            catch (IllegalAccessException e) {
131                Console.printerrln("Cannot access Class \"" + config.getExecutionStrategy() + "\"");
132                e.printStackTrace();
133            }
134            catch (ClassNotFoundException e) {
135                Console.printerrln("Class \"" + config.getExecutionStrategy() + "\" was not found");
136                e.printStackTrace();
137            }
138
139        }
140
141    }
[2]142}
Note: See TracBrowser for help on using the repository browser.