~tapaal-contributor/tapaal/disappearing-tokens-1940098

« back to all changes in this revision

Viewing changes to src/pipe/dataLayer/DataLayer.java

  • Committer: Kenneth Yrke Jørgensen
  • Date: 2011-04-12 09:50:16 UTC
  • mfrom: (329.1.188 tapaal-1.5)
  • Revision ID: mail@yrke.dk-20110412095016-e4hqdgab5596ja09
Merged with branch addning support for new 1.5 features

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
package pipe.dataLayer;
2
2
 
3
 
import java.io.File;
4
 
import java.math.BigDecimal;
5
3
import java.util.ArrayList;
6
 
import java.util.Collection;
7
 
import java.util.HashMap;
8
4
import java.util.Hashtable;
9
5
import java.util.Iterator;
10
 
import java.util.LinkedList;
11
 
import java.util.List;
12
6
import java.util.Observable;
13
 
import java.util.Random;
14
 
import java.util.Set;
15
 
import java.util.Map.Entry;
16
 
 
17
 
import javax.swing.JOptionPane;
18
 
 
19
 
import org.w3c.dom.Document;
20
 
import org.w3c.dom.Element;
21
 
import org.w3c.dom.Node;
22
 
import org.w3c.dom.NodeList;
23
 
 
24
 
import pipe.dataLayer.TAPNQuery.ExtrapolationOption;
25
 
import pipe.dataLayer.TAPNQuery.HashTableSize;
26
 
import pipe.dataLayer.TAPNQuery.SearchOption;
27
 
import pipe.dataLayer.TAPNQuery.TraceOption;
28
 
import pipe.dataLayer.colors.ColorSet;
29
 
import pipe.dataLayer.colors.ColoredInhibitorArc;
30
 
import pipe.dataLayer.colors.ColoredInputArc;
31
 
import pipe.dataLayer.colors.ColoredInterval;
32
 
import pipe.dataLayer.colors.ColoredOutputArc;
33
 
import pipe.dataLayer.colors.ColoredTimeInvariant;
34
 
import pipe.dataLayer.colors.ColoredTimedPlace;
35
 
import pipe.dataLayer.colors.ColoredToken;
36
 
import pipe.dataLayer.colors.ColoredTransportArc;
37
 
import pipe.dataLayer.colors.IntOrConstant;
38
 
import pipe.dataLayer.colors.Preserve;
39
 
import pipe.exception.InvariantViolatedAnimationException;
40
 
import pipe.gui.CreateGui;
41
 
import pipe.gui.Grid;
42
 
import pipe.gui.Pipe;
43
 
import pipe.gui.undo.UndoableEdit;
44
 
import dk.aau.cs.TCTL.TCTLAbstractProperty;
45
 
import dk.aau.cs.TCTL.Parsing.TAPAALQueryParser;
46
 
import dk.aau.cs.petrinet.TAPN;
47
 
import dk.aau.cs.translations.ReductionOption;
48
 
 
49
 
 
50
 
/**
51
 
 * <b>DataLayer</b> - Encapsulates entire Petri-Net, also contains functions to 
52
 
 * perform calculations
53
 
 *
54
 
 * @see <p><a href="..\PNMLSchema\index.html">PNML  -  Petri-Net XMLSchema 
55
 
 * (stNet.xsd)</a>
56
 
 * @see </p><p><a href="uml\DataLayer.png">DataLayer UML</a></p>
57
 
 * @version 1.0
58
 
 * @author James D Bloom
59
 
 *
60
 
 * @author David Patterson Jan 2, 2006: Changed the fireRandomTransition 
61
 
 * method to give precedence to immediate transitions.
62
 
 * 
63
 
 * @author Edwin Chung added a boolean attribute to each matrix generated to
64
 
 * prevent them from being created again when they have not been changed 
65
 
 * (6th Feb 2007)
66
 
 * 
67
 
 * @author Ben Kirby Feb 10, 2007: Removed savePNML method and the 
68
 
 * createPlaceElement, createAnnotationElement, createArcElement, createArcPoint,
69
 
 * createTransitionElement methods it uses to a separate DataLayerWriter class, 
70
 
 * as part of refactoring to remove XML related actions from the DataLayer class.
71
 
 * 
72
 
 * @author Ben Kirby Feb 10, 2007: Split loadPNML into two bits. All XML work 
73
 
 * (Files, transformers, documents) is done in new PNMLTransformer class. The 
74
 
 * calls to actually populate a DataLayer object with the info contained in the 
75
 
 * PNML document have been moved to a createFromPNML method. The DataLayer 
76
 
 * constructor which previously used loadPNML has been changed to reflect 
77
 
 * these modifications. Also moved getDOM methods to PNMLTranformer class, as 
78
 
 * getDom is XML related. Removed getDom() (no arguments) completely as this 
79
 
 * is not called anywhere in the application. 
80
 
 *
81
 
 * @author Will Master Feb 13 2007: Added methods getPlacesCount and 
82
 
 * getTransitionsCount to avoid needlessly copying place and transition 
83
 
 * arrayLists.
84
 
 * 
85
 
 * @author Edwin Chung 15th Mar 2007: modified the createFromPNML function so 
86
 
 * that DataLayer objects can be created outside GUI
87
 
 *
88
 
 * @author Dave Patterson 24 April 2007: Modified the fireRandomTransition 
89
 
 * method so it is quicker when there is only one transition to fire (just fire 
90
 
 * it, don't get a random variable first). Also, throw a RuntimeException if a 
91
 
 * rate less than 1 is detected. The current code uses the rate as a weight, 
92
 
 * and a rate such as 0.5 leads to a condition like that of bug 1699546 where no
93
 
 * transition is available to fire.
94
 
 * 
95
 
 * @author Dave Patterson 10 May 2007: Modified the fireRandomTransitino method
96
 
 * so it now properly handles fractional weights. There is no RuntimeException
97
 
 * thrown now. The code for timed transitions uses the same logic, but will soon
98
 
 * be changed to use exponentially distributed times where fractional rates 
99
 
 * are valid.
100
 
 *
101
 
 * @author Barry Kearns August 2007: Added clone functionality and storage of
102
 
 * state groups.
103
 
 * 
104
 
 **/
105
 
public class DataLayer 
106
 
extends Observable 
107
 
