2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
18 package gov.nasa.jpf.report;
20 import gov.nasa.jpf.Config;
21 import gov.nasa.jpf.Error;
22 import gov.nasa.jpf.JPF;
23 import gov.nasa.jpf.JPFListener;
24 import gov.nasa.jpf.search.Search;
25 import gov.nasa.jpf.search.SearchListenerAdapter;
26 import gov.nasa.jpf.vm.VM;
27 import gov.nasa.jpf.vm.Path;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.net.InetAddress;
32 import java.util.ArrayList;
33 import java.util.Date;
34 import java.util.List;
35 import java.util.Properties;
36 import java.util.logging.Logger;
39 * this is our default report generator, which is heavily configurable
40 * via our standard properties. Note this gets instantiated and
41 * registered automatically via JPF.addListeners(), so you don't
42 * have to add it explicitly
45 public class Reporter extends SearchListenerAdapter {
47 public static Logger log = JPF.getLogger("report");
49 protected Config conf;
51 protected Search search;
54 protected Date started, finished;
55 protected Statistics stat; // the object that collects statistics
56 protected List<Publisher> publishers = new ArrayList<Publisher>();
58 protected Thread probeTimer;
60 public Reporter (Config conf, JPF jpf) {
63 search = jpf.getSearch();
65 int probeInterval = conf.getInt("report.probe_interval");
66 boolean reportStats = conf.getBoolean("report.statistics", false) || (probeInterval > 0);
70 addConfiguredPublishers(conf);
72 for (Publisher publisher : publishers) {
73 if (reportStats || publisher.hasToReportStatistics()) {
77 if (publisher instanceof JPFListener) {
78 jpf.addListener((JPFListener)publisher);
83 getRegisteredStatistics();
86 if (probeInterval > 0){
87 probeTimer = createProbeIntervalTimer(probeInterval);
91 protected Thread createProbeIntervalTimer (final int probeInterval){
92 Thread timer = new Thread( new Runnable(){
95 log.info("probe timer running");
96 while (!search.isDone()){
98 Thread.sleep( probeInterval * 1000);
99 search.probeSearch(); // this is only a request
100 } catch (InterruptedException ix) {
104 log.info("probe timer terminating");
107 timer.setDaemon(true);
109 // we don't start before the Search is started
115 * called after the JPF run is finished. Shouldn't be public, but is called by JPF
117 public void cleanUp(){
121 public Statistics getRegisteredStatistics(){
123 if (stat == null){ // none yet, initialize
124 // first, check if somebody registered one explicitly
125 stat = vm.getNextListenerOfType(Statistics.class, null);
127 stat = conf.getInstance("report.statistics.class@stat", Statistics.class);
129 stat = new Statistics();
131 jpf.addListener(stat);
139 void addConfiguredPublishers (Config conf) {
140 String[] def = { "console" };
142 Class<?>[] argTypes = { Config.class, Reporter.class };
143 Object[] args = { conf, this };
145 for (String id : conf.getStringArray("report.publisher", def)){
146 Publisher p = conf.getInstance("report." + id + ".class",
147 Publisher.class, argTypes, args);
151 log.warning("could not instantiate publisher class: " + id);
156 public void addPublisher( Publisher newPublisher){
157 publishers.add(newPublisher);
160 public List<Publisher> getPublishers() {
164 public boolean hasToReportTrace() {
165 for (Publisher p : publishers) {
166 if (p.hasTopic("trace")) {
174 public boolean hasToReportOutput() {
175 for (Publisher p : publishers) {
176 if (p.hasTopic("output")) {
185 public <T extends Publisher> boolean addPublisherExtension (Class<T> publisherCls, PublisherExtension e) {
186 boolean added = false;
187 for (Publisher p : publishers) {
188 Class<?> pCls = p.getClass();
189 if (publisherCls.isAssignableFrom(pCls)) {
198 public <T extends Publisher> void setPublisherItems (Class<T> publisherCls,
199 int category, String[] topics){
200 for (Publisher p : publishers) {
201 if (publisherCls.isInstance(p)) {
202 p.setItems(category,topics);
208 boolean contains (String key, String[] list) {
209 for (String s : list) {
210 if (s.equalsIgnoreCase(key)){
218 //--- the publishing phases
220 protected void publishStart() {
221 for (Publisher publisher : publishers) {
222 publisher.openChannel();
223 publisher.publishProlog();
224 publisher.publishStart();
228 protected void publishTransition() {
229 for (Publisher publisher : publishers) {
230 publisher.publishTransition();
234 protected void publishPropertyViolation() {
235 for (Publisher publisher : publishers) {
236 publisher.publishPropertyViolation();
240 protected void publishConstraintHit() {
241 for (Publisher publisher : publishers) {
242 publisher.publishConstraintHit();
246 protected void publishFinished() {
247 for (Publisher publisher : publishers) {
248 publisher.publishFinished();
249 publisher.publishEpilog();
250 publisher.closeChannel();
254 protected void publishProbe(){
255 for (Publisher publisher : publishers) {
256 publisher.publishProbe();
260 //--- the listener interface that drives report generation
263 public void searchStarted (Search search){
266 if (probeTimer != null){
272 public void stateAdvanced (Search search) {
277 public void searchConstraintHit(Search search) {
278 publishConstraintHit();
282 public void searchProbed (Search search){
287 public void propertyViolated (Search search) {
288 publishPropertyViolation();
292 public void searchFinished (Search search){
293 finished = new Date();
297 if (probeTimer != null){
298 // we could interrupt, but it's a daemon anyways
304 //--- various getters
306 public Date getStartDate() {
310 public Date getFinishedDate () {
318 public Search getSearch() {
322 public List<Error> getErrors () {
323 return search.getErrors();
326 public Error getCurrentError () {
327 return search.getCurrentError();
330 public String getLastSearchConstraint () {
331 return search.getLastSearchConstraint();
334 public String getCurrentErrorId () {
335 Error e = getCurrentError();
337 return "#" + e.getId();
343 public int getNumberOfErrors() {
344 return search.getErrors().size();
347 public Statistics getStatistics() {
351 public Statistics getStatisticsSnapshot () {
358 public long getElapsedTime () {
359 Date d = (finished != null) ? finished : new Date();
360 long t = d.getTime() - started.getTime();
364 public Path getPath (){
365 return vm.getClonedPath();
368 public String getJPFBanner () {
369 StringBuilder sb = new StringBuilder();
371 sb.append("JavaPathfinder core system v");
372 sb.append(JPF.VERSION);
374 String rev = getRevision();
381 sb.append(" - (C) 2005-2014 United States Government. All rights reserved.");
383 if (conf.getBoolean("report.show_repository", false)) {
384 String repInfo = getRepositoryInfo();
385 if (repInfo != null) {
390 return sb.toString();
394 protected String getRevision() {
396 InputStream is = JPF.class.getResourceAsStream(".version");
398 int len = is.available();
399 byte[] data = new byte[len];
402 return new String(data).trim();
408 } catch (Throwable t){
413 protected String getRepositoryInfo() {
415 InputStream is = JPF.class.getResourceAsStream("build.properties");
417 Properties revInfo = new Properties();
420 StringBuffer sb = new StringBuffer();
421 String date = revInfo.getProperty("date");
422 String author = revInfo.getProperty("author");
423 String rev = revInfo.getProperty("rev");
424 String machine = revInfo.getProperty("hostname");
425 String loc = revInfo.getProperty("location");
426 String upstream = revInfo.getProperty("upstream");
428 return String.format("%s %s %s %s %s", date,author,rev,machine,loc);
430 } catch (IOException iox) {
438 public String getHostName () {
440 InetAddress in = InetAddress.getLocalHost();
441 String hostName = in.getHostName();
443 } catch (Throwable t) {
448 public String getUser() {
449 return System.getProperty("user.name");
452 public String getSuT() {
453 return vm.getSUTDescription();
456 public String getJava (){
457 String vendor = System.getProperty("java.vendor");
458 String version = System.getProperty("java.version");
459 return vendor + "/" + version;
462 public String getArch () {
463 String arch = System.getProperty("os.arch");
464 Runtime rt = Runtime.getRuntime();
465 String type = arch + "/" + rt.availableProcessors();
470 public String getOS () {
471 String name = System.getProperty("os.name");
472 String version = System.getProperty("os.version");
473 return name + "/" + version;