📄 petrinet.java
字号:
/* IMPORTS*/import java.applet.*;import java.awt.*;import java.lang.*;import java.util.*;import java.io.*;import java.net.*;/* PetriNet class - implements Petri Net abstraction - a collection of place and transition objects and operations on them.*/class PetriNet implements PlaceTransitionIdResolver{ /* CLASS CONSTANTS */ /* return values from some calls */ /* normal status */ final static int STATUS_NORMAL = 1; /* file specified for loading was not found */ final static int STATUS_FILE_NOT_FOUND = 2; /* file specified for savinging could not be created */ final static int STATUS_FILE_CREATE_ERROR = 3; /* file read error */ final static int STATUS_READ_ERROR = 4; /* file write error */ final static int STATUS_WRITE_ERROR = 5; /* input file improperly constructed error */ final static int STATUS_BAD_INPUT_FILE = 6; /* input file improperly constructed error */ final static int STATUS_NO_ACTIVABLE_TRANSITION = 7; /* maximum number of places */ final static int PLACES = 10; /* maximum number of transitions */ final static int TRANSITIONS = 10; /* code characters used to mark the file as created by this program */ final static int CODE1 = (int) 'T'; final static int CODE2 = (int) 'P'; final static int CODE3 = (int) 'N'; final static int CODE4 = (int) 'S'; /* codes characters used to delimit fields in the files */ /* beginning of place data */ final static int BOP = 0x7D; /* beginning of transition data */ final static int BOT = 0x7E; /* end of net data (end of file) */ final static int EON = 0x7F; /* INSTANCE VARIABLES */ /* array of place objects */ private Place places []; /* number of places currently present */ private int numPlaces; /* array of transition objects */ private Transition transitions []; /* number of transitions currently present */ private int numTransitions; /* place we are currently removing */ private Place removingPlace; /* transition we are currently removing */ private Transition removingTransition; /* Place of a new arc*/ private Place newArcPlace; /* Transition of a new arc*/ private Transition newArcTransition; /* Direction of a new arc and direction set flag */ public boolean newArcToPlace; public boolean newArcDirectionSet; /* Place being dragged */ private Place dragPlace; /* Transition being dragged */ private Transition dragTransition; /* Vector of activable transitions */ private Vector activableTransitions; /* Random number generator used to randomly select a transition */ private Random wheelOfFortune; /* PUBLIC METHODS */ /* constructor method */ PetriNet () { /* initialize instance variables */ removingPlace = newArcPlace = dragPlace = (Place) null; numPlaces = 0; places = new Place [PLACES]; for (int i = 0; i < PLACES; i ++) places [i] = (Place) null; removingTransition = newArcTransition = dragTransition = (Transition) null; numTransitions = 0; transitions = new Transition [TRANSITIONS]; for (int i = 0; i < TRANSITIONS; i ++) transitions [i] = (Transition) null; activableTransitions = new Vector(TRANSITIONS); wheelOfFortune = new Random(); newArcToPlace = false; newArcDirectionSet = false; } /* end PetriNet */ /* these routines implements StateIdResolver interface. returns the state corresponding to the specified id. */ public Place resolvePlaceId (int id) { if (id < 0 || id >= PLACES) return (Place) null; return places [id]; } /* end resolvePlaceId */ public Transition resolveTransitionId (int id) { if (id < 0 || id >= TRANSITIONS) return (Transition) null; return transitions [id]; } /* end resolveTransitionId */ /* display state machine on the specified graphics context. */ public synchronized void paint (Graphics g, int offset) { /* if a place was marked for removal - call its remove method to delete its image */ if (removingPlace != (Place) null) { removingPlace . remove (g); removingPlace = (Place) null; } /* if a transition was marked for removal - call its remove method to delete its image */ if (removingTransition != (Transition) null) { removingTransition . remove (g); removingTransition = (Transition) null; } /* paint all existing places on the screen. notice that we traverse the array in the reverse order, so that if places overlap on the screen, the ones with the lowest ids will end up on top and will correspond to the ones that get selected when user clicks on them */ for (int i = PLACES - 1; i >= 0; i --) if (places [i] != (Place) null) places [i] . paint (g); /* paint all existing transitions on the screen. notice that we traverse the array in the reverse order, so that if transitions overlap on the screen, the ones with the lowest ids will end up on top and will correspond to the ones that get selected when user clicks on them */ for (int i = TRANSITIONS - 1; i >= 0; i --) if (transitions [i] != (Transition) null) transitions [i] . paint (g); } /* end paint */ /* add a new place at specified coordinates. */ public boolean addPlace (int x, int y) { /* if maximum number of places already exists - exit */ if (numPlaces == PLACES) return false; numPlaces ++; int i; /* look for the first empty slot in the places array */ for (i = 0; i < PLACES; i ++) if (places [i] == (Place) null) break; /* create new place */ places [i] = new Place (i, x, y); return true; } /* end addPlace */ /* add a new place at specified coordinates. */ public boolean addTransition (int x, int y) { /* if maximum number of transitions already exists - exit */ if (numTransitions == TRANSITIONS) return false; numTransitions ++; int i; /* look for the first empty slot in the transitions array */ for (i = 0; i < TRANSITIONS; i ++) if (transitions [i] == (Transition) null) break; /* create new transition */ transitions [i] = new Transition (i, x, y); return true; } /* end addTransition */ /* remove place at specified coordinates. if more than one place exist at that location, the one with the lowest id will be removed. */ public boolean removePlace (int x, int y) { /* make sure we have something to remove */ if (numPlaces == 0) return false; numPlaces --; int i; /* find the first place that acknowledges that the point lays inside of its space */ for (i = 0; i < PLACES; i ++) if (places [i] != (Place) null && places [i] . inside (x, y)) break; if (i == PLACES) return false; /* we cannot completely get rid of it yet, as we will have to remove the places drawings. this will be done at the next call to paint, so just set the removingPlace to places object. */ removingPlace = places [i]; /* Invalidate the place. invalidation is needed to remove the arcs whose destination this place is. place knows only of arcs starting with it, but it cannot invalidate the ones ending at it. arcs will check if their traget state is valid during update calls and invalidate themselves if necessary. */ removingPlace . makeInvalid (); places [i] = (Place) null; return true; } /* end removePlace */ /* remove transition at specified coordinates. if more than one transition exist at that location, the one with the lowest id will be removed. */ public boolean removeTransition (int x, int y) { /* make sure we have something to remove */ if (numTransitions == 0) return false; numTransitions --; int i; /* find the first transition that acknowledges that the point lays inside of its space */ for (i = 0; i < TRANSITIONS; i ++) if (transitions [i] != (Transition) null && transitions [i] . inside (x, y)) break; if (i == TRANSITIONS) return false; /* we cannot completely get rid of it yet, as we will have to remove the transitions drawings. this will be done at the next call to paint, so just set the removingTransition to transitions object. */ removingTransition = transitions [i]; /* Invalidate the transition. invalidation is needed to remove the arcs whose destination this transition is. transition knows only of arcs starting with it, but it cannot invalidate the ones ending at it. arcs will check if their traget state is valid during update calls and invalidate themselves if necessary. */ removingTransition . makeInvalid (); transitions [i] = (Transition) null; return true; } /* end removeTransition */ /* add token to place at specified coordinates. if more than one place exist at that location, the one with the lowest id will have the token. */ public boolean addTokenToPlaceAt (int x, int y) { int i; /* find the first place that acknowledges that the point lays inside of its space */ for (i = 0; i < PLACES; i ++) if (places [i] != (Place) null && places [i] . inside (x, y)) break; if (i == PLACES) return false; /* add a token to the found place */ places [i] . addToken(); return true; } /* end addTokenToPlaceAt */ /* add token to place at specified coordinates. if more than one place exist at that location, the one with the lowest id will have the token. */ public boolean removeTokenFromPlaceAt (int x, int y) { int i; /* find the first place that acknowledges that the point lays inside of its space */ for (i = 0; i < PLACES; i ++) if (places [i] != (Place) null && places [i] . inside (x, y)) break; if (i == PLACES) return false; /* remove a token from the found place returns false if no token in that place*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -