comparison src/main/gov/nasa/jpf/util/event/EventTree.java @ 0:61d41facf527

initial v8 import (history reset)
author Peter Mehlitz <Peter.C.Mehlitz@nasa.gov>
date Fri, 23 Jan 2015 10:14:01 -0800
parents
children b920e6b1be83
comparison
equal deleted inserted replaced
-1:000000000000 0:61d41facf527
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
19 package gov.nasa.jpf.util.event;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 /**
25 * an abstract class that creates Event trees
26 *
27 * While there is no need to provide specialized Event types or additional event
28 * constructors, concrete subclasses have to provide a createEventTree() implementation.
29 *
30 * A typical implementation looks like this
31 *
32 * public Event createEventTree1() {
33 * return sequence(
34 * event("a"),
35 * alternatives(
36 * event("1"),
37 * iteration(2,
38 * event("x")
39 * )
40 * ),
41 * event("b")
42 * );
43 * }
44 */
45 public class EventTree extends EventConstructor {
46
47 public static final String CONFIG_KEY = "event.class";
48
49 protected Event root;
50
51 /**
52 * this is our purpose in life, which has to be provided by concrete subclasses
53 */
54 public Event createEventTree() {
55 // nothing here, needs to be overridden by subclass to populate tree
56 return null;
57 }
58
59 protected EventTree (){
60 root = createEventTree();
61 }
62
63 protected EventTree (Event root){
64 this.root = root;
65 }
66
67 public Event getRoot(){
68 return root;
69 }
70
71 //--- inspection and debugging
72
73 static final List<Event> NO_EVENTS = new ArrayList<Event>(0);
74
75 public List<Event> visibleEndEvents(){
76 if (root != null){
77 return root.visibleEndEvents();
78 } else {
79 return NO_EVENTS;
80 }
81 }
82
83 public void printPaths(){
84 for (Event es : visibleEndEvents()){
85 es.printPath(System.out);
86 System.out.println('.');
87 }
88 }
89
90 public void printTree (){
91 if (root != null){
92 root.printTree(System.out, 0);
93 }
94 }
95
96 /**
97 * this should be overridden in case we want to check if this is an expected trace
98 * The generic form can only check if this is a valid end event.
99 *
100 * To check for a whole trace, implementors should keep some sort of expected event specs
101 */
102 public boolean checkPath (Event lastEvent){
103 for (Event ee : root.visibleEndEvents()){
104 if (ee.equals(lastEvent)){
105 return true;
106 }
107 }
108
109 return false;
110 }
111
112 public boolean checkPath (Event lastEvent, String[] pathSpecs) {
113 String trace = lastEvent.getPathString(null);
114
115 for (int i = 0; i < pathSpecs.length; i++) {
116 if (trace.equals(pathSpecs[i])) {
117 pathSpecs[i] = null;
118 return true;
119 }
120 }
121
122 return false; // unexpected trace
123 }
124
125 /**
126 * override this if the concrete model keeps track of coverage
127 *
128 * @return [0.0..1.0]
129 */
130 public float getPathCoverage (){
131 throw new UnsupportedOperationException("path coverage not supported by generic EventTree");
132 }
133
134 /**
135 * override this if the concrete model can keep track of coverage
136 * call at the end of execution
137 */
138 public boolean isCompletelyCovered (){
139 throw new UnsupportedOperationException("path coverage not supported by generic EventTree");
140 }
141
142 /**
143 * extend this tree with a new path
144 */
145 public void addPath (Event... path){
146 if (root != null){
147 root.addPath(path.length, path);
148 } else {
149 root = sequence(path);
150 }
151 }
152
153 public Event interleave (Event... otherTrees){
154 if (root != null){
155 return root.interleave( otherTrees);
156
157 } else {
158 if (otherTrees == null || otherTrees.length == 0){
159 return root;
160
161 } else {
162 Event first = null;
163 List<Event> rest = new ArrayList<Event>();
164 for (int i=0; i< otherTrees.length; i++){
165 if (otherTrees[i] != null){
166 if (first == null){
167 first = otherTrees[i];
168 } else {
169 rest.add( otherTrees[i]);
170 }
171 }
172 }
173
174 if (first != null){
175 if (rest.isEmpty()){
176 return first;
177 } else {
178 Event[] ot = new Event[rest.size()];
179 rest.toArray(ot);
180 return first.interleave(ot);
181 }
182
183 } else { // nothing to interleave at all
184 return null;
185 }
186 }
187 }
188 }
189
190 public EventTree interleave (EventTree... otherTrees){
191 Event[] otherRoots = new Event[otherTrees.length];
192 for (int i=0; i<otherRoots.length; i++){
193 otherRoots[i] = otherTrees[i].root;
194 }
195 return new EventTree( interleave( otherRoots));
196 }
197
198 public Event removeSource (Object source){
199 if (root != null){
200 return root.removeSource(source);
201
202 } else { // nothing to remove from
203 return null;
204 }
205 }
206
207 public int getMaxDepth(){
208 if (root != null){
209 return root.getMaxDepth();
210 } else {
211 return 0;
212 }
213 }
214
215 }