| 1 | package desmoj.core.report; |
| 2 | |
| 3 | import desmoj.core.simulator.Model; |
| 4 | import desmoj.core.simulator.Reportable; |
| 5 | |
| 6 | import java.util.List; |
| 7 | import java.util.ArrayList; |
| 8 | |
| 9 | /** |
| 10 | * The Reporter for a model. Carries references to all Reportables and submodels |
| 11 | * of the Model it reports about. This enables it to produce a vector containing |
| 12 | * the reportable's reporters ordered by group-ID. It also takes care to |
| 13 | * retrieve the submodel's reporters recursively. |
| 14 | * |
| 15 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
| 16 | * @author Tim Lechler |
| 17 | * |
| 18 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 19 | * you may not use this file except in compliance with the License. You |
| 20 | * may obtain a copy of the License at |
| 21 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | * |
| 23 | * Unless required by applicable law or agreed to in writing, software |
| 24 | * distributed under the License is distributed on an "AS IS" |
| 25 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 26 | * or implied. See the License for the specific language governing |
| 27 | * permissions and limitations under the License. |
| 28 | * |
| 29 | */ |
| 30 | public class ModelReporter extends desmoj.core.report.Reporter { |
| 31 | |
| 32 | /** |
| 33 | * The list containing the reportable's reporters ordered by group-ID. |
| 34 | */ |
| 35 | private ArrayList<Reporter> _reporters; |
| 36 | |
| 37 | /** |
| 38 | * Creates a reporter for the given model. This special reporter retrieves |
| 39 | * all other reporters associated to the reportable objects registered at |
| 40 | * the model. These are collected in a vector, ordered by group-ID to be |
| 41 | * sent to the report output when a report is required. |
| 42 | * |
| 43 | * @param model |
| 44 | * Model : The model to report about. |
| 45 | */ |
| 46 | public ModelReporter(Model model) { |
| 47 | |
| 48 | super(model); |
| 49 | List<Reportable> reportables = model.getReportables(); |
| 50 | |
| 51 | _reporters = new ArrayList<Reporter>(); |
| 52 | groupID = 2147483647; // highest groupID possible, so always first in |
| 53 | // order |
| 54 | groupHeading = "Model " + source.getName(); |
| 55 | numColumns = 1; |
| 56 | columns = new String[numColumns]; |
| 57 | columns[0] = "Description"; |
| 58 | entries = new String[numColumns]; |
| 59 | |
| 60 | if (reportables == null) |
| 61 | return; // nothing to insert |
| 62 | if (reportables.isEmpty()) |
| 63 | return; // still nothing to insert |
| 64 | |
| 65 | // insert all reportable's reporters in order of group-id |
| 66 | // buffer frequent used variables for faster access |
| 67 | Reporter repoBuff; // buffer variable for Reporter |
| 68 | |
| 69 | for (Reportable r : reportables) { |
| 70 | |
| 71 | if (r != null) { |
| 72 | |
| 73 | repoBuff = r.createReporter(); |
| 74 | |
| 75 | if (repoBuff != null) { |
| 76 | |
| 77 | // insert according to group-ID |
| 78 | if (_reporters.isEmpty()) { |
| 79 | _reporters.add(repoBuff); |
| 80 | } else { // now sort until pos. found |
| 81 | |
| 82 | for (int k = 0; k < _reporters.size(); k++) { |
| 83 | |
| 84 | if (Reporter.isLarger(_reporters.get(k), repoBuff)) { |
| 85 | _reporters.add(k + 1, repoBuff); |
| 86 | } // endif pos. found |
| 87 | |
| 88 | } // endfor search position |
| 89 | |
| 90 | } // endelse special care for first reporter |
| 91 | |
| 92 | } // endif repoBuff == null |
| 93 | |
| 94 | } // endif ableBuff == null |
| 95 | |
| 96 | } // endfor |
| 97 | |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns an array of strings each containing the data for the |
| 102 | * corresponding column in array <code>columns[]</code>. The |
| 103 | * modelreporter returns the description and the last reset time for the |
| 104 | * associated model. |
| 105 | * |
| 106 | * @return java.lang.String[] : Array containing the data for reporting |
| 107 | */ |
| 108 | public String[] getEntries() { |
| 109 | |
| 110 | entries[0] = ((Model) source).description() |
| 111 | + " Report drawn at " |
| 112 | + ((Model) source).presentTime() |
| 113 | + ". Last reset at " |
| 114 | + source.resetAt().toString() + "."; |
| 115 | |
| 116 | return entries; |
| 117 | |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns a list of view of all reportable's reporters ordered by |
| 122 | * group-ID. |
| 123 | * |
| 124 | * @return java.util.List<Reporter> : The vector containing the reporters associated |
| 125 | * to the modelreporter |
| 126 | */ |
| 127 | List<Reporter> getReporters() { |
| 128 | |
| 129 | return new ArrayList<Reporter>(_reporters); |
| 130 | |
| 131 | } |
| 132 | } |