| 1 | package de.uka.ipd.sdq.simulation; |
| 2 | |
| 3 | import java.io.Serializable; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.launch.IRecorderConfiguration; |
| 9 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.launch.RecorderExtensionHelper; |
| 10 | import de.uka.ipd.sdq.probespec.framework.BlackboardType; |
| 11 | import de.uka.ipd.sdq.probfunction.math.IRandomGenerator; |
| 12 | import de.uka.ipd.sdq.simulation.abstractsimengine.ISimulationConfig; |
| 13 | import de.uka.ipd.sdq.workflow.pcm.runconfig.ExperimentRunDescriptor; |
| 14 | |
| 15 | /** |
| 16 | * This is the abstract base class for simulation configurations. It encapsulates the configuration |
| 17 | * elements that are common to all PCM simulators. |
| 18 | * |
| 19 | * @author roman |
| 20 | * @author Philipp Merkle |
| 21 | * |
| 22 | */ |
| 23 | public abstract class AbstractSimulationConfig implements Serializable, ISimulationConfig { |
| 24 | |
| 25 | /** Serialization ID of this class. */ |
| 26 | private static final long serialVersionUID = 891323270372759718L; |
| 27 | |
| 28 | public static final String SIMULATOR_ID = "simulatorId"; |
| 29 | public static final String PERSISTENCE_RECORDER_NAME = "persistenceFramework"; |
| 30 | public static final String USE_FIXED_SEED = "useFixedSeed"; |
| 31 | public static final String FIXED_SEED_PREFIX = "fixedSeed"; |
| 32 | public static final String MAXIMUM_MEASUREMENT_COUNT = "maximumMeasurementCount"; |
| 33 | |
| 34 | // Default values |
| 35 | /** Default simulator implementation */ |
| 36 | public static final String DEFAULT_SIMULATOR_ID = "de.uka.ipd.sdq.codegen.simucontroller.simucom"; |
| 37 | /** Default name for an experiment run. */ |
| 38 | public static final String DEFAULT_EXPERIMENT_RUN = "MyRun"; |
| 39 | /** Default for stop condition simulation time. */ |
| 40 | public static final String DEFAULT_SIMULATION_TIME = "150000"; |
| 41 | /** Default for stop condition maximum measurement count. */ |
| 42 | public static final String DEFAULT_MAXIMUM_MEASUREMENT_COUNT = "10000"; |
| 43 | /** Default name of persistence recorder. */ |
| 44 | public static final String DEFAULT_PERSISTENCE_RECORDER_NAME = ""; |
| 45 | |
| 46 | public static String VERBOSE_LOGGING = "verboseLogging"; |
| 47 | /** the type of the ProbeSpec blackboard */ |
| 48 | public static String BLACKBOARD_TYPE = "blackboardType"; |
| 49 | |
| 50 | /** Simulation configuration tab */ |
| 51 | public static final String EXPERIMENT_RUN = "experimentRun"; |
| 52 | public static final String SIMULATION_TIME = "simTime"; |
| 53 | |
| 54 | private boolean verboseLogging; |
| 55 | private boolean isDebug; |
| 56 | private BlackboardType blackboardType; |
| 57 | |
| 58 | private final List<ISimulationListener> listeners; |
| 59 | /** configuration options */ |
| 60 | protected String nameExperimentRun; |
| 61 | private String additionalExperimentRunDescription; |
| 62 | protected long simuTime; |
| 63 | protected Long maxMeasurementsCount; |
| 64 | protected long[] randomSeed = null; |
| 65 | protected IRandomGenerator randomNumberGenerator = null; |
| 66 | protected String recorderName; |
| 67 | protected IRecorderConfiguration recorderConfig; |
| 68 | protected ExperimentRunDescriptor descriptor = null; |
| 69 | private String simulatorId; |
| 70 | |
| 71 | /** |
| 72 | * @param configuration |
| 73 | * a map which maps configuration option IDs to their values. The required keys are |
| 74 | * <ul> |
| 75 | * <li>SimuComConfig.VERBOSE_LOGGING |
| 76 | * </ul> |
| 77 | */ |
| 78 | public AbstractSimulationConfig(Map<String, Object> configuration, boolean debug) { |
| 79 | try { |
| 80 | this.verboseLogging = (Boolean) configuration.get(VERBOSE_LOGGING); |
| 81 | this.isDebug = debug; |
| 82 | |
| 83 | this.simulatorId = (String) configuration.get(SIMULATOR_ID); |
| 84 | this.nameExperimentRun = (String) configuration.get(EXPERIMENT_RUN); |
| 85 | this.simuTime = Long.valueOf((String) configuration.get(SIMULATION_TIME)); |
| 86 | this.maxMeasurementsCount = Long.valueOf((String) configuration.get(MAXIMUM_MEASUREMENT_COUNT)); |
| 87 | this.randomSeed = getSeedFromConfig(configuration); |
| 88 | |
| 89 | // set the blackboard type |
| 90 | try { |
| 91 | this.blackboardType = BlackboardType.valueOf(asString(configuration, BLACKBOARD_TYPE)); |
| 92 | } catch (IllegalArgumentException e) { |
| 93 | // this exception is thrown, if no enum name matches the passed String |
| 94 | this.blackboardType = null; |
| 95 | } |
| 96 | if (this.blackboardType == null) { |
| 97 | // use default blackboard type |
| 98 | this.blackboardType = BlackboardType.CONCURRENT; |
| 99 | } |
| 100 | |
| 101 | this.recorderName = (String) configuration.get(PERSISTENCE_RECORDER_NAME); |
| 102 | recorderConfig = RecorderExtensionHelper.getRecorderConfigForName(recorderName); |
| 103 | if (recorderConfig != null) { |
| 104 | recorderConfig.setConfiguration(configuration); |
| 105 | } |
| 106 | |
| 107 | this.listeners = new ArrayList<ISimulationListener>(); |
| 108 | } catch (Exception e) { |
| 109 | throw new RuntimeException("Setting up properties failed, please check launch config (check all tabs).", e); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public boolean getVerboseLogging() { |
| 114 | return verboseLogging || isDebug; |
| 115 | } |
| 116 | |
| 117 | public boolean isDebug() { |
| 118 | return this.isDebug; |
| 119 | } |
| 120 | |
| 121 | public void addListener(ISimulationListener l) { |
| 122 | listeners.add(l); |
| 123 | } |
| 124 | |
| 125 | public List<ISimulationListener> getListeners() { |
| 126 | return listeners; |
| 127 | } |
| 128 | |
| 129 | public BlackboardType getBlackboardType() { |
| 130 | return blackboardType; |
| 131 | } |
| 132 | |
| 133 | private String asString(Map<String, Object> configuration, String propertyName) { |
| 134 | String result = (String) configuration.get(propertyName); |
| 135 | return result == null ? "" : result; |
| 136 | } |
| 137 | |
| 138 | public String getAdditionalExperimentRunDescription() { |
| 139 | return additionalExperimentRunDescription; |
| 140 | } |
| 141 | |
| 142 | public void setAdditionalExperimentRunDescription(String additionalExperimentRunDescription) { |
| 143 | this.additionalExperimentRunDescription = additionalExperimentRunDescription; |
| 144 | } |
| 145 | |
| 146 | protected long[] getSeedFromConfig(Map<String, Object> configuration) { |
| 147 | if ((Boolean) configuration.get(USE_FIXED_SEED)) { |
| 148 | long[] seed = new long[6]; |
| 149 | for (int i = 0; i < 6; i++) { |
| 150 | seed[i] = Long.parseLong((String) configuration.get(FIXED_SEED_PREFIX + i)); |
| 151 | } |
| 152 | return seed; |
| 153 | } |
| 154 | return null; |
| 155 | } |
| 156 | |
| 157 | public String getNameExperimentRun() { |
| 158 | String name = ""; |
| 159 | if (descriptor != null) { |
| 160 | name += descriptor.getNameExperimentRun(); |
| 161 | } else { |
| 162 | name += getNameBase(); |
| 163 | } |
| 164 | if (additionalExperimentRunDescription != null) { |
| 165 | name += additionalExperimentRunDescription; |
| 166 | } |
| 167 | return name; |
| 168 | } |
| 169 | |
| 170 | public String getNameBase() { |
| 171 | return nameExperimentRun; |
| 172 | } |
| 173 | |
| 174 | public void setNameBase(String name) { |
| 175 | this.nameExperimentRun = name; |
| 176 | } |
| 177 | |
| 178 | public long getSimuTime() { |
| 179 | return simuTime; |
| 180 | } |
| 181 | |
| 182 | public long getMaxMeasurementsCount() { |
| 183 | return maxMeasurementsCount; |
| 184 | } |
| 185 | |
| 186 | public String getRecorderName() { |
| 187 | return recorderName; |
| 188 | } |
| 189 | |
| 190 | public String getEngine() { |
| 191 | return "de.uka.ipd.sdq.simulation.abstractsimengine.ssj.SSJSimEngineFactory"; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Dispose random generator and delete reference to it so that this {@link SimuComConfig} can be |
| 196 | * started again and will create a new RandomGenerator. |
| 197 | * |
| 198 | * @author martens |
| 199 | */ |
| 200 | public void disposeRandomGenerator() { |
| 201 | this.randomNumberGenerator.dispose(); |
| 202 | this.randomNumberGenerator = null; |
| 203 | } |
| 204 | |
| 205 | public void setExperimentRunDescriptor(ExperimentRunDescriptor descriptor) { |
| 206 | this.descriptor = descriptor; |
| 207 | } |
| 208 | |
| 209 | public ExperimentRunDescriptor getExperimentRunDescriptor() { |
| 210 | return descriptor; |
| 211 | } |
| 212 | |
| 213 | public IRecorderConfiguration getRecorderConfig() { |
| 214 | return recorderConfig; |
| 215 | } |
| 216 | |
| 217 | public String getSimulatorId() { |
| 218 | return simulatorId; |
| 219 | } |
| 220 | |
| 221 | } |