1 | package desmoj.core.report; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.List; |
5 | |
6 | import desmoj.core.simulator.NamedObject; |
7 | |
8 | /** |
9 | * Controls all reports given by reportable model components used during an |
10 | * experiment. |
11 | * |
12 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
13 | * @author Tim Lechler |
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 | */ |
27 | public class ReportManager extends NamedObject { |
28 | |
29 | /** |
30 | * Keeps references to all Reporters of this experiment |
31 | */ |
32 | private ArrayList<Reporter> _reporters; |
33 | |
34 | /** |
35 | * Creates a new reportmanager with the given name. |
36 | * |
37 | * @param name |
38 | * java.lang.String : the reportmanager's name |
39 | */ |
40 | public ReportManager(String name) { |
41 | |
42 | super(name + "_ReportManager"); // create the NamedObject |
43 | |
44 | _reporters = new ArrayList<Reporter>(); // init list for reporters |
45 | |
46 | } |
47 | |
48 | /** |
49 | * Adds a report to the very end of the vector hanlded by this |
50 | * reportmanager. This is needed to place submodel reporters behind existing |
51 | * model reporters to prevent multiple models to be mixed. |
52 | * |
53 | * @param r |
54 | * desmoj.report.Reporter |
55 | */ |
56 | public void addLast(Reporter r) { |
57 | |
58 | if (r == null) |
59 | return; |
60 | else |
61 | _reporters.add(r); |
62 | |
63 | } |
64 | |
65 | /** |
66 | * De-registers the given reporter from the experiment's reportmanager. The |
67 | * reporter will be removed from the list of current available reporters and |
68 | * thus will not produce output whenever a report has to be produced. When |
69 | * de-registering, the order according to group-ID is preserved. If an |
70 | * invalid parameter is given (i.e. a <code>null</code> reference) this |
71 | * method simply returns |
72 | * |
73 | * @param rep |
74 | * desmoj.report.Reporter : The reporter to be de-registered |
75 | */ |
76 | public void deRegister(Reporter rep) { |
77 | |
78 | // check parameter |
79 | if (rep == null) |
80 | return; // wrong parameters ??? |
81 | _reporters.remove(rep); // no check if contained or not |
82 | // necessary |
83 | |
84 | } |
85 | |
86 | /** |
87 | * Returns a list view of all registered reporters in the appropriate |
88 | * order. |
89 | * |
90 | * @return java.util.List : The list of reporters |
91 | * registered at the reportmanager |
92 | */ |
93 | public List<Reporter> elements() { |
94 | |
95 | return new ArrayList<Reporter>(_reporters); |
96 | |
97 | } |
98 | |
99 | /** |
100 | * Returns a boolean value indicating whether this reportmanager contains |
101 | * reporters to be sent to the reportoutput or not. |
102 | * |
103 | * @return boolean : Is <code>true</code> if the reportmanager conatains |
104 | * reporters, <code>false</code> otherwise |
105 | */ |
106 | public boolean isEmpty() { |
107 | |
108 | return _reporters.isEmpty(); |
109 | |
110 | } |
111 | |
112 | /** |
113 | * Registers the given reporter at the experiment's reportmanager. All |
114 | * reporters registered will be sent to the reportout whenever a report has |
115 | * to be produced. When registering, the order according to group-ID is |
116 | * preserved. |
117 | * |
118 | * @param rep |
119 | * desmoj.report.Reporter : The reporter to be registered |
120 | */ |
121 | public void register(Reporter rep) { |
122 | |
123 | // check parameter |
124 | if (rep == null) |
125 | return; // wrong parameter |
126 | |
127 | if (_reporters.contains(rep)) |
128 | return; // already listed |
129 | |
130 | // register rep with special care for fist element |
131 | if (_reporters.isEmpty()) |
132 | _reporters.add(rep); |
133 | else { |
134 | |
135 | for (int i = 0; i < _reporters.size(); i++) { |
136 | |
137 | if (Reporter.isLarger(rep, _reporters.get(i))) { |
138 | _reporters.add(i, rep); |
139 | return; |
140 | } |
141 | |
142 | } |
143 | |
144 | // if come to here, it must be smaller than all other reporters in |
145 | // vector |
146 | _reporters.add(rep); |
147 | |
148 | } |
149 | |
150 | } |
151 | } |