implements Cloneable {
108
 
 
109
 
        private static Random randomNumber = new Random(); // Random number generator
 
7
 
 
8
import dk.aau.cs.model.tapn.TimeInvariant;
 
9
import dk.aau.cs.model.tapn.TimedArcPetriNet;
 
10
import dk.aau.cs.util.Require;
 
11
 
 
12
 
 
13
public class DataLayer extends Observable implements Cloneable {
110
14
 
111
15
        /** PNML File Name */
112
16
        public String pnmlName = null;
113
 
 
114
17
        /** List containing all the Place objects in the Petri-Net */
115
18
        private ArrayList<Place> placesArray = null;
116
19
        /** ArrayList containing all the Transition objects in the Petri-Net */
119
22
        private ArrayList<Arc> arcsArray = null;
120
23
 
121
24
        /** ArrayList containing all the Arc objects in the Petri-Net */
122
 
        private ArrayList<InhibitorArc> inhibitorsArray = null;  
 
25
        private ArrayList<InhibitorArc> inhibitorsArray = null;
123
26
 
124
 
        /** ArrayList for net-level label objects (as opposed to element-level labels).*/
 
27
        /**
 
28
         * ArrayList for net-level label objects (as opposed to element-level
 
29
         * labels).
 
30
         */
125
31
        private ArrayList<AnnotationNote> labelsArray = null;
126
32
 
127
 
        /** An ArrayList used to point to either the Arc, Place or Transition 
128
 
         * ArrayLists when these ArrayLists are being update */
 
33
        /**
 
34
         * An ArrayList used to point to either the Arc, Place or Transition
 
35
         * ArrayLists when these ArrayLists are being update
 
36
         */
129
37
        private ArrayList changeArrayList = null;
130
38
 
131
 
        /** Marking Vector Storage used during animation */
132
 
        private int[] markingVectorAnimationStorage = null;
133
 
 
134
39
        /** Used to determine whether the matrixes have been modified */
135
40
        static boolean initialMarkingVectorChanged = true;
136
41
 
137
42
        static boolean currentMarkingVectorChanged = true;
138
43
 
139
 
        /** Hashtable which maps PlaceTransitionObjects to their list of connected arcs */
140
 
        private Hashtable<PlaceTransitionObject, ArrayList<NormalArc>> arcsMap = null;
 
44
        /**
 
45
         * Hashtable which maps PlaceTransitionObjects to their list of connected
 
46
         * arcs
 
47
         */
 
48
        private Hashtable<PlaceTransitionObject, ArrayList<TimedOutputArcComponent>> arcsMap = null;
141
49
 
142
 
        /** Hashtable which maps PlaceTransitionObjects to their list of connected arcs */
 
50
        /**
 
51
         * Hashtable which maps PlaceTransitionObjects to their list of connected
 
52
         * arcs
 
53
         */
143
54
        private Hashtable<PlaceTransitionObject, ArrayList<InhibitorArc>> inhibitorsMap = null;
144
 
        private Hashtable<PlaceTransitionObject, ArrayList<TAPNInhibitorArc>> tapnInhibitorsMap = null;
145
 
 
146
 
        private HashMap<Transition, HashMap<TransportArc, TransportArc> > transportArcMap;
147
 
 
148
 
        private HashMap<TimedPlace, ArrayList<BigDecimal>> placeMarkingStorageMap = null;
149
 
 
150
 
        private ArrayList<TAPNQuery> queries = null;
151
 
        private ConstantStore constants = new ConstantStore();
 
55
        private Hashtable<PlaceTransitionObject, ArrayList<TimedInhibitorArcComponent>> tapnInhibitorsMap = null;
 
56
 
152
57
        private NetType type = NetType.TAPN;
153
58
 
154
 
        private HashMap<ColoredTimedPlace, List<ColoredToken>> coloredPlaceMarkingStorageMap;
155
 
 
156
 
        /**
157
 
         * Create Petri-Net object from PNML file with URI pnmlFileName
158
 
         * @param pnmlFileName Name of PNML File
159
 
         */
160
 
        public DataLayer(String pnmlFileName) {
161
 
 
162
 
                queries = new ArrayList<TAPNQuery>();
163
 
                initializeMatrices();
164
 
                PNMLTransformer transform = new PNMLTransformer();
165
 
                File temp = new File(pnmlFileName);
166
 
                pnmlName = temp.getName();
167
 
                createFromPNML(transform.transformPNML(pnmlFileName));
168
 
                transportArcMap = new HashMap<Transition, HashMap<TransportArc,TransportArc> >();
169
 
        }  
170
 
        
171
 
        /**
172
 
         * Create Petri-Net object from pnmlFile
173
 
         * @param pnmlFile PNML File
174
 
         */
175
 
        public DataLayer(File pnmlFile) {
176
 
                this(pnmlFile.getAbsolutePath());
177
 
        }
178
 
 
179
 
 
180
59
        /**
181
60
         * Create empty Petri-Net object
182
61
         */
183
62
        public DataLayer() {
184
63
                initializeMatrices();
185
 
                queries = new ArrayList<TAPNQuery>();
186
64
        }
187
65
 
188
 
 
189
66
        /**
190
67
         * Initialize Arrays
191
68
         */
195
72
                transitionsArray = new ArrayList<Transition>();
196
73
                arcsArray = new ArrayList<Arc>();
197
74
                inhibitorsArray = new ArrayList<InhibitorArc>();
198
 
                //tapnInhibitorsArray = new ArrayList();
 
75
                // tapnInhibitorsArray = new ArrayList();
199
76
                labelsArray = new ArrayList<AnnotationNote>();
200
77
 
201
 
 
202
78
                // may as well do the hashtable here as well
203
 
                arcsMap = new Hashtable<PlaceTransitionObject, ArrayList<NormalArc>>();
 
79
                arcsMap = new Hashtable<PlaceTransitionObject, ArrayList<TimedOutputArcComponent>>();
204
80
                inhibitorsMap = new Hashtable<PlaceTransitionObject, ArrayList<InhibitorArc>>();
205
 
                tapnInhibitorsMap = new Hashtable<PlaceTransitionObject, ArrayList<TAPNInhibitorArc>>();
206
 
        }
207
 
 
208
 
 
209
 
        /**
210
 
         * Add placeInput to the back of the Place ArrayList
211
 
         * All observers are notified of this change (Model-View Architecture)
212
 
         * @param placeInput Place Object to add
213
 
         */
214
 
        private void addPlace(Place placeInput) {
215
 
                boolean unique = true;
216
 
 
217
 
                if (placeInput != null) {
218
 
                        if (placeInput.getId() != null && placeInput.getId().length() > 0) {
219
 
                                for (int i = 0; i < placesArray.size(); i++) {
220
 
                                        if (placeInput.getId().equals(((Place)placesArray.get(i)).getId())) {
221
 
                                                unique = false;
222
 
                                        }
223
 
                                }
224
 
                        } else {
225
 
                                String id = null;
226
 
                                if (placesArray != null && placesArray.size() > 0) {
227
 
                                        int no = placesArray.size();
228
 
                                        //          id = "P" + no;
229
 
                                        do {
230
 
                                                //                                              System.out.println("in while loop"); //DBG
231
 
                                                for (int i = 0; i < placesArray.size(); i++) {
232
 
                                                        id = "P" + no;
233
 
                                                        if (placesArray.get(i) != null) {
234
 
                                                                if (id.equals(((Place)placesArray.get(i)).getId())) {
235
 
                                                                        //                                                                      System.out.println("testing id: " + id); //DBG
236
 
                                                                        unique = false;
237
 
                                                                        no++;
238
 
                                                                } else {
239
 
                                                                        unique = true;
240
 
                                                                }
241
 
                                                        }
242
 
                                                }
243
 
                                        } while(!unique);
244
 
                                } else {
245
 
                                        id = "P0";
246
 
                                }
247
 
 
248
 
                                if (id != null) {
249
 
                                        placeInput.setId(id);
250
 
                                        if (getPlaceByName(placeInput.getName()) != null){
251
 
                                                boolean hasUniqueName = false;
252
 
                                                int nameID = 0;
253
 
                                                while (!hasUniqueName){
254
 
                                                        if (getPlaceByName("P"+nameID) == null){
255
 
                                                                placeInput.setName("P"+nameID);
256
 
                                                                hasUniqueName = true;
257
 
                                                        }else{
258
 
                                                                nameID++;
259
 
                                                        }
260
 
                                                }
261
 
                                        }
262
 
                                } else {
263
 
                                        placeInput.setId("error");
264
 
                                }
265
 
                        }
266
 
                        placesArray.add(placeInput);
267
 
                        setChanged();
268
 
                        // notifyObservers(placeInput.getBounds());
269
 
                        notifyObservers(placeInput);
270
 
                }
271
 
        }
272
 
 
273
 
 
274
 
        /**
275
 
         * Add labelInput to the back of the AnnotationNote ArrayList
276
 
         * All observers are notified of this change (Model-View Architecture)
277
 
         * @param labelInput AnnotationNote Object to add
278
 
         */
279
 
        private void addAnnotation(AnnotationNote labelInput) {
 
81
                tapnInhibitorsMap = new Hashtable<PlaceTransitionObject, ArrayList<TimedInhibitorArcComponent>>();
 
82
        }
 
83
 
 
84
        /**
 
85
         * Add placeInput to the back of the Place ArrayList All observers are
 
86
         * notified of this change (Model-View Architecture)
 
87
         * 
 
88
         * @param placeInput
 
89
         *            Place Object to add
 
90
         */
 
91
        public void addPlace(Place placeInput) {
 
92
                Require.that(placeInput != null, "input place was null");
 
93
 
 
94
                placesArray.add(placeInput);
 
95
                setChanged();
 
96
                notifyObservers(placeInput);
 
97
        }
 
98
 
 
99
        /**
 
100
         * Add labelInput to the back of the AnnotationNote ArrayList All observers
 
101
         * are notified of this change (Model-View Architecture)
 
102
         * 
 
103
         * @param labelInput
 
104
         *            AnnotationNote Object to add
 
105
         */
 
106
        public void addAnnotation(AnnotationNote labelInput) {
280
107
                labelsArray.add(labelInput);
281
108
                setChanged();
282
109
                notifyObservers(labelInput);
283
110
        }
284
111
 
285
112
        /**
286
 
         * Add transitionInput to back of the Transition ArrayList
287
 
         * All observers are notified of this change (Model-View Architecture)
288
 
         * @param transitionInput Transition Object to add
 
113
         * Add transitionInput to back of the Transition ArrayList All observers are
 
114
         * notified of this change (Model-View Architecture)
 
115
         * 
 
116
         * @param transitionInput
 
117
         *            Transition Object to add
289
118
         */
290
 
        private void addTransition(Transition transitionInput) {
291
 
                boolean unique = true;
292
 
 
293
 
                if (transitionInput != null) {
294
 
                        if (transitionInput.getId() != null
295
 
                                        && transitionInput.getId().length() > 0) {
296
 
                                for (int i = 0; i < transitionsArray.size(); i++) {
297
 
                                        if (transitionInput.getId().equals(
298
 
                                                        ((Transition)transitionsArray.get(i)).getId())) {
299
 
                                                unique = false;
300
 
                                        }
301
 
                                }
302
 
                        } else {
303
 
                                String id = null;
304
 
                                if (transitionsArray != null && transitionsArray.size() > 0) {
305
 
                                        int no = transitionsArray.size();
306
 
                                        do {
307
 
                                                // System.out.println("transition while loop");
308
 
                                                for (int i = 0; i < transitionsArray.size(); i++) {
309
 
                                                        id = "T" + no;
310
 
                                                        if (transitionsArray.get(i) != null) {
311
 
                                                                if (id.equals(
312
 
                                                                                ((Transition)transitionsArray.get(i)).getId())) {
313
 
                                                                        unique = false;
314
 
                                                                        no++;
315
 
                                                                } else {
316
 
                                                                        unique = true;
317
 
                                                                }
318
 
                                                        }
319
 
                                                }
320
 
                                        } while(!unique);
321
 
                                } else {
322
 
                                        id = "T0";
323
 
                                }
324
 
 
325
 
                                if(id != null) {
326
 
                                        transitionInput.setId(id);
327
 
                                        if (getTransitionByName(transitionInput.getName()) != null){
328
 
                                                boolean hasUniqueName = false;
329
 
                                                int nameID = 0;
330
 
                                                while (!hasUniqueName){
331
 
                                                        if (getTransitionByName("T"+nameID) == null){
332
 
                                                                transitionInput.setName("T"+nameID);
333
 
                                                                hasUniqueName = true;
334
 
                                                        }else{
335
 
                                                                nameID++;
336
 
                                                        }
337
 
                                                }
338
 
                                        }
339
 
                                } else {
340
 
                                        transitionInput.setId("error");
341
 
                                }
342
 
                        }
343
 
                        transitionsArray.add(transitionInput);
344
 
                        setChanged();
345
 
 
346
 
                        //    notifyObservers(transitionInput.getBounds());
347
 
                        notifyObservers(transitionInput);
348
 
                }
 
119
        public void addTransition(Transition transitionInput) {
 
120
                Require.that(transitionInput != null, "input transition was null");
 
121
                
 
122
                transitionsArray.add(transitionInput);
 
123
                setChanged();
 
124
                notifyObservers(transitionInput);
349
125
        }
350
126
 
351
 
 
352
127
        /**
353
 
         * Add arcInput to back of the Arc ArrayList
354
 
         * All observers are notified of this change (Model-View Architecture)
355
 
         * @param arcInput Arc Object to add
 
128
         * Add arcInput to back of the Arc ArrayList All observers are notified of
 
129
         * this change (Model-View Architecture)
 
130
         * 
 
131
         * @param arcInput
 
132
         *            Arc Object to add
356
133
         */
357
 
        public void addArc(NormalArc arcInput) {
 
134
        public void addArc(TimedOutputArcComponent arcInput) {
358
135
                boolean unique = true;
359
136
 
360
 
                //Check if the arcs have a valid source and target
361
 
                if (arcInput.getSource() == null || arcInput.getTarget() == null){
362
 
                        System.err.println(("Cant add arc" + arcInput + " where source: " + 
363
 
                                        arcInput.getSource() + " or target: " + arcInput.getTarget() + " is null"));
 
137
                // Check if the arcs have a valid source and target
 
138
                if (arcInput.getSource() == null || arcInput.getTarget() == null) {
 
139
                        System.err.println(("Cant add arc" + arcInput + " where source: "
 
140
                                        + arcInput.getSource() + " or target: "
 
141
                                        + arcInput.getTarget() + " is null"));
364
142
                        return;
365
143
                }
366
144
 
367
145
                if (arcInput != null) {
368
146
                        if (arcInput.getId() != null && arcInput.getId().length() > 0) {
369
 
                                for (int i = 0 ; i < arcsArray.size() ; i++) {
370
 
                                        if (arcInput.getId().equals(((Arc)arcsArray.get(i)).getId())) {
 
147
                                for (int i = 0; i < arcsArray.size(); i++) {
 
148
                                        if (arcInput.getId().equals((arcsArray.get(i)).getId())) {
371
149
                                                unique = false;
372
150
                                        }
373
151
                                }
379
157
                                                for (int i = 0; i < arcsArray.size(); i++) {
380
158
                                                        id = "A" + no;
381
159
                                                        if (arcsArray.get(i) != null) {
382
 
                                                                if (id.equals(((Arc)arcsArray.get(i)).getId())) {
 
160
                                                                if (id.equals((arcsArray.get(i)).getId())) {
383
161
                                                                        unique = false;
384
162
                                                                        no++;
385
163
                                                                } else {
391
169
                                } else {
392
170
                                        id = "A0";
393
171
                                }
394
 
                                if(id != null) {
 
172
                                if (id != null) {
395
173
                                        arcInput.setId(id);
396
174
                                } else {
397
175
                                        arcInput.setId("error");
398
176
                                }
399
177
                        }
400
178
 
401
 
                        //Transportarc fix boddy
402
 
                        if (arcInput instanceof TransportArc){
403
 
                                TransportArc tmp = (TransportArc)arcInput;
 
179
                        // Transportarc fix boddy
 
180
                        if (arcInput instanceof TransportArcComponent) {
 
181
                                TransportArcComponent tmp = (TransportArcComponent) arcInput;
404
182
                                PlaceTransitionObject first = tmp.getSource();
405
183
 
406
 
                                if (tmp.getConnectedTo() == null){
407
 
                                        if (first instanceof TimedPlace){
408
 
 
409
 
                                                for (Object o : tmp.getTarget().getPostset()){
410
 
 
411
 
                                                        if (o instanceof TransportArc){
412
 
                                                                if (tmp.getGroupNr() == ((TransportArc)o).getGroupNr()){
413
 
                                                                        //Found partner
414
 
 
415
 
                                                                        tmp.setConnectedTo(((TransportArc)o));
416
 
                                                                        ((TransportArc)o).setConnectedTo(tmp); 
 
184
                                if (tmp.getConnectedTo() == null) {
 
185
                                        if (first instanceof TimedPlaceComponent) {
 
186
 
 
187
                                                for (Object o : tmp.getTarget().getPostset()) {
 
188
 
 
189
                                                        if (o instanceof TransportArcComponent) {
 
190
                                                                if (tmp.getGroupNr() == ((TransportArcComponent) o)
 
191
                                                                                .getGroupNr()) {
 
192
                                                                        // Found partner
 
193
 
 
194
                                                                        tmp
 
195
                                                                                        .setConnectedTo(((TransportArcComponent) o));
 
196
                                                                        ((TransportArcComponent) o)
 
197
                                                                                        .setConnectedTo(tmp);
417
198
 
418
199
                                                                        break;
419
200
                                                                }
421
202
 
422
203
                                                }
423
204
 
424
 
                                        } else{
 
205
                                        } else {
425
206
                                                // First is TimedTransition
426
 
                                                tmp = (TransportArc)arcInput;
427
 
 
428
 
                                                for (Object o : tmp.getSource().getPreset()){
429
 
 
430
 
                                                        if (o instanceof TransportArc){
431
 
                                                                if (tmp.getGroupNr() == ((TransportArc)o).getGroupNr()){
432
 
                                                                        //Found partner
433
 
 
434
 
                                                                        tmp.setConnectedTo(((TransportArc)o));
435
 
                                                                        ((TransportArc)o).setConnectedTo(tmp); 
 
207
                                                tmp = (TransportArcComponent) arcInput;
 
208
 
 
209
                                                for (Object o : tmp.getSource().getPreset()) {
 
210
 
 
211
                                                        if (o instanceof TransportArcComponent) {
 
212
                                                                if (tmp.getGroupNr() == ((TransportArcComponent) o)
 
213
                                                                                .getGroupNr()) {
 
214
                                                                        // Found partner
 
215
 
 
216
                                                                        tmp
 
217
                                                                                        .setConnectedTo(((TransportArcComponent) o));
 
218
                                                                        ((TransportArcComponent) o)
 
219
                                                                                        .setConnectedTo(tmp);
436
220
 
437
221
                                                                        break;
438
222
                                                                }
440
224
 
441
225
                                                }
442
226
 
443
 
 
444
227
                                        }
445
228
                                }
446
229
 
447
 
 
448
230
                        }
449
231
 
450
232
                        arcsArray.add(arcInput);
452
234
 
453
235
                        setChanged();
454
236
 
455
 
                        //notifyObservers(arcInput.getBounds());
 
237
                        // notifyObservers(arcInput.getBounds());
456
238
                        notifyObservers(arcInput);
457
239
                }
458
240
        }
459
241
 
460
 
        public void addArc(TAPNInhibitorArc inhibitorArcInput) {
 
242
        public void addTransportArc(TransportArcComponent transportArc) {
 
243
                arcsArray.add(transportArc);
 
244
                addArcToArcsMap(transportArc);
 
245
                setChanged();
 
246
                notifyObservers();
 
247
        }
 
248
 
 
249
        public void addArc(TimedInhibitorArcComponent inhibitorArcInput) {
461
250
                boolean unique = true;
462
251
 
463
252
                if (inhibitorArcInput != null) {
464
 
                        if (inhibitorArcInput.getId() != null &&
465
 
                                        inhibitorArcInput.getId().length() > 0) {
466
 
                                for (int i = 0 ; i < arcsArray.size() ; i++) {
 
253
                        if (inhibitorArcInput.getId() != null
 
254
                                        && inhibitorArcInput.getId().length() > 0) {
 
255
                                for (int i = 0; i < arcsArray.size(); i++) {
467
256
                                        if (inhibitorArcInput.getId().equals(
468
 
                                                        ((Arc)arcsArray.get(i)).getId())) {
 
257
                                                        (arcsArray.get(i)).getId())) {
469
258
                                                unique = false;
470
259
                                        }
471
260
                                }
477
266
                                                for (int i = 0; i < arcsArray.size(); i++) {
478
267
                                                        id = "I" + no;
479
268
                                                        if (arcsArray.get(i) != null) {
480
 
                                                                if (id.equals(((Arc)arcsArray.get(i)).getId())) {
 
269
                                                                if (id.equals((arcsArray.get(i)).getId())) {
481
270
                                                                        unique = false;
482
271
                                                                        no++;
483
272
                                                                } else {
489
278
                                } else {
490
279
                                        id = "I0";
491
280
                                }
492
 
                                if(id != null) {
 
281
                                if (id != null) {
493
282
                                        inhibitorArcInput.setId(id);
494
283
                                } else {
495
284
                                        inhibitorArcInput.setId("error");
501
290
 
502
291
                        setChanged();
503
292
 
504
 
                        //notifyObservers(arcInput.getBounds());
 
293
                        // notifyObservers(arcInput.getBounds());
505
294
                        notifyObservers(inhibitorArcInput);
506
295
                }
507
296
        }
 
297
 
508
298
        /**
509
 
         * Add inhibitorArcInput to back of the InhibitorArc ArrayList
510
 
         * All observers are notified of this change (Model-View Architecture)
511
 
         * @param arcInput Arc Object to add
 
299
         * Add inhibitorArcInput to back of the InhibitorArc ArrayList All observers
 
300
         * are notified of this change (Model-View Architecture)
 
301
         * 
 
302
         * @param arcInput
 
303
         *            Arc Object to add
512
304
         */
513
305
        public void addArc(InhibitorArc inhibitorArcInput) {
514
306
                boolean unique = true;
515
307
 
516
308
                if (inhibitorArcInput != null) {
517
 
                        if (inhibitorArcInput.getId() != null &&
518
 
                                        inhibitorArcInput.getId().length() > 0) {
519
 
                                for (int i = 0 ; i < inhibitorsArray.size() ; i++) {
 
309
                        if (inhibitorArcInput.getId() != null
 
310
                                        && inhibitorArcInput.getId().length() > 0) {
 
311
                                for (int i = 0; i < inhibitorsArray.size(); i++) {
520
312
                                        if (inhibitorArcInput.getId().equals(
521
 
                                                        ((Arc)inhibitorsArray.get(i)).getId())) {
 
313
                                                        ((Arc) inhibitorsArray.get(i)).getId())) {
522
314
                                                unique = false;
523
315
                                        }
524
316
                                }
530
322
                                                for (int i = 0; i < inhibitorsArray.size(); i++) {
531
323
                                                        id = "I" + no;
532
324
                                                        if (inhibitorsArray.get(i) != null) {
533
 
                                                                if (id.equals(((Arc)inhibitorsArray.get(i)).getId())) {
 
325
                                                                if (id.equals(((Arc) inhibitorsArray.get(i))
 
326
                                                                                .getId())) {
534
327
                                                                        unique = false;
535
328
                                                                        no++;
536
329
                                                                } else {
542
335
                                } else {
543
336
                                        id = "I0";
544
337
                                }
545
 
                                if(id != null) {
 
338
                                if (id != null) {
546
339
                                        inhibitorArcInput.setId(id);
547
340
                                } else {
548
341
                                        inhibitorArcInput.setId("error");
553
346
 
554
347
                        setChanged();
555
348
 
556
 
                        //notifyObservers(arcInput.getBounds());
 
349
                        // notifyObservers(arcInput.getBounds());
557
350
                        notifyObservers(inhibitorArcInput);
558
351
                }
559
 
        }   
 
352
        }
560
353
 
561
 
        /** Update the arcsMap hashtable to reflect the new arc
562
 
         * @param arcInput New Arc
 
354
        /**
 
355
         * Update the arcsMap hashtable to reflect the new arc
 
356
         * 
 
357
         * @param arcInput
 
358
         *            New Arc
563
359
         * */
564
 
        private void addArcToArcsMap(NormalArc arcInput) {
565
 
                // now we want to add the arc to the list of arcs for it's source and target
 
360
        private void addArcToArcsMap(TimedOutputArcComponent arcInput) {
 
361
                // now we want to add the arc to the list of arcs for it's source and
 
362
                // target
566
363
                PlaceTransitionObject source = arcInput.getSource();
567
364
                PlaceTransitionObject target = arcInput.getTarget();
568
 
                ArrayList<NormalArc> newList = null;
 
365
                ArrayList<TimedOutputArcComponent> newList = null;
569
366
 
570
367
                if (source != null) {
571
 
                        //                      Pete: Place/Transitions now always moveable
572
 
                        //                      source.setMovable(false);
573
 
                        if (arcsMap.get(source)!=null) {
574
 
                                //                              System.out.println("adding arc to existing list");
 
368
                        // Pete: Place/Transitions now always moveable
 
369
                        // source.setMovable(false);
 
370
                        if (arcsMap.get(source) != null) {
 
371
                                // System.out.println("adding arc to existing list");
575
372
                                arcsMap.get(source).add(arcInput);
576
373
                        } else {
577
 
                                //                              System.out.println("creating new arc list");
578
 
                                newList = new ArrayList<NormalArc>();
 
374
                                // System.out.println("creating new arc list");
 
375
                                newList = new ArrayList<TimedOutputArcComponent>();
579
376
                                newList.add(arcInput);
580
 
                                arcsMap.put(source,newList);
 
377
                                arcsMap.put(source, newList);
581
378
                        }
582
379
                }
583
380
 
584
381
                if (target != null) {
585
 
                        //                      Pete: Place/Transitions now always moveable
586
 
                        //                      target.setMovable(false);
587
 
                        if (arcsMap.get(target)!=null) {
588
 
                                //                              System.out.println("adding arc to existing list2");
 
382
                        // Pete: Place/Transitions now always moveable
 
383
                        // target.setMovable(false);
 
384
                        if (arcsMap.get(target) != null) {
 
385
                                // System.out.println("adding arc to existing list2");
589
386
                                arcsMap.get(target).add(arcInput);
590
387
                        } else {
591
 
                                //                              System.out.println("creating new arc list2");
592
 
                                newList = new ArrayList<NormalArc>();
 
388
                                // System.out.println("creating new arc list2");
 
389
                                newList = new ArrayList<TimedOutputArcComponent>();
593
390
                                newList.add(arcInput);
594
 
                                arcsMap.put(target,newList);
 
391
                                arcsMap.put(target, newList);
595
392
                        }
596
393
                }
597
 
                //              System.out.println("arcsMap size: " + arcsMap.size());
 
394
                // System.out.println("arcsMap size: " + arcsMap.size());
598
395
        }
599
396
 
600
 
        /** 
 
397
        /**
601
398
         * Update the inhibitorsMap hashtable to reflect the new inhibitor arc
602
 
         * @param arcInput New Arc
 
399
         * 
 
400
         * @param arcInput
 
401
         *            New Arc
603
402
         */
604
 
        private void addInhibitorArcToInhibitorsMap(TAPNInhibitorArc inhibitorArcInput) {
605
 
                // now we want to add the inhibitor arc to the list of inhibitor arcs for
 
403
        private void addInhibitorArcToInhibitorsMap(TimedInhibitorArcComponent inhibitorArcInput) {
 
404
                // now we want to add the inhibitor arc to the list of inhibitor arcs
 
405
                // for
606
406
                // it's source and target
607
407
                PlaceTransitionObject source = inhibitorArcInput.getSource();
608
408
                PlaceTransitionObject target = inhibitorArcInput.getTarget();
609
 
                ArrayList<TAPNInhibitorArc> newList = null;
 
409
                ArrayList<TimedInhibitorArcComponent> newList = null;
610
410
 
611
411
                if (source != null) {
612
412
                        if (tapnInhibitorsMap.get(source) != null) {
613
413
                                tapnInhibitorsMap.get(source).add(inhibitorArcInput);
614
414
                        } else {
615
 
                                newList = new ArrayList<TAPNInhibitorArc>();
 
415
                                newList = new ArrayList<TimedInhibitorArcComponent>();
616
416
                                newList.add(inhibitorArcInput);
617
417
                                tapnInhibitorsMap.put(source, newList);
618
418
                        }
622
422
                        if (tapnInhibitorsMap.get(target) != null) {
623
423
                                tapnInhibitorsMap.get(target).add(inhibitorArcInput);
624
424
                        } else {
625
 
                                newList = new ArrayList<TAPNInhibitorArc>();
 
425
                                newList = new ArrayList<TimedInhibitorArcComponent>();
626
426
                                newList.add(inhibitorArcInput);
627
427
                                tapnInhibitorsMap.put(target, newList);
628
 
                        } 
 
428
                        }
629
429
                }
630
 
                //              System.out.println("inhibitorsMap size: " + inhibitorsMap.size());
 
430
                // System.out.println("inhibitorsMap size: " + inhibitorsMap.size());
631
431
        }
632
432
 
633
 
        /** 
 
433
        /**
634
434
         * Update the inhibitorsMap hashtable to reflect the new inhibitor arc
635
 
         * @param arcInput New Arc
 
435
         * 
 
436
         * @param arcInput
 
437
         *            New Arc
636
438
         */
637
439
        private void addInhibitorArcToInhibitorsMap(InhibitorArc inhibitorArcInput) {
638
 
                // now we want to add the inhibitor arc to the list of inhibitor arcs for
 
440
                // now we want to add the inhibitor arc to the list of inhibitor arcs
 
441
                // for
639
442
                // it's source and target
640
443
                PlaceTransitionObject source = inhibitorArcInput.getSource();
641
444
                PlaceTransitionObject target = inhibitorArcInput.getTarget();
658
461
                                newList = new ArrayList<InhibitorArc>();
659
462
                                newList.add(inhibitorArcInput);
660
463
                                inhibitorsMap.put(target, newList);
661
 
                        } 
 
464
                        }
662
465
                }
663
 
                //              System.out.println("inhibitorsMap size: " + inhibitorsMap.size());
 
466
                // System.out.println("inhibitorsMap size: " + inhibitorsMap.size());
664
467
        }
665
468
 
666
 
 
667
469
        /**
668
 
         * Add any PetriNetObject - the object will be added to the appropriate list.
669
 
         * If the object passed in isn't a Transition, Place or Arc nothing will happen.
670
 
         * All observers are notified of this change.
671
 
         * @param pnObject The PetriNetObject to be added.
 
470
         * Add any PetriNetObject - the object will be added to the appropriate
 
471
         * list. If the object passed in isn't a Transition, Place or Arc nothing
 
472
         * will happen. All observers are notified of this change.
 
473
         * 
 
474
         * @param pnObject
 
475
         *            The PetriNetObject to be added.
672
476
         */
673
477
        public void addPetriNetObject(PetriNetObject pnObject) {
674
478
                if (setPetriNetObjectArrayList(pnObject)) {
675
 
                        if (pnObject instanceof NormalArc) {
676
 
                                addArcToArcsMap((NormalArc)pnObject);
677
 
                                addArc((NormalArc)pnObject);
 
479
                        if (pnObject instanceof TimedInhibitorArcComponent) {
 
480
                                addArc((TimedInhibitorArcComponent) pnObject);
 
481
                        } else if (pnObject instanceof TimedOutputArcComponent) {
 
482
                                addArc((TimedOutputArcComponent) pnObject);
678
483
                        } else if (pnObject instanceof InhibitorArc) {
679
 
                                addInhibitorArcToInhibitorsMap((InhibitorArc)pnObject);
680
 
                                addArc((InhibitorArc)pnObject);
681
 
                        } else if (pnObject instanceof TAPNInhibitorArc) {
682
 
                                addInhibitorArcToInhibitorsMap((TAPNInhibitorArc)pnObject);
683
 
                                addArc((TAPNInhibitorArc)pnObject);
 
484
                                addArc((InhibitorArc) pnObject);
684
485
                        } else if (pnObject instanceof Place) {
685
 
                                addPlace((Place)pnObject);
686
 
                                //                      } else if (pnObject instanceof TAPNTransition){
687
 
                                //                              addTAPNTransition((TAPNTransition)pnObject);
688
 
                        }else if (pnObject instanceof Transition) {
689
 
                                addTransition((Transition)pnObject);
 
486
                                addPlace((Place) pnObject);
 
487
                        } else if (pnObject instanceof Transition) {
 
488
                                addTransition((Transition) pnObject);
690
489
                        } else if (pnObject instanceof AnnotationNote) {
691
 
                                labelsArray.add((AnnotationNote)pnObject);
 
490
                                addAnnotation((AnnotationNote)pnObject);                                
692
491
                        } else { // arrows, other labels.
693
492
                                changeArrayList.add(pnObject);
694
493
                                setChanged();
703
502
        /**
704
503
         * Removes the specified object from the appropriate ArrayList of objects.
705
504
         * All observers are notified of this change.
706
 
         * @param pnObject The PetriNetObject to be removed.
 
505
         * 
 
506
         * @param pnObject
 
507
         *            The PetriNetObject to be removed.
707
508
         */
708
 
        public void removePetriNetObject(PetriNetObject pnObject){
 
509
        public void removePetriNetObject(PetriNetObject pnObject) {
709
510
                boolean didSomething = false;
710
511
                ArrayList<?> attachedArcs = null;
711
512
 
712
 
                try{
 
513
                try {
713
514
                        if (setPetriNetObjectArrayList(pnObject)) {
714
515
                                didSomething = changeArrayList.remove(pnObject);
715
516
                                // we want to remove all attached arcs also
716
517
                                if (pnObject instanceof PlaceTransitionObject) {
717
 
                                        if(pnObject instanceof ColoredTimedPlace){
718
 
                                                ((ColoredTimedPlace)pnObject).showAgeOfTokens(false);
719
 
                                        }
720
 
 
721
 
                                        if ( arcsMap.get(pnObject) != null) {
722
 
 
723
 
                                                // get the list of attached arcs for the object we are removing
 
518
                                        if (arcsMap.get(pnObject) != null) {
 
519
 
 
520
                                                // get the list of attached arcs for the object we are
 
521
                                                // removing
724
522
                                                attachedArcs = arcsMap.get(pnObject);
725
523
 
726
524
                                                // iterate over all the attached arcs, removing them all
727
525
                                                // Pere: in inverse order!
728
 
                                                //for (int i=0; i < attachedArcs.size(); i++){
729
 
                                                for (int i = attachedArcs.size() - 1; i >= 0; i--){
 
526
                                                // for (int i=0; i < attachedArcs.size(); i++){
 
527
                                                for (int i = attachedArcs.size() - 1; i >= 0; i--) {
730
528
                                                        try {
731
 
                                                                ((Arc)attachedArcs.get(i)).delete();
 
529
                                                                ((Arc) attachedArcs.get(i)).delete();
732
530
                                                        } catch (IndexOutOfBoundsException e) {
733
531
                                                                // XXX - this is a hack
734
 
                                                                // This is OK, it just means that the transport arc
 
532
                                                                // This is OK, it just means that the transport
 
533
                                                                // arc
735
534
                                                                // already has been removed
736
535
 
737
536
                                                        }
738
537
 
739
 
 
740
538
                                                }
741
539
                                                arcsMap.remove(pnObject);
742
540
                                        }
743
541
 
744
 
                                        if ( inhibitorsMap.get(pnObject) != null) {
 
542
                                        if (inhibitorsMap.get(pnObject) != null) {
745
543
 
746
 
                                                // get the list of attached arcs for the object we are removing
 
544
                                                // get the list of attached arcs for the object we are
 
545
                                                // removing
747
546
                                                attachedArcs = inhibitorsMap.get(pnObject);
748
547
 
749
548
                                                // iterate over all the attached arcs, removing them all
750
549
                                                // Pere: in inverse order!
751
 
                                                //for (int i=0; i < attachedArcs.size(); i++){
752
 
                                                for (int i = attachedArcs.size() - 1; i >= 0; i--){
753
 
                                                        ((Arc)attachedArcs.get(i)).delete();
 
550
                                                // for (int i=0; i < attachedArcs.size(); i++){
 
551
                                                for (int i = attachedArcs.size() - 1; i >= 0; i--) {
 
552
                                                        ((Arc) attachedArcs.get(i)).delete();
754
553
                                                }
755
554
                                                inhibitorsMap.remove(pnObject);
756
555
                                        }
757
 
                                        if ( tapnInhibitorsMap.get(pnObject) != null) {
 
556
                                        if (tapnInhibitorsMap.get(pnObject) != null) {
758
557
 
759
 
                                                // get the list of attached arcs for the object we are removing
 
558
                                                // get the list of attached arcs for the object we are
 
559
                                                // removing
760
560
                                                attachedArcs = tapnInhibitorsMap.get(pnObject);
761
561
 
762
562
                                                // iterate over all the attached arcs, removing them all
763
563
                                                // Pere: in inverse order!
764
 
                                                //for (int i=0; i < attachedArcs.size(); i++){
765
 
                                                for (int i = attachedArcs.size() - 1; i >= 0; i--){
766
 
                                                        ((Arc)attachedArcs.get(i)).delete();
 
564
                                                // for (int i=0; i < attachedArcs.size(); i++){
 
565
                                                for (int i = attachedArcs.size() - 1; i >= 0; i--) {
 
566
                                                        ((Arc) attachedArcs.get(i)).delete();
767
567
                                                }
768
568
                                                tapnInhibitorsMap.remove(pnObject);
769
569
                                        }
770
 
                                } else if (pnObject instanceof NormalArc) {
771
 
 
772
 
                                        // get source and target of the arc
773
 
                                        PlaceTransitionObject attached = ((Arc)pnObject).getSource();
774
 
 
775
 
                                        if (attached != null) {
776
 
                                                ArrayList<NormalArc> a = arcsMap.get(attached);
777
 
                                                if (a!=null) {
778
 
                                                        a.remove(pnObject);
779
 
                                                }
780
 
 
781
 
                                                attached.removeFromArc((Arc)pnObject);
782
 
                                                if (attached instanceof Transition){
783
 
                                                        ((Transition)attached).removeArcCompareObject((Arc)pnObject);
 
570
                                } else if (pnObject instanceof TimedInhibitorArcComponent) {
 
571
 
 
572
                                        // get source and target of the arc
 
573
                                        PlaceTransitionObject attached = ((Arc) pnObject).getSource();
 
574
 
 
575
                                        if (attached != null) {
 
576
                                                ArrayList<TimedInhibitorArcComponent> a = tapnInhibitorsMap.get(attached);
 
577
                                                if (a != null) {
 
578
                                                        a.remove(pnObject);
 
579
                                                }
 
580
 
 
581
                                                attached.removeFromArc((Arc) pnObject);
 
582
                                                if (attached instanceof Transition) {
 
583
                                                        ((Transition) attached).removeArcCompareObject((Arc) pnObject);
 
584
                                                }
 
585
                                                // attached.updateConnected(); //causing null pointer
 
586
                                                // exceptions (?)
 
587
                                        }
 
588
 
 
589
                                        attached = ((Arc) pnObject).getTarget();
 
590
 
 
591
                                        if (attached != null) {
 
592
                                                if (tapnInhibitorsMap.get(attached) != null) { // causing
 
593
                                                                                                                                                // null
 
594
                                                                                                                                                // pointer
 
595
                                                                                                                                                // exceptions
 
596
                                                                                                                                                // (!)
 
597
                                                        tapnInhibitorsMap.get(attached).remove(pnObject);
 
598
                                                }
 
599
 
 
600
                                                attached.removeToArc((Arc) pnObject);
 
601
                                                if (attached instanceof Transition) {
 
602
                                                        ((Transition) attached)
 
603
                                                                        .removeArcCompareObject((Arc) pnObject);
 
604
                                                }
 
605
                                                // attached.updateConnected(); //causing null pointer
 
606
                                                // exceptions (?)
 
607
                                        }
 
608
                                } else if (pnObject instanceof TimedOutputArcComponent) {
 
609
 
 
610
                                        // get source and target of the arc
 
611
                                        PlaceTransitionObject attached = ((Arc) pnObject).getSource();
 
612
 
 
613
                                        if (attached != null) {
 
614
                                                ArrayList<TimedOutputArcComponent> a = arcsMap.get(attached);
 
615
                                                if (a != null) {
 
616
                                                        a.remove(pnObject);
 
617
                                                }
 
618
 
 
619
                                                attached.removeFromArc((Arc) pnObject);
 
620
                                                if (attached instanceof Transition) {
 
621
                                                        ((Transition) attached).removeArcCompareObject((Arc) pnObject);
784
622
                                                        attached.updateConnected();
785
623
                                                }
786
 
                                                //attached.updateConnected(); //causing null pointer exceptions (?)
 
624
                                                // attached.updateConnected(); //causing null pointer
 
625
                                                // exceptions (?)
787
626
                                        }
788
627
 
789
 
                                        attached = ((Arc)pnObject).getTarget();           
 
628
                                        attached = ((Arc) pnObject).getTarget();
790
629
                                        if (attached != null) {
791
 
                                                if (arcsMap.get(attached) != null) { //causing null pointer exceptions (!)
 
630
                                                if (arcsMap.get(attached) != null) { // causing null
 
631
                                                                                                                                // pointer
 
632
                                                                                                                                // exceptions
 
633
                                                                                                                                // (!)
792
634
                                                        arcsMap.get(attached).remove(pnObject);
793
635
                                                }
794
636
 
795
 
                                                attached.removeToArc((Arc)pnObject);
 
637
                                                attached.removeToArc((Arc) pnObject);
796
638
                                                if (attached instanceof Transition) {
797
 
                                                        ((Transition)attached).removeArcCompareObject((Arc)pnObject);
 
639
                                                        ((Transition) attached).removeArcCompareObject((Arc) pnObject);
798
640
                                                        attached.updateConnected();
799
641
                                                }
800
 
                                                //                                              attached.updateConnected(); //causing null pointer exceptions (?)
 
642
                                                // attached.updateConnected(); //causing null pointer
 
643
                                                // exceptions (?)
801
644
                                        }
802
645
                                } else if (pnObject instanceof InhibitorArc) {
803
646
 
804
647
                                        // get source and target of the arc
805
 
                                        PlaceTransitionObject attached = ((Arc)pnObject).getSource();
 
648
                                        PlaceTransitionObject attached = ((Arc) pnObject)
 
649
                                                        .getSource();
806
650
 
807
651
                                        if (attached != null) {
808
 
                                                ArrayList<InhibitorArc> a=inhibitorsMap.get(attached);
809
 
                                                if (a!=null) {
 
652
                                                ArrayList<InhibitorArc> a = inhibitorsMap.get(attached);
 
653
                                                if (a != null) {
810
654
                                                        a.remove(pnObject);
811
655
                                                }
812
656
 
813
 
                                                attached.removeFromArc((Arc)pnObject);
814
 
                                                if (attached instanceof Transition){
815
 
                                                        ((Transition)attached).removeArcCompareObject((Arc)pnObject);
 
657
                                                attached.removeFromArc((Arc) pnObject);
 
658
                                                if (attached instanceof Transition) {
 
659
                                                        ((Transition) attached)
 
660
                                                                        .removeArcCompareObject((Arc) pnObject);
816
661
                                                }
817
 
                                                //attached.updateConnected(); //causing null pointer exceptions (?)
 
662
                                                // attached.updateConnected(); //causing null pointer
 
663
                                                // exceptions (?)
818
664
                                        }
819
665
 
820
 
 
821
 
                                        attached = ((Arc)pnObject).getTarget();
 
666
                                        attached = ((Arc) pnObject).getTarget();
822
667
 
823
668
                                        if (attached != null) {
824
 
                                                if (inhibitorsMap.get(attached) != null) { //causing null pointer exceptions (!)
 
669
                                                if (inhibitorsMap.get(attached) != null) { // causing
 
670
                                                                                                                                        // null
 
671
                                                                                                                                        // pointer
 
672
                                                                                                                                        // exceptions
 
673
                                                                                                                                        // (!)
825
674
                                                        inhibitorsMap.get(attached).remove(pnObject);
826
675
                                                }
827
676
 
828
 
                                                attached.removeToArc((Arc)pnObject);
829
 
                                                if (attached instanceof Transition) {
830
 
                                                        ((Transition)attached).removeArcCompareObject((Arc)pnObject);
831
 
                                                }
832
 
                                                //                                              attached.updateConnected(); //causing null pointer exceptions (?)
833
 
                                        }
834
 
                                }else if (pnObject instanceof TAPNInhibitorArc) {
835
 
 
836
 
                                        // get source and target of the arc
837
 
                                        PlaceTransitionObject attached = ((Arc)pnObject).getSource();
838
 
 
839
 
                                        if (attached != null) {
840
 
                                                ArrayList<TAPNInhibitorArc> a=tapnInhibitorsMap.get(attached);
841
 
                                                if (a!=null) {
842
 
                                                        a.remove(pnObject);
843
 
                                                }
844
 
 
845
 
                                                attached.removeFromArc((Arc)pnObject);
846
 
                                                if (attached instanceof Transition){
847
 
                                                        ((Transition)attached).removeArcCompareObject((Arc)pnObject);
848
 
                                                }
849
 
                                                //attached.updateConnected(); //causing null pointer exceptions (?)
850
 
                                        }
851
 
 
852
 
 
853
 
                                        attached = ((Arc)pnObject).getTarget();
854
 
 
855
 
                                        if (attached != null) {
856
 
                                                if (tapnInhibitorsMap.get(attached) != null) { //causing null pointer exceptions (!)
857
 
                                                        tapnInhibitorsMap.get(attached).remove(pnObject);
858
 
                                                }
859
 
 
860
 
                                                attached.removeToArc((Arc)pnObject);
861
 
                                                if (attached instanceof Transition) {
862
 
                                                        ((Transition)attached).removeArcCompareObject((Arc)pnObject);
863
 
                                                }
864
 
                                                //                                              attached.updateConnected(); //causing null pointer exceptions (?)
 
677
                                                attached.removeToArc((Arc) pnObject);
 
678
                                                if (attached instanceof Transition) {
 
679
                                                        ((Transition) attached)
 
680
                                                                        .removeArcCompareObject((Arc) pnObject);
 
681
                                                }
 
682
                                                // attached.updateConnected(); //causing null pointer
 
683
                                                // exceptions (?)
865
684
                                        }
866
685
                                }
867
686
 
872
691
                                        notifyObservers(pnObject);
873
692
                                }
874
693
                        }
875
 
                } catch(NullPointerException npe){
876
 
                        System.out.println("NullPointerException [debug]\n" + npe.getMessage());
 
694
                } catch (NullPointerException npe) {
 
695
                        System.out.println("NullPointerException [debug]\n"
 
696
                                        + npe.getMessage());
877
697
                        throw npe;
878
698
                }
879
699
                // we reset to null so that the wrong ArrayList can't get added to
880
700
                changeArrayList = null;
881
701
        }
882
702
 
883
 
 
884
703
        /**
885
 
         * Returns an iterator for the transitions array.
886
 
         * Used by Animator.class to set all enabled transitions to highlighted 
 
704
         * Returns an iterator for the transitions array. Used by Animator.class to
 
705
         * set all enabled transitions to highlighted
887
706
         */
888
 
        public Iterator<Transition> returnTransitions(){
 
707
        public Iterator<Transition> returnTransitions() {
889
708
                return transitionsArray.iterator();
890
709
        }
891
710
 
892
 
 
893
 
        public boolean isConstantNameUsed(String name){
894
 
                return constants.isConstantNameUsed(name);
895
 
        }
896
711
        /**
897
 
         * Sets an internal ArrayList according to the class of the object passed in.
898
 
         * @param pnObject The pnObject in question.
 
712
         * Sets an internal ArrayList according to the class of the object passed
 
713
         * in.
 
714
         * 
 
715
         * @param pnObject
 
716
         *            The pnObject in question.
899
717
         * @return Returns True if the pnObject is of type Place, Transition or Arc
900
718
         */
901
719
        private boolean setPetriNetObjectArrayList(PetriNetObject pnObject) {
904
722
                if (pnObject instanceof Transition) {
905
723
                        changeArrayList = transitionsArray;
906
724
                        return true;
907
 
                } else if (pnObject instanceof Place){
 
725
                } else if (pnObject instanceof Place) {
908
726
                        changeArrayList = placesArray;
909
727
                        return true;
910
 
                }
911
 
                else if (pnObject instanceof TAPNInhibitorArc) {
 
728
                } else if (pnObject instanceof TimedInhibitorArcComponent) {
912
729
                        changeArrayList = arcsArray;
913
730
                        return true;
914
 
                }       
915
 
                else if (pnObject instanceof NormalArc) {
 
731
                } else if (pnObject instanceof TimedOutputArcComponent) {
916
732
                        changeArrayList = arcsArray;
917
733
                        return true;
918
734
                } else if (pnObject instanceof InhibitorArc) {
919
735
                        changeArrayList = inhibitorsArray;
920
736
                        return true;
921
 
                }
922
 
                else if(pnObject instanceof AnnotationNote) {
 
737
                } else if (pnObject instanceof AnnotationNote) {
923
738
                        changeArrayList = labelsArray;
924
739
                        return true;
925
 
                }    
 
740
                }
926
741
                return false;
927
742
        }
928
743
 
929
 
 
930
744
        /**
931
 
         * Returns an iterator of all PetriNetObjects - the order of these cannot be guaranteed.
 
745
         * Returns an iterator of all PetriNetObjects - the order of these cannot be
 
746
         * guaranteed.
 
747
         * 
932
748
         * @return An iterator of all PetriNetObjects
933
749
         */
934
 
        public Iterator<PetriNetObject> getPetriNetObjects(){
 
750
        public Iterable<PetriNetObject> getPetriNetObjects() {
935
751
                ArrayList<PetriNetObject> all = new ArrayList<PetriNetObject>(placesArray);
936
752
                all.addAll(transitionsArray);
937
753
                all.addAll(arcsArray);
938
754
                all.addAll(labelsArray);
939
755
 
940
 
                return all.iterator();
941
 
        }
942
 
 
943
 
 
944
 
        public boolean hasPlaceTransitionObjects(){
945
 
                return (placesArray.size() + transitionsArray.size()) > 0;
946
 
        }
947
 
 
948
 
 
949
 
        /**
950
 
         * Creates a Label object from a Label DOM Element
951
 
         * @param inputLabelElement Input Label DOM Element
952
 
         * @return Label Object
953
 
         */
954
 
        private AnnotationNote createAnnotation (Element inputLabelElement) {
955
 
                int positionXInput = 0;
956
 
                int positionYInput = 0;
957
 
                int widthInput = 0;
958
 
                int heightInput = 0;
959
 
                String text = null;
960
 
                boolean borderInput = true;
961
 
 
962
 
                String positionXTempStorage = inputLabelElement.getAttribute("xPosition");
963
 
                String positionYTempStorage = inputLabelElement.getAttribute("yPosition");
964
 
                String widthTemp = inputLabelElement.getAttribute("w");
965
 
                String heightTemp = inputLabelElement.getAttribute("h");
966
 
                String textTempStorage = inputLabelElement.getAttribute("txt");
967
 
                String borderTemp = inputLabelElement.getAttribute("border");
968
 
 
969
 
                if (positionXTempStorage.length() > 0) {
970
 
                        positionXInput = Integer.valueOf(positionXTempStorage).intValue() + 1;
971
 
                }
972
 
 
973
 
                if (positionYTempStorage.length() > 0){
974
 
                        positionYInput = Integer.valueOf(positionYTempStorage).intValue() + 1;
975
 
                }
976
 
 
977
 
                if (widthTemp.length() > 0) {
978
 
                        widthInput = Integer.valueOf(widthTemp).intValue() + 1;
979
 
                }
980
 
 
981
 
                if (heightTemp.length() > 0) {
982
 
                        heightInput = Integer.valueOf(heightTemp).intValue() + 1;
983
 
                }
984
 
 
985
 
                if (borderTemp.length()>0) {
986
 
                        borderInput = Boolean.valueOf(borderTemp).booleanValue();
987
 
                } else {
988
 
                        borderInput = true;
989
 
                }
990
 
 
991
 
                if (textTempStorage.length() > 0) {
992
 
                        text = textTempStorage;
993
 
                } else {
994
 
                        text = "";
995
 
                }
996
 
 
997
 
                return new AnnotationNote(text, positionXInput, positionYInput, 
998
 
                                widthInput, heightInput, borderInput);
999
 
        }   
1000
 
 
1001
 
 
1002
 
 
1003
 
        /**
1004
 
         * Creates a Transition object from a Transition DOM Element
1005
 
         * @param inputTransitionElement Input Transition DOM Element
1006
 
         * @return Transition Object
1007
 
         */
1008
 
        private Transition createTransition(Element element){
1009
 
                double positionXInput = 0;
1010
 
                double positionYInput = 0;
1011
 
                String idInput = null;
1012
 
                String nameInput = null;
1013
 
                double nameOffsetYInput = 0;
1014
 
                double nameOffsetXInput = 0;
1015
 
                boolean timedTransition;
1016
 
                boolean infiniteServer;
1017
 
                int angle = 0;
1018
 
                int priority = 1;
1019
 
                String positionXTempStorage = element.getAttribute("positionX");
1020
 
                String positionYTempStorage = element.getAttribute("positionY");
1021
 
                String idTempStorage = element.getAttribute("id");
1022
 
                String nameTempStorage = element.getAttribute("name");
1023
 
                String nameOffsetXTempStorage = element.getAttribute("nameOffsetX");
1024
 
                String nameOffsetYTempStorage = element.getAttribute("nameOffsetY");
1025
 
                String nameTimed = element.getAttribute("timed");
1026
 
                String nameInfiniteServer = element.getAttribute("infiniteServer");
1027
 
                String nameAngle = element.getAttribute("angle");
1028
 
                String namePriority = element.getAttribute("priority");
1029
 
 
1030
 
                if (nameTimed.length() == 0) {
1031
 
                        timedTransition = false;
1032
 
                } else if (nameTimed.length()== 5) {
1033
 
                        timedTransition = false;
1034
 
                } else {
1035
 
                        timedTransition = true;
1036
 
                }
1037
 
 
1038
 
                infiniteServer = !(nameInfiniteServer.length() == 0 ||
1039
 
                                nameInfiniteServer.length() == 5);
1040
 
 
1041
 
                if (positionXTempStorage.length() > 0) {
1042
 
                        positionXInput = Double.valueOf(positionXTempStorage).doubleValue() *
1043
 
                        (1) +
1044
 
                        (1);
1045
 
                }
1046
 
                if (positionYTempStorage.length() > 0) {
1047
 
                        positionYInput = Double.valueOf(positionYTempStorage).doubleValue() *
1048
 
                        (1) +
1049
 
                        (1);
1050
 
                }
1051
 
 
1052
 
                positionXInput = Grid.getModifiedX(positionXInput);
1053
 
                positionYInput = Grid.getModifiedY(positionYInput);
1054
 
 
1055
 
                if (idTempStorage.length() > 0) {
1056
 
                        idInput = idTempStorage;
1057
 
                } else if (nameTempStorage.length() > 0) {
1058
 
                        idInput = nameTempStorage;
1059
 
                }
1060
 
 
1061
 
                if (nameTempStorage.length() > 0) {
1062
 
                        nameInput = nameTempStorage;
1063
 
                } else if (idTempStorage.length() > 0) {
1064
 
                        nameInput = idTempStorage;
1065
 
                }
1066
 
 
1067
 
                if (nameOffsetXTempStorage.length() > 0) {
1068
 
                        nameOffsetXInput = Double.valueOf(nameOffsetXTempStorage).doubleValue();
1069
 
                }
1070
 
 
1071
 
                if (nameOffsetYTempStorage.length() > 0) {
1072
 
                        nameOffsetYInput = Double.valueOf(nameOffsetYTempStorage).doubleValue();
1073
 
                }
1074
 
 
1075
 
                if (nameAngle.length() > 0) {
1076
 
                        angle = Integer.valueOf(nameAngle).intValue();
1077
 
                }
1078
 
 
1079
 
                if (namePriority.length() > 0) {
1080
 
                        priority = Integer.valueOf(namePriority).intValue();
1081
 
                }
1082
 
 
1083
 
                Transition transition = null;
1084
 
                {
1085
 
                        transition =  
1086
 
                                new TAPNTransition(positionXInput, positionYInput,     
1087
 
                                                idInput, 
1088
 
                                                nameInput, 
1089
 
                                                nameOffsetXInput, nameOffsetYInput, 
1090
 
                                                timedTransition, 
1091
 
                                                infiniteServer,
1092
 
                                                angle,
1093
 
                                                priority);
1094
 
                }
1095
 
 
1096
 
                return transition;
1097
 
        }
1098
 
 
1099
 
 
1100
 
        private Place createPlace(Element element){
1101
 
                double positionXInput = 0;
1102
 
                double positionYInput = 0;
1103
 
                String idInput = null;
1104
 
                String nameInput = null;
1105
 
                double nameOffsetYInput = 0;
1106
 
                double nameOffsetXInput = 0;
1107
 
                int initialMarkingInput = 0;
1108
 
                double markingOffsetXInput = 0;
1109
 
                double markingOffsetYInput = 0;
1110
 
                int capacityInput = 0;
1111
 
 
1112
 
                String positionXTempStorage = element.getAttribute("positionX");
1113
 
                String positionYTempStorage = element.getAttribute("positionY");
1114
 
                String idTempStorage = element.getAttribute("id");
1115
 
                String nameTempStorage = element.getAttribute("name");
1116
 
                String nameOffsetXTempStorage = element.getAttribute("nameOffsetX");
1117
 
                String nameOffsetYTempStorage = element.getAttribute("nameOffsetY");
1118
 
                String initialMarkingTempStorage = element.getAttribute("initialMarking");
1119
 
                String markingOffsetXTempStorage = element.getAttribute("markingOffsetX");
1120
 
                String markingOffsetYTempStorage = element.getAttribute("markingOffsetY");
1121
 
                String capacityTempStorage = element.getAttribute("capacity");
1122
 
                String invariantTempStorage = element.getAttribute("invariant");
1123
 
 
1124
 
 
1125
 
                if (positionXTempStorage.length() > 0) {
1126
 
                        positionXInput = Double.valueOf(positionXTempStorage).doubleValue() + 1;
1127
 
                }
1128
 
                if (positionYTempStorage.length() > 0) {
1129
 
                        positionYInput = Double.valueOf(positionYTempStorage).doubleValue() + 1;
1130
 
                }
1131
 
                positionXInput = Grid.getModifiedX(positionXInput);
1132
 
                positionYInput = Grid.getModifiedY(positionYInput);
1133
 
 
1134
 
                if (idTempStorage.length() > 0) {
1135
 
                        idInput = idTempStorage;
1136
 
                } else if (nameTempStorage.length() > 0) {
1137
 
                        idInput = nameTempStorage;
1138
 
                }
1139
 
 
1140
 
                if (nameTempStorage.length() > 0) {
1141
 
                        nameInput = nameTempStorage;
1142
 
                } else if (idTempStorage.length() > 0) {
1143
 
                        nameInput = idTempStorage;
1144
 
                }
1145
 
 
1146
 
                if (nameOffsetYTempStorage.length() > 0) {
1147
 
                        nameOffsetXInput = Double.valueOf(nameOffsetXTempStorage).doubleValue();
1148
 
                }
1149
 
                if (nameOffsetXTempStorage.length() > 0) {
1150
 
                        nameOffsetYInput = Double.valueOf(nameOffsetYTempStorage).doubleValue();
1151
 
                }
1152
 
 
1153
 
                if (initialMarkingTempStorage.length() > 0) {
1154
 
                        initialMarkingInput = Integer.valueOf(initialMarkingTempStorage).intValue();
1155
 
                }
1156
 
                if (markingOffsetXTempStorage.length() > 0) {
1157
 
                        markingOffsetXInput = Double.valueOf(markingOffsetXTempStorage).doubleValue();
1158
 
                }
1159
 
                if (markingOffsetYTempStorage.length() > 0) {
1160
 
                        markingOffsetYInput = Double.valueOf(markingOffsetYTempStorage).doubleValue();
1161
 
                }
1162
 
 
1163
 
                if (capacityTempStorage.length() > 0) {
1164
 
                        capacityInput = Integer.valueOf(capacityTempStorage).intValue();
1165
 
                }
1166
 
 
1167
 
                Place  place = null;
1168
 
 
1169
 
                if (invariantTempStorage == null || invariantTempStorage == ""){
1170
 
                        place = new Place(positionXInput, positionYInput,
1171
 
                                        idInput,  
1172
 
                                        nameInput, 
1173
 
                                        nameOffsetXInput, nameOffsetYInput,
1174
 
                                        initialMarkingInput,
1175
 
                                        markingOffsetXInput, markingOffsetYInput,  
1176
 
                                        capacityInput);
1177
 
 
1178
 
                } else {
1179
 
 
1180
 
                        place = new TimedPlace(positionXInput, positionYInput,
1181
 
                                        idInput,  
1182
 
                                        nameInput, 
1183
 
                                        nameOffsetXInput, nameOffsetYInput,
1184
 
                                        initialMarkingInput,
1185
 
                                        markingOffsetXInput, markingOffsetYInput,  
1186
 
                                        capacityInput, invariantTempStorage);
1187
 
                }
1188
 
 
1189
 
                return place;
1190
 
        }
1191
 
 
1192
 
 
1193
 
        /**
1194
 
         * Creates a Arc object from a Arc DOM Element
1195
 
         * @param inputArcElement Input Arc DOM Element
1196
 
         * @return Arc Object
1197
 
         */
1198
 
        private Arc createArc(Element inputArcElement){
1199
 
                String idInput = null;
1200
 
                String sourceInput = null;
1201
 
                String targetInput = null;
1202
 
                double startX = 0;
1203
 
                double startY = 0;
1204
 
                boolean taggedArc;
1205
 
 
1206
 
                sourceInput = inputArcElement.getAttribute("source");
1207
 
                targetInput = inputArcElement.getAttribute("target");
1208
 
                String idTempStorage = inputArcElement.getAttribute("id");
1209
 
                String sourceTempStorage = inputArcElement.getAttribute("source");
1210
 
                String targetTempStorage = inputArcElement.getAttribute("target");
1211
 
                String inscriptionTempStorage = inputArcElement.getAttribute("inscription");
1212
 
 
1213
 
                String taggedTempStorage = inputArcElement.getAttribute("tagged");
1214
 
                //              String inscriptionOffsetXTempStorage = inputArcElement.getAttribute("inscriptionOffsetX");
1215
 
                //              String inscriptionOffsetYTempStorage = inputArcElement.getAttribute("inscriptionOffsetY");
1216
 
 
1217
 
                taggedArc = !(taggedTempStorage.length() == 0 ||
1218
 
                                taggedTempStorage.length() == 5);
1219
 
 
1220
 
                if (idTempStorage.length() > 0) {
1221
 
                        idInput = idTempStorage;
1222
 
                }
1223
 
                if (sourceTempStorage.length() > 0) {
1224
 
                        sourceInput = sourceTempStorage;
1225
 
                }
1226
 
                if (targetTempStorage.length() > 0) {
1227
 
                        targetInput = targetTempStorage;
1228
 
                }
1229
 
 
1230
 
 
1231
 
                if (sourceInput.length() > 0) {
1232
 
                        if (getPlaceTransitionObject(sourceInput) != null) {
1233
 
                                //                              System.out.println("PNMLDATA: sourceInput is not null");
1234
 
                                startX = getPlaceTransitionObject(sourceInput).getPositionX();
1235
 
                                startX += getPlaceTransitionObject(sourceInput).centreOffsetLeft();
1236
 
                                startY = getPlaceTransitionObject(sourceInput).getPositionY();
1237
 
                                startY += getPlaceTransitionObject(sourceInput).centreOffsetTop();
1238
 
                        }
1239
 
                }
1240
 
                if (targetInput.length() > 0) {
1241
 
                        if (getPlaceTransitionObject(targetInput) != null) {
1242
 
                                getPlaceTransitionObject(targetInput).getPositionX();
1243
 
                                getPlaceTransitionObject(targetInput).getPositionY();
1244
 
                        }
1245
 
                }
1246
 
 
1247
 
                PlaceTransitionObject sourceIn = getPlaceTransitionObject(sourceInput);
1248
 
                PlaceTransitionObject targetIn = getPlaceTransitionObject(targetInput);
1249
 
 
1250
 
                // add the insets and offset
1251
 
                int aStartx = sourceIn.getX() + sourceIn.centreOffsetLeft();
1252
 
                int aStarty = sourceIn.getY() + sourceIn.centreOffsetTop();
1253
 
 
1254
 
                int aEndx = targetIn.getX() + targetIn.centreOffsetLeft();
1255
 
                int aEndy = targetIn.getY() + targetIn.centreOffsetTop();
1256
 
 
1257
 
 
1258
 
                double _startx = aStartx;
1259
 
                double _starty = aStarty;
1260
 
                double _endx = aEndx;
1261
 
                double _endy = aEndy;
1262
 
                //TODO
1263
 
 
1264
 
                Arc tempArc;
1265
 
 
1266
 
 
1267
 
 
1268
 
                String type = "normal";  // default value
1269
 
                NodeList nl = inputArcElement.getElementsByTagName("type");
1270
 
                if (nl.getLength()>0) {
1271
 
                        type = ((Element)(nl.item(0))).getAttribute("type");
1272
 
                }
1273
 
 
1274
 
 
1275
 
                if (type.equals("inhibitor")) {
1276
 
                        tempArc = new InhibitorArc(_startx, _starty, 
1277
 
                                        _endx, _endy, 
1278
 
                                        sourceIn, 
1279
 
                                        targetIn,       
1280
 
                                        /*CB Joakim Byg - inscribtion is inserted as the arcs weight*/                                    
1281
 
                                        Integer.valueOf(inscriptionTempStorage),
1282
 
                                        /*EOC*/                                    
1283
 
                                        idInput);
1284
 
                } else if (type.equals("tapnInhibitor")){
1285
 
                        tempArc = new TAPNInhibitorArc(new TimedArc(new NormalArc(_startx, _starty, _endx, _endy, sourceIn, targetIn, 1, idInput, taggedArc)), (inscriptionTempStorage!=null ? inscriptionTempStorage : ""));
1286
 
                } else {
1287
 
 
1288
 
 
1289
 
 
1290
 
                        //XXX - cant check for if arc is timed, check pn-type instead
1291
 
                        if (type.equals("timed")){
1292
 
                                tempArc = new TimedArc(new NormalArc (_startx, _starty,
1293
 
                                                _endx, _endy,
1294
 
                                                sourceIn,
1295
 
                                                targetIn,
1296
 
                                                1,
1297
 
                                                idInput,
1298
 
                                                taggedArc), (inscriptionTempStorage!=null ? inscriptionTempStorage : ""));
1299
 
                        }else if (type.equals("transport")){
1300
 
                                String[] inscriptionSplit = {};
1301
 
                                if (inscriptionTempStorage.contains(":")){
1302
 
                                        inscriptionSplit = inscriptionTempStorage.split(":");
1303
 
                                }
1304
 
                                boolean isInPreSet = false;
1305
 
                                if ( sourceIn instanceof Place ) {
1306
 
                                        isInPreSet = true;
1307
 
                                }
1308
 
                                tempArc = new TransportArc( new TimedArc( new NormalArc(_startx, _starty,
1309
 
                                                _endx, _endy,
1310
 
                                                sourceIn,
1311
 
                                                targetIn,
1312
 
                                                1,
1313
 
                                                idInput,
1314
 
                                                taggedArc), inscriptionSplit[0]), Integer.parseInt(inscriptionSplit[1]), isInPreSet );
1315
 
                        }else {
1316
 
                                tempArc = new NormalArc(        _startx, _starty,
1317
 
                                                _endx, _endy,
1318
 
                                                sourceIn,
1319
 
                                                targetIn,
1320
 
                                                //inscribtion is inserted as the arcs weight                                                                            
1321
 
                                                Integer.valueOf(inscriptionTempStorage),
1322
 
                                                idInput,
1323
 
                                                taggedArc);
1324
 
                        }
1325
 
 
1326
 
                }
1327
 
 
1328
 
                getPlaceTransitionObject(sourceInput).addConnectFrom(tempArc);
1329
 
                getPlaceTransitionObject(targetInput).addConnectTo(tempArc);
1330
 
 
1331
 
                //              **********************************************************************************
1332
 
                //              The following section attempts to load and display arcpath details****************
1333
 
 
1334
 
                //NodeList nodelist = inputArcElement.getChildNodes();
1335
 
                NodeList nodelist = inputArcElement.getElementsByTagName("arcpath");
1336
 
                if (nodelist.getLength()>0) {
1337
 
                        tempArc.getArcPath().purgePathPoints();
1338
 
                        for (int i = 0; i < nodelist.getLength(); i++) {         
1339
 
                                Node node = nodelist.item(i);
1340
 
                                if(node instanceof Element) {
1341
 
                                        Element element = (Element)node;
1342
 
                                        if ("arcpath".equals(element.getNodeName())){
1343
 
                                                String arcTempX = element.getAttribute("x");
1344
 
                                                String arcTempY = element.getAttribute("y");
1345
 
                                                String arcTempType = element.getAttribute("arcPointType");
1346
 
                                                float arcPointX = Float.valueOf(arcTempX).floatValue();
1347
 
                                                float arcPointY = Float.valueOf(arcTempY).floatValue();
1348
 
                                                arcPointX += Pipe.ARC_CONTROL_POINT_CONSTANT + 1;
1349
 
                                                arcPointY += Pipe.ARC_CONTROL_POINT_CONSTANT + 1;
1350
 
                                                boolean arcPointType = 
1351
 
                                                        Boolean.valueOf(arcTempType).booleanValue();
1352
 
                                                tempArc.getArcPath().addPoint(arcPointX,arcPointY,arcPointType);
1353
 
                                        }
1354
 
                                }
1355
 
                        }
1356
 
                }
1357
 
 
1358
 
                //              Arc path creation ends here***************************************************************
1359
 
                //              ******************************************************************************************
1360
 
                return tempArc;
1361
 
        }
1362
 
 
1363
 
 
1364
 
        /**
1365
 
         * Stores Current Marking
1366
 
         */
1367
 
        public void storeState(){
1368
 
                boolean isTAPN = true;
1369
 
                if (isTAPN){
1370
 
                        if(!isUsingColors()){
1371
 
                                placeMarkingStorageMap = new HashMap<TimedPlace, ArrayList<BigDecimal>>();
1372
 
                                for (Place p : getPlaces()){
1373
 
                                        placeMarkingStorageMap.put( (TimedPlace)p, (ArrayList<BigDecimal>)((TimedPlace)p).getTokens().clone() );
1374
 
                                }
1375
 
                        }else{
1376
 
                                coloredPlaceMarkingStorageMap = new HashMap<ColoredTimedPlace, List<ColoredToken>>();
1377
 
                                for(Place p : getPlaces()){
1378
 
                                        ColoredTimedPlace ctp = (ColoredTimedPlace)p;
1379
 
                                        coloredPlaceMarkingStorageMap.put(ctp, copyListOfColoredTokens(ctp.getColoredTokens()));
1380
 
                                }
1381
 
                        }
1382
 
                }else {
1383
 
                        int placeSize = placesArray.size();
1384
 
                        markingVectorAnimationStorage = new int[placeSize];
1385
 
                        for (int placeNo = 0; placeNo < placeSize; placeNo++) {
1386
 
                                markingVectorAnimationStorage[placeNo] = 
1387
 
                                        ((Place)placesArray.get(placeNo)).getCurrentMarking();
1388
 
                        }
1389
 
                }
1390
 
        }
1391
 
 
1392
 
        private List<ColoredToken> copyListOfColoredTokens(
1393
 
                        List<ColoredToken> coloredTokens) {
1394
 
                ArrayList<ColoredToken> list = new ArrayList<ColoredToken>(coloredTokens.size());
1395
 
                for(ColoredToken token : coloredTokens){
1396
 
                        list.add(new ColoredToken(token));
1397
 
                }
1398
 
                return list;
1399
 
        }
1400
 
 
1401
 
 
1402
 
        public HashMap<TimedPlace, ArrayList<BigDecimal>> getCurrentMarking(){
1403
 
                boolean isTAPN = true;
1404
 
 
1405
 
                HashMap<TimedPlace, ArrayList<BigDecimal>> toReturn = new HashMap<TimedPlace, ArrayList<BigDecimal>>();
1406
 
                if (isTAPN){
1407
 
                        for (Place p : getPlaces()){
1408
 
                                toReturn.put( (TimedPlace)p, (ArrayList<BigDecimal>)((TimedPlace)p).getTokens().clone() );
1409
 
                        }
1410
 
                }
1411
 
                return toReturn;
1412
 
        }
1413
 
 
1414
 
 
1415
 
        /**
1416
 
         * Restores To previous Stored Marking
1417
 
         */
1418
 
        public void restoreState(){
1419
 
                boolean isTAPN = true;
1420
 
                if(isTAPN){
1421
 
                        if(!isUsingColors()){
1422
 
                                if (placeMarkingStorageMap!=null){
1423
 
                                        for (Place p : getPlaces()){
1424
 
                                                ArrayList<BigDecimal> markingOfP = placeMarkingStorageMap.get((TimedPlace)p);
1425
 
                                                p.setCurrentMarking( markingOfP.size() );
1426
 
                                                ((TimedPlace)p).setAgeOfTokens(markingOfP);
1427
 
                                                setChanged();
1428
 
                                                notifyObservers(p);
1429
 
 
1430
 
                                        }
1431
 
                                }
1432
 
                        }else{
1433
 
                                if(coloredPlaceMarkingStorageMap != null){
1434
 
                                        for(Place p : getPlaces()){
1435
 
                                                ColoredTimedPlace ctp = (ColoredTimedPlace)p;
1436
 
                                                ctp.setColoredTokens(coloredPlaceMarkingStorageMap.get(ctp));
1437
 
                                                setChanged();
1438
 
                                                notifyObservers(p);
1439
 
 
1440
 
                                        }
1441
 
                                }
1442
 
                        }
1443
 
                }else{
1444
 
                        if(markingVectorAnimationStorage != null) {
1445
 
                                int placeSize = placesArray.size();
1446
 
                                for (int placeNo = 0; placeNo < placeSize; placeNo++) {
1447
 
                                        Place place = ((Place)placesArray.get(placeNo));
1448
 
                                        if (place != null) {
1449
 
                                                place.setCurrentMarking(markingVectorAnimationStorage[placeNo]);
1450
 
                                                setChanged();
1451
 
                                                notifyObservers(place);
1452
 
 
1453
 
                                        }
1454
 
                                }
1455
 
                        }
1456
 
                }
1457
 
        }
1458
 
 
1459
 
 
1460
 
        /**
1461
 
         * Fire a specified transition, no affect if transtions not enabled
1462
 
         * @param transition Reference of specifiec Transition
1463
 
         *
1464
 
         * @author Kenneth Yrke Joergensen <kenneth@yrke.dk> Changed to handeling
1465
 
         * firing modes when working with TAPN.
1466
 
         */
1467
 
        public FiringAction fireTransition(Transition transition) {
1468
 
                FiringAction toReturn = null;
1469
 
 
1470
 
                // If it is a TAPN
1471
 
                if (Pipe.drawingmode == Pipe.drawmodes.TIMEDARCPETRINET){
1472
 
                        if(!isUsingColors()){
1473
 
                                toReturn = fireTransitionInNonColoredTAPN(transition);  
1474
 
                        }else{
1475
 
                                toReturn = fireTransitionInColoredTAPN(transition);
1476
 
                        }
1477
 
                }
1478
 
 
1479
 
                return toReturn;
1480
 
        }
1481
 
 
1482
 
 
1483
 
        private FiringAction fireTransitionInColoredTAPN(
1484
 
                        Transition transition) {
1485
 
                ColoredDiscreteFiringAction firingAction = new ColoredDiscreteFiringAction((TAPNTransition)transition);
1486
 
 
1487
 
                setEnabledTransitions();
1488
 
                if(transition.isEnabled()){
1489
 
                        HashMap<Integer, ColoredToken> tokensConsumedByTransportArcs = new HashMap<Integer, ColoredToken>();
1490
 
 
1491
 
                        for(Arc arc : transition.getPreset()){                          
1492
 
                                if(arc instanceof ColoredTransportArc){
1493
 
                                        ColoredTransportArc tarc = (ColoredTransportArc)arc;
1494
 
                                        ArrayList<ColoredToken> possibleTokens = new ArrayList<ColoredToken>();
1495
 
                                        ColoredTimedPlace source = (ColoredTimedPlace)arc.getSource();
1496
 
                                        ColoredTimedPlace target = (ColoredTimedPlace)tarc.getConnectedTo().getTarget();
1497
 
 
1498
 
                                        for(ColoredToken token : source.getColoredTokens()){
1499
 
                                                if(tarc.satisfiesGuard(token) && target.satisfiesInvariant(token)){
1500
 
                                                        possibleTokens.add(token);
1501
 
                                                }
1502
 
                                        }
1503
 
 
1504
 
                                        ColoredToken usedToken = CreateGui.getAnimator().firingmode.fire(possibleTokens);
1505
 
                                        firingAction.addConsumedToken(source, usedToken);
1506
 
                                        tokensConsumedByTransportArcs.put(tarc.getGroupNr(), usedToken);
1507
 
                                        source.removeColoredToken(usedToken);
1508
 
                                }else if(arc instanceof ColoredInputArc){
1509
 
                                        ColoredInputArc inputArc = (ColoredInputArc)arc;
1510
 
                                        ArrayList<ColoredToken> possibleTokens = new ArrayList<ColoredToken>();
1511
 
                                        ColoredTimedPlace source = (ColoredTimedPlace)arc.getSource();
1512
 
 
1513
 
                                        for(ColoredToken token : source.getColoredTokens()){
1514
 
                                                if(inputArc.satisfiesGuard(token)){
1515
 
                                                        possibleTokens.add(token);
1516
 
                                                }
1517
 
                                        }
1518
 
 
1519
 
                                        ColoredToken usedToken = CreateGui.getAnimator().firingmode.fire(possibleTokens);
1520
 
                                        firingAction.addConsumedToken(source, usedToken);
1521
 
                                        source.removeColoredToken(usedToken);
1522
 
                                }
1523
 
                        }
1524
 
 
1525
 
                        for(Arc arc : transition.getPostset()){
1526
 
                                if(arc instanceof ColoredTransportArc){
1527
 
                                        ColoredTransportArc tarc = (ColoredTransportArc)arc;
1528
 
                                        ColoredToken consumed = tokensConsumedByTransportArcs.get(tarc.getGroupNr());
1529
 
                                        ColoredToken newToken = tarc.generateOutputToken(consumed);
1530
 
 
1531
 
                                        ColoredTimedPlace target = (ColoredTimedPlace)tarc.getTarget();
1532
 
                                        firingAction.addProducedToken(target, newToken);
1533
 
                                        target.addColoredToken(newToken);
1534
 
                                }else if(arc instanceof ColoredOutputArc){
1535
 
                                        ColoredOutputArc outputArc = (ColoredOutputArc)arc;
1536
 
                                        ColoredToken token = outputArc.generateOutputToken();
1537
 
                                        ColoredTimedPlace target = (ColoredTimedPlace)outputArc.getTarget();
1538
 
 
1539
 
                                        firingAction.addProducedToken(target, token);
1540
 
                                        target.addColoredToken(token);
1541
 
                                }
1542
 
                        }
1543
 
                }               
1544
 
 
1545
 
                return firingAction;
1546
 
        }
1547
 
 
1548
 
 
1549
 
        private FiringAction fireTransitionInNonColoredTAPN(Transition transition) {
1550
 
                DiscreetFiringAction toReturn = new DiscreetFiringAction(transition);
1551
 
                if (transition != null){
1552
 
                        setEnabledTransitions();
1553
 
 
1554
 
                        // Index:
1555
 
                        // Check transition is enables
1556
 
                        //Find the tokens valid to fire (check guard)
1557
 
                        // - If transportarc check if invariant is ok
1558
 
                        // Select the tokens to consume, based on firing stradegy
1559
 
                        // Consume the tokens and create the tokens
1560
 
 
1561
 
                        if (transition.isEnabled()){
1562
 
                                HashMap<Integer, BigDecimal> tokensConsumedByTransportArcs = new HashMap<Integer, BigDecimal>();
1563
 
 
1564
 
                                for (Arc a : (LinkedList<Arc>)transition.getPreset() ){
1565
 
 
1566
 
 
1567
 
 
1568
 
                                        if (a instanceof TransportArc){
1569
 
                                                ArrayList<BigDecimal> eligableToken = new ArrayList<BigDecimal>();
1570
 
 
1571
 
 
1572
 
                                                TimedPlace p = (TimedPlace)a.getSource();
1573
 
 
1574
 
                                                ArrayList<BigDecimal> tokensOfPlace = p.getTokens();                                    
1575
 
 
1576
 
                                                TimedPlace targetPlace = (TimedPlace)((TransportArc)a).getConnectedTo().getTarget();
1577
 
 
1578
 
                                                for (int i=0; i< tokensOfPlace.size(); i++){
1579
 
                                                        if ( ((TimedArc)a).satisfiesGuard(tokensOfPlace.get(i)) && targetPlace.satisfiesInvariant(tokensOfPlace.get(i))) {
1580
 
                                                                eligableToken.add(tokensOfPlace.get(i));
1581
 
                                                        }
1582
 
                                                }       
1583
 
                                                BigDecimal tokenToRemove = CreateGui.getAnimator().firingmode.fire(eligableToken);
1584
 
 
1585
 
                                                //                                                      XXX  - This will break if two tokens from the same place is consumed
1586
 
                                                toReturn.addConsumedToken(p, tokenToRemove);
1587
 
 
1588
 
                                                tokensConsumedByTransportArcs.put(((TransportArc) a).getGroupNr(), tokenToRemove);
1589
 
 
1590
 
 
1591
 
 
1592
 
                                                p.removeTokenofAge(tokenToRemove);
1593
 
                                        }
1594
 
                                        // if arc is an inhibitor arc then do nothing.
1595
 
                                        else if(a instanceof TAPNInhibitorArc)
1596
 
                                        {
1597
 
 
1598
 
                                        }
1599
 
                                        else if (a instanceof TimedArc){
1600
 
                                                ArrayList<BigDecimal> eligableToken = new ArrayList<BigDecimal>();
1601
 
                                                //int indexOfOldestEligebleToken = 0;
1602
 
 
1603
 
                                                TimedPlace p = (TimedPlace)a.getSource();
1604
 
 
1605
 
                                                ArrayList<BigDecimal> tokensOfPlace = p.getTokens();                                               
1606
 
                                                for (int i=0; i< tokensOfPlace.size(); i++){
1607
 
                                                        if ( ((TimedArc)a).satisfiesGuard(tokensOfPlace.get(i))){
1608
 
                                                                eligableToken.add(tokensOfPlace.get(i));
1609
 
                                                        }
1610
 
                                                }                                                  
1611
 
 
1612
 
                                                //Select torken to remove based on firing mode
1613
 
                                                BigDecimal tokenToRemove = CreateGui.getAnimator().firingmode.fire(eligableToken);
1614
 
 
1615
 
                                                //                                                      XXX  - This will break if two tokens from the same place is consumed
1616
 
                                                toReturn.addConsumedToken(p, tokenToRemove);
1617
 
 
1618
 
                                                p.removeTokenofAge(tokenToRemove);
1619
 
 
1620
 
 
1621
 
 
1622
 
                                        } 
1623
 
                                        else {
1624
 
                                                //Should not be possible
1625
 
                                        }
1626
 
                                }
1627
 
 
1628
 
 
1629
 
 
1630
 
                                for (Arc a : (LinkedList<Arc>)transition.getPostset() ){
1631
 
                                        if (a instanceof TransportArc){
1632
 
                                                TimedPlace p = (TimedPlace)a.getTarget();
1633
 
                                                int newNumberOfTokens = p.getTokens().size()+1;
1634
 
                                                p.setCurrentMarking(newNumberOfTokens);
1635
 
                                                ArrayList<BigDecimal> markingToBeSet = p.getTokens();
1636
 
                                                BigDecimal ageOfTokenToSet = tokensConsumedByTransportArcs.get( ((TransportArc) a).getGroupNr() );
1637
 
                                                markingToBeSet.set(markingToBeSet.size()-1,ageOfTokenToSet);
1638
 
 
1639
 
                                                p.setAgeOfTokens(markingToBeSet);
1640
 
                                        }
1641
 
                                        else{
1642
 
                                                TimedPlace p = (TimedPlace)a.getTarget();
1643
 
                                                int newNumberOfTokens = p.getTokens().size()+1;
1644
 
                                                p.setCurrentMarking(newNumberOfTokens);
1645
 
 
1646
 
                                        }
1647
 
                                }
1648
 
 
1649
 
                        }
1650
 
                }
1651
 
 
1652
 
                return toReturn;
1653
 
        }
1654
 
 
1655
 
 
1656
 
        /**
1657
 
         * Fire a specified transition and specific tokens, no affect if transtions not enabled
1658
 
         * @param transition Reference of specifiec Transition
1659
 
         *
1660
 
         * @author Kenneth Yrke Joergensen <kenneth@yrke.dk> Changed to handeling
1661
 
         * firing modes when working with TAPN.
1662
 
         */
1663
 
        public void fireTransition(Transition transition, HashMap<Place, ArrayList<BigDecimal>> consumedTokens) {
1664
 
 
1665
 
 
1666
 
                // If it is a TAPN
1667
 
                if (Pipe.drawingmode == Pipe.drawmodes.TIMEDARCPETRINET){
1668
 
 
1669
 
                        if (transition != null){
1670
 
                                setEnabledTransitions();
1671
 
 
1672
 
                                // Index:
1673
 
                                // Check transition is enables
1674
 
                                //Find the tokens valid to fire (check guard)
1675
 
                                // - If transportarc check if invariant is ok
1676
 
                                // Select the tokens to consume, based on firing stradegy
1677
 
                                // Consume the tokens and create the tokens
1678
 
 
1679
 
                                if (transition.isEnabled()){
1680
 
                                        HashMap<Integer, BigDecimal> tokensConsumedByTransportArcs = new HashMap<Integer, BigDecimal>();
1681
 
 
1682
 
                                        for ( Arc a : (LinkedList<Arc>)transition.getPreset() ){
1683
 
                                                TimedPlace p = (TimedPlace)a.getSource();
1684
 
 
1685
 
                                                if (a instanceof TransportArc){
1686
 
                                                        BigDecimal tokenToRemove = consumedTokens.get(p).get(0);
1687
 
 
1688
 
                                                        //XXX  - This will break if two tokens from the same place is consumed
1689
 
 
1690
 
                                                        tokensConsumedByTransportArcs.put(((TransportArc) a).getGroupNr(), tokenToRemove);
1691
 
 
1692
 
                                                        p.removeTokenofAge(tokenToRemove);
1693
 
 
1694
 
                                                }
1695
 
                                                else if (a instanceof TimedArc && !(a instanceof TAPNInhibitorArc)){
1696
 
                                                        //Select torken to remove based on firing mode
1697
 
                                                        BigDecimal tokenToRemove = consumedTokens.get(p).get(0);
1698
 
 
1699
 
                                                        //XXX  - This will break if two tokens from the same place is consumed
1700
 
 
1701
 
                                                        p.removeTokenofAge(tokenToRemove);
1702
 
 
1703
 
                                                }
1704
 
                                        }
1705
 
 
1706
 
 
1707
 
 
1708
 
                                        for (Arc a : (LinkedList<Arc>)transition.getPostset() ){
1709
 
                                                if (a instanceof TransportArc){
1710
 
                                                        TimedPlace p = (TimedPlace)a.getTarget();
1711
 
                                                        int newNumberOfTokens = p.getTokens().size()+1;
1712
 
                                                        p.setCurrentMarking(newNumberOfTokens);
1713
 
                                                        ArrayList<BigDecimal> markingToBeSet = p.getTokens();
1714
 
                                                        BigDecimal ageOfTokenToSet = tokensConsumedByTransportArcs.get( ((TransportArc) a).getGroupNr() );
1715
 
                                                        markingToBeSet.set(markingToBeSet.size()-1,ageOfTokenToSet);
1716
 
 
1717
 
                                                        p.setAgeOfTokens(markingToBeSet);
1718
 
                                                }
1719
 
                                                else{
1720
 
                                                        TimedPlace p = (TimedPlace)a.getTarget();
1721
 
                                                        int newNumberOfTokens = p.getTokens().size()+1;
1722
 
                                                        p.setCurrentMarking(newNumberOfTokens);
1723
 
 
1724
 
                                                }
1725
 
                                        }
1726
 
 
1727
 
                                }
1728
 
                        }                  
1729
 
 
1730
 
                }
1731
 
        }
1732
 
 
1733
 
 
1734
 
 
1735
 
        /**
1736
 
         * Fire a random transition, takes rate (probability) of Transitions into account
1737
 
         */
1738
 
        public Transition fireRandomTransition() {
1739
 
 
1740
 
                setEnabledTransitions();
1741
 
                // All the enabled transitions are of the same type:
1742
 
                // a) all are immediate transitions; or 
1743
 
                // b) all are timed transitions.
1744
 
 
1745
 
                ArrayList<Transition> enabledTransitions = new ArrayList<Transition>();
1746
 
 
1747
 
                for (int i = 0; i < transitionsArray.size(); i++) {
1748
 
                        Transition transition = (Transition)transitionsArray.get(i);
1749
 
                        if (transition.isEnabled()) {
1750
 
                                enabledTransitions.add(transition);
1751
 
                        }
1752
 
                }
1753
 
 
1754
 
                // if there is only one enabled transition, return this transition
1755
 
                if (enabledTransitions.size() == 1) {
1756
 
                        return enabledTransitions.get(0);
1757
 
                }      
1758
 
 
1759
 
                int random = randomNumber.nextInt(enabledTransitions.size());
1760
 
 
1761
 
                // no enabled transition found, so no transition can be fired
1762
 
                return enabledTransitions.get(random);
1763
 
        }
1764
 
 
1765
 
 
1766
 
        public void fireTimedTransitionBackwards(HashMap<TimedPlace, ArrayList<BigDecimal>> presetMarking, 
1767
 
                        HashMap<TimedPlace, ArrayList<BigDecimal>> postsetMarking, 
1768
 
                        TAPNTransition transition){
1769
 
                for (Arc a : (LinkedList<Arc>)transition.getPreset()){
1770
 
                        if (! presetMarking.containsKey(a.getSource()) )
1771
 
                                throw new IllegalArgumentException("Incorrect Preset for transition argument!");
1772
 
                }
1773
 
                for (Arc a : (LinkedList<Arc>)transition.getPostset()){
1774
 
                        if (! postsetMarking.containsKey(a.getTarget()) )
1775
 
                                throw new IllegalArgumentException("Incorrect Postset for transition argument!");
1776
 
                }
1777
 
 
1778
 
 
1779
 
                for (Arc a : (LinkedList<Arc>)transition.getPreset()){
1780
 
                        TimedPlace place = (TimedPlace)a.getSource();
1781
 
                        //place.setCurrentMarking( (presetMarking.get(place)).size() );
1782
 
                        //place.setAgeOfTokens(presetMarking.get(place));
1783
 
 
1784
 
                        // XXX - kyrke Hav now idea on why the two above functions do net do the work, 
1785
 
                        // But the dont... So we will just use this function for now instead
1786
 
                        place.setTokensAndAgeOfTokens(presetMarking.get(place));
1787
 
                }
1788
 
                for (Arc a : (LinkedList<Arc>)transition.getPostset()){
1789
 
                        TimedPlace place = (TimedPlace)a.getTarget();
1790
 
                        //place.setCurrentMarking( ((ArrayList<Float>)postsetMarking.get(place)).size() );
1791
 
                        //place.setAgeOfTokens(postsetMarking.get(place));
1792
 
 
1793
 
                        // XXX - kyrke Hav now idea on why the two above functions do net do the work, 
1794
 
                        // But the dont... So we will just use this function for now instead
1795
 
                        place.setTokensAndAgeOfTokens(postsetMarking.get(place));
1796
 
                }
1797
 
        }
1798
 
 
1799
 
 
1800
 
 
1801
 
 
1802
 
        public void resetEnabledTransitions()  {
1803
 
                for (int i = 0; i < transitionsArray.size(); i++) {
1804
 
                        Transition transition = (Transition)transitionsArray.get(i);
1805
 
                        transition.setEnabled(false);
1806
 
                        setChanged();
1807
 
                        notifyObservers(transition);
1808
 
                }
1809
 
        }
1810
 
 
1811
 
 
1812
 
 
1813
 
 
1814
 
 
1815
 
        public void setEnabledTransitions(){
1816
 
                for ( Transition t : getTransitions() ){
1817
 
                        boolean isEnabled = false;
1818
 
 
1819
 
                        if(!isUsingColors()){
1820
 
                                isEnabled = isTransitionEnabledNonColored(t);
1821
 
                        }else{
1822
 
                                isEnabled = isTransitionEnabledColored(t);
1823
 
                        }
1824
 
                        t.setEnabled(isEnabled);
1825
 
                        setChanged();
1826
 
                        notifyObservers(t);
1827
 
                }
1828
 
        }
1829
 
 
1830
 
 
1831
 
        private boolean isTransitionEnabledColored(Transition t) {
1832
 
                boolean enabled = true;
1833
 
                for(Arc arc : t.getPreset()){
1834
 
                        boolean arcEnabled = false;
1835
 
                        ColoredTimedPlace inputPlace = (ColoredTimedPlace)arc.getSource();
1836
 
 
1837
 
                        for(ColoredToken token : inputPlace.getColoredTokens()){
1838
 
                                if(arc instanceof ColoredInputArc){
1839
 
                                        arcEnabled = arcEnabled || ((ColoredInputArc)arc).satisfiesGuard(token);
1840
 
                                }else if(arc instanceof ColoredTransportArc){
1841
 
                                        boolean guardSatisfied = ((ColoredTransportArc)arc).satisfiesGuard(token);
1842
 
                                        boolean targetInvariantSatisfied = ((ColoredTransportArc)arc).satisfiesTargetInvariant(token);
1843
 
 
1844
 
                                        arcEnabled = arcEnabled || (guardSatisfied && targetInvariantSatisfied);
1845
 
                                }else if(arc instanceof ColoredInhibitorArc){
1846
 
                                        arcEnabled = arcEnabled || ((ColoredInhibitorArc)arc).satisfiesGuard(token);
1847
 
                                }
1848
 
 
1849
 
                                if(arcEnabled){
1850
 
                                        break;
1851
 
                                }
1852
 
                        }
1853
 
 
1854
 
                        enabled = enabled && arcEnabled;
1855
 
                }
1856
 
 
1857
 
                for(Arc arc : t.getPostset()){
1858
 
                        if(arc instanceof ColoredOutputArc){
1859
 
                                int value = ((ColoredOutputArc)arc).getOutputValue().getValue();
1860
 
                                ColorSet colorInvariant = ((ColoredTimedPlace)arc.getTarget()).getColorInvariant();
1861
 
                                enabled = enabled && colorInvariant.contains(value);
1862
 
 
1863
 
                        }
1864
 
                }
1865
 
 
1866
 
                return enabled;
1867
 
        }
1868
 
 
1869
 
 
1870
 
        private boolean isTransitionEnabledNonColored(Transition t) {
1871
 
                boolean isEnabled = true;
1872
 
 
1873
 
                Collection<Arc> presetArcs = t.getPreset();
1874
 
                for ( Arc a : presetArcs ){
1875
 
 
1876
 
                        Place p = (Place)a.getSource();
1877
 
                        if (p instanceof TimedPlace){
1878
 
 
1879
 
                                boolean ageIsSatisfied;
1880
 
 
1881
 
                                if(a instanceof TAPNInhibitorArc)
1882
 
                                        ageIsSatisfied = true;
1883
 
                                else
1884
 
                                        ageIsSatisfied = false;
1885
 
 
1886
 
 
1887
 
                                if (p.currentMarking > 0){
1888
 
 
1889
 
                                        for ( BigDecimal token : ((TimedPlace)p).getTokens() ){
1890
 
                                                if(a instanceof TAPNInhibitorArc)
1891
 
                                                {
1892
 
                                                        if(!((TimedArc)a).satisfiesGuard(token))
1893
 
                                                        {
1894
 
                                                                ageIsSatisfied = false;
1895
 
                                                                break;
1896
 
                                                        }
1897
 
 
1898
 
 
1899
 
                                                }
1900
 
                                                else
1901
 
                                                {
1902
 
                                                        if ( ((TimedArc)a).satisfiesGuard(token) ){
1903
 
 
1904
 
                                                                //make sure no invariants are violated
1905
 
                                                                if (a instanceof TransportArc){
1906
 
                                                                        for ( Arc postsetArc : (LinkedList<Arc>)t.getPostset() ){
1907
 
                                                                                if (postsetArc instanceof TransportArc){
1908
 
                                                                                        if ( ((TransportArc) postsetArc).getGroupNr() == ((TransportArc)a).getGroupNr()){
1909
 
                                                                                                if ( ((TimedPlace)postsetArc.getTarget()).satisfiesInvariant(token) ){
1910
 
                                                                                                        ageIsSatisfied = true;
1911
 
                                                                                                        break;
1912
 
                                                                                                }
1913
 
                                                                                        }
1914
 
                                                                                }
1915
 
                                                                        }
1916
 
                                                                        //invariants are not violated, if it is not a transport arc
1917
 
                                                                }else {
1918
 
                                                                        ageIsSatisfied = true;
1919
 
                                                                        break;
1920
 
                                                                }
1921
 
                                                        }
1922
 
                                                }
1923
 
                                        }
1924
 
                                }
1925
 
 
1926
 
                                isEnabled = ageIsSatisfied;
1927
 
 
1928
 
                                if (!isEnabled){
1929
 
                                        break;
1930
 
                                }
1931
 
 
1932
 
                        }else {
1933
 
                                //p should always be a TimedPlace unless we have introduced hybrid nets
1934
 
                        }
1935
 
                }
1936
 
                return isEnabled;
1937
 
        }
1938
 
 
1939
 
 
1940
 
 
1941
 
        /**
1942
 
         * Empty all attributes, turn into empty Petri-Net
1943
 
         */
1944
 
        private void emptyPNML() {
1945
 
                pnmlName = null;
1946
 
                placesArray = null;
1947
 
                transitionsArray = null;
1948
 
                arcsArray = null;
1949
 
                labelsArray = null;
1950
 
                changeArrayList = null;
1951
 
                arcsMap = null;
1952
 
                initializeMatrices();
 
756
                return all;
1953
757
        }
1954
758
 
1955
759
        /**
1956
760
         * Get a List of all the Place objects in the Petri-Net
 
761
         * 
1957
762
         * @return A List of all the Place objects
1958
763
         */
1959
764
        public Place[] getPlaces() {
1960
765
                Place[] returnArray = new Place[placesArray.size()];
1961
766
 
1962
 
                for (int i = 0; i < placesArray.size(); i++){
1963
 
                        returnArray[i] = (Place)placesArray.get(i);
 
767
                for (int i = 0; i < placesArray.size(); i++) {
 
768
                        returnArray[i] = placesArray.get(i);
1964
769
                }
1965
770
                return returnArray;
1966
771
        }
1967
772
 
1968
 
 
1969
 
        public int getPlacesCount(){
1970
 
                if (placesArray == null) {
1971
 
                        return 0;
1972
 
                } else {
1973
 
                        return placesArray.size();
1974
 
                }         
1975
 
        }   
1976
 
 
1977
 
 
1978
 
        /* wjk added 03/10/2007 */
1979
 
        /**
1980
 
         * Get the current marking of the Petri net
1981
 
         *
1982
 
         * @return The current marking of the Petri net
1983
 
         */
1984
 
        public int[] getMarking() {
1985
 
                int [] result = new int[placesArray.size()];
1986
 
 
1987
 
                for (int i=0; i < placesArray.size(); i++) {
1988
 
                        result[i] = ((Place) placesArray.get(i)).getCurrentMarking();
1989
 
                }
1990
 
                return result;
1991
 
        }   
1992
 
 
1993
 
 
1994
773
        /**
1995
774
         * Get a List of all the net-level NameLabel objects in the Petri-Net
1996
 
         * @return A List of all the net-level (as opposed to element-specific) label objects
 
775
         * 
 
776
         * @return A List of all the net-level (as opposed to element-specific)
 
777
         *         label objects
1997
778
         */
1998
779
        public AnnotationNote[] getLabels() {
1999
780
                AnnotationNote[] returnArray = new AnnotationNote[labelsArray.size()];
2000
781
 
2001
 
                for (int i = 0; i < labelsArray.size(); i++){
2002
 
                        returnArray[i] = (AnnotationNote)labelsArray.get(i);
 
782
                for (int i = 0; i < labelsArray.size(); i++) {
 
783
                        returnArray[i] = labelsArray.get(i);
2003
784
                }
2004
785
                return returnArray;
2005
786
        }
2006
787
 
2007
788
        /**
2008
789
         * Get an List of all the Transition objects in the Petri-Net
 
790
         * 
2009
791
         * @return An List of all the Transition objects
2010
792
         */
2011
793
        public Transition[] getTransitions() {
2012
794
                Transition[] returnArray = new Transition[transitionsArray.size()];
2013
795
 
2014
 
                for (int i = 0; i < transitionsArray.size(); i++){
2015
 
                        returnArray[i] = (Transition)transitionsArray.get(i);
 
796
                for (int i = 0; i < transitionsArray.size(); i++) {
 
797
                        returnArray[i] = transitionsArray.get(i);
2016
798
                }
2017
799
                return returnArray;
2018
800
        }
2019
801
 
2020
 
 
2021
 
        public int getTransitionsCount() {
2022
 
                if (transitionsArray == null) {
2023
 
                        return 0;
2024
 
                } else {
2025
 
                        return transitionsArray.size();
2026
 
                }         
2027
 
        }   
2028
 
 
2029
802
        /**
2030
803
         * Get an List of all the Arcs objects in the Petri-Net
 
804
         * 
2031
805
         * @return An List of all the Arc objects
2032
806
         */
2033
807
        public Arc[] getArcs() {
2034
808
                Arc[] returnArray = new Arc[arcsArray.size()];
2035
809
 
2036
 
                for (int i = 0; i < arcsArray.size(); i++){
2037
 
                        returnArray[i] = (Arc)arcsArray.get(i);
 
810
                for (int i = 0; i < arcsArray.size(); i++) {
 
811
                        returnArray[i] = arcsArray.get(i);
2038
812
                }
2039
813
                return returnArray;
2040
814
        }
2041
815
 
2042
 
 
2043
816
        /**
2044
817
         * Get an List of all the InhibitorArc objects in the Petri-Net
 
818
         * 
2045
819
         * @return An List of all the InhibitorArc objects
2046
820
         */
2047
821
        public InhibitorArc[] getInhibitors() {
2048
822
                InhibitorArc[] returnArray = new InhibitorArc[inhibitorsArray.size()];
2049
823
 
2050
 
                for (int i = 0; i < inhibitorsArray.size(); i++){
2051
 
                        returnArray[i] = (InhibitorArc)inhibitorsArray.get(i);
 
824
                for (int i = 0; i < inhibitorsArray.size(); i++) {
 
825
                        returnArray[i] = inhibitorsArray.get(i);
2052
826
                }
2053
827
                return returnArray;
2054
828
        }
2055
829
 
2056
 
        public TAPNInhibitorArc[] getTAPNInhibitors() {
2057
 
                int numInhibArcs = 0;
2058
 
                for (int i = 0; i < arcsArray.size(); i++){
2059
 
                        if(arcsArray.get(i) instanceof TAPNInhibitorArc)
2060
 
                                numInhibArcs++;
2061
 
                }
2062
 
 
2063
 
                TAPNInhibitorArc[] returnArray = new TAPNInhibitorArc[numInhibArcs];
2064
 
 
2065
 
                for(int i = 0; i < numInhibArcs; i++)
2066
 
                {
2067
 
                        for (int j = 0; j < arcsArray.size(); j++)
2068
 
                        {
2069
 
                                if(arcsArray.get(j) instanceof TAPNInhibitorArc)
2070
 
                                        returnArray[i] = (TAPNInhibitorArc)arcsArray.get(j);
2071
 
                        }
2072
 
                }
2073
 
 
2074
 
                return returnArray;
2075
 
        }   
2076
 
 
2077
 
 
2078
830
        /**
2079
831
         * Return the Transition called transitionName from the Petri-Net
2080
 
         * @param transitionID ID of Transition object to return
2081
 
         * @return The first Transition object found with a name equal to transitionName
 
832
         * 
 
833
         * @param transitionID
 
834
         *            ID of Transition object to return
 
835
         * @return The first Transition object found with a name equal to
 
836
         *         transitionName
2082
837
         */
2083
838
        public Transition getTransitionById(String transitionID) {
2084
839
                Transition returnTransition = null;
2085
840
 
2086
841
                if (transitionsArray != null) {
2087
842
                        if (transitionID != null) {
2088
 
                                for (int i = 0 ; i < transitionsArray.size(); i++) {
2089
 
                                        if (transitionID.equalsIgnoreCase(
2090
 
                                                        ((Transition)transitionsArray.get(i)).getId())) {
2091
 
                                                returnTransition = (Transition)transitionsArray.get(i);
 
843
                                for (int i = 0; i < transitionsArray.size(); i++) {
 
844
                                        if (transitionID.equalsIgnoreCase((transitionsArray.get(i))
 
845
                                                        .getId())) {
 
846
                                                returnTransition = transitionsArray.get(i);
2092
847
                                        }
2093
848
                                }
2094
849
                        }
2095
850
                }
2096
851
                return returnTransition;
2097
 
        }   
2098
 
 
 
852
        }
2099
853
 
2100
854
        /**
2101
855
         * Return the Transition called transitionName from the Petri-Net
2102
 
         * @param transitionName Name of Transition object to return
2103
 
         * @return The first Transition object found with a name equal to transitionName
 
856
         * 
 
857
         * @param transitionName
 
858
         *            Name of Transition object to return
 
859
         * @return The first Transition object found with a name equal to
 
860
         *         transitionName
2104
861
         */
2105
862
        public Transition getTransitionByName(String transitionName) {
2106
863
                Transition returnTransition = null;
2108
865
                if (transitionsArray != null) {
2109
866
                        if (transitionName != null) {
2110
867
                                for (int i = 0; i < transitionsArray.size(); i++) {
2111
 
                                        if (transitionName.equalsIgnoreCase(
2112
 
                                                        ((Transition)transitionsArray.get(i)).getName())) {
2113
 
                                                returnTransition = (Transition)transitionsArray.get(i);
 
868
                                        if (transitionName.equalsIgnoreCase((transitionsArray
 
869
                                                        .get(i)).getName())) {
 
870
                                                returnTransition = transitionsArray.get(i);
2114
871
                                        }
2115
872
                                }
2116
873
                        }
2118
875
                return returnTransition;
2119
876
        }
2120
877
 
2121
 
        public Transition getTransitionByNameIgnoreGiven(Transition ignore, String transitionName) {
 
878
        public Transition getTransitionByNameIgnoreGiven(Transition ignore,
 
879
                        String transitionName) {
2122
880
                Transition returnTransition = null;
2123
881
 
2124
882
                if (transitionsArray != null) {
2125
883
                        if (transitionName != null) {
2126
884
                                for (int i = 0; i < transitionsArray.size(); i++) {
2127
 
                                        if(!transitionsArray.get(i).equals(ignore)) {
2128
 
                                                if (transitionName.equalsIgnoreCase(
2129
 
                                                                ((Transition)transitionsArray.get(i)).getName())) {
2130
 
                                                        returnTransition = (Transition)transitionsArray.get(i);
 
885
                                        if (!transitionsArray.get(i).equals(ignore)) {
 
886
                                                if (transitionName.equalsIgnoreCase((transitionsArray
 
887
                                                                .get(i)).getName())) {
 
888
                                                        returnTransition = transitionsArray.get(i);
2131
889
                                                }
2132
890
                                        }
2133
891
                                }
2136
894
                return returnTransition;
2137
895
        }
2138
896
 
2139
 
 
2140
897
        /**
2141
 
         * Return the Transition called transitionName from the Petri-Net
2142
 
         * @param transitionNo No of Transition object to return
2143
 
         * @return The Transition object
2144
 
         */
2145
 
        public Transition getTransition(int transitionNo) {
2146
 
                Transition returnTransition = null;
2147
 
 
2148
 
                if (transitionsArray != null) {
2149
 
                        if (transitionNo < transitionsArray.size()) {
2150
 
                                returnTransition = (Transition)transitionsArray.get(transitionNo);
2151
 
                        }
2152
 
                }
2153
 
                return returnTransition;
2154
 
        }
2155
 
 
2156
 
 
2157
 
        /** Return the Place called placeName from the Petri-Net
2158
 
         * @param placeId ID of Place object to return
 
898
         * Return the Place called placeName from the Petri-Net
 
899
         * 
 
900
         * @param placeId
 
901
         *            ID of Place object to return
2159
902
         * @return The first Place object found with id equal to placeId
2160
903
         */
2161
904
        public Place getPlaceById(String placeID) {
2163
906
 
2164
907
                if (placesArray != null) {
2165
908
                        if (placeID != null) {
2166
 
                                for (int i = 0 ; i < placesArray.size(); i++) {
2167
 
                                        if (placeID.equalsIgnoreCase(
2168
 
                                                        ((Place)placesArray.get(i)).getId())) {
2169
 
                                                returnPlace = (Place)placesArray.get(i);
 
909
                                for (int i = 0; i < placesArray.size(); i++) {
 
910
                                        if (placeID.equalsIgnoreCase((placesArray.get(i)).getId())) {
 
911
                                                returnPlace = placesArray.get(i);
2170
912
                                        }
2171
913
                                }
2172
914
                        }
2173
915
                }
2174
916
                return returnPlace;
2175
 
        }   
2176
 
 
 
917
        }
2177
918
 
2178
919
        /**
2179
920
         * Return the Place called placeName from the Petri-Net
2180
 
         * @param placeName Name of Place object to return
 
921
         * 
 
922
         * @param placeName
 
923
         *            Name of Place object to return
2181
924
         * @return The first Place object found with a name equal to placeName
2182
925
         */
2183
926
        public Place getPlaceByName(String placeName) {
2186
929
                if (placesArray != null) {
2187
930
                        if (placeName != null) {
2188
931
                                for (int i = 0; i < placesArray.size(); i++) {
2189
 
                                        if (placeName.equalsIgnoreCase(
2190
 
                                                        ((Place)placesArray.get(i)).getName())) {
2191
 
                                                returnPlace = (Place)placesArray.get(i);
 
932
                                        if (placeName.equalsIgnoreCase((placesArray.get(i))
 
933
                                                        .getName())) {
 
934
                                                returnPlace = placesArray.get(i);
2192
935
                                        }
2193
936
                                }
2194
937
                        }
2196
939
                return returnPlace;
2197
940
        }
2198
941
 
2199
 
        public Place getPlaceByNameIgnoreGiven(Place ignore, String placeName)
2200
 
        {
 
942
        public Place getPlaceByNameIgnoreGiven(Place ignore, String placeName) {
2201
943
                Place returnPlace = null;
2202
944
 
2203
945
                if (placesArray != null) {
2204
946
                        if (placeName != null) {
2205
947
                                for (int i = 0; i < placesArray.size(); i++) {
2206
 
                                        if(!placesArray.get(i).equals(ignore)) {
2207
 
                                                if (placeName.equalsIgnoreCase(
2208
 
                                                                ((Place)placesArray.get(i)).getName())) {
2209
 
                                                        returnPlace = (Place)placesArray.get(i);
 
948
                                        if (!placesArray.get(i).equals(ignore)) {
 
949
                                                if (placeName.equalsIgnoreCase((placesArray.get(i))
 
950
                                                                .getName())) {
 
951
                                                        returnPlace = placesArray.get(i);
2210
952
                                                }
2211
953
                                        }
2212
954
                                }
2215
957
                return returnPlace;
2216
958
        }
2217
959
 
2218
 
 
2219
 
        /**
2220
 
         * Return the Place called placeName from the Petri-Net
2221
 
         * @param placeNo No of Place object to return
2222
 
         * @return The Place object
2223
 
         */
2224
 
        public Place getPlace(int placeNo) {
2225
 
                Place returnPlace = null;
2226
 
 
2227
 
                if (placesArray != null) {
2228
 
                        if (placeNo < placesArray.size()) {
2229
 
                                returnPlace = (Place)placesArray.get(placeNo);
2230
 
                        }
2231
 
                }
2232
 
                return returnPlace;
2233
 
        }
2234
 
 
2235
 
 
2236
960
        /**
2237
961
         * Return the PlaceTransitionObject called ptoName from the Petri-Net
2238
 
         * @param ptoId Id of PlaceTransitionObject object to return
2239
 
         * @return The first Arc PlaceTransitionObject found with a name equal to ptoName
 
962
         * 
 
963
         * @param ptoId
 
964
         *            Id of PlaceTransitionObject object to return
 
965
         * @return The first Arc PlaceTransitionObject found with a name equal to
 
966
         *         ptoName
2240
967
         */
2241
968
        public PlaceTransitionObject getPlaceTransitionObject(String ptoId) {
2242
969
                if (ptoId != null) {
2243
 
                        if (getPlaceById(ptoId) != null){
 
970
                        if (getPlaceById(ptoId) != null) {
2244
971
                                return getPlaceById(ptoId);
2245
972
                        } else if (getTransitionById(ptoId) != null) {
2246
973
                                return getTransitionById(ptoId);
2250
977
                return null;
2251
978
        }
2252
979
 
2253
 
 
2254
 
 
2255
 
        /**
2256
 
         * Create model from TAPN model
2257
 
         * @author Kenneth Yrke Jørgensen <kyrke@cs.aau.dk>
2258
 
         * @param TAPN model
2259
 
         */
2260
 
        public void createFromTAPN(TAPN model) {
2261
 
 
2262
 
                //              double xcord = 10;
2263
 
                //              double ycord = 10;
2264
 
                //
2265
 
                //              List<dk.aau.cs.petrinet.TAPNPlace> places = model.getPlaces(); 
2266
 
                //              List<dk.aau.cs.petrinet.TAPNTransition> transitions = model.getTransitions();
2267
 
                //              List<dk.aau.cs.petrinet.Arc> arcs = model.getArcs();
2268
 
                //
2269
 
                //              // Add places
2270
 
                //              for (dk.aau.cs.petrinet.TAPNPlace p : places){
2271
 
                //                      Place tmp = new Place(xcord, ycord, ""+p.getID(), p.getName(), 10.0, 10.0, 0, 0, 0, 0);
2272
 
                //                      TimedPlace tp = new TimedPlace(tmp, p.getInvariant());
2273
 
                //                      
2274
 
                //                      addPlace(tp);
2275
 
                //
2276
 
                //                      xcord = xcord + 10;
2277
 
                //                      ycord = ycord + 10;
2278
 
                //              }
2279
 
                //
2280
 
                //              // Add transitions
2281
 
                //              for (dk.aau.cs.petrinet.TAPNTransition t : transitions){
2282
 
                //                      Transition trans = new Transition(xcord, ycord, ""+t.getID(), t.getName(), 10.0, 10.0, 10.0, false, false, 1, 1);
2283
 
                //                      addTransition(new TAPNTransition(trans));
2284
 
                //                      xcord = xcord + 10;
2285
 
                //                      ycord = ycord + 10;
2286
 
                //              }
2287
 
                //
2288
 
                //              // Add arcs
2289
 
                //              for (dk.aau.cs.petrinet.Arc a : arcs){
2290
 
                //                      
2291
 
                //              }
2292
 
                //
2293
 
                //              if (CreateGui.getApp()!=null) {
2294
 
                //                      CreateGui.getApp().restoreMode();
2295
 
                //              }
2296
 
 
2297
 
        }
2298
 
 
2299
 
 
2300
 
        /**
2301
 
         * Create model from transformed PNML file
2302
 
         * @author Ben Kirby, 10 Feb 2007
2303
 
         * @param filename URI location of PNML
2304
 
         * 
2305
 
         * @author Edwin Chung 
2306
 
         * This code is modified so that dataLayer objects can be created
2307
 
         * outside the GUI
2308
 
         */
2309
 
        public void createFromPNML(Document PNMLDoc){
2310
 
                createFromPNML(PNMLDoc, NetType.TAPN);          
2311
 
        }
2312
 
 
2313
 
        public void createFromPNML(Document PNMLDoc, NetType type)      {
2314
 
                // XXX - kyrke debug
2315
 
                emptyPNML();
2316
 
                this.setNetType(type);
2317
 
                Node node = null;
2318
 
                NodeList nodeList = null;
2319
 
 
2320
 
                try {
2321
 
                        if (CreateGui.getApp()!=null) {
2322
 
                                // Notifies used to indicate new instances.
2323
 
                                CreateGui.getApp().setMode(Pipe.CREATING); 
2324
 
                        }
2325
 
 
2326
 
                        if(type.equals(NetType.COLORED)){
2327
 
                                NodeList places = PNMLDoc.getElementsByTagName("net").item(0).getChildNodes();
2328
 
                                for(int i = 0; i < places.getLength(); i++){
2329
 
                                        this.parseColoredElement(places.item(i));
2330
 
                                }
2331
 
                        }else{
2332
 
                                nodeList = PNMLDoc.getDocumentElement().getChildNodes();
2333
 
                                for(int i = 0 ; i < nodeList.getLength() ; i++) {
2334
 
                                        node = nodeList.item(i);
2335
 
                                                parseElement(node);
2336
 
                                }
2337
 
                        }
2338
 
 
2339
 
                        if (CreateGui.getApp()!=null) {
2340
 
                                CreateGui.getApp().restoreMode();
2341
 
                        }
2342
 
                        buildConstraints();
2343
 
                        
2344
 
 
2345
 
                } catch (Exception e) {
2346
 
                        System.out.println("runtime except");
2347
 
                        throw new RuntimeException(e);
2348
 
                }
2349
 
        }
2350
 
 
2351
 
        private void parseColoredElement(Node node) {
2352
 
                Element element;
2353
 
                if(node instanceof Element) {
2354
 
                        element = (Element)node;
2355
 
                        if ("labels".equals(element.getNodeName())){
2356
 
                                addAnnotation(createAnnotation(element));
2357
 
                        } else if("place".equals(element.getNodeName())){
2358
 
                                addPlace(createColoredPlace(element));
2359
 
                        } else if ("transition".equals(element.getNodeName())){
2360
 
                                addTransition(createTransition(element));
2361
 
                        } else if ("arc".equals(element.getNodeName())) {
2362
 
                                Arc newArc = createColoredArc(element);
2363
 
                                if (newArc instanceof InhibitorArc) {
2364
 
                                        addArc((InhibitorArc) newArc);
2365
 
                                } else if (newArc instanceof TAPNInhibitorArc) {
2366
 
                                        addArc((TAPNInhibitorArc) newArc);
2367
 
                                } else {
2368
 
                                        addArc((NormalArc) newArc);
2369
 
                                        checkForInverseArc((NormalArc) newArc);
2370
 
                                }                  
2371
 
                        } else if( "queries".equals(element.getNodeName()) ){
2372
 
                                TAPNQuery query = createQuery(element);
2373
 
                                if(query != null)
2374
 
                                        queries.add(query);
2375
 
                        } else if ("constant".equals(element.getNodeName())){
2376
 
                                String name = element.getAttribute("name");
2377
 
                                int value = Integer.parseInt(element.getAttribute("value"));
2378
 
                                if(!name.isEmpty() && value >= 0)
2379
 
                                        addConstant(name, value);
2380
 
                        } else {
2381
 
                                System.out.println("!" + element.getNodeName());
2382
 
                        }
2383
 
                }
2384
 
 
2385
 
        }
2386
 
 
2387
 
 
2388
 
        private Arc createColoredArc(Element inputArcElement) {
2389
 
                String idInput = null;
2390
 
                String sourceInput = null;
2391
 
                String targetInput = null;
2392
 
                double startX = 0;
2393
 
                double startY = 0;
2394
 
                boolean taggedArc;
2395
 
 
2396
 
                sourceInput = inputArcElement.getAttribute("source");
2397
 
                targetInput = inputArcElement.getAttribute("target");
2398
 
                idInput = inputArcElement.getAttribute("id");
2399
 
 
2400
 
                String taggedTempStorage = inputArcElement.getAttribute("tagged");
2401
 
                //              String inscriptionOffsetXTempStorage = inputArcElement.getAttribute("inscriptionOffsetX");
2402
 
                //              String inscriptionOffsetYTempStorage = inputArcElement.getAttribute("inscriptionOffsetY");
2403
 
 
2404
 
                taggedArc = !(taggedTempStorage.length() == 0 ||
2405
 
                                taggedTempStorage.length() == 5);
2406
 
 
2407
 
                if (sourceInput.length() > 0) {
2408
 
                        if (getPlaceTransitionObject(sourceInput) != null) {
2409
 
                                //                              System.out.println("PNMLDATA: sourceInput is not null");
2410
 
                                startX = getPlaceTransitionObject(sourceInput).getPositionX();
2411
 
                                startX += getPlaceTransitionObject(sourceInput).centreOffsetLeft();
2412
 
                                startY = getPlaceTransitionObject(sourceInput).getPositionY();
2413
 
                                startY += getPlaceTransitionObject(sourceInput).centreOffsetTop();
2414
 
                        }
2415
 
                }
2416
 
                if (targetInput.length() > 0) {
2417
 
                        if (getPlaceTransitionObject(targetInput) != null) {
2418
 
                        }
2419
 
                }
2420
 
 
2421
 
                PlaceTransitionObject sourceIn = getPlaceTransitionObject(sourceInput);
2422
 
                PlaceTransitionObject targetIn = getPlaceTransitionObject(targetInput);
2423
 
 
2424
 
                // add the insets and offset
2425
 
                int aStartx = sourceIn.getX() + sourceIn.centreOffsetLeft();
2426
 
                int aStarty = sourceIn.getY() + sourceIn.centreOffsetTop();
2427
 
 
2428
 
                int aEndx = targetIn.getX() + targetIn.centreOffsetLeft();
2429
 
                int aEndy = targetIn.getY() + targetIn.centreOffsetTop();
2430
 
 
2431
 
 
2432
 
                double _startx = aStartx;
2433
 
                double _starty = aStarty;
2434
 
                double _endx = aEndx;
2435
 
                double _endy = aEndy;
2436
 
 
2437
 
                String type = inputArcElement.getAttribute("type");
2438
 
 
2439
 
                Arc arc = null;
2440
 
 
2441
 
                if(type.equals("ColoredInputArc")){
2442
 
                        ColoredInputArc cia = new ColoredInputArc(new NormalArc(_startx, _starty, _endx, _endy, sourceIn, targetIn,1,idInput, taggedArc));
2443
 
                        ColoredInterval timeGuard = new ColoredInterval(inputArcElement.getAttribute("timeGuard"));
2444
 
                        cia.setTimeGuard(timeGuard);
2445
 
 
2446
 
                        ColorSet colorGuard = new ColorSet(inputArcElement.getAttribute("colorGuard"));
2447
 
                        cia.setColorGuard(colorGuard);
2448
 
 
2449
 
                        arc = cia;
2450
 
                }else if(type.equals("ColoredOutputArc")){
2451
 
                        ColoredOutputArc coa = new ColoredOutputArc(_startx, _starty, _endx, _endy, sourceIn, targetIn,1,idInput, taggedArc);
2452
 
                        coa.setOutputValue(new IntOrConstant(inputArcElement.getAttribute("outputValue")));
2453
 
 
2454
 
                        arc = coa;
2455
 
                }else if(type.equals("ColoredInhibitorArc")){
2456
 
                        ColoredInhibitorArc cia = new ColoredInhibitorArc(new NormalArc(_startx, _starty, _endx, _endy, sourceIn, targetIn,1,idInput, taggedArc));
2457
 
                        ColoredInterval timeGuard = new ColoredInterval(inputArcElement.getAttribute("timeGuard"));
2458
 
                        cia.setTimeGuard(timeGuard);
2459
 
 
2460
 
                        ColorSet colorGuard = new ColorSet(inputArcElement.getAttribute("colorGuard"));
2461
 
                        cia.setColorGuard(colorGuard);
2462
 
 
2463
 
                        arc = cia;
2464
 
 
2465
 
                }else if(type.equals("ColoredTransportArc")){
2466
 
                        ColoredTransportArc cta = new ColoredTransportArc(
2467
 
                                        new TimedArc( new NormalArc(_startx, _starty,
2468
 
                                                        _endx, _endy,
2469
 
                                                        sourceIn,
2470
 
                                                        targetIn,
2471
 
                                                        1,
2472
 
                                                        idInput,
2473
 
                                                        taggedArc),
2474
 
                                        ""),
2475
 
                                        Integer.parseInt(inputArcElement.getAttribute("groupNo")), 
2476
 
                                        sourceIn instanceof Place);
2477
 
 
2478
 
                        ColoredInterval timeGuard = new ColoredInterval(inputArcElement.getAttribute("timeGuard"));
2479
 
                        cta.setTimeGuard(timeGuard);
2480
 
 
2481
 
                        ColorSet colorGuard = new ColorSet(inputArcElement.getAttribute("colorGuard"));
2482
 
                        cta.setColorGuard(colorGuard);
2483
 
 
2484
 
                        Preserve preservation = Preserve.valueOf(inputArcElement.getAttribute("preservation"));
2485
 
                        cta.setPreservation(preservation);
2486
 
 
2487
 
                        IntOrConstant outputValue = new IntOrConstant(inputArcElement.getAttribute("outputValue"));
2488
 
                        cta.setOutputValue(outputValue);
2489
 
 
2490
 
                        arc = cta;
2491
 
                }
2492
 
 
2493
 
                getPlaceTransitionObject(sourceInput).addConnectFrom(arc);
2494
 
                getPlaceTransitionObject(targetInput).addConnectTo(arc);
2495
 
 
2496
 
                //              **********************************************************************************
2497
 
                //              The following section attempts to load and display arcpath details****************
2498
 
 
2499
 
                //NodeList nodelist = inputArcElement.getChildNodes();
2500
 
                NodeList nodelist = inputArcElement.getElementsByTagName("arcpath");
2501
 
                if (nodelist.getLength()>0) {
2502
 
                        arc.getArcPath().purgePathPoints();
2503
 
                        for (int i = 0; i < nodelist.getLength(); i++) {         
2504
 
                                Node node = nodelist.item(i);
2505
 
                                if(node instanceof Element) {
2506
 
                                        Element element = (Element)node;
2507
 
                                        if ("arcpath".equals(element.getNodeName())){
2508
 
                                                String arcTempX = element.getAttribute("xCoord");
2509
 
                                                String arcTempY = element.getAttribute("yCoord");
2510
 
                                                String arcTempType = element.getAttribute("arcPointType");
2511
 
                                                float arcPointX = Float.valueOf(arcTempX).floatValue();
2512
 
                                                float arcPointY = Float.valueOf(arcTempY).floatValue();
2513
 
                                                arcPointX += Pipe.ARC_CONTROL_POINT_CONSTANT + 1;
2514
 
                                                arcPointY += Pipe.ARC_CONTROL_POINT_CONSTANT + 1;
2515
 
                                                boolean arcPointType = 
2516
 
                                                        Boolean.valueOf(arcTempType).booleanValue();
2517
 
                                                arc.getArcPath().addPoint(arcPointX,arcPointY,arcPointType);
2518
 
                                        }
2519
 
                                }
2520
 
                        }
2521
 
                }
2522
 
 
2523
 
                //              Arc path creation ends here***************************************************************
2524
 
                //              ******************************************************************************************
2525
 
                return arc;
2526
 
        }
2527
 
 
2528
 
 
2529
 
        private Place createColoredPlace(Element element) {
2530
 
                double positionXInput = 0;
2531
 
                double positionYInput = 0;
2532
 
                String idInput = element.getAttribute("id");
2533
 
                String nameInput = element.getAttribute("name");
2534
 
                double nameOffsetYInput = 0;
2535
 
                double nameOffsetXInput = 0;
2536
 
                double markingOffsetXInput = 0;
2537
 
                double markingOffsetYInput = 0;
2538
 
                int capacityInput = 0;
2539
 
 
2540
 
                String positionXTempStorage = element.getAttribute("positionX");
2541
 
                String positionYTempStorage = element.getAttribute("positionY");
2542
 
                String nameOffsetXTempStorage = element.getAttribute("nameOffsetX");
2543
 
                String nameOffsetYTempStorage = element.getAttribute("nameOffsetY");
2544
 
                String initialMarkingTempStorage = element.getAttribute("initialMarking");
2545
 
                String markingOffsetXTempStorage = element.getAttribute("markingOffsetX");
2546
 
                String markingOffsetYTempStorage = element.getAttribute("markingOffsetY");
2547
 
                String capacityTempStorage = element.getAttribute("capacity");
2548
 
 
2549
 
                if (positionXTempStorage.length() > 0) {
2550
 
                        positionXInput = Double.valueOf(positionXTempStorage).doubleValue() + 1;
2551
 
                }
2552
 
                if (positionYTempStorage.length() > 0) {
2553
 
                        positionYInput = Double.valueOf(positionYTempStorage).doubleValue() + 1;
2554
 
                }
2555
 
 
2556
 
                positionXInput = Grid.getModifiedX(positionXInput);
2557
 
                positionYInput = Grid.getModifiedY(positionYInput);
2558
 
 
2559
 
                if (nameOffsetYTempStorage.length() > 0) {
2560
 
                        nameOffsetXInput = Double.valueOf(nameOffsetXTempStorage).doubleValue();
2561
 
                }
2562
 
                if (nameOffsetXTempStorage.length() > 0) {
2563
 
                        nameOffsetYInput = Double.valueOf(nameOffsetYTempStorage).doubleValue();
2564
 
                }
2565
 
 
2566
 
                if (initialMarkingTempStorage.length() > 0) {
2567
 
                }
2568
 
                if (markingOffsetXTempStorage.length() > 0) {
2569
 
                        markingOffsetXInput = Double.valueOf(markingOffsetXTempStorage).doubleValue();
2570
 
                }
2571
 
                if (markingOffsetYTempStorage.length() > 0) {
2572
 
                        markingOffsetYInput = Double.valueOf(markingOffsetYTempStorage).doubleValue();
2573
 
                }
2574
 
 
2575
 
                if (capacityTempStorage.length() > 0) {
2576
 
                        capacityInput = Integer.valueOf(capacityTempStorage).intValue();
2577
 
                }
2578
 
 
2579
 
                ColoredTimeInvariant timeInvariant = new ColoredTimeInvariant(element.getAttribute("timeInvariant"));
2580
 
                ColorSet colorInvariant = new ColorSet(element.getAttribute("colorInvariant"));
2581
 
 
2582
 
                ColoredTimedPlace place = new ColoredTimedPlace(positionXInput,positionYInput, idInput, nameInput,nameOffsetXInput, nameOffsetYInput, 0, markingOffsetXInput, markingOffsetYInput, capacityInput, "");
2583
 
                place.setTimeInvariant(timeInvariant);
2584
 
                place.setColorInvariant(colorInvariant);
2585
 
 
2586
 
                NodeList tokenNodes = element.getElementsByTagName("colored-token");
2587
 
                List<ColoredToken> tokens = parseTokens(tokenNodes);
2588
 
                place.setColoredTokens(tokens);
2589
 
 
2590
 
 
2591
 
                return place;
2592
 
        }
2593
 
 
2594
 
 
2595
 
        private List<ColoredToken> parseTokens(NodeList tokenNodes) {
2596
 
                ArrayList<ColoredToken> list = new ArrayList<ColoredToken>();
2597
 
 
2598
 
                for(int i = 0; i < tokenNodes.getLength(); i++){
2599
 
                        Element element = (Element)tokenNodes.item(i);
2600
 
 
2601
 
                        IntOrConstant value = new IntOrConstant(element.getAttribute("value"));
2602
 
                        list.add(new ColoredToken(value));
2603
 
                }
2604
 
 
2605
 
                return list;
2606
 
        }
2607
 
 
2608
 
        private void parseElement(Node node) {
2609
 
                Element element;
2610
 
                if(node instanceof Element) {
2611
 
                        element = (Element)node;
2612
 
                        if ("labels".equals(element.getNodeName())){
2613
 
                                addAnnotation(createAnnotation(element));
2614
 
                        }  else if("place".equals(element.getNodeName())){
2615
 
                                addPlace(createPlace(element));
2616
 
                        } else if ("transition".equals(element.getNodeName())){
2617
 
                                addTransition(createTransition(element));
2618
 
                        } else if ("arc".equals(element.getNodeName())) {
2619
 
                                Arc newArc = createArc(element);
2620
 
                                if (newArc instanceof InhibitorArc) {
2621
 
                                        addArc((InhibitorArc) newArc);
2622
 
                                } else if (newArc instanceof TAPNInhibitorArc) {
2623
 
                                        addArc((TAPNInhibitorArc) newArc);
2624
 
                                } else {
2625
 
                                        addArc((NormalArc) newArc);
2626
 
                                        checkForInverseArc((NormalArc) newArc);
2627
 
                                }                  
2628
 
                        } else if( "queries".equals(element.getNodeName()) ){
2629
 
                                TAPNQuery query = createQuery(element);
2630
 
                                if(query != null)
2631
 
                                        queries.add(query);
2632
 
                        } else if ("constant".equals(element.getNodeName())){
2633
 
                                String name = element.getAttribute("name");
2634
 
                                int value = Integer.parseInt(element.getAttribute("value"));
2635
 
                                if(!name.isEmpty() && value >= 0)
2636
 
                                        addConstant(name, value);
2637
 
                        } else {
2638
 
                                System.out.println("!" + element.getNodeName());
2639
 
                        }
2640
 
                }
2641
 
        }   
2642
 
 
2643
 
 
2644
 
        /**  
2645
 
         * This method creates a new datalayer based on a datalayer,
2646
 
         * this can be used for drawing a datalayer after having modyfied it
2647
 
         * by some external code.
2648
 
         * 
2649
 
         *  This method is used for drawing a datalayer after it has been
2650
 
         *  transformed by dk.aau.cs.TAPN.transformer
2651
 
         *  
2652
 
         * @author Kenneth Yrke Joergensen (kyrke@cs.aau.dk)  **/ 
2653
 
 
2654
 
        public void createFromDataLayer(DataLayer m){
2655
 
                if (CreateGui.getApp()!=null) {
2656
 
                        // Notifies used to indicate new instances.
2657
 
                        CreateGui.getApp().setMode(Pipe.CREATING); 
2658
 
                }
2659
 
 
2660
 
                for (Transition t : m.getTransitions()){
2661
 
                        addTransition(new Transition(t));                  
2662
 
                }
2663
 
 
2664
 
                for (Place p : m.getPlaces()){
2665
 
                        addPlace(p.clone());
2666
 
                }
2667
 
 
2668
 
 
2669
 
                for (Arc a:m.getArcs()){
2670
 
                        NormalArc tmp = (NormalArc)a.clone();
2671
 
 
2672
 
                        PlaceTransitionObject source = getPlaceTransitionObject(tmp.getSource().getName());
2673
 
                        PlaceTransitionObject target =  getPlaceTransitionObject(tmp.getTarget().getName());
2674
 
 
2675
 
                        target.addConnectTo(tmp);
2676
 
                        source.addConnectFrom(tmp);
2677
 
 
2678
 
 
2679
 
                        /* */
2680
 
 
2681
 
                        /*            
2682
 
                         if (getPlaceTransitionObject(sourceInput) != null) {
2683
 
//                      System.out.println("PNMLDATA: sourceInput is not null");
2684
 
                            startX = getPlaceTransitionObject(sourceInput).getPositionX();
2685
 
                            startX += getPlaceTransitionObject(sourceInput).centreOffsetLeft();
2686
 
                            startY = getPlaceTransitionObject(sourceInput).getPositionY();
2687
 
                            startY += getPlaceTransitionObject(sourceInput).centreOffsetTop();
2688
 
                         }
2689
 
 
2690
 
                         if (getPlaceTransitionObject(targetInput) != null) {
2691
 
//                      System.out.println("PNMLDATA: targetInput is not null");
2692
 
                            endX = getPlaceTransitionObject(targetInput).getPositionX();
2693
 
                            endY = getPlaceTransitionObject(targetInput).getPositionY();
2694
 
                         }
2695
 
 
2696
 
 
2697
 
                      PlaceTransitionObject sourceIn = getPlaceTransitionObject(sourceInput);
2698
 
                      PlaceTransitionObject targetIn = getPlaceTransitionObject(targetInput);
2699
 
 
2700
 
                      // add the insets and offset
2701
 
                      int aStartx = sourceIn.getX() + sourceIn.centreOffsetLeft();
2702
 
                      int aStarty = sourceIn.getY() + sourceIn.centreOffsetTop();
2703
 
 
2704
 
                      int aEndx = targetIn.getX() + targetIn.centreOffsetLeft();
2705
 
                      int aEndy = targetIn.getY() + targetIn.centreOffsetTop();
2706
 
 
2707
 
 
2708
 
                      double _startx = aStartx;
2709
 
                      double _starty = aStarty;
2710
 
                      double _endx = aEndx;
2711
 
                      double _endy = aEndy; */
2712
 
                        /* */
2713
 
 
2714
 
                        addArc(tmp);
2715
 
                }
2716
 
 
2717
 
 
2718
 
                if (CreateGui.getApp()!=null) {
2719
 
                        CreateGui.getApp().restoreMode();
2720
 
                } 
2721
 
        }
2722
 
 
2723
 
 
2724
 
 
2725
 
        private TAPNQuery createQuery(Element queryElement) {
2726
 
 
2727
 
                String comment;
2728
 
                try{
2729
 
                        comment = queryElement.getAttribute("name");
2730
 
                }catch (Exception e) {
2731
 
                        comment = "No comment specified";
2732
 
                }
2733
 
 
2734
 
                TraceOption traceOption;
2735
 
                try{
2736
 
                        traceOption = TraceOption.valueOf(queryElement.getAttribute("traceOption"));
2737
 
                }catch (Exception e) {
2738
 
                        traceOption = TraceOption.NONE;
2739
 
                }
2740
 
 
2741
 
                SearchOption searchOption;
2742
 
                try{
2743
 
                        searchOption = SearchOption.valueOf(queryElement.getAttribute("searchOption"));
2744
 
                }catch (Exception e) {
2745
 
                        searchOption = SearchOption.BFS;
2746
 
                }
2747
 
 
2748
 
                HashTableSize hashTableSize;
2749
 
                try{
2750
 
                        hashTableSize = HashTableSize.valueOf(queryElement.getAttribute("hashTableSize"));              
2751
 
                }catch (Exception e) {
2752
 
                        hashTableSize = HashTableSize.MB_16;
2753
 
                }
2754
 
 
2755
 
                ExtrapolationOption extrapolationOption;
2756
 
                try{
2757
 
                        extrapolationOption = ExtrapolationOption.valueOf(queryElement.getAttribute("extrapolationOption"));            
2758
 
                }catch (Exception e) {
2759
 
                        extrapolationOption = ExtrapolationOption.AUTOMATIC;
2760
 
                }
2761
 
 
2762
 
                ReductionOption reductionOption;
2763
 
                try{
2764
 
                        reductionOption = ReductionOption.valueOf(queryElement.getAttribute("reductionOption"));
2765
 
                }catch (Exception e) {
2766
 
                        reductionOption = ReductionOption.STANDARD;
2767
 
                }
2768
 
 
2769
 
                String capacity;
2770
 
                try{
2771
 
                        capacity =  queryElement.getAttribute("capacity");              
2772
 
                }catch (Exception e) {
2773
 
                        capacity = "3";
2774
 
                }
2775
 
                TAPAALQueryParser queryParser = new TAPAALQueryParser();
2776
 
                TCTLAbstractProperty query;
2777
 
                try{
2778
 
                        query = queryParser.parse(queryElement.getAttribute("query"));
2779
 
                        if(query != null)
2780
 
                                return new TAPNQuery(comment, Integer.parseInt(capacity), query, traceOption, searchOption, reductionOption, hashTableSize, extrapolationOption);
2781
 
                        else
2782
 
                                return null;
2783
 
                }catch (Exception e) {
2784
 
                        JOptionPane.showMessageDialog(CreateGui.getApp(), "TAPAAL encountered an error trying to parse the queries in the model.\n\nThe queries that could not be parsed will not show up in the query list.", "Error Parsing Query", JOptionPane.ERROR_MESSAGE);
2785
 
                        System.err.println("No query was specified: " + e.getStackTrace());
2786
 
                        return null;
2787
 
                }
2788
 
 
2789
 
 
2790
 
        }
2791
 
 
2792
 
 
2793
 
        /**
2794
 
         * Return a URI for the PNML file for the Petri-Net
2795
 
         * @return A DOM for the Petri-Net
2796
 
         */
2797
 
        public String getURI() {
2798
 
                return pnmlName;
2799
 
        }
2800
 
 
2801
 
 
2802
 
        /** prints out a brief representation of the dataLayer object */
2803
 
        public void print() {
2804
 
                System.out.println("No of Places = " + placesArray.size() + "\"");
2805
 
                System.out.println("No of Transitions = " + transitionsArray.size() + "\"");
2806
 
                System.out.println("No of Arcs = " + arcsArray.size() + "\"");
2807
 
                System.out.println("No of Labels = " + labelsArray.size() + 
2808
 
                "\" (Model View Controller Design Pattern)");
2809
 
        }
2810
 
 
2811
 
 
2812
 
 
2813
 
        /**
2814
 
         * See if the supplied net has any timed transitions.
2815
 
         * @param DataLayer
2816
 
         * @return boolean
2817
 
         * @author Matthew
2818
 
         */
2819
 
        public boolean hasTimedTransitions(){
2820
 
                Transition[] transitions = this.getTransitions();
2821
 
                int transCount = transitions.length;
2822
 
 
2823
 
                for (int i = 0; i< transCount; i++) {
2824
 
                        if (transitions[i].isTimed() == true) {
2825
 
                                return true;
2826
 
                        }
2827
 
                }
2828
 
                return false;      
2829
 
        }
2830
 
 
2831
 
 
2832
 
        /**
2833
 
         * See if the net has any timed transitions.
2834
 
         * @return boolean
2835
 
         * @author Matthew
2836
 
         */
2837
 
        public boolean hasImmediateTransitions(){
2838
 
                Transition[] transitions = this.getTransitions();
2839
 
                int transCount = transitions.length;
2840
 
 
2841
 
                for (int i = 0; i< transCount; i++) {
2842
 
                        if (transitions[i].isTimed() == false) {
2843
 
                                return true;
2844
 
                        }
2845
 
                }
2846
 
                return false;
2847
 
        }   
2848
 
 
2849
 
 
2850
 
 
2851
 
 
2852
 
        private void checkForInverseArc(NormalArc newArc) {
2853
 
                Iterator<Arc> iterator = newArc.getSource().getConnectToIterator();
2854
 
 
2855
 
                Arc anArc;
2856
 
                while (iterator.hasNext()){
2857
 
                        anArc = iterator.next();
2858
 
                        if (anArc.getTarget() == newArc.getSource() && 
2859
 
                                        anArc.getSource() == newArc.getTarget()) {
2860
 
                                if (anArc.getClass() == NormalArc.class) {
2861
 
                                        if (!newArc.hasInverse()) {
2862
 
                                                ((NormalArc)anArc).setInverse(newArc, Pipe.JOIN_ARCS);
2863
 
                                        }               
2864
 
                                }
2865
 
                        }
2866
 
                }
2867
 
        }
2868
 
 
2869
 
 
2870
 
        public String getTransitionName(int i) {
2871
 
                return ((Transition)transitionsArray.get(i)).getName();
2872
 
        }
2873
 
 
2874
 
 
2875
 
        public HashMap<Transition, HashMap<TransportArc, TransportArc> > getTransportArcMap() {
2876
 
                return transportArcMap;
2877
 
        }
2878
 
 
2879
 
        public void letTimePass(BigDecimal timeToPass) throws InvariantViolatedAnimationException{
2880
 
                if(!canTimePass(timeToPass)){
2881
 
                        throw new InvariantViolatedAnimationException();
2882
 
                }
2883
 
 
2884
 
                //Do the update
2885
 
                for (Place p : getPlaces()){
2886
 
                        if(isUsingColors()){
2887
 
                                ColoredTimedPlace place = (ColoredTimedPlace)p;
2888
 
                                for(ColoredToken token : place.getColoredTokens()){
2889
 
                                        token.doTimeDelay(timeToPass);
2890
 
                                }
2891
 
                        }else{
2892
 
                                if (p instanceof TimedPlace){
2893
 
                                        TimedPlace place = (TimedPlace)p;
2894
 
                                        int sizeOfArray = place.getTokens().size();
2895
 
 
2896
 
                                        for (int i = 0; i< sizeOfArray; i++){
2897
 
                                                BigDecimal token = place.getTokens().get(i);
2898
 
                                                place.getTokens().set(i, token.add(timeToPass));           
2899
 
                                        }                                       
2900
 
                                }
2901
 
                        }
2902
 
 
2903
 
                        setChanged();
2904
 
                        notifyObservers(p);
2905
 
                }
2906
 
 
2907
 
 
2908
 
                setEnabledTransitions();
2909
 
        }
2910
 
 
2911
 
 
2912
 
        public boolean canTimePass(BigDecimal timeToPass) throws InvariantViolatedAnimationException{
2913
 
                //       Can we do time delay
2914
 
                for (Place p : getPlaces()){
2915
 
                        if(isUsingColors()){
2916
 
                                ColoredTimedPlace ctp = (ColoredTimedPlace)p;
2917
 
 
2918
 
                                for(ColoredToken token : ctp.getColoredTokens()){
2919
 
                                        ColoredToken newToken = new ColoredToken(token.getAge().add(timeToPass), token.getColor());
2920
 
                                        if(!ctp.satisfiesInvariant(newToken)){
2921
 
                                                return false;
2922
 
                                        }
2923
 
                                }
2924
 
                        }else{
2925
 
                                if (p instanceof TimedPlace){
2926
 
                                        TimedPlace timedPlace = (TimedPlace)p;
2927
 
 
2928
 
                                        for (BigDecimal token : timedPlace.getTokens()){
2929
 
                                                if (!timedPlace.satisfiesInvariant(token.add(timeToPass))){   
2930
 
                                                        return false;
2931
 
                                                }
2932
 
                                        }
2933
 
                                }
2934
 
                        }
2935
 
                }
2936
 
                return true;
2937
 
        }
2938
 
 
2939
 
        public void setQueries(ArrayList<TAPNQuery> queries) {
2940
 
                this.queries = queries;
2941
 
 
2942
 
        }
2943
 
        public ArrayList<TAPNQuery> getQueries(){
2944
 
                return queries;
2945
 
        }
2946
 
 
2947
 
        public Collection<Constant> getConstants()
2948
 
        {
2949
 
                return constants.getConstants();
2950
 
        }
2951
 
 
2952
 
        public UndoableEdit updateConstant(String oldName, Constant constant)
2953
 
        {
2954
 
                UndoableEdit edit = constants.updateConstant(oldName, constant, this);
2955
 
                if(edit != null){
2956
 
                        correctGuards(oldName, constant.getName());
2957
 
                }
2958
 
 
2959
 
                return edit;
2960
 
        }
2961
 
 
2962
 
        public void correctGuards(String oldName, String newName) {
2963
 
                updateArcGuards(oldName, newName);
2964
 
                for(Place p : placesArray){
2965
 
                        if(!isUsingColors()){
2966
 
                                if(p instanceof TimedPlace){
2967
 
                                        TimedPlace tp = (TimedPlace)p;
2968
 
                                        String inv = tp.getInvariant();
2969
 
 
2970
 
                                        String operator = inv.contains("<=") ? "<=" : "<";
2971
 
                                        String first = inv.substring(operator.length());
2972
 
 
2973
 
                                        if(first.equals(oldName)){
2974
 
                                                first = newName;
2975
 
                                        }
2976
 
 
2977
 
                                        tp.setInvariant(operator + first);
2978
 
                                }
2979
 
                        }else{
2980
 
                                ((ColoredTimedPlace)p).updateConstantName(oldName, newName);
2981
 
                        }
2982
 
                }
2983
 
 
2984
 
        }
2985
 
 
2986
 
 
2987
 
        private void updateArcGuards(String oldName, String newName) {
2988
 
                for(Arc arc : arcsArray){
2989
 
                        if(!isUsingColors()){
2990
 
                                if(arc instanceof TimedArc || arc instanceof TransportArc){
2991
 
                                        TimedArc tarc = (TimedArc)arc;
2992
 
                                        String guard = tarc.getGuard();
2993
 
                                        String leftDelim = guard.substring(0,1);
2994
 
                                        String rightDelim = guard.substring(guard.length()-1, guard.length());
2995
 
                                        String first = guard.substring(1, guard.indexOf(","));
2996
 
                                        String second = guard.substring(guard.indexOf(",")+1, guard.length()-1);
2997
 
 
2998
 
                                        if(first.equals(oldName)){
2999
 
                                                first = newName;
3000
 
                                        }
3001
 
                                        if(second.equals(oldName)){
3002
 
                                                second = newName;
3003
 
                                        }
3004
 
 
3005
 
                                        tarc.setGuard(leftDelim + first + "," + second + rightDelim);
3006
 
                                }
3007
 
                        }else{
3008
 
                                if(arc instanceof ColoredTransportArc){
3009
 
                                        ColoredTransportArc cta = (ColoredTransportArc)arc;
3010
 
                                        cta.updateConstantName(oldName, newName);
3011
 
                                }else if(arc instanceof ColoredInhibitorArc){
3012
 
                                        ((ColoredInhibitorArc)arc).updateConstantName(oldName, newName);
3013
 
                                }else if(arc instanceof ColoredInputArc){
3014
 
                                        ((ColoredInputArc)arc).updateConstantName(oldName, newName);
3015
 
                                }else if(arc instanceof ColoredOutputArc){
3016
 
                                        ((ColoredOutputArc)arc).updateConstantName(oldName, newName);
3017
 
                                }
3018
 
                        }
3019
 
                }
3020
 
        }
3021
 
 
3022
 
 
3023
 
        public UndoableEdit addConstant(String name, int value) {       
3024
 
                return constants.addConstant(name, value);
3025
 
        }
3026
 
 
3027
 
        public UndoableEdit removeConstant(String name){
3028
 
                return constants.removeConstant(name);          
3029
 
        }
3030
 
 
3031
 
        public void addQuery(TAPNQuery query) {
3032
 
                queries.add(query);
3033
 
 
3034
 
        }
3035
 
 
3036
 
 
3037
 
        public Set<String> getConstantNames() {
3038
 
                return constants.getConstantNames();
3039
 
        }
3040
 
 
3041
 
 
3042
 
 
3043
 
        public int getConstantValue(String constantName) {
3044
 
                return constants.getConstant(constantName).getValue();
3045
 
        }
3046
 
 
3047
 
 
3048
 
        public int getLargestConstantValue() {
3049
 
                return constants.getLargestConstantValue();
3050
 
        }
3051
 
 
3052
 
 
3053
 
        public void buildConstraints() {
3054
 
                constants.buildConstraints(placesArray,arcsArray, isUsingColors());
3055
 
        }       
3056
 
 
3057
 
        public boolean hasTAPNInhibitorArcs(){ // TODO: Fix this to make it faster
3058
 
                for(Arc arc : arcsArray){
3059
 
                        if(arc instanceof TAPNInhibitorArc){
3060
 
                                return true;
3061
 
                        }
3062
 
                }
3063
 
                return false;
3064
 
        }
3065
 
 
3066
 
        public boolean isUsingColors() {
3067
 
                return type.equals(NetType.COLORED);
3068
 
        }
3069
 
                
3070
 
        public NetType netType(){
 
980
        public boolean hasTAPNInhibitorArcs() { // TODO: Fix this to make it faster
 
981
                for (Arc arc : arcsArray) {
 
982
                        if (arc instanceof TimedInhibitorArcComponent) {
 
983
                                return true;
 
984
                        }
 
985
                }
 
986
                return false;
 
987
        }
 
988
        
 
989
        public boolean hasTransportArcs() {
 
990
                for(Arc arc : arcsArray) {
 
991
                        if(arc instanceof TransportArcComponent) {
 
992
                                return true;
 
993
                        }
 
994
                }
 
995
                return false;
 
996
        }
 
997
 
 
998
        public boolean hasInvariants() {
 
999
                for(Place p : placesArray) {
 
1000
                        if(!((TimedPlaceComponent)p).underlyingPlace().invariant().equals(TimeInvariant.LESS_THAN_INFINITY))
 
1001
                                return true;
 
1002
                }
 
1003
                return false;
 
1004
        }
 
1005
 
 
1006
        public NetType netType() {
3071
1007
                return type;
3072
1008
        }
3073
 
        
 
1009
 
3074
1010
        public void setNetType(NetType type) {
3075
1011
                this.type = type;
3076
1012
        }
3077
1013
 
3078
 
 
3079
 
        public void fireColoredTransitionBackwards(
3080
 
                        ColoredDiscreteFiringAction action) {
3081
 
                for(Entry<ColoredTimedPlace, ColoredToken> entry : action.getConsumedTokens().entrySet()){
3082
 
                        entry.getKey().addColoredToken(entry.getValue());
3083
 
                }
3084
 
 
3085
 
                for(Entry<ColoredTimedPlace, ColoredToken> entry : action.getProducedTokens().entrySet()){
3086
 
                        entry.getKey().removeColoredToken(entry.getValue());
3087
 
                }
3088
 
 
3089
 
                //fireTransitionBackwards(action.getTransition());
3090
 
        }
3091
 
 
3092
 
 
3093
 
        public void fireTransition(ColoredDiscreteFiringAction action) {
3094
 
 
3095
 
                for(Entry<ColoredTimedPlace, ColoredToken> entry : action.getConsumedTokens().entrySet()){
3096
 
                        ColoredTimedPlace place = entry.getKey();
3097
 
                        ColoredToken token = entry.getValue();
3098
 
 
3099
 
                        place.removeColoredToken(token);
3100
 
                }
3101
 
 
3102
 
                for(Entry<ColoredTimedPlace, ColoredToken> entry : action.getProducedTokens().entrySet()){
3103
 
                        entry.getKey().addColoredToken(entry.getValue());
3104
 
                }
3105
 
        }
3106
 
 
3107
 
        public boolean isDegree2(){
3108
 
                boolean isDegree2=true;
3109
 
 
3110
 
                for (Transition t : getTransitions()){
3111
 
 
3112
 
                        if (t.getPostset().size() > 2 || t.getPreset().size() > 2){
3113
 
                                isDegree2 = false;
3114
 
                                break;
3115
 
                        }
3116
 
 
3117
 
                }
3118
 
 
3119
 
                return isDegree2;
3120
 
 
3121
 
        }
3122
 
 
3123
 
 
3124
1014
        public void redrawVisibleTokenLists() {
3125
1015
                for (Place place : placesArray) {
3126
 
                        if(place instanceof TimedPlace){
3127
 
                                TimedPlace tp = (TimedPlace)place;
3128
 
                                if(tp.isAgeOfTokensShown()){
 
1016
                        if (place instanceof TimedPlaceComponent) {
 
1017
                                TimedPlaceComponent tp = (TimedPlaceComponent) place;
 
1018
                                if (tp.isAgeOfTokensShown()) {
3129
1019
                                        tp.showAgeOfTokens(true);
3130
1020
                                }
3131
1021
                        }
3132
1022
                }
3133
1023
        }
 
1024
 
 
1025
        public void repaintPlaces() {
 
1026
                for (Place p : placesArray) {
 
1027
                        p.repaint();
 
1028
                }
 
1029
        }
 
1030
 
 
1031
        public void repaintPlaces(boolean displayConstantNames) {
 
1032
                for (Place p : placesArray) {
 
1033
                        p.update(displayConstantNames);
 
1034
                        p.repaint();
 
1035
                }
 
1036
        }
 
1037
 
 
1038
        public void repaintAll(boolean displayConstantNames) {
 
1039
                repaintPlaces(displayConstantNames);
 
1040
 
 
1041
                for (Transition t : transitionsArray) {
 
1042
                        t.repaint();
 
1043
                }
 
1044
 
 
1045
                for (Arc arc : arcsArray) {
 
1046
                        arc.updateWeightLabel(displayConstantNames);
 
1047
                        arc.repaint();
 
1048
                }
 
1049
 
 
1050
                for (InhibitorArc arc : inhibitorsArray) {
 
1051
                        arc.updateWeightLabel(displayConstantNames);
 
1052
                        arc.repaint();
 
1053
                }
 
1054
 
 
1055
        }
 
1056
        
 
1057
        public DataLayer copy(TimedArcPetriNet tapn) {
 
1058
                DataLayer guiModel = new DataLayer();
 
1059
                Hashtable<PlaceTransitionObject, PlaceTransitionObject> oldToNewMapping = new Hashtable<PlaceTransitionObject, PlaceTransitionObject>();
 
1060
                
 
1061
                for(Place p : placesArray) {
 
1062
                        if(p instanceof TimedPlaceComponent) {
 
1063
                                TimedPlaceComponent place = ((TimedPlaceComponent)p).copy(tapn, guiModel);
 
1064
                                oldToNewMapping.put(p, place);
 
1065
                                guiModel.addPetriNetObject(place);
 
1066
                        }
 
1067
                }
 
1068
                
 
1069
                for(Transition t : transitionsArray) {
 
1070
                        if(t instanceof TimedTransitionComponent) {
 
1071
                                TimedTransitionComponent trans = ((TimedTransitionComponent)t).copy(tapn, guiModel);
 
1072
                                oldToNewMapping.put(t, trans);
 
1073
                                guiModel.addPetriNetObject(trans);
 
1074
                        }
 
1075
                }
 
1076
                
 
1077
                for(Arc arc : arcsArray) {
 
1078
                        if(arc instanceof TransportArcComponent) {
 
1079
                                TransportArcComponent transArc = ((TransportArcComponent)arc).copy(tapn, guiModel, oldToNewMapping);
 
1080
                                guiModel.addPetriNetObject(transArc);
 
1081
                        }
 
1082
                        else if(arc instanceof TimedInhibitorArcComponent) {
 
1083
                                TimedInhibitorArcComponent inhibArc = ((TimedInhibitorArcComponent)arc).copy(tapn, guiModel, oldToNewMapping);                          
 
1084
                                guiModel.addPetriNetObject(inhibArc);
 
1085
                        }
 
1086
                        else if(arc instanceof TimedInputArcComponent) {
 
1087
                                TimedInputArcComponent inputArc = ((TimedInputArcComponent)arc).copy(tapn, guiModel, oldToNewMapping);
 
1088
                                guiModel.addPetriNetObject(inputArc);
 
1089
                        }
 
1090
                        else if(arc instanceof TimedOutputArcComponent) {
 
1091
                                TimedOutputArcComponent outputArc = ((TimedOutputArcComponent)arc).copy(tapn, guiModel, oldToNewMapping);
 
1092
                                guiModel.addPetriNetObject(outputArc);
 
1093
                        }
 
1094
                        else {
 
1095
                                throw new RuntimeException("Arc was unknown type");
 
1096
                        }
 
1097
                }
 
1098
                
 
1099
                for(AnnotationNote note : labelsArray) {
 
1100
                        AnnotationNote annotation = note.copy();
 
1101
                        annotation.setGuiModel(guiModel);
 
1102
                        guiModel.addPetriNetObject(annotation);
 
1103
                }
 
1104
                        
 
1105
                
 
1106
                return guiModel;
 
1107
        }
 
1108
 
 
1109
        
3134
1110
}