Statistiques
| Révision:

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

Historique | Voir | Annoter | Télécharger (4,268 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
        //constant for the methode add arc
18
        public static int ArcOutering = 2;
19
        public static int ArcCleaner = 1;
20
        public static int ArcZero = 0;
21
        public static int ArcEntering = 3;
22

    
23
        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
        /**
32
         * create a new transition
33
         */
34
        public void addTransition() {
35
                Transition transition = new Transition();
36
                listTransition.add(transition);
37
        }
38
        /**
39
         * create a new place
40
         */
41
        public void addPlace(int value) throws UndefinedToken {
42
                Place place = new Place(value);
43
                placeList.add(place);
44
        }
45
        @SuppressWarnings("serial")
46
        public class UndefinedIdentifier extends Exception{
47
                public UndefinedIdentifier() {
48
                        super("the identifier cannot take this value");
49
                }
50

    
51
        }
52
        /**
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
        public void addArc(Transition transition, Place place, int value, int identifier) throws UndefinedIdentifier {
60

    
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
                }else {
72
                        throw new UndefinedIdentifier();
73
                }
74

    
75
        }
76
        /**
77
         * delete an arc
78
         */
79
        public void deleteArc(Arc a) {
80
                Arc aToDelete = null;
81
                for(Transition transition: listTransition) {
82
                        for(Arc arc:transition.getArcList()) {
83
                                if(arc.equals(a)) {
84
                                        aToDelete=arc;                                }
85
                        }
86
                        transition.getArcList().remove(aToDelete);
87
                }
88

    
89
        }
90
        /**
91
         * delete a place an the arcs link to it
92
         */
93
        public void deletePlace( Place place) {
94
                List<Arc> aToDelete = new ArrayList<Arc>();
95
                for(Transition transition: listTransition) {
96
                        for(Arc arc:transition.getArcList()) {
97
                                if(arc.getPlace().equals(place)) {
98
                                        aToDelete.add(arc);
99
                                }
100
                        }
101
                        aToDelete.forEach((Arc a)->{
102
                                deleteArc(a);
103
                        });
104
                        placeList.remove(place);
105

    
106
                }
107

    
108
        }
109
        /**
110
         * delete a transition
111
         */
112
        public void deleteTransition(Transition transition) {
113

    
114
                listTransition.remove(transition);
115
        }
116
        /**
117
         * fire the transition selected
118
         */
119
        public void fire(Transition transition) {
120
                if (transition.isPullable()==true) {
121
                        transition.doTransition();
122
                }
123
        }
124
        /**
125
         * string  representation of a network
126
         */
127
        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
        //example of Petri network
136
        public static void main(String[] args) throws UndefinedToken, UndefinedIdentifier {
137

    
138
                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
                System.out.println("RESEAU DE PETRI une transtion, un arcOutering un arcEntering");
146
                System.out.println("");
147
                System.out.println("situtation initial : ");
148
                System.out.println(n0);
149

    
150
                System.out.println("fire 1 : ");
151
                n0.fire(n0.listTransition.get(0));
152
                System.out.println(n0);
153

    
154
                System.out.println("fire 2 : ");
155
                n0.fire(n0.listTransition.get(0));
156
                System.out.println(n0);
157

    
158
                System.out.println("fire 3 : ");
159
                n0.fire(n0.listTransition.get(0));
160
                System.out.println(n0);
161

    
162

    
163
        }
164

    
165

    
166
        public List<Place> getPlaceList() {
167
                return placeList;
168
        }
169

    
170

    
171
        public List<Transition> getListTransition() {
172
                return listTransition;
173
        }
174

    
175
}