diff --git a/.gitignore b/.gitignore index d6648aa..6df6619 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ nbproject/private/ out/ .idea/workspace.xml .idea/tasks.xml +/bin/ diff --git a/src/org/pneditor/editor/Root.java b/src/org/pneditor/editor/Root.java index 7fbdd07..b437ad7 100644 --- a/src/org/pneditor/editor/Root.java +++ b/src/org/pneditor/editor/Root.java @@ -292,7 +292,7 @@ public JPopupMenu getCanvasPopup() { private Action convertTransitionToSubnet; private Action replaceSubnet; private Action saveSubnetAs; - private Action cutAction, copyAction, pasteAction, selectAllAction; + private Action cutAction, copyAction, pasteAction, selectAllAction,fireAction1; //per application private Action openSubnet; @@ -335,6 +335,7 @@ private void enableOnlyPossibleActions() { boolean roleSelected = !roleEditor.getSelectedElements().isEmpty(); boolean isParent = !document.petriNet.isCurrentSubnetRoot(); boolean isPtoT = false; + boolean is = clickedElement instanceof Transition; if (isArc) { Arc test; @@ -362,6 +363,7 @@ private void enableOnlyPossibleActions() { undo.setEnabled(getUndoManager().isUndoable()); redo.setEnabled(getUndoManager().isRedoable()); setPlaceStatic.setEnabled(isPlaceNode); + fireAction1.setEnabled(is); } @Override @@ -465,6 +467,7 @@ private void setupMainFrame() { copyAction = new CopyAction(this); pasteAction = new PasteAction(this); selectAllAction = new SelectAllAction(); + fireAction1 = new FireAction(this,0); // initializes to zero Action selectTool_SelectionAction = new SelectionSelectToolAction(this); Action selectTool_PlaceAction = new PlaceSelectToolAction(this); @@ -622,6 +625,7 @@ private void setupMainFrame() { transitionPopup.add(cutAction); transitionPopup.add(copyAction); transitionPopup.add(delete); + transitionPopup.add(fireAction1); Font boldFont = new Font(Font.SANS_SERIF, Font.BOLD, 12); diff --git a/src/org/pneditor/editor/actions/FireAction.java b/src/org/pneditor/editor/actions/FireAction.java new file mode 100644 index 0000000..cf54563 --- /dev/null +++ b/src/org/pneditor/editor/actions/FireAction.java @@ -0,0 +1,73 @@ +package org.pneditor.editor.actions; + +import java.awt.event.ActionEvent; +import javax.swing.AbstractAction; +import javax.swing.JOptionPane; +import org.pneditor.editor.PNEditor; +import org.pneditor.editor.Root; +import org.pneditor.petrinet.Marking; +import org.pneditor.util.GraphicsTools; +import com.sun.glass.events.KeyEvent; +import org.pneditor.editor.commands.FireTransitionCommand; +import org.pneditor.petrinet.Transition; + +/** + * FireAction controller + * @author goudiaby + * + */ +@SuppressWarnings("serial") +public class FireAction extends AbstractAction { + + private Root root; + + private int _i=1; + public FireAction(Root root2, int i) { + // TODO Auto-generated constructor stub + this.root = root2; + + + + String nameO = "click here to activate fire N token(s)"; + putValue(NAME , nameO); + putValue(SMALL_ICON , GraphicsTools.getIcon("pneditor/play.gif")); + putValue(SHORT_DESCRIPTION , nameO); + putValue(MNEMONIC_KEY, KeyEvent.VK_T); + setEnabled(false); + _i=i; + + } + + @Override + public void actionPerformed(ActionEvent arg0) { + // TODO Auto-generated method stub + + Marking initialMarking = PNEditor.getRoot().getCurrentMarking(); + root.getClipboard().setContents(root.getSelectedElementsWithClickedElement() , root.getDocument().petriNet); + + if (root.getClickedElement() != null) { + if (root.getClickedElement() instanceof Transition ) { + Transition transition = (Transition) root.getClickedElement(); + String newLabel = JOptionPane.showInputDialog(root.getParentFrame(), "insert a positive number:", _i); + try { + int n = Integer.parseInt(newLabel); + if(n>0) { + root.getUndoManager().executeCommand(new FireTransitionCommand(transition , initialMarking, n)) ;} + else if(n<0){ + Object[] options = {"OK"}; + int show = JOptionPane.showOptionDialog(null,"You can only insert positive numbers", "Title", JOptionPane.PLAIN_MESSAGE, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); + + } + }catch(NumberFormatException eee) { + JOptionPane.showMessageDialog(null,"Characters are not allow.\n Please insert a positive integer number "); + } + } + // + } + + + } + + + +} diff --git a/src/org/pneditor/editor/commands/FireTransitionCommand.java b/src/org/pneditor/editor/commands/FireTransitionCommand.java index 0375173..fad369b 100644 --- a/src/org/pneditor/editor/commands/FireTransitionCommand.java +++ b/src/org/pneditor/editor/commands/FireTransitionCommand.java @@ -28,6 +28,18 @@ public class FireTransitionCommand implements Command { private Transition transition; private Marking marking; + private int _i= 1; + /** + * Overload the fireTransitionCommand + * @param transition : the transition activated + * @param marking + * @param i : the number of tokens put by the user + */ + public FireTransitionCommand(Transition transition, Marking marking, int i) { + this.transition = transition; + this.marking = marking; + this._i=i; + } public FireTransitionCommand(Transition transition, Marking marking) { this.transition = transition; @@ -35,14 +47,17 @@ public FireTransitionCommand(Transition transition, Marking marking) { } public void execute() { - if (marking.isEnabled(transition)) { - marking.fire(transition); + if (marking.isEnabled(transition,_i)) { + marking.fire(transition,_i); } } - +/** + * + * Marking.java receiver Call and UNDO management . + */ public void undo() { if (marking.canBeUnfired(transition)) { - marking.undoFire(transition); + marking.undoFire(transition,_i); } } diff --git a/src/org/pneditor/petrinet/Marking.java b/src/org/pneditor/petrinet/Marking.java index bdaa94e..299f2ad 100644 --- a/src/org/pneditor/petrinet/Marking.java +++ b/src/org/pneditor/petrinet/Marking.java @@ -33,7 +33,9 @@ * @author Martin Riesz */ public class Marking { - + + public static int initialNumberOftokens; /* initialNumberOftokens is a static variable that allows us to store a number of tokens upstream instead of downstream*/ + public static int vaft; /*vaft is a static variable that allows us to store a number of tokens downstream instead of upstream */ protected Map map = new ConcurrentHashMap(); private PetriNet petriNet; private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); //fair @@ -165,7 +167,40 @@ public boolean isEnabled(Transition transition) { } return isEnabled; } - +//Overload method isenabled . + + public boolean isEnabled(Transition transition,int i) { + boolean isEnabled = true; + lock.readLock().lock(); + try { + for (Arc arc : transition.getConnectedArcs()) { + if (arc.isPlaceToTransition()) { + if (arc.getType().equals(Arc.RESET)) {//reset arc is always fireable + continue; //but can be blocked by other arcs + } else { + if (!arc.getType().equals(Arc.INHIBITOR)) { + if (getTokens(arc.getPlaceNode()) < i*arc.getMultiplicity()) { //normal arc + //The i value contributes to the prohibition to send + //Token(s) located upstream place(s) + isEnabled = false; + break; + } + } else { + if (getTokens(arc.getPlaceNode()) >= arc.getMultiplicity()) {//inhibitory arc and allow tokens not to be sent + //when the value of arc multiplied by the number of toKens are numerous + // the number of tokens on the place + isEnabled = false; + break; + } + } + } + } + } + } finally { + lock.readLock().unlock(); + } + return isEnabled; + } /** * Fires a transition in this marking. Changes this marking. * @@ -222,6 +257,8 @@ public boolean canBeUnfired(Transition transition) { } return canBeUnfired; } + + public void undoFire(Transition transition) { lock.writeLock().lock(); @@ -468,5 +505,152 @@ public int hashCode() { } return hash; } + + /** + * Return the number of tokens located at the place(s) that are positioned before the transition to activate + * @param t : transition + * @return + * + */ + public int returnTokens(Transition t) { + int n = 0; // Initializing the number of tokens , this variable n is used(in Line 481) to calculate of tokens of places or a place placed before a transition. + if(isEnabled(t)){ + + for (Arc arc : t.getConnectedArcs()) { + if(arc.isPlaceToTransition()) { + int tokens = getTokens(arc.getPlaceNode()); + n= n + tokens; + } + + } + + + } + return n; + + } + + /** + * Return the number of tokens located at the place(s) that are positioned before the transition to activate + * @param t + * @return + */ + public int returnTokensAfter(Transition t) { + + int n =0; // Initializing the number of tokens , this variable n is used(in Line 501 and 506) to calculate of tokens of places or a place placed before a transition. + if(isEnabled(t)) { + + for (Arc arc : t.getConnectedArcs()) { + if(!arc.isPlaceToTransition()) { + int tokens = getTokens(arc.getPlaceNode()); + n= n + tokens; + } + + } + } + return n; + + } + + + + /** + * Overload of the method fire allowing to send N token(s) from place(s) to the following place(s) + * @param transition + * @param i + * @return + */ + public boolean fire(Transition transition, int i) { + // TODO Auto-generated method stub + boolean success; + lock.writeLock().lock(); + try { + if (isEnabled(transition)) { + for (Arc arc1 : transition.getConnectedArcs()) { + if (!arc1.isPlaceToTransition()) { + + int tokens = getTokens(arc1.getPlaceNode()); + initialNumberOftokens = returnTokens(transition); + if(iinitialNumberOftokens || i==initialNumberOftokens){ + setTokens(arc1.getPlaceNode(), tokens + initialNumberOftokens*arc1.getMultiplicity()); + vaft= tokens + initialNumberOftokens*arc1.getMultiplicity(); // Stock the value in variable defined in line 38. + } + + + } + } + + for (Arc arc : transition.getConnectedArcs()) { + if (arc.isPlaceToTransition()) { + int tokens = getTokens(arc.getPlaceNode()); + if(iinitialNumberOftokens) { + setTokens(arc1.getPlaceNode(), tokens + initialNumberOftokens*arc1.getMultiplicity()); + } + + } + } + for (Arc arc1 : transition.getConnectedArcs()) { + + if (!arc1.isPlaceToTransition()) { + + int tokens = getTokens(arc1.getPlaceNode()); + if(iinitialNumberOftokens){ + setTokens(arc1.getPlaceNode(), Math.abs(tokens - initialNumberOftokens*arc1.getMultiplicity())); + } + + } + + } + } + + } + finally { + lock.writeLock().unlock(); + } + + } } diff --git a/src/resources/pneditor/play.gif b/src/resources/pneditor/play.gif new file mode 100644 index 0000000..d88d515 Binary files /dev/null and b/src/resources/pneditor/play.gif differ