/Users/lyon/j4p/src/sound/PetriNodes.java
|
1 package sound;
2
3 import java.util.Vector;
4
5
6 public class PetriNodes {
7 private Vector v = new Vector();
8
9
10 public void add(PetriNode pn) {
11 v.addElement(pn);
12 }
13
14 public PetriNode getNext(PetriNode pn) {
15 return getNode(pn.getNext());
16 }
17
18 void buildPetriNet() {
19 for (int i = 0; i < (v.size() - 1); i++) {
20 PetriNode root = getNode(i);
21 root.addNext(i + 1);
22 for (int j = i + 1; j < (v.size() - 1); j++) {
23 PetriNode nextCandidate = getNode(j);
24 if (root.equals(nextCandidate))
25 root.addNext(j + 1);
26 }
27 }
28 }
29
30 public PetriNode addNoDupes(PetriNode pn) {
31 int i = getNode(pn);
32 if (i == -1) {
33 v.addElement(pn);
34 return pn;
35 }
36 PetriNode pn2 = getNode(i);
37 return pn2;
38 }
39
40 /**
41 return -1 if node not present, otherwise return
42 location of node.
43 */
44 public int getNode(PetriNode pn) {
45 for (int i = 0; i < v.size(); i++) {
46 PetriNode pn2 = getNode(i);
47 if (pn2.equals(pn)) return i;
48 }
49 return -1;
50 }
51
52 public void init() {
53 buildPetriNet();
54 }
55
56 public PetriNode getNode(int i) {
57 if (i < v.size())
58 return (PetriNode) v.elementAt(i);
59 return (PetriNode) v.elementAt(0);
60 }
61
62 public void print() {
63 System.out.println(toString());
64 }
65
66 public String toString() {
67 String s = "";
68 for (int i = 0; i < v.size(); i++)
69 s = s + getNode(i).toString() + "\n";
70 return s + "\n";
71 }
72
73 public int getSize() {
74 return v.size();
75 }
76 }