| 1 | package desmoj.core.simulator; |
| 2 | |
| 3 | import desmoj.core.report.Reporter; |
| 4 | import desmoj.core.report.StandardReporter; |
| 5 | |
| 6 | /** |
| 7 | * All classes that want to publish their information have to extend this class |
| 8 | * in order to provide the necessary functions to represent their information in |
| 9 | * reports. |
| 10 | * |
| 11 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
| 12 | * @author Tim Lechler |
| 13 | * |
| 14 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 15 | * you may not use this file except in compliance with the License. You |
| 16 | * may obtain a copy of the License at |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" |
| 21 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 22 | * or implied. See the License for the specific language governing |
| 23 | * permissions and limitations under the License. |
| 24 | * |
| 25 | */ |
| 26 | public abstract class Reportable extends ModelComponent { |
| 27 | |
| 28 | /** |
| 29 | * Flag indicating if this reportable should produce a report. |
| 30 | */ |
| 31 | private boolean _reportMode; |
| 32 | |
| 33 | /** |
| 34 | * The number of observations that this reportable can report about |
| 35 | */ |
| 36 | private long _observations; |
| 37 | |
| 38 | /** |
| 39 | * stores the last time that this reportable object was reset. |
| 40 | */ |
| 41 | private TimeInstant _lastReset; |
| 42 | |
| 43 | /** |
| 44 | * schedulable (e.g. entity) corresponding to this reportable |
| 45 | * maybe null if no schedulable corresponds to this reportable |
| 46 | */ |
| 47 | private Schedulable _correspondingSchedulable = null; |
| 48 | |
| 49 | /** |
| 50 | * Creates a reportable object with all parameters required. The reportable |
| 51 | * registers itself at the given model |
| 52 | * |
| 53 | * @param name |
| 54 | * java.lang.String : The name of this reportable |
| 55 | * @param owner |
| 56 | * Model : The model this reportable is associated to |
| 57 | * @param showInReport |
| 58 | * boolean : Flag for showing the report Set it to |
| 59 | * <code>true</code> if reportable should show up in report. |
| 60 | * Set it to <code>false</code> if reportable should not be |
| 61 | * shown in report. |
| 62 | * @param showInTrace |
| 63 | * boolean : Flag for showing this reportable in trace files. Set |
| 64 | * it to <code>true</code> if reportable should show up in |
| 65 | * trace. Set it to <code>false</code> if reportable should not |
| 66 | * be shown in trace. |
| 67 | */ |
| 68 | public Reportable(Model owner, String name, boolean showInReport, |
| 69 | boolean showInTrace) { |
| 70 | |
| 71 | super(owner, name, showInTrace); // create the ModelComponent |
| 72 | _reportMode = showInReport; // set report flag |
| 73 | _observations = 0; // reset observations counter |
| 74 | |
| 75 | // check for non-null before registering. |
| 76 | // Registration even if showInReport=false! |
| 77 | // Otherwise such reportables are not reseted at the start of the experiment run! //TODO |
| 78 | if ((owner != null)) { |
| 79 | owner.register(this); |
| 80 | } |
| 81 | |
| 82 | // reset in case experiment running |
| 83 | // (for not yet running experiments, this happens automatically |
| 84 | // when the experiment is started) |
| 85 | if (owner != null && owner.getExperiment() != null && owner.getExperiment().isRunning()) { |
| 86 | this.reset(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Override this method to return the appropriate reporter to the reportable |
| 92 | * object. To ensure that always the appropriate reporter is used, it has to |
| 93 | * be obtained from the reportable object that is of interest. All |
| 94 | * reportable objects in desmoj that want to deliver special repoters have |
| 95 | * to implement this method to hand back the correct reporter. The default |
| 96 | * implementation returns an object of type <code>StandardReporter</code> |
| 97 | * If you want to extend the framework by new concepts, you have to make |
| 98 | * sure, that classes inheriting direct or indirect from reportable have to |
| 99 | * implement this method correctly, especially by implementing an |
| 100 | * appropriate reporter first and give a reference to it back through this |
| 101 | * method. |
| 102 | * |
| 103 | * @return desmoj.report.Reporter : A specific reporter for this reportable |
| 104 | */ |
| 105 | public Reporter createReporter() { |
| 106 | |
| 107 | return new StandardReporter(this); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns the number of observations made by the reportable object. |
| 113 | * |
| 114 | * @return long : The number of observations made by the reportable object. |
| 115 | */ |
| 116 | public long getObservations() { |
| 117 | |
| 118 | return _observations; |
| 119 | |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Increments the number of observations made by this reportable by one (1). |
| 124 | */ |
| 125 | public void incrementObservations() { |
| 126 | |
| 127 | _observations++; |
| 128 | |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Increments the number of observations by the amount given as parameter. |
| 133 | * |
| 134 | * @param multiObservations |
| 135 | * long : The number to increase the number of observations by |
| 136 | */ |
| 137 | public void incrementObservations(long multiObservations) { |
| 138 | |
| 139 | _observations += multiObservations; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Checks if this reportable produces a report. |
| 145 | * |
| 146 | * @return boolean : true if report will be produced, false otherwise |
| 147 | */ |
| 148 | public boolean reportIsOn() { |
| 149 | |
| 150 | return _reportMode; |
| 151 | |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Switches report mode to prevent this reportable to produce reports. |
| 156 | */ |
| 157 | public void reportOff() { |
| 158 | |
| 159 | _reportMode = false; |
| 160 | |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Switches report mode of this reportable on to produce a report. |
| 165 | */ |
| 166 | public void reportOn() { |
| 167 | |
| 168 | _reportMode = true; |
| 169 | |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Resets the counter for observations made by this reportable. The point of |
| 174 | * simulation time this method was called will be stored and can be |
| 175 | * retrieved using method <code>resetAt()</code>. |
| 176 | */ |
| 177 | public void reset() { |
| 178 | |
| 179 | _observations = 0; // reset observations |
| 180 | _lastReset = presentTime(); // register the reset time |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Shows the point in simulation time when the last reset of this reportable |
| 185 | * was made. |
| 186 | * |
| 187 | * @return TimeInstant : The point of simulation time of the last reset. |
| 188 | */ |
| 189 | public TimeInstant resetAt() { |
| 190 | |
| 191 | return _lastReset; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Gets the schedulable (e.g. entity) corresponding to this reportable |
| 196 | * maybe null if no schedulable corresponds to this reportable |
| 197 | * |
| 198 | * @return Schedulable : schedulable (e.g. entity) corresponding to this reportable (maybe null if no schedulable corresponds to this reportable!) |
| 199 | */ |
| 200 | public Schedulable getCorrespondingSchedulable() { |
| 201 | return _correspondingSchedulable; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Sets the schedulable (e.g. entity) corresponding to this Reportable. |
| 206 | * Maybe null if no schedulable corresponds to this reportable. |
| 207 | * If set, schedulable must have the same model as this reportable! |
| 208 | * A model may not have a corresponding schedulable. |
| 209 | * |
| 210 | * @param correspondingSchedulable |
| 211 | * Schedulable : The Schedulable corresponding to this Reportable. |
| 212 | */ |
| 213 | public void setCorrespondingSchedulable(Schedulable correspondingSchedulable) { |
| 214 | |
| 215 | if (this instanceof Model){ |
| 216 | this.sendWarning( |
| 217 | "Model may not have a corresponding schedulable. Method call ignored.", |
| 218 | "Reportable.setCorrespondingSchedulable(Schedulable)", |
| 219 | "Model may not have a corresponding schedulable, because Model contains many corresponding schedulables.", |
| 220 | "Do not set corresponding schedulable to a model!" |
| 221 | ); |
| 222 | return; |
| 223 | }; |
| 224 | |
| 225 | if(correspondingSchedulable!=null && this.getModel()!=correspondingSchedulable.getModel()) { |
| 226 | this.sendWarning( |
| 227 | "Schedulable to correspond to this Reportable must belong to the same model!", |
| 228 | "Reportable.setCorrespondingSchedulable(Schedulable)", |
| 229 | "Model of Reportable and corresponding schedulable must be identical.", |
| 230 | "Do not set a corresponding schedulable to another model's Schedulable."); |
| 231 | return; |
| 232 | } |
| 233 | this._correspondingSchedulable = correspondingSchedulable; |
| 234 | } |
| 235 | } |