1 | package desmoj.core.simulator; |
2 | |
3 | /** |
4 | * Class to Represent a Parameter. This parameter can be an experiment- |
5 | * parameter or a model-parameter. Parameters are variables that are |
6 | * accessible from everywhere in the model. Model-parameters are assignable |
7 | * from model, experiment-parameters from experiment. |
8 | * |
9 | * @see desmoj.core.simulator.ParameterManager |
10 | * @see desmoj.core.simulator.ModelParameterManager |
11 | * @see desmoj.core.simulator.ExperimentParameterManager |
12 | * |
13 | * @author Tim Janz |
14 | * |
15 | * Licensed under the Apache License, Version 2.0 (the "License"); |
16 | * you may not use this file except in compliance with the License. You |
17 | * may obtain a copy of the License at |
18 | * http://www.apache.org/licenses/LICENSE-2.0 |
19 | * |
20 | * Unless required by applicable law or agreed to in writing, software |
21 | * distributed under the License is distributed on an "AS IS" |
22 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
23 | * or implied. See the License for the specific language governing |
24 | * permissions and limitations under the License. |
25 | */ |
26 | public class Parameter |
27 | { |
28 | /** |
29 | * Enum to decide, if a parameter is a model-parameter or an experiment- |
30 | * parameter |
31 | */ |
32 | public enum ParameterType |
33 | { |
34 | EXPERIMENTPARAMETER, MODELPARAMETER; |
35 | } |
36 | |
37 | /** |
38 | * Parameter can be a model-parameter or an experiment-parameter |
39 | */ |
40 | private ParameterType _parameterType; |
41 | |
42 | /** |
43 | * The type of the parameter's value |
44 | */ |
45 | private Class<?> _type; |
46 | |
47 | /** |
48 | * The parameter's name |
49 | */ |
50 | private String _name; |
51 | |
52 | /** |
53 | * The parameter's value |
54 | */ |
55 | private Object _value; |
56 | |
57 | /** |
58 | * The parameter's default value (only for experiment-parameters) |
59 | */ |
60 | private Object _defaultValue; |
61 | |
62 | /** |
63 | * Constructs a new parameter. New Parameters should be created by the factory- |
64 | * methods of this class. |
65 | * |
66 | * @param parameterType |
67 | * is this parameter a model-parameter or an experiment-parameter |
68 | * @param type |
69 | * the parameter's type |
70 | * @param name |
71 | * the parameter's name |
72 | * @param value |
73 | * the parameter's value |
74 | * @param defaultValue |
75 | * the parameter's default value |
76 | */ |
77 | private Parameter(ParameterType parameterType, Class<?> type, String name, Object value, Object defaultValue) |
78 | { |
79 | this._parameterType = parameterType; |
80 | this._type = type; |
81 | this._name = name; |
82 | this._value = value; |
83 | this._defaultValue = defaultValue; |
84 | } |
85 | |
86 | /** |
87 | * Returns the parameter's type. |
88 | * |
89 | * @return |
90 | * the parameter's type |
91 | */ |
92 | public Class<?> getType() |
93 | { |
94 | return _type; |
95 | } |
96 | |
97 | /** |
98 | * Returns the parameter's name |
99 | * |
100 | * @return |
101 | * the parameter's name |
102 | */ |
103 | public String getName() |
104 | { |
105 | return _name; |
106 | } |
107 | |
108 | /** |
109 | * sets the parameter's value. |
110 | * |
111 | * @param value |
112 | * the parameter's new value |
113 | */ |
114 | public void setValue(Object value) |
115 | { |
116 | if (_type.isAssignableFrom(value.getClass())) |
117 | { |
118 | this._value = value; |
119 | } |
120 | else |
121 | { |
122 | //TODO Exception (wrong type) |
123 | } |
124 | } |
125 | |
126 | /** |
127 | * returns the parameter's value |
128 | * |
129 | * @return |
130 | * the parameter's value |
131 | */ |
132 | public Object getValue() |
133 | { |
134 | if (!hasValue() && !hasDefaultValue()) |
135 | { |
136 | //TODO Exception |
137 | } |
138 | |
139 | return hasValue() ? _value : _defaultValue; |
140 | } |
141 | |
142 | /** |
143 | * Checks, if this parameter has a value. |
144 | * |
145 | * @return |
146 | * if parameter has a value |
147 | */ |
148 | public boolean hasValue() |
149 | { |
150 | return (_value != null); |
151 | } |
152 | |
153 | /** |
154 | * Checks, if this parameter has a default value. |
155 | * |
156 | * @return |
157 | * if parameter has a default value |
158 | */ |
159 | public boolean hasDefaultValue() |
160 | { |
161 | return (_defaultValue != null); |
162 | } |
163 | |
164 | /** |
165 | * Is this parameter a model-parameter or an experiment-parameter? |
166 | * |
167 | * @return |
168 | * ParameterType.MODELPARAMETER if this parameter is a model-parameter, or |
169 | * ParameterType.EXPERIMENTPARAMETER if this parameter is an experiment-parameter |
170 | */ |
171 | public ParameterType getParameterType() |
172 | { |
173 | return _parameterType; |
174 | } |
175 | |
176 | /** |
177 | * Returns the parameter as a String |
178 | * |
179 | * @return |
180 | * the parameter as a String |
181 | */ |
182 | @Override |
183 | public String toString() |
184 | { |
185 | return "(" + _type.toString() + ") " + _name + ": " + _value.toString(); |
186 | } |
187 | |
188 | /** |
189 | * Factory-method to create an experiment-parameter. |
190 | * |
191 | * @param type |
192 | * the parameter's type |
193 | * @param name |
194 | * the parameter's name |
195 | * @param defaultValue |
196 | * the parameter's default value |
197 | * @return |
198 | * the created instance of Parameter |
199 | */ |
200 | public static Parameter createExperimentParameter(Class<?> type, String name, Object defaultValue) |
201 | { |
202 | return new Parameter(Parameter.ParameterType.EXPERIMENTPARAMETER, type, name, null, defaultValue); |
203 | } |
204 | |
205 | /** |
206 | * Factory-method to create an experiment-parameter. |
207 | * |
208 | * @param type |
209 | * the parameter's type |
210 | * @param name |
211 | * the parameter's name |
212 | * @return |
213 | * the created instance of Parameter |
214 | */ |
215 | public static Parameter createExperimentParameter(Class<?> type, String name) |
216 | { |
217 | return createExperimentParameter(type, name, null); |
218 | } |
219 | |
220 | /** |
221 | * Factory-method to create a model-parameter. |
222 | * |
223 | * @param type |
224 | * the parameter's type |
225 | * @param name |
226 | * the parameter's name |
227 | * @param value |
228 | * the parameter's value |
229 | * @return |
230 | * the created instance of Parameter |
231 | */ |
232 | public static Parameter createModelParameter(Class<?> type, String name, Object value) |
233 | { |
234 | return new Parameter(Parameter.ParameterType.MODELPARAMETER, type, name, value, null); |
235 | } |
236 | |
237 | /** |
238 | * Factory-method to create a model-parameter. |
239 | * |
240 | * @param type |
241 | * the parameter's type |
242 | * @param name |
243 | * the parameter's name |
244 | * @return |
245 | * the created instance of Parameter |
246 | */ |
247 | public static Parameter createModelParameter(Class<?> type, String name) |
248 | { |
249 | return createModelParameter(type, name, null); |
250 | } |
251 | } |
252 | |