1 | package de.uka.ipd.sdq.simucomframework; |
2 | |
3 | import java.io.Serializable; |
4 | import java.util.HashMap; |
5 | import java.util.Map; |
6 | |
7 | import org.apache.log4j.Logger; |
8 | import org.eclipse.emf.common.util.URI; |
9 | |
10 | import de.uka.ipd.sdq.probfunction.math.IRandomGenerator; |
11 | import de.uka.ipd.sdq.simulation.AbstractSimulationConfig; |
12 | import de.uka.ipd.sdq.workflow.pcm.runconfig.ExperimentRunDescriptor; |
13 | |
14 | /** |
15 | * @author roman |
16 | * |
17 | * The class encapsulates all configuration options for SimuCom. |
18 | */ |
19 | public class SimuComConfig extends AbstractSimulationConfig implements Serializable, Cloneable { |
20 | /** Logger of this class. */ |
21 | private static final Logger logger = Logger.getLogger(SimuComConfig.class); |
22 | |
23 | /** Serialization ID of this class. */ |
24 | private static final long serialVersionUID = -3364130550065874984L; |
25 | |
26 | public static final String SHOULD_THROW_EXCEPTION = "shouldThrowException"; |
27 | |
28 | // Default values |
29 | /** Default name of model element for the stop condition confidence. */ |
30 | public static final String DEFAULT_CONFIDENCE_MODELELEMENT_NAME = ""; |
31 | /** Stop condition confidence, URI to model element ? */ |
32 | public static final String DEFAULT_CONFIDENCE_MODELELEMENT_URI = ""; |
33 | /** Default selection if stop condition confidence is used. */ |
34 | public static final Boolean DEFAULT_USE_CONFIDENCE = false; |
35 | /** Default value for confidence level of the stop condition confidence. */ |
36 | public static final Integer DEFAULT_CONFIDENCE_LEVEL = 95; |
37 | /** Default value for the half width of the stop condition confidence.*/ |
38 | public static final Integer DEFAULT_CONFIDENCE_HALFWIDTH = 10; |
39 | /** Default value for the automated batch calculation */ |
40 | public static final Boolean DEFAULT_CONFIDENCE_USE_AUTOMATIC_BATCHES = true; |
41 | /** default batch size, arbitrarily chosen */ |
42 | public static final Integer DEFAULT_CONFIDENCE_BATCH_SIZE = 200; |
43 | /** default minimum number of batches, arbitrarily chosen */ |
44 | public static final Integer DEFAULT_CONFIDENCE_MIN_NUMBER_OF_BATCHES = 60; |
45 | |
46 | |
47 | |
48 | /** SimuCom configuration tab */ |
49 | public static final String SIMULATE_FAILURES = "simulateFailures"; |
50 | public static final String SIMULATE_LINKING_RESOURCES = "simulateLinkingResources"; |
51 | public static final String USE_CONFIDENCE = "useConfidenceStopCondition"; |
52 | public static final String CONFIDENCE_LEVEL = "confidenceLevel"; |
53 | public static final String CONFIDENCE_HALFWIDTH = "confidenceHalfWidth"; |
54 | public static final String CONFIDENCE_MODELELEMENT_URI = "confidenceModelElementURI"; |
55 | public static final String CONFIDENCE_MODELELEMENT_NAME = "confidenceModelElementName"; |
56 | public static final String CONFIDENCE_USE_AUTOMATIC_BATCHES = "confidenceUseAutomaticBatches"; |
57 | public static final String CONFIDENCE_BATCH_SIZE = "confidenceBatchSize"; |
58 | public static final String CONFIDENCE_MIN_NUMBER_OF_BATCHES = "confidenceMinNumberOfBatches"; |
59 | |
60 | private boolean simulateFailures = false; |
61 | private boolean simulateLinkingResources = false; |
62 | private boolean useConfidence = false; |
63 | private int confidenceLevel = 0; |
64 | private int confidenceHalfWidth = 0; |
65 | private URI confidenceModelElementURI; |
66 | private String confidenceModelElementName; |
67 | /* next three are batch algorithm settings */ |
68 | private boolean automaticBatches; |
69 | private int batchSize; |
70 | private int minNumberOfBatches; |
71 | // SimuCom extensions can also provide extension to the SimuCom configuration. |
72 | // This map stores the extension configurations. |
73 | private HashMap<String, SimuComConfigExtension> simuComConfigExtensions = null; |
74 | |
75 | |
76 | |
77 | |
78 | /** |
79 | * @param configuration a map which maps configuration option IDs to their values. |
80 | * The required keys are SimuComConfig.EXPERIMENT_RUN, SimuComConfig.SIMULATION_TIME |
81 | * SimuComConfig.MAXIMUM_MEASUREMENT_COUNT SimuComConfig.VERBOSE_LOGGING, |
82 | * SimuComConfig.DATASOURCE_ID. Optional keys are SimuComConfig.SIMULATE_FAILURES, |
83 | * SimuComConfig.SIMULATE_LINKING_RESOURCES and SimuComConfig.USE_CONFIDENCE. If |
84 | * SimuComConfig.USE_CONFIDENCE is set to true, you also need to set |
85 | * SimuComConfig.CONFIDENCE_LEVEL, SimuComConfig.CONFIDENCE_HALFWIDTH, |
86 | * SimuComConfig.CONFIDENCE_MODELELEMENT_NAME, SimuComConfig.CONFIDENCE_MODELELEMENT_URI |
87 | * |
88 | */ |
89 | public SimuComConfig(Map<String,Object> configuration, boolean debug){ |
90 | super(configuration, debug); |
91 | simuComConfigExtensions = new HashMap<String, SimuComConfigExtension>(); |
92 | try { |
93 | if (configuration.containsKey(SIMULATE_FAILURES)){ |
94 | this.simulateFailures = (Boolean)configuration.get( |
95 | SIMULATE_FAILURES); |
96 | } |
97 | |
98 | if (configuration.containsKey(SIMULATE_LINKING_RESOURCES)){ |
99 | this.simulateLinkingResources = (Boolean)configuration.get( |
100 | SIMULATE_LINKING_RESOURCES); |
101 | } |
102 | |
103 | // confidence information is optional in the map. It this.useConfidence defaults to false. |
104 | if (configuration.containsKey(USE_CONFIDENCE)) { |
105 | this.useConfidence = (Boolean) configuration |
106 | .get(USE_CONFIDENCE); |
107 | this.confidenceLevel = Integer.valueOf((String) configuration |
108 | .get(CONFIDENCE_LEVEL)); |
109 | this.confidenceHalfWidth = Integer |
110 | .valueOf((String) configuration |
111 | .get(CONFIDENCE_HALFWIDTH)); |
112 | this.confidenceModelElementName = (String) configuration |
113 | .get(CONFIDENCE_MODELELEMENT_NAME); |
114 | this.confidenceModelElementURI = URI |
115 | .createURI((String) configuration |
116 | .get(CONFIDENCE_MODELELEMENT_URI)); |
117 | |
118 | this.automaticBatches = (Boolean) configuration.get(CONFIDENCE_USE_AUTOMATIC_BATCHES); |
119 | if (!this.automaticBatches){ |
120 | //only need batch settings if they are manually defined |
121 | this.batchSize = Integer.valueOf((String) configuration |
122 | .get(CONFIDENCE_BATCH_SIZE)); |
123 | this.minNumberOfBatches = Integer.valueOf((String) configuration |
124 | .get(CONFIDENCE_MIN_NUMBER_OF_BATCHES)); |
125 | |
126 | } |
127 | |
128 | } |
129 | |
130 | } catch (Exception e) { |
131 | throw new RuntimeException("Setting up properties failed, please check launch config (check all tabs).", e); |
132 | } |
133 | } |
134 | |
135 | public void addSimuComConfigExtension(String id, SimuComConfigExtension simuComConfigExtension) { |
136 | simuComConfigExtensions.put(id, simuComConfigExtension); |
137 | } |
138 | |
139 | public SimuComConfigExtension getSimuComConfigExtension(String id) { |
140 | return simuComConfigExtensions.get(id); |
141 | } |
142 | |
143 | public boolean getSimulateFailures() { |
144 | return simulateFailures; |
145 | } |
146 | |
147 | public boolean getSimulateLinkingResources() { |
148 | return simulateLinkingResources; |
149 | } |
150 | |
151 | public boolean isUseConfidence() { |
152 | return useConfidence; |
153 | } |
154 | |
155 | public int getConfidenceLevel() { |
156 | return confidenceLevel; |
157 | } |
158 | |
159 | public int getConfidenceHalfWidth() { |
160 | return confidenceHalfWidth; |
161 | } |
162 | |
163 | public String getConfidenceModelElementName() { |
164 | return confidenceModelElementName; |
165 | } |
166 | |
167 | public URI getConfidenceModelElementURI() { |
168 | return confidenceModelElementURI; |
169 | } |
170 | |
171 | /**Returns a copy of the instance with a replaced descriptor. |
172 | * @param descriptor Descriptor of the new instance. |
173 | * @return Copy of the instance. |
174 | */ |
175 | public SimuComConfig copy(ExperimentRunDescriptor descriptor) { |
176 | SimuComConfig result = getClone(); |
177 | |
178 | result.descriptor = descriptor; |
179 | return result; |
180 | } |
181 | |
182 | @Override |
183 | protected Object clone() throws CloneNotSupportedException { |
184 | SimuComConfig config = (SimuComConfig) super.clone(); |
185 | config.confidenceHalfWidth = this.confidenceHalfWidth; |
186 | config.confidenceLevel = this.confidenceLevel; |
187 | config.confidenceModelElementName = new String(this.confidenceModelElementName); |
188 | config.maxMeasurementsCount = this.maxMeasurementsCount; |
189 | config.nameExperimentRun = new String (this.nameExperimentRun); |
190 | config.recorderName = new String(this.recorderName); |
191 | config.simulateFailures = this.simulateFailures; |
192 | config.simulateLinkingResources = this.simulateLinkingResources; |
193 | config.simuTime = this.simuTime; |
194 | config.useConfidence = this.useConfidence; |
195 | |
196 | // Warning: References are used in the following section instead of cloning the objects/arrays. |
197 | config.confidenceModelElementURI = this.confidenceModelElementURI; |
198 | config.descriptor = this.descriptor; |
199 | config.randomNumberGenerator = this.randomNumberGenerator; |
200 | config.randomSeed = this.randomSeed; |
201 | config.recorderConfig = this.recorderConfig; |
202 | |
203 | return config; |
204 | } |
205 | |
206 | /** |
207 | * @return Returns a clone of this instance. |
208 | */ |
209 | public SimuComConfig getClone() { |
210 | SimuComConfig config = null; |
211 | try { |
212 | config = (SimuComConfig) clone(); |
213 | } catch (CloneNotSupportedException e) { |
214 | logger.fatal("Could not clone configuration.", e); |
215 | } |
216 | return config; |
217 | } |
218 | |
219 | public void setAutomaticBatches(boolean automaticBatches) { |
220 | this.automaticBatches = automaticBatches; |
221 | } |
222 | |
223 | public boolean isAutomaticBatches() { |
224 | return this.automaticBatches; |
225 | } |
226 | |
227 | public void getBatchSize(int batchSize) { |
228 | this.batchSize = batchSize; |
229 | } |
230 | |
231 | public int getBatchSize() { |
232 | return this.batchSize; |
233 | } |
234 | |
235 | public void setMinNumberOfBatches(int minNumberOfBatches) { |
236 | this.minNumberOfBatches = minNumberOfBatches; |
237 | } |
238 | |
239 | public int getMinNumberOfBatches() { |
240 | return this.minNumberOfBatches; |
241 | } |
242 | |
243 | public IRandomGenerator getRandomGenerator() { |
244 | if (randomNumberGenerator == null) { |
245 | randomNumberGenerator = new SimuComDefaultRandomNumberGenerator(this.randomSeed); |
246 | } |
247 | return randomNumberGenerator; |
248 | } |
249 | |
250 | |
251 | } |