Statistiques
| Révision:

fr_b02 / petri / src / main / Network.java @ 7

Historique | Voir | Annoter | Télécharger (4,268 ko)

1 2 a19coudr
package main;
2
3
4
import java.util.ArrayList;
5
import java.util.List;
6
7 4 a19coudr
import arcElement.Arc;
8
import arcElement.ArcCleaner;
9
import arcElement.ArcEntering;
10
import arcElement.ArcOutering;
11
import arcElement.ArcZero;
12 3 a19coudr
import element.Place;
13 4 a19coudr
import element.Place.UndefinedToken;
14 3 a19coudr
import element.Transition;
15
16 2 a19coudr
public class Network {
17 7 a19coudr
        //constant for the methode add arc
18 2 a19coudr
        public static int ArcOutering = 2;
19
        public static int ArcCleaner = 1;
20
        public static int ArcZero = 0;
21
        public static int ArcEntering = 3;
22 5 a19coudr
23 2 a19coudr
        private List<Place> placeList;
24
        private List<Transition> listTransition;
25
        public Network() {
26
                this.placeList=new ArrayList<Place>();
27
                this.listTransition=new ArrayList<Transition>();
28
29
        }
30
31 7 a19coudr
        /**
32
         * create a new transition
33
         */
34 2 a19coudr
        public void addTransition() {
35
                Transition transition = new Transition();
36
                listTransition.add(transition);
37
        }
38 7 a19coudr
        /**
39
         * create a new place
40
         */
41 4 a19coudr
        public void addPlace(int value) throws UndefinedToken {
42 2 a19coudr
                Place place = new Place(value);
43
                placeList.add(place);
44
        }
45 5 a19coudr
        @SuppressWarnings("serial")
46
        public class UndefinedIdentifier extends Exception{
47
                public UndefinedIdentifier() {
48
                        super("the identifier cannot take this value");
49
                }
50 2 a19coudr
51 5 a19coudr
        }
52 7 a19coudr
        /**
53
         * create a new arc
54
         * @param transtion the transition link to this arc
55
         * @param place the place link to this arc
56
         * @param the value of this arc
57
         * @param identifier the integer that correspond to the type of arc (define as static)
58
         */
59 5 a19coudr
        public void addArc(Transition transition, Place place, int value, int identifier) throws UndefinedIdentifier {
60 2 a19coudr
61
                if (identifier==ArcZero) {
62
                        transition.getArcList().add(new ArcZero(place, value));
63
                }
64
                else if(identifier==ArcCleaner) {
65
                        transition.getArcList().add(new ArcCleaner(place));
66
                }
67
                else if(identifier==ArcOutering) {
68
                        transition.getArcList().add(new ArcOutering(place,value));
69
                }else if(identifier==ArcEntering) {
70
                        transition.getArcList().add( new ArcEntering(place,value));
71 5 a19coudr
                }else {
72
                        throw new UndefinedIdentifier();
73 2 a19coudr
                }
74
75
        }
76 7 a19coudr
        /**
77
         * delete an arc
78
         */
79 2 a19coudr
        public void deleteArc(Arc a) {
80 5 a19coudr
                Arc aToDelete = null;
81 2 a19coudr
                for(Transition transition: listTransition) {
82
                        for(Arc arc:transition.getArcList()) {
83
                                if(arc.equals(a)) {
84 5 a19coudr
                                        aToDelete=arc;                                }
85 2 a19coudr
                        }
86 5 a19coudr
                        transition.getArcList().remove(aToDelete);
87 2 a19coudr
                }
88
89
        }
90 7 a19coudr
        /**
91
         * delete a place an the arcs link to it
92
         */
93 2 a19coudr
        public void deletePlace( Place place) {
94 7 a19coudr
                List<Arc> aToDelete = new ArrayList<Arc>();
95 2 a19coudr
                for(Transition transition: listTransition) {
96
                        for(Arc arc:transition.getArcList()) {
97
                                if(arc.getPlace().equals(place)) {
98 7 a19coudr
                                        aToDelete.add(arc);
99 2 a19coudr
                                }
100
                        }
101 7 a19coudr
                        aToDelete.forEach((Arc a)->{
102
                                deleteArc(a);
103
                        });
104 5 a19coudr
                        placeList.remove(place);
105
106 2 a19coudr
                }
107 5 a19coudr
108 2 a19coudr
        }
109 7 a19coudr
        /**
110
         * delete a transition
111
         */
112 2 a19coudr
        public void deleteTransition(Transition transition) {
113 5 a19coudr
114 2 a19coudr
                listTransition.remove(transition);
115
        }
116 7 a19coudr
        /**
117
         * fire the transition selected
118
         */
119 2 a19coudr
        public void fire(Transition transition) {
120
                if (transition.isPullable()==true) {
121
                        transition.doTransition();
122
                }
123
        }
124 7 a19coudr
        /**
125
         * string  representation of a network
126
         */
127 2 a19coudr
        public String toString() {
128
                String res= "[";
129
                for(int i=0;listTransition.size()>i;i++ ) {
130
                        res=res+"transtion "+i+" :"+listTransition.get(i).toString()+", ";
131
                }
132
                res=res+"]";
133
                return res;
134
        }
135 7 a19coudr
        //example of Petri network
136 5 a19coudr
        public static void main(String[] args) throws UndefinedToken, UndefinedIdentifier {
137
138 2 a19coudr
                Network n0=new Network();
139
                n0.addPlace(5);
140
                n0.addPlace(4);
141
                n0.addTransition();
142
                n0.addArc(n0.listTransition.get(0), n0.placeList.get(0), 2, ArcEntering);
143
                n0.addArc(n0.listTransition.get(0), n0.placeList.get(1), 2, ArcOutering);
144
                System.out.println("");
145 5 a19coudr
                System.out.println("RESEAU DE PETRI une transtion, un arcOutering un arcEntering");
146 2 a19coudr
                System.out.println("");
147
                System.out.println("situtation initial : ");
148
                System.out.println(n0);
149 5 a19coudr
150 2 a19coudr
                System.out.println("fire 1 : ");
151
                n0.fire(n0.listTransition.get(0));
152
                System.out.println(n0);
153 5 a19coudr
154 2 a19coudr
                System.out.println("fire 2 : ");
155
                n0.fire(n0.listTransition.get(0));
156
                System.out.println(n0);
157 5 a19coudr
158 2 a19coudr
                System.out.println("fire 3 : ");
159
                n0.fire(n0.listTransition.get(0));
160
                System.out.println(n0);
161
162 5 a19coudr
163 2 a19coudr
        }
164
165 4 a19coudr
166
        public List<Place> getPlaceList() {
167
                return placeList;
168
        }
169
170
171
        public List<Transition> getListTransition() {
172
                return listTransition;
173
        }
174
175 2 a19coudr
}