KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > Path


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf;
20
21 import gov.nasa.jpf.util.Printable;
22
23 import java.io.PrintWriter JavaDoc;
24
25 import java.util.LinkedList JavaDoc;
26
27
28 /**
29  * Path represents the data structure in which a execution trace is recorded.
30  *
31  * It usually gets created by a VirtualMachine, can be stored in a file
32  * via the JPF '-save-path' option, re-executed with '-execute-path', or
33  * explicitly loaded via 'JPF.loadPath(fileName)'
34  *
35  * A path instance is specific to a certain 'application', and consists of a
36  * list of 'Transition' objects
37  */

38 public class Path implements Printable {
39   String JavaDoc application;
40   private LinkedList JavaDoc list;
41
42   public Path (String JavaDoc app) {
43     application = app;
44     list = new LinkedList JavaDoc();
45   }
46
47   private Path (String JavaDoc app, LinkedList JavaDoc l) {
48     application = app;
49     list = l;
50   }
51
52   public String JavaDoc getApplication () {
53     return application;
54   }
55
56   public Transition getLast () {
57     if (list.size() > 0) {
58       return (Transition) list.getLast();
59     } else {
60       return null;
61     }
62   }
63
64   public void add (Transition t) {
65     list.add(t);
66   }
67
68   public Object JavaDoc clone () {
69     // <2do> clone path elements, too
70
return new Path(application, (LinkedList JavaDoc) list.clone());
71   }
72
73   public Transition get (int pos) {
74     return (Transition) list.get(pos);
75   }
76
77   public int length () {
78     return list.size();
79   }
80
81   public boolean hasOutput () {
82     int len = list.size();
83     for (int i=0; i<len; i++) {
84       Transition t = (Transition)list.get(i);
85       if (t.getOutput() != null) {
86         return true;
87       }
88     }
89     
90     return false;
91   }
92   
93   public void printOutputOn (PrintWriter JavaDoc pw) {
94     int i;
95     int length = list.size();
96     Transition t;
97     String JavaDoc s;
98     
99     for (i=0; i<length; i++) {
100       t = (Transition)list.get(i);
101       s = t.getOutput();
102       if (s != null) {
103         pw.print(s);
104       }
105     }
106   }
107   
108   public void printOn (PrintWriter JavaDoc pw) {
109     int length = list.size();
110     Object JavaDoc entry;
111
112     for (int index = 0; index < length; index++) {
113       pw.print("Transition #");
114       pw.print(index);
115       
116       if ((entry = list.get(index)) != null) {
117         pw.print(' ');
118
119         if (entry instanceof Printable) {
120           ((Printable) entry).printOn(pw);
121         } else {
122           pw.print(entry);
123           pw.flush();
124         }
125       }
126     }
127   }
128
129   public void removeLast () {
130     list.removeLast();
131   }
132 }
133
Popular Tags