fr_b02 / petri / src / main / Network.java @ 5
Historique | Voir | Annoter | Télécharger (3,611 ko)
1 |
package main; |
---|---|
2 |
|
3 |
|
4 |
import java.util.ArrayList; |
5 |
import java.util.List; |
6 |
|
7 |
import arcElement.Arc; |
8 |
import arcElement.ArcCleaner; |
9 |
import arcElement.ArcEntering; |
10 |
import arcElement.ArcOutering; |
11 |
import arcElement.ArcZero; |
12 |
import element.Place; |
13 |
import element.Place.UndefinedToken; |
14 |
import element.Transition; |
15 |
|
16 |
public class Network { |
17 |
public static int ArcOutering = 2; |
18 |
public static int ArcCleaner = 1; |
19 |
public static int ArcZero = 0; |
20 |
public static int ArcEntering = 3; |
21 |
|
22 |
private List<Place> placeList; |
23 |
private List<Transition> listTransition; |
24 |
public Network() {
|
25 |
this.placeList=new ArrayList<Place>(); |
26 |
this.listTransition=new ArrayList<Transition>(); |
27 |
|
28 |
} |
29 |
|
30 |
|
31 |
public void addTransition() { |
32 |
Transition transition = new Transition();
|
33 |
listTransition.add(transition); |
34 |
} |
35 |
public void addPlace(int value) throws UndefinedToken { |
36 |
Place place = new Place(value);
|
37 |
placeList.add(place); |
38 |
} |
39 |
@SuppressWarnings("serial") |
40 |
public class UndefinedIdentifier extends Exception{ |
41 |
public UndefinedIdentifier() {
|
42 |
super("the identifier cannot take this value"); |
43 |
} |
44 |
|
45 |
} |
46 |
public void addArc(Transition transition, Place place, int value, int identifier) throws UndefinedIdentifier { |
47 |
|
48 |
if (identifier==ArcZero) {
|
49 |
transition.getArcList().add(new ArcZero(place, value));
|
50 |
} |
51 |
else if(identifier==ArcCleaner) { |
52 |
transition.getArcList().add(new ArcCleaner(place));
|
53 |
} |
54 |
else if(identifier==ArcOutering) { |
55 |
transition.getArcList().add(new ArcOutering(place,value));
|
56 |
}else if(identifier==ArcEntering) { |
57 |
transition.getArcList().add( new ArcEntering(place,value));
|
58 |
}else {
|
59 |
throw new UndefinedIdentifier(); |
60 |
} |
61 |
|
62 |
} |
63 |
public void deleteArc(Arc a) { |
64 |
Arc aToDelete = null;
|
65 |
for(Transition transition: listTransition) {
|
66 |
for(Arc arc:transition.getArcList()) {
|
67 |
if(arc.equals(a)) {
|
68 |
aToDelete=arc; } |
69 |
} |
70 |
transition.getArcList().remove(aToDelete); |
71 |
} |
72 |
|
73 |
} |
74 |
|
75 |
public void deletePlace( Place place) { |
76 |
Arc aToDelete = null;
|
77 |
for(Transition transition: listTransition) {
|
78 |
for(Arc arc:transition.getArcList()) {
|
79 |
if(arc.getPlace().equals(place)) {
|
80 |
aToDelete=arc; |
81 |
} |
82 |
} |
83 |
deleteArc( aToDelete); |
84 |
placeList.remove(place); |
85 |
|
86 |
} |
87 |
|
88 |
} |
89 |
|
90 |
public void deleteTransition(Transition transition) { |
91 |
|
92 |
listTransition.remove(transition); |
93 |
} |
94 |
|
95 |
public void fire(Transition transition) { |
96 |
if (transition.isPullable()==true) { |
97 |
transition.doTransition(); |
98 |
} |
99 |
} |
100 |
public String toString() { |
101 |
String res= "["; |
102 |
for(int i=0;listTransition.size()>i;i++ ) { |
103 |
res=res+"transtion "+i+" :"+listTransition.get(i).toString()+", "; |
104 |
} |
105 |
res=res+"]";
|
106 |
return res;
|
107 |
} |
108 |
public static void main(String[] args) throws UndefinedToken, UndefinedIdentifier { |
109 |
|
110 |
Network n0=new Network();
|
111 |
n0.addPlace(5);
|
112 |
n0.addPlace(4);
|
113 |
n0.addTransition(); |
114 |
n0.addArc(n0.listTransition.get(0), n0.placeList.get(0), 2, ArcEntering); |
115 |
n0.addArc(n0.listTransition.get(0), n0.placeList.get(1), 2, ArcOutering); |
116 |
System.out.println(""); |
117 |
System.out.println("RESEAU DE PETRI une transtion, un arcOutering un arcEntering"); |
118 |
System.out.println(""); |
119 |
System.out.println("situtation initial : "); |
120 |
System.out.println(n0);
|
121 |
|
122 |
System.out.println("fire 1 : "); |
123 |
n0.fire(n0.listTransition.get(0));
|
124 |
System.out.println(n0);
|
125 |
|
126 |
System.out.println("fire 2 : "); |
127 |
n0.fire(n0.listTransition.get(0));
|
128 |
System.out.println(n0);
|
129 |
|
130 |
System.out.println("fire 3 : "); |
131 |
n0.fire(n0.listTransition.get(0));
|
132 |
System.out.println(n0);
|
133 |
|
134 |
|
135 |
} |
136 |
|
137 |
|
138 |
public List<Place> getPlaceList() { |
139 |
return placeList;
|
140 |
} |
141 |
|
142 |
|
143 |
public List<Transition> getListTransition() { |
144 |
return listTransition;
|
145 |
} |
146 |
|
147 |
} |