Fixing a few bugs in the statistics printout.
[jpf-core.git] / src / main / gov / nasa / jpf / report / Publisher.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.report;
19
20 import gov.nasa.jpf.Config;
21
22 import java.io.PrintWriter;
23 import java.text.DateFormat;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.List;
27
28 /**
29  * abstract base for all format specific publishers. Note that this interface
30  * also has to work for non-stream based reporting, i.e. within Eclipse
31  * (we don't want to re-parse from text reports there)
32  */
33 public abstract class Publisher {
34
35   // output phases
36   public static final int START = 1;
37   public static final int TRANSITION = 2;
38   public static final int PROBE = 3;
39   public static final int CONSTRAINT = 4;
40   public static final int PROPERTY_VIOLATION = 5;
41   public static final int FINISHED = 6;
42
43   protected Config conf;
44   protected Reporter reporter; // our master
45
46   protected String[] startItems = {};
47   protected String[] transitionItems = {};
48   protected String[] propertyViolationItems = {};
49   protected String[] constraintItems = {};
50   protected String[] finishedItems = {};
51   protected String[] probeItems = {};
52
53   DateFormat dtgFormatter = DateFormat.getDateTimeInstance(DateFormat.SHORT,
54       DateFormat.SHORT);
55
56   ArrayList<PublisherExtension> extensions;
57
58   /**
59    * to be initialized in openChannel
60    * NOTE - not all publishers need to have one
61    */
62   protected PrintWriter out;
63
64   public PrintWriter getOut () {
65     return out;
66   }
67
68   protected Publisher (Config conf, Reporter reporter){
69     this.conf = conf;
70     this.reporter = reporter;
71
72     setTopicItems();
73   }
74
75   public void setItems (int category, String[] newTopics){
76     switch (category){
77     case START:
78       startItems = newTopics; break;
79     case PROBE:
80       probeItems = newTopics; break;
81     case TRANSITION:
82       transitionItems = newTopics; break;
83     case CONSTRAINT:
84       constraintItems = newTopics; break;
85     case PROPERTY_VIOLATION:
86       propertyViolationItems = newTopics; break;
87     case FINISHED:
88       finishedItems = newTopics; break;
89     default:
90       Reporter.log.warning("unknown publisher topic: " + category);
91     }
92   }
93
94   public abstract String getName();
95
96   protected void setTopicItems () {
97     setTopicItems(getName());
98   }
99   
100   protected void setTopicItems (String name) {
101     String prefix = "report." + name;
102     startItems = conf.getStringArray(prefix + ".start", startItems);
103     transitionItems = conf.getStringArray(prefix + ".transition", transitionItems);
104     probeItems = conf.getStringArray(prefix + ".probe", transitionItems);
105     propertyViolationItems = conf.getStringArray(prefix + ".property_violation", propertyViolationItems);
106     constraintItems = conf.getStringArray(prefix + ".constraint", constraintItems);
107     finishedItems = conf.getStringArray(prefix + ".finished", finishedItems);    
108   }
109   
110   public void addExtension (PublisherExtension ext) {
111     if (extensions == null) {
112       extensions = new ArrayList<PublisherExtension>();
113     }
114     extensions.add(ext);
115   }
116
117   // <2do> should not be a list we can add to
118   private static final List<PublisherExtension> EMPTY_LIST = new ArrayList<PublisherExtension>(0);
119   
120   public List<PublisherExtension> getExtensions(){
121     if (extensions != null){
122       return extensions;
123     } else {
124       return EMPTY_LIST; // make life easier for callers
125     }
126   }
127   
128   public String getLastErrorId() {
129     return reporter.getCurrentErrorId();
130   }
131
132   public boolean hasTopic (String topic) {
133     for (String s : startItems) {
134       if (s.equalsIgnoreCase(topic)){
135         return true;
136       }
137     }
138     for (String s : transitionItems) {
139       if (s.equalsIgnoreCase(topic)){
140         return true;
141       }
142     }
143     for (String s : constraintItems) {
144       if (s.equalsIgnoreCase(topic)){
145         return true;
146       }
147     }
148     for (String s : propertyViolationItems) {
149       if (s.equalsIgnoreCase(topic)){
150         return true;
151       }
152     }
153     for (String s : finishedItems) {
154       if (s.equalsIgnoreCase(topic)){
155         return true;
156       }
157     }
158
159     return false;
160   }
161
162   public String formatDTG (Date date) {
163     return dtgFormatter.format(date);
164   }
165
166   /**
167   static public String _formatHMS (long t) {
168     t /= 1000; // convert to sec
169
170     long s = t % 60;
171     long h = t / 3600;
172     long m = (t % 3600) / 60;
173
174     StringBuilder sb = new StringBuilder();
175     sb.append(h);
176     sb.append(':');
177     if (m < 10){
178       sb.append('0');
179     }
180     sb.append(m);
181     sb.append(':');
182     if (s < 10){
183       sb.append('0');
184     }
185     sb.append(s);
186
187     return sb.toString();
188   }
189   **/
190
191   static char[] tBuf = { '0', '0', ':', '0', '0', ':', '0', '0' };
192   
193   static synchronized public String formatHMS (long t) {
194     int h = (int) (t / 3600000);
195     int m = (int) ((t / 60000) % 60);
196     int s = (int) ((t / 1000) % 60);
197     
198     tBuf[0] = (char) ('0' + (h / 10));
199     tBuf[1] = (char) ('0' + (h % 10));
200     
201     tBuf[3] = (char) ('0' + (m / 10));
202     tBuf[4] = (char) ('0' + (m % 10));
203     
204     tBuf[6] = (char) ('0' + (s / 10));
205     tBuf[7] = (char) ('0' + (s % 10));
206     
207     return new String(tBuf);
208   }
209   
210   public String getReportFileName (String key) {
211     String fname = conf.getString(key);
212     if (fname == null){
213       fname = conf.getString("report.file");
214       if (fname == null) {
215         fname = "report";
216       }
217     }
218
219     return fname;
220   }
221
222   public void publishTopicStart (String topic) {
223     // to be done by subclasses
224   }
225
226   public void publishTopicEnd (String topic) {
227     // to be done by subclasses
228   }
229
230   public boolean hasToReportStatistics() {
231     for (String s : finishedItems) {
232       if ("statistics".equalsIgnoreCase(s)){
233         return true;
234       }
235     }
236     return false;
237   }
238
239   //--- open/close streams etc
240   protected void openChannel(){}
241   protected void closeChannel(){}
242
243   //--- if you have different preferences about when to report things, override those
244   public void publishStart() {
245     for (String item : startItems) {
246       if ("jpf".equalsIgnoreCase(item)){
247         publishJPF();
248       } else if ("platform".equalsIgnoreCase(item)){
249         publishPlatform();
250       } else if ("user".equalsIgnoreCase(item)) {
251         publishUser();
252       } else if ("dtg".equalsIgnoreCase(item)) {
253         publishDTG();
254       } else if ("config".equalsIgnoreCase(item)){
255         publishJPFConfig();
256       } else if ("sut".equalsIgnoreCase(item)){
257         publishSuT();
258       }
259     }
260
261     if (extensions != null) {
262       for (PublisherExtension e : extensions) {
263         e.publishStart(this);
264       }
265     }
266   }
267
268   public void publishTransition() {
269     for (String topic : transitionItems) {
270       if ("statistics".equalsIgnoreCase(topic)){
271         publishStatistics();
272       }
273     }
274     
275     if (extensions != null) {
276       for (PublisherExtension e : extensions) {
277         e.publishTransition(this);
278       }
279     }
280   }
281
282   public void publishConstraintHit() {
283     for (String item : constraintItems) {
284       if ("constraint".equalsIgnoreCase(item)) {
285         publishConstraint();
286       } else if ("trace".equalsIgnoreCase(item)){
287         publishTrace();
288       } else if ("snapshot".equalsIgnoreCase(item)){
289         publishSnapshot();
290       } else if ("output".equalsIgnoreCase(item)){
291         publishOutput();
292       } else if ("statistics".equalsIgnoreCase(item)){
293         publishStatistics(); // not sure if that is good for anything
294       }
295     }
296
297     if (extensions != null) {
298       for (PublisherExtension e : extensions) {
299         e.publishConstraintHit(this);
300       }
301     }
302   }
303   
304   public void publishProbe(){
305     for (String topic : probeItems) {
306       if ("statistics".equalsIgnoreCase(topic)){
307         publishStatistics();
308       }
309     }    
310     
311     if (extensions != null) {
312       for (PublisherExtension e : extensions) {
313         e.publishProbe(this);
314       }
315     }
316   }
317
318   public void publishPropertyViolation() {
319
320     for (String topic : propertyViolationItems) {
321       if ("error".equalsIgnoreCase(topic)) {
322         publishError();
323       } else if ("trace".equalsIgnoreCase(topic)){
324         publishTrace();
325       } else if ("snapshot".equalsIgnoreCase(topic)){
326         publishSnapshot();
327       } else if ("output".equalsIgnoreCase(topic)){
328         publishOutput();
329       } else if ("statistics".equalsIgnoreCase(topic)){
330         publishStatistics(); // not sure if that is good for anything
331       }
332     }
333
334     if (extensions != null) {
335       for (PublisherExtension e : extensions) {
336         e.publishPropertyViolation(this);
337       }
338     }
339
340   }
341
342   public void publishFinished() {
343     if (extensions != null) {
344       for (PublisherExtension e : extensions) {
345         e.publishFinished(this);
346       }
347     }
348
349     for (String topic : finishedItems) {
350       if ("result".equalsIgnoreCase(topic)){
351         publishResult();
352       } else if ("statistics".equalsIgnoreCase(topic)){
353         publishStatistics();
354       }
355     }
356   }
357
358   protected void publishProlog() {} // XML headers etc
359   protected void publishEpilog() {} // likewise at the end
360
361   //--- our standard topics (only placeholders here)
362   protected void publishJPF() {}
363   protected void publishJPFConfig() {}
364   protected void publishPlatform() {}
365   protected void publishUser() {}
366   protected void publishDTG() {}
367   protected void publishJava() {}
368   protected void publishSuT() {}
369   protected void publishResult() {}
370   protected void publishError() {}
371   protected void publishConstraint() {}
372   protected void publishTrace() {}
373   protected void publishOutput() {}
374   protected void publishSnapshot() {}
375   protected void publishStatistics() {}
376
377   //--- internal helpers
378 }