?? petrinet.java
字號:
return places [i] . removeToken(); } /* end removeTokenFromPlaceAt */ /* select a place for adding an arc. if more than one place exist at that location, the one with the lowest id will be marked. */ public boolean selectNewArcPlace (int x, int y) { newArcPlace = locatePlace (x, y); if (newArcPlace == (Place) null) return false; /* highlight the place and update the hint line */ newArcPlace . highlightOn (); if (! newArcDirectionSet) { newArcToPlace = false; newArcDirectionSet = true; } return true; } /* end selectNewArcPlace */ /* select a transition for adding an arc. if more than one transition exist at that location, the one with the lowest id will be marked. */ public boolean selectNewArcTransition (int x, int y) { newArcTransition = locateTransition (x, y); if (newArcTransition == (Transition) null) return false; /* highlight the Transition and update the hint line */ newArcTransition . highlightOn (); if (! newArcDirectionSet) { newArcToPlace = true; newArcDirectionSet = true; } return true; } /* end selectNewArcTransition */ /* add arc between the specified place and transition. */ public void addArc () { if (newArcPlace != (Place) null && newArcTransition != (Transition) null) { if (newArcToPlace) newArcTransition . addArcOut (newArcPlace); else newArcPlace . addArc (newArcTransition); newArcPlace = (Place) null; newArcTransition = (Transition) null; } newArcDirectionSet = false; } /* end addArc */ /* remove arc between the specified place and transition. */ public void removeArc () { if (newArcPlace != (Place) null && newArcTransition != (Transition) null) { if (newArcToPlace) newArcTransition . removeArcOut (newArcPlace); else newArcPlace . removeArc (newArcTransition); newArcPlace = (Place) null; newArcTransition = (Transition) null; } newArcDirectionSet = false; } /* end removeArc */ /* select the place or transition for dragging. if more than one state have these coordinates, the one with the lowest id will be selected. */ public boolean selectDrag (int x, int y) { dragPlace = locatePlace (x, y); if (dragPlace == (Place) null) { dragTransition = locateTransition (x, y); if (dragTransition == (Transition) null) { return false; } else { dragTransition . dragStart (x, y); return true; } } else { dragPlace . dragStart (x, y); return true; } } /* end selectDrag */ /* stop dragging the current place or transition. */ public void deselectDrag (int x, int y) { if (dragPlace != (Place) null) dragPlace . dragStop (x, y); else if (dragTransition != (Transition) null) dragTransition . dragStop (x, y); dragPlace = (Place) null; dragTransition = (Transition) null; } /* end deselectDrag */ /* drag the current drag state. */ public void drag (int x, int y) { if (dragPlace != (Place) null) dragPlace . drag (x, y); else if (dragTransition != (Transition) null) dragTransition . drag (x, y); } /* end dragState */ /* make sure simulation can be started (necessary conditions exist), stop the previous one if necessary. */ public int startSimulation () { /* stop previous simulation */ stopSimulation (); return STATUS_NORMAL; } /* end startSimulation */ /* stop simulation in progress. */ public void stopSimulation () { } /* end stopSimulation */ /* run one round of simulation. */ public int runSimulation () { /* first, empty the set of activable transitions */ activableTransitions.setSize(0); /* compute the set of activable transition */ for (int i = 0; i < TRANSITIONS && i < numTransitions; i ++) { if (transitions [i] == (Transition) null) continue; if (transitions [i] . isActive()) activableTransitions.addElement((Object) (transitions[i])); } if ( activableTransitions.size() == 0 ) return STATUS_NO_ACTIVABLE_TRANSITION; /* choose a random transition */ int transitionNumber = (int) (wheelOfFortune.nextFloat() * activableTransitions.size()); /* fire the transition */ Transition toFire = (Transition) activableTransitions.elementAt(transitionNumber); toFire . fire(); return STATUS_NORMAL; } /* end runSimulation */ /* load new state machine from the specified file. */ public synchronized int loadFile (String fileName) { FileInputStream file; /* open the file, catching the file not found exception */ try { file = new FileInputStream (fileName); } catch (FileNotFoundException e) { return STATUS_FILE_NOT_FOUND; } /* read from the file catching the I/O error exception */ try { /* make sure the file was created by this program */ if (file . read () != CODE1 || file . read () != CODE2 || file . read () != CODE3 || file . read () != CODE4) { return STATUS_BAD_INPUT_FILE; } int val = 0; /* while delimiter is not End Of Network - keep reading */ while (val != EON) { /* read the next delimiter */ val = file . read (); /* if the delimiter marks Beginning Of Place information */ if (val == BOP) { /* read the byte encoding place's id */ val = file . read (); if (val < 0 || val >= PLACES) return STATUS_BAD_INPUT_FILE; /* create new state with this id. call the constructor that will initialize the state with the information from the file. */ places [val] = new Place (val, file); } /* if the delimiter marks Beginning Of Input string */ else if (val == BOT) { /* read the byte encoding place's id */ val = file . read (); if (val < 0 || val >= TRANSITIONS) return STATUS_BAD_INPUT_FILE; /* create new state with this id. call the constructor that will initialize the state with the information from the file. */ transitions [val] = new Transition (val, file); } /* any delimiter other than EON means garbage */ else if (val != EON) return STATUS_BAD_INPUT_FILE; } /* resolve place and transition IDs in arcs. transitions got initialized with state IDs, not objects since some of them were read from the file before the corresponding states. this step will resovle that. */ numPlaces = 0; for (int i = 0; i < PLACES; i ++) { if (places [i] != (Place) null) { numPlaces ++; places [i] . resolveId ((PlaceTransitionIdResolver) this); } } numTransitions = 0; for (int i = 0; i < TRANSITIONS; i ++) { if (transitions [i] != (Transition) null) { numTransitions ++; transitions [i] . resolveId ((PlaceTransitionIdResolver) this); } } } catch (IOException e) { return STATUS_READ_ERROR; } return STATUS_NORMAL; } /* end loadFile */ /* save current state machine data into specified file. */ public int saveFile (String fileName) { FileOutputStream file; /* open the output file catching the I/O error exception. */ try { file = new FileOutputStream (fileName); } catch (IOException e) { return STATUS_FILE_CREATE_ERROR; } /* write to file catching the I/O error exception. */ try { /* write out file signature */ file . write (CODE1); file . write (CODE2); file . write (CODE3); file . write (CODE4); /* write out place information */ for (int i = 0; i < PLACES; i ++) { if (places [i] == (Place) null) continue; /* write Beginning Of Place delimiter and state id */ file . write (BOP); file . write (i); /* make state write its own information, throw I/O exception if the operation failed */ if (! places [i] . saveFile (file)) throw new IOException (); } /* write out transition information */ for (int i = 0; i < TRANSITIONS; i ++) { if (transitions [i] == (Transition) null) continue; /* write Beginning Of Transition delimiter and state id */ file . write (BOT); file . write (i); /* make state write its own information, throw I/O exception if the operation failed */ if (! transitions [i] . saveFile (file)) throw new IOException (); } /* write End Of Network delimiter */ file . write (EON); } catch (IOException e) { return STATUS_WRITE_ERROR; } return STATUS_NORMAL; } /* end saveFile */ /* PRIVATE METHODS */ /* return place object at the specified location. if more that one place are located in the same space, return the one with the lowest id. */ private Place locatePlace (int x, int y) { for (int i = 0; i < PLACES; i ++) if (places [i] != (Place) null && places [i] . inside (x, y)) return places [i]; return (Place) null; } /* end locatePlace */ /* return transition object at the specified location. if more that one trans are located in the same space, return the one with the lowest id. */ private Transition locateTransition (int x, int y) { for (int i = 0; i < TRANSITIONS; i ++) if (transitions [i] != (Transition) null && transitions [i] . inside (x, y)) return transitions [i]; return (Transition) null; } /* end locateTransition */} /* end PetriNet */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -