Updating main.jpf; Cleaning up the StateReducer.
[jpf-core.git] / src / main / gov / nasa / jpf / listener / StateTracker.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.listener;
19
20 import gov.nasa.jpf.Config;
21 import gov.nasa.jpf.JPF;
22 import gov.nasa.jpf.ListenerAdapter;
23 import gov.nasa.jpf.search.Search;
24
25 import java.io.PrintWriter;
26
27 /**
28  * simple tool to log state changes
29  */
30 public class StateTracker extends ListenerAdapter {
31
32   private final PrintWriter out;
33   private final int logPeriod;
34   volatile private String operation;
35   volatile private String detail;
36   volatile private int depth;
37   volatile private int id;
38
39   public StateTracker (Config conf, JPF jpf) {
40     out = new PrintWriter(System.out, true);
41     logPeriod = conf.getInt("jpf.state_tracker.log_period", 0);
42     Runnable task = new Runnable() {@Override
43         public void run() {logger();}};
44     Thread thread = new Thread(task);
45     thread.setDaemon(true);
46     thread.setName("StateTracker Logger");
47     thread.start();
48   }
49
50   private void logger() {
51     StringBuilder buffer = new StringBuilder();
52
53     buffer.append("----------------------------------- [");
54     int len = buffer.length();
55
56     while (true) {
57       try {
58         Thread.sleep(logPeriod);
59       } catch (InterruptedException e) {
60         e.printStackTrace();
61       }
62
63       buffer.append(depth);
64       buffer.append(']');
65       buffer.append(operation);
66       buffer.append(": ");
67       buffer.append(id);
68
69       if (detail != null) {
70         buffer.append(' ');
71         buffer.append(detail);
72       }
73
74       out.println(buffer.toString());
75
76       buffer.setLength(len);
77     }
78   }
79
80   @Override
81   public void stateRestored(Search search) {
82     id = search.getStateId();
83     depth = search.getDepth();
84     operation = "restored";
85     detail = null;
86   }
87
88   //--- the ones we are interested in
89   @Override
90   public void searchStarted(Search search) {
91     out.println("----------------------------------- search started");
92   }
93
94   @Override
95   public void stateAdvanced(Search search) {
96     id = search.getStateId();
97     depth = search.getDepth();
98     operation = "forward";
99     if (search.isNewState()) {
100       detail = "new";
101     } else {
102       detail = "visited";
103     }
104
105     if (search.isEndState()) {
106       detail += " end";
107     }
108   }
109
110   @Override
111   public void stateBacktracked(Search search) {
112     id = search.getStateId();
113     depth = search.getDepth();
114     operation = "backtrack";
115     detail = null;
116   }
117
118   @Override
119   public void searchFinished(Search search) {
120     out.println("----------------------------------- search finished");
121   }
122
123 }