Skip to content
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ nbproject/private/
out/
.idea/workspace.xml
.idea/tasks.xml
/bin/
6 changes: 5 additions & 1 deletion src/org/pneditor/editor/Root.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -362,6 +363,7 @@ private void enableOnlyPossibleActions() {
undo.setEnabled(getUndoManager().isUndoable());
redo.setEnabled(getUndoManager().isRedoable());
setPlaceStatic.setEnabled(isPlaceNode);
fireAction1.setEnabled(is);
}

@Override
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
73 changes: 73 additions & 0 deletions src/org/pneditor/editor/actions/FireAction.java
Original file line number Diff line number Diff line change
@@ -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 ");
}
}
//
}


}



}
23 changes: 19 additions & 4 deletions src/org/pneditor/editor/commands/FireTransitionCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,36 @@ 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;
this.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);
}
}

Expand Down
188 changes: 186 additions & 2 deletions src/org/pneditor/petrinet/Marking.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
* @author Martin Riesz <riesz.martin at gmail.com>
*/
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<Place, Integer> map = new ConcurrentHashMap<Place, Integer>();
private PetriNet petriNet;
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); //fair
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -222,6 +257,8 @@ public boolean canBeUnfired(Transition transition) {
}
return canBeUnfired;
}



public void undoFire(Transition transition) {
lock.writeLock().lock();
Expand Down Expand Up @@ -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(i<initialNumberOftokens)
setTokens(arc1.getPlaceNode(), tokens + i*arc1.getMultiplicity());
else if(i>initialNumberOftokens || 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(i<tokens || i==tokens) {
if (!arc.getType().equals(Arc.INHIBITOR)) { //inhibitor arc doesnt consume tokens
if (arc.getType().equals(Arc.RESET)) { //reset arc consumes them all
setTokens(arc.getPlaceNode(), 0);
} else {
setTokens(arc.getPlaceNode(), tokens - i*arc.getMultiplicity());
}
}
}

}
}
success = true;
} else {
success = false;
}
} finally {
lock.writeLock().unlock();
}
return success;

}

/**
* Management of the backtrack taking into account the initial state of the network and the final
and the final state. Calculations have been made with precision for a good management of the Marking
* @param transition
* @param i
*/
public void undoFire(Transition transition, int i) {
// TODO Auto-generated method stub
lock.writeLock().lock();
try {
if (canBeUnfired(transition)) {
for (Arc arc1 : transition.getConnectedArcs()) {
if(arc1.isPlaceToTransition()) {
int tokens = getTokens(arc1.getPlaceNode());
if(i<initialNumberOftokens || i==initialNumberOftokens ) {
setTokens(arc1.getPlaceNode(), tokens + i*arc1.getMultiplicity());}
else if(i>initialNumberOftokens) {
setTokens(arc1.getPlaceNode(), tokens + initialNumberOftokens*arc1.getMultiplicity());
}

}
}
for (Arc arc1 : transition.getConnectedArcs()) {

if (!arc1.isPlaceToTransition()) {

int tokens = getTokens(arc1.getPlaceNode());
if(i<initialNumberOftokens || i==initialNumberOftokens) {
setTokens(arc1.getPlaceNode(), tokens - i*arc1.getMultiplicity());}
else if(i>initialNumberOftokens){
setTokens(arc1.getPlaceNode(), Math.abs(tokens - initialNumberOftokens*arc1.getMultiplicity()));
}

}

}
}

}
finally {
lock.writeLock().unlock();
}

}

}
Binary file added src/resources/pneditor/play.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.