3935bfc0947a6578e0cd0d06d0c704ee47809db5
[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       } else if ("dtg".equalsIgnoreCase(item)) {
252         publishDTG();
253       } else if ("config".equalsIgnoreCase(item)){
254         publishJPFConfig();
255       } else if ("sut".equalsIgnoreCase(item)){
256         publishSuT();
257       }
258     }
259
260     if (extensions != null) {
261       for (PublisherExtension e : extensions) {
262         e.publishStart(this);
263       }
264     }
265   }
266
267   public void publishTransition() {
268     for (String topic : transitionItems) {
269       if ("statistics".equalsIgnoreCase(topic)){
270         publishStatistics();
271       }
272     }
273     
274     if (extensions != null) {
275       for (PublisherExtension e : extensions) {
276         e.publishTransition(this);
277       }
278     }
279   }
280
281   public void publishConstraintHit() {
282     for (String item : constraintItems) {
283       if ("constraint".equalsIgnoreCase(item)) {
284         publishConstraint();
285       } else if ("trace".equalsIgnoreCase(item)){
286         publishTrace();
287       } else if ("snapshot".equalsIgnoreCase(item)){
288         publishSnapshot();
289       } else if ("output".equalsIgnoreCase(item)){
290         publishOutput();
291       } else if ("statistics".equalsIgnoreCase(item)){
292         publishStatistics(); // not sure if that is good for anything
293       }
294     }
295
296     if (extensions != null) {
297       for (PublisherExtension e : extensions) {
298         e.publishConstraintHit(this);
299       }
300     }
301   }
302   
303   public void publishProbe(){
304     for (String topic : probeItems) {
305       if ("statistics".equalsIgnoreCase(topic)){
306         publishStatistics();
307       }
308     }    
309     
310     if (extensions != null) {
311       for (PublisherExtension e : extensions) {
312         e.publishProbe(this);
313       }
314     }
315   }
316
317   public void publishPropertyViolation() {
318
319     for (String topic : propertyViolationItems) {
320       if ("error".equalsIgnoreCase(topic)) {
321         publishError();
322       } else if ("trace".equalsIgnoreCase(topic)){
323         publishTrace();
324       } else if ("snapshot".equalsIgnoreCase(topic)){
325         publishSnapshot();
326       } else if ("output".equalsIgnoreCase(topic)){
327         publishOutput();
328       } else if ("statistics".equalsIgnoreCase(topic)){
329         publishStatistics(); // not sure if that is good for anything
330       }
331     }
332
333     if (extensions != null) {
334       for (PublisherExtension e : extensions) {
335         e.publishPropertyViolation(this);
336       }
337     }
338
339   }
340
341   public void publishFinished() {
342     if (extensions != null) {
343       for (PublisherExtension e : extensions) {
344         e.publishFinished(this);
345       }
346     }
347
348     for (String topic : finishedItems) {
349       if ("result".equalsIgnoreCase(topic)){
350         publishResult();
351       } else if ("statistics".equalsIgnoreCase(topic)){
352         publishStatistics();
353       }
354     }
355   }
356
357   protected void publishProlog() {} // XML headers etc
358   protected void publishEpilog() {} // likewise at the end
359
360   //--- our standard topics (only placeholders here)
361   protected void publishJPF() {}
362   protected void publishJPFConfig() {}
363   protected void publishPlatform() {}
364   protected void publishUser() {}
365   protected void publishDTG() {}
366   protected void publishJava() {}
367   protected void publishSuT() {}
368   protected void publishResult() {}
369   protected void publishError() {}
370   protected void publishConstraint() {}
371   protected void publishTrace() {}
372   protected void publishOutput() {}
373   protected void publishSnapshot() {}
374   protected void publishStatistics() {}
375
376   //--- internal helpers
377 }