| 1 | package desmoj.core.simulator; |
| 2 | |
| 3 | import java.text.Collator; |
| 4 | import java.util.Collection; |
| 5 | import java.util.TreeMap; |
| 6 | |
| 7 | /** |
| 8 | * Class to manage parameters. Parameters are values, which affect a simulation- |
| 9 | * model or an experiment. Model-parameters are model's constants. Experiment- |
| 10 | * parameters are experiment-constants which can be used in the experiment's model. |
| 11 | * |
| 12 | * This class is accessible from Model through the ModelParameterManager-interface to |
| 13 | * restrict the accessible methods from a model's view. It is accessible from Experiment |
| 14 | * through the ExperimentParameterManager-interface to restrict the accessible methods |
| 15 | * from a experiment's view. |
| 16 | * |
| 17 | * @see desmoj.core.simulator.ModelParameterManager |
| 18 | * @see desmoj.core.simulator.ExperimentParameterManager |
| 19 | * |
| 20 | * @author Tim Janz |
| 21 | * |
| 22 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 23 | * you may not use this file except in compliance with the License. You |
| 24 | * may obtain a copy of the License at |
| 25 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 26 | * |
| 27 | * Unless required by applicable law or agreed to in writing, software |
| 28 | * distributed under the License is distributed on an "AS IS" |
| 29 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 30 | * or implied. See the License for the specific language governing |
| 31 | * permissions and limitations under the License. |
| 32 | */ |
| 33 | public class ParameterManager implements ModelParameterManager, ExperimentParameterManager |
| 34 | { |
| 35 | /** |
| 36 | * A map containing the parameter's name and the parameter itself |
| 37 | */ |
| 38 | //private IdentityHashMap<String, Parameter> parameters; |
| 39 | private TreeMap<String, Parameter> _parameters; |
| 40 | |
| 41 | /** |
| 42 | * Constructs a ParameterManager |
| 43 | */ |
| 44 | public ParameterManager() |
| 45 | { |
| 46 | super(); |
| 47 | |
| 48 | //parameters = new IdentityHashMap<String, Parameter>(); |
| 49 | _parameters = new TreeMap<String, Parameter>(Collator.getInstance()); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Method to declare a experiment-parameter. The experiment-parameter's |
| 54 | * declarations are part of the simulation-model. The experiment- |
| 55 | * parameter's assignments are in contrast part of an experiment. |
| 56 | * |
| 57 | * @param type |
| 58 | * the experiment-parameter's type |
| 59 | * @param name |
| 60 | * the experiment-parameter's name |
| 61 | */ |
| 62 | public void declareExperimentParameter(Class<?> type, String name) |
| 63 | { |
| 64 | _parameters.put(name, Parameter.createExperimentParameter(type, name)); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Method to declare a experiment-parameter. The experiment-parameter's |
| 69 | * declarations are part of the simulation-model. The experiment- |
| 70 | * parameter's assignments are in contrast part of an experiment. |
| 71 | * The default value is used, if an experiment dosen't assign a |
| 72 | * value to this parameter. |
| 73 | * |
| 74 | * @param type |
| 75 | * the experiment-parameter's type |
| 76 | * @param name |
| 77 | * the experiment-parameter's name |
| 78 | * @param defaultValue |
| 79 | * the experiment-parameter's default value |
| 80 | */ |
| 81 | public void declareExperimentParameter(Class<?> type, String name, Object defaultValue) |
| 82 | { |
| 83 | _parameters.put(name, Parameter.createExperimentParameter(type, name, defaultValue)); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Method to declare a model-parameter. |
| 88 | * |
| 89 | * @param type |
| 90 | * the model-parameter's type |
| 91 | * @param name |
| 92 | * the model-parameter's name |
| 93 | */ |
| 94 | public void declareModelParameter(Class<?> type, String name) |
| 95 | { |
| 96 | _parameters.put(name, Parameter.createModelParameter(type, name)); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Method to initialize (declare and assign) a model-parameter. |
| 101 | * |
| 102 | * @param type |
| 103 | * the model-parameter's type |
| 104 | * @param name |
| 105 | * the model-parameter's name |
| 106 | * @param value |
| 107 | * the model-parameter's value |
| 108 | */ |
| 109 | public void initializeModelParameter(Class<?> type, String name, Object value) |
| 110 | { |
| 111 | _parameters.put(name, Parameter.createModelParameter(type, name, value)); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Method to assign a value to a model-parameter declared previously. |
| 116 | * |
| 117 | * @param name |
| 118 | * the model-parameter's name |
| 119 | * @param value |
| 120 | * the model-parameter's value |
| 121 | */ |
| 122 | public void assignModelParameter(String name, Object value) |
| 123 | { |
| 124 | Parameter param = _parameters.get(name); |
| 125 | |
| 126 | if (param.getParameterType() == Parameter.ParameterType.MODELPARAMETER) |
| 127 | { |
| 128 | param.setValue(value); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | //TODO: (Exception) param not found. |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Method assign a value to an experiment-parameter declared |
| 138 | * previously by calling the model's parameter-manager method |
| 139 | * to declare an experiment-parameter |
| 140 | * |
| 141 | * @param name |
| 142 | * the parameter's name |
| 143 | * @param value |
| 144 | * the parameter's value |
| 145 | */ |
| 146 | public void assignExperimentParameter(String name, Object value) |
| 147 | { |
| 148 | Parameter param = _parameters.get(name); |
| 149 | |
| 150 | if (param.getParameterType() == Parameter.ParameterType.EXPERIMENTPARAMETER) |
| 151 | { |
| 152 | param.setValue(value); |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | //TODO: (Exception) param not found. |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns the value of an experiment- or model-parameter. Experiment- |
| 162 | * and model-parameters are accessible through the model, to be able |
| 163 | * to use the parameter's values at design-time. |
| 164 | * |
| 165 | * @param name |
| 166 | * the parameter's name |
| 167 | * @return |
| 168 | * the parameter's value |
| 169 | */ |
| 170 | public Object getParameterValue(String name) |
| 171 | { |
| 172 | Object result = null; |
| 173 | |
| 174 | if (_parameters.containsKey(name)) |
| 175 | { |
| 176 | result = _parameters.get(name).getValue(); |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | //TODO: (Exception) param + name + not declared. |
| 181 | } |
| 182 | |
| 183 | return result; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns all declared parameters. |
| 188 | * |
| 189 | * @return |
| 190 | * the parameters |
| 191 | */ |
| 192 | public Collection<Parameter> getParameters() |
| 193 | { |
| 194 | return _parameters.values(); |
| 195 | } |
| 196 | } |