diff --git a/src/main/java/org/petriNet/Arc.java b/src/main/java/org/petriNet/Arc.java index e30be0f851f7f25363a236e917d269b47e4e0811..ee4a3d3390a77cd8e65b0a02c3b26e7a78c2c828 100644 --- a/src/main/java/org/petriNet/Arc.java +++ b/src/main/java/org/petriNet/Arc.java @@ -2,9 +2,9 @@ package org.petriNet; public abstract class Arc { - private int id; + private final int id; private Place place; - private Transition transition; + private final Transition transition; private int weight; public Arc(Transition transition, Place place, int weight, int id) { @@ -16,7 +16,7 @@ public abstract class Arc { } else { System.out.println("The weight cannot be negative."); } - this.id = id; // Assign a unique ID with the generateId method in the ReseauPetri class + this.id = id; // Assign a unique ID with the generateId method in the PetriNet class } public abstract void validate(); @@ -45,9 +45,6 @@ public abstract class Arc { return this.transition; } - public void removePlace(Place place) { - this.place = null; - } } diff --git a/src/main/java/org/petriNet/Arc_ENTRANT.java b/src/main/java/org/petriNet/Arc_ENTRANT.java deleted file mode 100644 index 59d0ff569016689c4a6ebcb737648b84b31d3348..0000000000000000000000000000000000000000 --- a/src/main/java/org/petriNet/Arc_ENTRANT.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.petriNet; - -public abstract class Arc_ENTRANT extends Arc { - - /** - * Les Arc_ENTRANT sont entrants à une transition et sortants d'une place - * Ils permettent de retirer des jetons d'une place - * Ils sont donc la classe abstraite mère des Arc_sortant_simple, Arc_videur et Arc_zero - * C'est une différence par rapport au diagramme de classe soumis - */ - - private Place place; - private Transition transition; - private int poids; - private int id; - - public Arc_ENTRANT( Transition transition, Place place, int poids, int id) { - super(transition, place, poids, id); - } - - /** - * Dans la classe Arc_ENTRANT, on enlève les jetons à la place seulement, - * il n'y a pas de jetons à ajouter - * ceci pour justifier le cahngement par rapport au diagramme de classe soumis - */ - - public boolean verifier_tirable() { - return this.getPlace().get_nombre_jetons() >= this.getWeight(); - } - - @Override - public void validate() { - // On retire le nombre de jetons du poids de l'arc - this.getPlace().enlever_jeton(this.getWeight()); - } - -} \ No newline at end of file diff --git a/src/main/java/org/petriNet/Arc_SORTANT.java b/src/main/java/org/petriNet/Arc_SORTANT.java deleted file mode 100644 index 44413ae9f9ad40a44311a4de686de55b2a4b6130..0000000000000000000000000000000000000000 --- a/src/main/java/org/petriNet/Arc_SORTANT.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.petriNet; - -public class Arc_SORTANT extends Arc { - - private Place place; - private Transition transition; - private int poids; - private int id; - - public Arc_SORTANT( Transition transition, Place place, int poids, int id) { - super(transition, place, poids, id); - } - - - @Override - public void validate() { - // On ajoute le nombre de jetons du poids de l'arc - this.getPlace().ajouter_jeton(this.getWeight()); - } - -} \ No newline at end of file diff --git a/src/main/java/org/petriNet/Arc_entrant_simple.java b/src/main/java/org/petriNet/Arc_entrant_simple.java deleted file mode 100644 index 370be3f210b1f5897bf604a8427350b9d91db43d..0000000000000000000000000000000000000000 --- a/src/main/java/org/petriNet/Arc_entrant_simple.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.petriNet; - -public class Arc_entrant_simple extends Arc_ENTRANT { - - Place place; - Transition transition; - int poids; - int id; - - - public Arc_entrant_simple( Transition transition, Place place, int poids, int id) { - super(transition, place, poids, id); - } - -} diff --git a/src/main/java/org/petriNet/Arc_zero.java b/src/main/java/org/petriNet/Arc_zero.java deleted file mode 100644 index 3fd600e82f5423e9a340539d3853fefa055d840e..0000000000000000000000000000000000000000 --- a/src/main/java/org/petriNet/Arc_zero.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.petriNet; - - public class Arc_zero extends Arc_ENTRANT { - - Place place; - Transition transition; - int poids; - boolean etat; - int id; - - - public Arc_zero( Transition transition, Place place, int poids, int id) { - super(transition, place, poids, id); - } - - // Les arcs « zéro » qui ne sont actifs que quand la place source est vide. - - @Override - public boolean verifier_tirable() { - return this.getPlace().get_nombre_jetons() == 0; - } -} diff --git a/src/main/java/org/petriNet/IncomingArc.java b/src/main/java/org/petriNet/IncomingArc.java new file mode 100644 index 0000000000000000000000000000000000000000..5f66af3ddd528cab2ea5bf1aa280e0332ed55410 --- /dev/null +++ b/src/main/java/org/petriNet/IncomingArc.java @@ -0,0 +1,31 @@ +package org.petriNet; + +public abstract class IncomingArc extends Arc { + + /** + * IncomingArc are incoming to a transition and outgoing from a place. + * They allow tokens to be removed from a place. + * They are therefore the abstract parent class of OutgoingArc_Simple, IncomingArc_Videur, and IncomingArc_Zero. + * This is a difference from the submitted class diagram. + */ + + public IncomingArc(Transition transition, Place place, int weight, int id) { + super(transition, place, weight, id); + } + + /** + * In the IncomingArc class, tokens are only removed from the place, + * there are no tokens to add. + * This justifies the change from the submitted class diagram. + */ + + public boolean canFire() { + return this.getPlace().getTokenCount() >= this.getWeight(); + } + + @Override + public void validate() { + // Remove the number of tokens equal to the weight of the arc + this.getPlace().removeTokens(this.getWeight()); + } +} \ No newline at end of file diff --git a/src/main/java/org/petriNet/IncomingArc_Simple.java b/src/main/java/org/petriNet/IncomingArc_Simple.java new file mode 100644 index 0000000000000000000000000000000000000000..7c1f2e8080e84e25c31e0d114a4115293e012102 --- /dev/null +++ b/src/main/java/org/petriNet/IncomingArc_Simple.java @@ -0,0 +1,9 @@ +package org.petriNet; + +public class IncomingArc_Simple extends IncomingArc { + + public IncomingArc_Simple(Transition transition, Place place, int weight, int id) { + super(transition, place, weight, id); + } + +} diff --git a/src/main/java/org/petriNet/Arc_videur.java b/src/main/java/org/petriNet/IncomingArc_Videur.java similarity index 64% rename from src/main/java/org/petriNet/Arc_videur.java rename to src/main/java/org/petriNet/IncomingArc_Videur.java index 041068e043f2c21a61cc929541a311b8ef054beb..5c29cadbb0f68bbf3b8bacbbbe5e2c0d8db3f08a 100644 --- a/src/main/java/org/petriNet/Arc_videur.java +++ b/src/main/java/org/petriNet/IncomingArc_Videur.java @@ -1,13 +1,13 @@ package org.petriNet; -public class Arc_videur extends Arc_ENTRANT { +public class IncomingArc_Videur extends IncomingArc { Place place; Transition transition; int poids; int id; - public Arc_videur( Transition transition, Place place, int poids, int id) { + public IncomingArc_Videur(Transition transition, Place place, int poids, int id) { super(transition, place, poids, id); } @@ -15,8 +15,8 @@ public class Arc_videur extends Arc_ENTRANT { // tous les jetons présents lorsqu’ils sont activés. @Override - public boolean verifier_tirable() { - if ( this.getPlace().get_nombre_jetons() > 0) { + public boolean canFire() { + if ( this.getPlace().getTokenCount() > 0) { return true; } return false; @@ -26,7 +26,7 @@ public class Arc_videur extends Arc_ENTRANT { public void validate() { // On retire le nombre de jetons du poids de l'arc // prendre en cond la place choisie - this.getPlace().enlever_jeton(this.getPlace().get_nombre_jetons()); + this.getPlace().removeTokens(this.getPlace().getTokenCount()); } diff --git a/src/main/java/org/petriNet/IncomingArc_Zero.java b/src/main/java/org/petriNet/IncomingArc_Zero.java new file mode 100644 index 0000000000000000000000000000000000000000..dd48bb62bccfb6e93c32e35e42c3f9540bfc6687 --- /dev/null +++ b/src/main/java/org/petriNet/IncomingArc_Zero.java @@ -0,0 +1,13 @@ +package org.petriNet; + + public class IncomingArc_Zero extends IncomingArc { + + public IncomingArc_Zero(Transition transition, Place place, int weight, int id) { + super(transition, place, weight, id); + } + + @Override + public boolean canFire() { + return this.getPlace().getTokenCount() == 0; + } +} diff --git a/src/main/java/org/petriNet/Main.java b/src/main/java/org/petriNet/Main.java index 2a1a22f495f7808a956245ce2efb28fe7d1beeeb..7143b00703aca3f001e2d525fc5abdfb6425acaf 100644 --- a/src/main/java/org/petriNet/Main.java +++ b/src/main/java/org/petriNet/Main.java @@ -4,48 +4,48 @@ public class Main { public static void main(String[] args) { // Initialize the Petri network - ReseauPetri petriNetwork = new ReseauPetri(); + PetriNet petriNetwork = new PetriNet(); // Create places Place place1 = new Place(3, petriNetwork.generateId(1)); Place place2 = new Place(0, petriNetwork.generateId(1)); // Add places to the network - petriNetwork.ajouterPlace(place1); - petriNetwork.ajouterPlace(place2); + petriNetwork.addPlace(place1); + petriNetwork.addPlace(place2); // Create a transition Transition transition1 = new Transition("T1", petriNetwork.generateId(2)); // Add the transition to the network - petriNetwork.ajouterTransition(transition1); + petriNetwork.addTransition(transition1); // Create arcs and add them to the network // Incoming arc from place1 to transition1 with weight 1 - Arc_ENTRANT incomingArc = new Arc_entrant_simple(transition1, place1, 1, petriNetwork.generateId(0)); - petriNetwork.ajouterArc(incomingArc); - transition1.ajouterArc_ENTRANT(incomingArc); + IncomingArc incomingArc = new IncomingArc_Simple(transition1, place1, 1, petriNetwork.generateId(0)); + petriNetwork.addArc(incomingArc); + transition1.addIncomingArc(incomingArc); // Outgoing arc from transition1 to place2 with weight 1 - Arc_SORTANT outgoingArc = new Arc_SORTANT(transition1, place2, 1, petriNetwork.generateId(0)); - petriNetwork.ajouterArc(outgoingArc); - transition1.ajouterArc_SORTANT(outgoingArc); + OutgoingArc outgoingArc = new OutgoingArc(transition1, place2, 1, petriNetwork.generateId(0)); + petriNetwork.addArc(outgoingArc); + transition1.addOutgoingArc(outgoingArc); // Display the initial state of the Petri network System.out.println("Initial State of Petri Network:"); - petriNetwork.afficherReseau(); + petriNetwork.displayNetwork(); // Activate the transition System.out.println("\nActivating transition T1..."); - petriNetwork.tirer_transition(String.valueOf(transition1.getId())); + petriNetwork.fireTransition(String.valueOf(transition1.getId())); // Display the state of the Petri network after activation System.out.println("\nState of Petri Network after Transition T1 Activation:"); - petriNetwork.afficherReseau(); + petriNetwork.displayNetwork(); // Example assertions to verify changes System.out.println("\nVerifying state:"); - System.out.println("Tokens in Place 1 (expected 2): " + place1.get_nombre_jetons()); - System.out.println("Tokens in Place 2 (expected 1): " + place2.get_nombre_jetons()); + System.out.println("Tokens in Place 1 (expected 2): " + place1.getTokenCount()); + System.out.println("Tokens in Place 2 (expected 1): " + place2.getTokenCount()); } } diff --git a/src/main/java/org/petriNet/MainPetriNet.java b/src/main/java/org/petriNet/MainPetriNet.java index 657fdda4d8b3423b5482588ebbeee075f5cd8a48..2e80162a4f950e558f63eb6f90a92131001c7696 100644 --- a/src/main/java/org/petriNet/MainPetriNet.java +++ b/src/main/java/org/petriNet/MainPetriNet.java @@ -4,7 +4,7 @@ public class MainPetriNet { public static void main(String[] args) { // Create a Petri net - ReseauPetri reseauPetri = new ReseauPetri(); + PetriNet reseauPetri = new PetriNet(); // Create places Place p1 = new Place(1, reseauPetri.generateId(1)); @@ -16,16 +16,16 @@ public class MainPetriNet { Transition t2 = new Transition("t2", reseauPetri.generateId(2)); // Create arcs - Arc_ENTRANT a1 = new Arc_entrant_simple(t1, p1, 1, reseauPetri.generateId(0)); - Arc_SORTANT a2 = new Arc_SORTANT(t1, p2, 1, reseauPetri.generateId(0)); - Arc_ENTRANT a3 = new Arc_entrant_simple(t2, p2, 1, reseauPetri.generateId(0)); - Arc_SORTANT a4 = new Arc_SORTANT(t2, p3, 1, reseauPetri.generateId(0)); + IncomingArc a1 = new IncomingArc_Simple(t1, p1, 1, reseauPetri.generateId(0)); + OutgoingArc a2 = new OutgoingArc(t1, p2, 1, reseauPetri.generateId(0)); + IncomingArc a3 = new IncomingArc_Simple(t2, p2, 1, reseauPetri.generateId(0)); + OutgoingArc a4 = new OutgoingArc(t2, p3, 1, reseauPetri.generateId(0)); // Add arcs to transitions - t1.ajouterArc_ENTRANT(a1); - t1.ajouterArc_SORTANT(a2); - t2.ajouterArc_ENTRANT(a3); - t2.ajouterArc_SORTANT(a4); + t1.addIncomingArc(a1); + t1.addOutgoingArc(a2); + t2.addIncomingArc(a3); + t2.addOutgoingArc(a4); // Add a place to each arc a1.setPlace(p1); @@ -34,13 +34,13 @@ public class MainPetriNet { a4.setPlace(p3); // Add places and transitions to the Petri net - reseauPetri.ajouterPlace(p1); - reseauPetri.ajouterPlace(p2); - reseauPetri.ajouterPlace(p3); - reseauPetri.ajouterTransition(t1); - reseauPetri.ajouterTransition(t2); + reseauPetri.addPlace(p1); + reseauPetri.addPlace(p2); + reseauPetri.addPlace(p3); + reseauPetri.addTransition(t1); + reseauPetri.addTransition(t2); // Display the Petri net - reseauPetri.afficherEtat(); + reseauPetri.displayState(); } } \ No newline at end of file diff --git a/src/main/java/org/petriNet/MainSpecialVideur.java b/src/main/java/org/petriNet/MainSpecialVideur.java index 53e3915a08376c53ce5579e9c9d53e71071dbfb5..2d2a91f61095aa53efc4bca30b00ec114a279c9b 100644 --- a/src/main/java/org/petriNet/MainSpecialVideur.java +++ b/src/main/java/org/petriNet/MainSpecialVideur.java @@ -4,48 +4,48 @@ public class MainSpecialVideur { public static void main(String[] args) { // Initialize the Petri network - ReseauPetri petriNetwork = new ReseauPetri(); + PetriNet petriNetwork = new PetriNet(); // Create places Place place1 = new Place(2, petriNetwork.generateId(1)); Place place2 = new Place(4, petriNetwork.generateId(1)); // Add places to the network - petriNetwork.ajouterPlace(place1); - petriNetwork.ajouterPlace(place2); + petriNetwork.addPlace(place1); + petriNetwork.addPlace(place2); // Create a transition Transition transition1 = new Transition("T1", petriNetwork.generateId(2)); // Add the transition to the network - petriNetwork.ajouterTransition(transition1); + petriNetwork.addTransition(transition1); // Create arcs and add them to the network // Incoming arc from place2 to transition1 with weight 1 - Arc_videur incomingArc_videur = new Arc_videur(transition1, place2, 1, petriNetwork.generateId(0)); - petriNetwork.ajouterArc(incomingArc_videur); - transition1.ajouterArc_ENTRANT(incomingArc_videur); + IncomingArc_Videur incomingArc_videur = new IncomingArc_Videur(transition1, place2, 1, petriNetwork.generateId(0)); + petriNetwork.addArc(incomingArc_videur); + transition1.addIncomingArc(incomingArc_videur); // Outgoing arc from transition1 to place1 with weight 1 - Arc_SORTANT outgoingArc = new Arc_SORTANT(transition1, place1, 1, petriNetwork.generateId(0)); - petriNetwork.ajouterArc(outgoingArc); - transition1.ajouterArc_SORTANT(outgoingArc); + OutgoingArc outgoingArc = new OutgoingArc(transition1, place1, 1, petriNetwork.generateId(0)); + petriNetwork.addArc(outgoingArc); + transition1.addOutgoingArc(outgoingArc); // Display the initial state of the Petri network System.out.println("Initial State of Petri Network:"); - petriNetwork.afficherReseau(); + petriNetwork.displayNetwork(); // Activate the transition System.out.println("\nActivating transition T1..."); - petriNetwork.tirer_transition(String.valueOf(transition1.getId())); + petriNetwork.fireTransition(String.valueOf(transition1.getId())); // Display the state of the Petri network after activation System.out.println("\nState of Petri Network after Transition T1 Activation:"); - petriNetwork.afficherReseau(); + petriNetwork.displayNetwork(); // Example assertions to verify changes System.out.println("\nVerifying state:"); - System.out.println("Tokens in Place 1 (expected 3): " + place1.get_nombre_jetons()); - System.out.println("Tokens in Place 2 (expected 0): " + place2.get_nombre_jetons()); + System.out.println("Tokens in Place 1 (expected 3): " + place1.getTokenCount()); + System.out.println("Tokens in Place 2 (expected 0): " + place2.getTokenCount()); } } diff --git a/src/main/java/org/petriNet/MainSpecialZero.java b/src/main/java/org/petriNet/MainSpecialZero.java index 3b68d1b27e677b97d29519e2b665d8e713d503ec..4db9faa4259d3e0c695f8cae5e16fa29bef9226b 100644 --- a/src/main/java/org/petriNet/MainSpecialZero.java +++ b/src/main/java/org/petriNet/MainSpecialZero.java @@ -4,48 +4,48 @@ public class MainSpecialZero { public static void main(String[] args) { // Initialize the Petri network - ReseauPetri petriNetwork = new ReseauPetri(); + PetriNet petriNetwork = new PetriNet(); // Create places Place place1 = new Place(3, petriNetwork.generateId(1)); Place place2 = new Place(0, petriNetwork.generateId(1)); // Add places to the network - petriNetwork.ajouterPlace(place1); - petriNetwork.ajouterPlace(place2); + petriNetwork.addPlace(place1); + petriNetwork.addPlace(place2); // Create a transition Transition transition1 = new Transition("T1", petriNetwork.generateId(2)); // Add the transition to the network - petriNetwork.ajouterTransition(transition1); + petriNetwork.addTransition(transition1); // Create arcs and add them to the network // Incoming arc from place2 to transition1 with weight 1 - Arc_zero incomingArc_zero = new Arc_zero(transition1, place2, 1, petriNetwork.generateId(0)); - petriNetwork.ajouterArc(incomingArc_zero); - transition1.ajouterArc_ENTRANT(incomingArc_zero); + IncomingArc_Zero incomingArc_zero = new IncomingArc_Zero(transition1, place2, 1, petriNetwork.generateId(0)); + petriNetwork.addArc(incomingArc_zero); + transition1.addIncomingArc(incomingArc_zero); // Outgoing arc from transition1 to place1 with weight 1 - Arc_SORTANT outgoingArc = new Arc_SORTANT(transition1, place1, 1, petriNetwork.generateId(0)); - petriNetwork.ajouterArc(outgoingArc); - transition1.ajouterArc_SORTANT(outgoingArc); + OutgoingArc outgoingArc = new OutgoingArc(transition1, place1, 1, petriNetwork.generateId(0)); + petriNetwork.addArc(outgoingArc); + transition1.addOutgoingArc(outgoingArc); // Display the initial state of the Petri network System.out.println("Initial State of Petri Network:"); - petriNetwork.afficherReseau(); + petriNetwork.displayNetwork(); // Activate the transition System.out.println("\nActivating transition T1..."); - petriNetwork.tirer_transition(String.valueOf(transition1.getId())); + petriNetwork.fireTransition(String.valueOf(transition1.getId())); // Display the state of the Petri network after activation System.out.println("\nState of Petri Network after Transition T1 Activation:"); - petriNetwork.afficherReseau(); + petriNetwork.displayNetwork(); // Example assertions to verify changes System.out.println("\nVerifying state:"); - System.out.println("Tokens in Place 1 (expected 4): " + place1.get_nombre_jetons()); - System.out.println("Tokens in Place 2 (expected 0): " + place2.get_nombre_jetons()); + System.out.println("Tokens in Place 1 (expected 4): " + place1.getTokenCount()); + System.out.println("Tokens in Place 2 (expected 0): " + place2.getTokenCount()); } } diff --git a/src/main/java/org/petriNet/OutgoingArc.java b/src/main/java/org/petriNet/OutgoingArc.java new file mode 100644 index 0000000000000000000000000000000000000000..589a91e08a03daaf4f49026864cbe9e3f62ef5c3 --- /dev/null +++ b/src/main/java/org/petriNet/OutgoingArc.java @@ -0,0 +1,15 @@ +package org.petriNet; + +public class OutgoingArc extends Arc { + + public OutgoingArc(Transition transition, Place place, int weight, int id) { + super(transition, place, weight, id); + } + + + @Override + public void validate() { + this.getPlace().addTokens(this.getWeight()); + } + +} \ No newline at end of file diff --git a/src/main/java/org/petriNet/PetriNet.java b/src/main/java/org/petriNet/PetriNet.java new file mode 100644 index 0000000000000000000000000000000000000000..586b5f272ba39ac2e88f0f4544ca4a09d32284e2 --- /dev/null +++ b/src/main/java/org/petriNet/PetriNet.java @@ -0,0 +1,219 @@ +package org.petriNet; + +import java.util.*; + +public class PetriNet implements PetriNetService { + + // Create attributes as Lists of Places, Transitions, and Arcs + private final LinkedList<Integer> idCounters; + private List<Place> places; + private List<Transition> transitions; + private LinkedList<Arc> arcs; + private String networkState = "No transition fired"; + + // Create a constructor + public PetriNet() { + // Initialize the lists as empty + this.places = new ArrayList<>(); + this.transitions = new ArrayList<>(); + this.arcs = new LinkedList<>(); + idCounters = new LinkedList<>(); + + // Initialize the id counters for Arcs, Places, and Transitions + idCounters.add(0); + idCounters.add(0); + idCounters.add(0); + } + + // Create a method to generate unique ids + public int generateId(int index) { + int id = idCounters.get(index); + idCounters.set(index, id + 1); + return id; + } + + // Create getters and setters + public List<Place> getPlaces() { + return places; + } + + public void setPlaces(List<Place> places) { + this.places = places; + } + + public List<Transition> getTransitions() { + return transitions; + } + + public void setTransitions(List<Transition> transitions) { + this.transitions = transitions; + } + + public LinkedList<Arc> getArcs() { + return this.arcs; + } + + public void setArcs(LinkedList<Arc> arcs) { + this.arcs = arcs; + } + + @Override + public void addPlace(Place place) { + // Verify that there is no similar place in the list + for (Place place1 : this.places) { + if (place1.getId() == place.getId()) { + System.out.println("Place already exists"); + return; + } + } + this.places.add(place); + } + + @Override + public void addTransition(Transition transition) { + // Verify that there is no similar transition in the list + for (Transition transition1 : this.transitions) { + if (transition1.getId() == transition.getId()) { + System.out.println("Transition already exists"); + return; + } + } + this.transitions.add(transition); + } + + @Override + public void addArc(Arc arc) { + // Verify that there is no similar arc in the list + for (Arc arc1 : this.arcs) { + if (arc1.getPlace().getId() == arc.getPlace().getId() && + arc1.getTransition().getId() == arc.getTransition().getId() && + arc1.getClass() == arc.getClass()) { + System.out.println("Arc already exists"); + return; + } + } + this.arcs.add(arc); + } + + /** + * Removing a place results in the removal of all arcs linked to it. + * Removing a transition results in the removal of all arcs linked to it. + */ + @Override + public void removePlace(Place place) { + this.places.remove(place); + // Remove all arcs linked to the place + this.arcs.removeIf(arc -> arc.getPlace().getId() == place.getId()); + } + + @Override + public void removeTransition(Transition transition) { + this.transitions.remove(transition); + // Remove all arcs linked to the transition + this.arcs.removeIf(arc -> arc.getTransition().getId() == transition.getId()); + } + + @Override + public void removeArc(Arc arc) { + this.arcs.remove(arc); + // Remove it from the list of arcs of the transition + if (arc instanceof IncomingArc) { + arc.getTransition().getIncomingArcs().remove(arc); + } else if (arc instanceof OutgoingArc) { + arc.getTransition().getOutgoingArcs().remove(arc); + } + } + + @Override + public void fireTransition(String id) { + // Set the state of the network to "Transition being validated" + this.networkState = "Transition being validated"; + + // Find the transition with the id + Transition chosenTransition = null; + for (Transition transition : this.transitions) { + if (String.valueOf(transition.getId()).equals(id)) { + chosenTransition = transition; + } + } + + // Check if the transition id is valid + if (chosenTransition == null) { + System.out.println("The transition id is not valid"); + return; + } else { + chosenTransition.fire(); + } + + // Set the state of the network to "No transition fired" + this.networkState = "No transition fired"; + } + + @Override + public void displayState() { + if (this.networkState.equals("No transition fired")) { + System.out.println("No transition fired"); + // Ask the user if they want to fire a transition + Scanner scanner = new Scanner(System.in); + System.out.println("Do you want to fire a transition? (Y/N)"); + String response = scanner.nextLine(); + if (response.equals("Y")) { + // Show the Petri Net + displayNetwork(); + // Ask the user for the id of the transition to fire + System.out.println("Enter the id of the transition to fire: "); + String id = scanner.nextLine(); + fireTransition(id); + } + } else { + System.out.println("Transition being validated"); + } + } + + @Override + public void displayNetwork() { + System.out.println("Petri Net"); + System.out.println(this.places.size() + " places"); + System.out.println(this.transitions.size() + " transitions"); + System.out.println(this.arcs.size() + " arcs"); + + System.out.println("List of places:"); + // Iterate through the list of arcs to display the places + // A list for incoming arcs and a list for outgoing arcs + for (Place place : this.places) { + List<Arc> incomingArcs = new ArrayList<>(); + List<Arc> outgoingArcs = new ArrayList<>(); + for (Arc arc : this.arcs) { + // Verify if arc.getPlace() == place + if (arc.getPlace().getId() == place.getId()) { + // Verify if it's an incoming arc + if (arc instanceof IncomingArc) { + incomingArcs.add(arc); + } else if (arc instanceof OutgoingArc) { + outgoingArcs.add(arc); + } + } + } + // Display incoming/outgoing arcs for the place + System.out.println(place.getId() + " : place with " + place.getTokenCount() + " tokens, " + + incomingArcs.size() + " outgoing arc(s), " + outgoingArcs.size() + " incoming arc(s)"); + } + + System.out.println("List of transitions:"); + for (Transition transition : this.transitions) { + System.out.println(transition.getId() + " : transition " + transition.getName() + " " + transition.getIncomingArcs().size() + + " incoming arc(s), " + transition.getOutgoingArcs().size() + " outgoing arc(s)"); + } + + System.out.println("List of arcs:"); + for (Arc arc : this.arcs) { + if (arc instanceof IncomingArc) { + System.out.println(arc.getId() + " : simple arc with weight " + arc.getWeight() + " (" + + "Place with Id " + arc.getPlace().getId() + " to " + arc.getTransition().getName() + ")"); + } else { + System.out.println(arc.getId() + " : simple arc with weight " + arc.getWeight() + " (" + + arc.getTransition().getName() + " to " + "Place with Id " + arc.getPlace().getId() + ")"); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/org/petriNet/PetriNetService.java b/src/main/java/org/petriNet/PetriNetService.java index 918fda7dfc6767fc3d8f11509cf2997ff2e46f2b..8a5c5aa84dbf09d9452fd4214ee95fdbeaf16547 100644 --- a/src/main/java/org/petriNet/PetriNetService.java +++ b/src/main/java/org/petriNet/PetriNetService.java @@ -1,28 +1,27 @@ package org.petriNet; public interface PetriNetService { - - public void ajouterPlace(Place place); - - public void ajouterTransition(Transition transition); - - public void ajouterArc(Arc arc); - public void supprimerPlace(Place place); + void addPlace(Place place); - public void supprimerTransition(Transition transition); + void addTransition(Transition transition); - public void supprimerArc(Arc arc); - - public void afficherEtat(); + void addArc(Arc arc); + + void removePlace(Place place); + + void removeTransition(Transition transition); + + void removeArc(Arc arc); + + void displayState(); /** - * afficher le réseau de petri - * On a trouver que cette fonction et utile pour visualiser - * le réseau de petri + * Display the Petri net + * We found that this function is useful for visualizing + * the Petri net */ - public void afficherReseau(); - - public void tirer_transition(String id); + void displayNetwork(); + void fireTransition(String id); } diff --git a/src/main/java/org/petriNet/Place.java b/src/main/java/org/petriNet/Place.java index 68d990b888cc5129ea44e79a734ca7e61f1493b7..bed9249f553b0420768593e7e93ae2832c47aa93 100644 --- a/src/main/java/org/petriNet/Place.java +++ b/src/main/java/org/petriNet/Place.java @@ -2,54 +2,49 @@ package org.petriNet; public class Place { - private int id; - private int nombre_jeton; + private final int id; + private int tokenCount; - public Place(int nombre_jeton, int id) { + public Place(int tokenCount, int id) { this.id = id; // VERIFY THAT THE NUMBER OF TOKENS IS NOT NEGATIVE - if (nombre_jeton >= 0) { - this.nombre_jeton = nombre_jeton; + if (tokenCount >= 0) { + this.tokenCount = tokenCount; } else { System.out.println("The number of tokens cannot be negative."); } } - public void ajouter_jeton(int jetons) { + public void addTokens(int tokens) { // verify that the number of tokens is not negative - if (jetons >= 0) { - this.nombre_jeton += jetons; + if (tokens >= 0) { + this.tokenCount += tokens; } else { System.out.println("The number of tokens cannot be negative."); } } - public void enlever_jeton(int jetons) { + public void removeTokens(int tokens) { // verify that the number of tokens is not negative - if (jetons >= 0) { - this.nombre_jeton = Math.max(0, this.nombre_jeton - jetons); - + if (tokens >= 0) { + this.tokenCount = Math.max(0, this.tokenCount - tokens); } else { System.out.println("The number of tokens cannot be negative."); } } - public int get_nombre_jetons() { - return this.nombre_jeton; + public int getTokenCount() { + return this.tokenCount; } public int getId() { return id; } - public void setId(int id) { - this.id = id; - } - - public void setNombre_jeton(int nombre_jeton) { - this.nombre_jeton = nombre_jeton; + public void setTokenCount(int tokenCount) { + this.tokenCount = tokenCount; } - // Une fonction pour trouver les arcs entrants d'une place + // A function to find the incoming arcs of a place } diff --git a/src/main/java/org/petriNet/ReseauPetri.java b/src/main/java/org/petriNet/ReseauPetri.java deleted file mode 100644 index 5da57000402b648c9ac49400607f3d8103a9a61c..0000000000000000000000000000000000000000 --- a/src/main/java/org/petriNet/ReseauPetri.java +++ /dev/null @@ -1,251 +0,0 @@ -package org.petriNet; - -import java.util.*; - -public class ReseauPetri implements PetriNetService { - - // Creat attributes as Lists of Places, Transitions and Arcs - - private LinkedList<Integer> Id_couters; - private List<Place> places; - private List<Transition> transitions; - // create a LinkedHashSet to store the arcs in order to have unique ids - private LinkedList<Arc> arcs; - private String etat_reseau = "Pas de transition tirée"; - - // Create a constructor - public ReseauPetri() { - // initialiser les listes vide - this.places = new ArrayList<>(); - this.transitions = new ArrayList<>(); - this.arcs = new LinkedList<>(); - Id_couters = new LinkedList<>(); - - // initialiser les compteurs des ids pour Arcs, Places et Transitions - Id_couters.add(0); - Id_couters.add(0); - Id_couters.add(0); - } - - // Create a method to generate unique ids - public int generateId(int index) { - int id = Id_couters.get(index); - Id_couters.set(index, id + 1); - return id; - } - - // Create getters and setters - public List<Place> getPlaces() { - return places; - } - - public void setPlaces(List<Place> places) { - this.places = places; - } - - public List<Transition> getTransitions() { - return transitions; - } - - public void setTransitions(List<Transition> transitions) { - this.transitions = transitions; - } - - public LinkedList<Arc> getArcs() { - return this.arcs; - } - - public void setArcs(LinkedList arcs) { - this.arcs = arcs; - } - - - @Override - public void ajouterPlace(Place place) { - // TODO Auto-generated method stub - // verify that there is no similar place in the list - for (Place place1 : this.places) { - if (place1.getId() == place.getId()) { - System.out.println("Place already exists"); - return; - } - } - this.places.add(place); - } - - @Override - public void ajouterTransition(Transition transition) { - // TODO Auto-generated method stub - // verify that there is no similar transition in the list - for (Transition transition1 : this.transitions) { - if (transition1.getId() == transition.getId()) { - System.out.println("Transition already exists"); - return; - } - } - this.transitions.add(transition); - } - - @Override - public void ajouterArc(Arc arc) { - // TODO Auto-generated method stub - // verify that there is no similar arc in the list - for (Arc arc1 : this.arcs) { - if (arc1.getPlace().getId() == arc.getPlace().getId() && - arc1.getTransition().getId() == arc.getTransition().getId() && - arc1.getClass() == arc.getClass()) { - - System.out.println("Arc already exists"); - return; - } - } - this.arcs.add(arc); - } - - /** - * Supprimer une place entraîne la suppression de tous les arcs qui y sont liés. - * Supprimer une transition entraîne la suppression de tous les arcs qui y sont liés. - */ - - @Override - public void supprimerPlace(Place place) { - this.places.remove(place); - // remove all arcs linked to the place - this.arcs.removeIf(arc -> arc.getPlace().getId() == place.getId()); - } - - @Override - public void supprimerTransition(Transition transition) { - this.transitions.remove(transition); - // remove all arcs linked to the transition - this.arcs.removeIf(arc -> arc.getTransition().getId() == transition.getId()); - } - - @Override - public void supprimerArc(Arc arc) { - this.arcs.remove(arc); - // remove it from the list of arcs of the transition - if (arc instanceof Arc_ENTRANT) { - arc.getTransition().getArcs_ENTRANTS().remove(arc); - } else if (arc instanceof Arc_SORTANT) { - arc.getTransition().getArcs_SORTANTS().remove(arc); - } - } - - @Override - public void tirer_transition(String id) { - - // set the state of the network to "Transition en cours de verification" - this.etat_reseau = "Transition en cours de validation"; - - // Find the transition with the id - Transition transition_choisie = null; - for (Transition transition : this.transitions) { - if (String.valueOf(transition.getId()).equals(id)) { - transition_choisie = transition; - } - } - - // Check if the transition id is valid - if (transition_choisie == null) { - System.out.println("L'id de la transition n'est pas valide"); - return; - } else { - transition_choisie.tirer(); - } - - // set the state of the network to "Pas de transition tirée" - this.etat_reseau = "Pas de transition tirée"; - - } - - - @Override - public void afficherEtat() { - // TODO Auto-generated method stub - if (this.etat_reseau.equals("Pas de transition tirée")) { - System.out.println("Pas de transition tirée"); - // Ask the user if they want to fire a transition - Scanner scanner = new Scanner(System.in); - System.out.println("Voulez-vous tirer une transition? (O/N)"); - String reponse = scanner.nextLine(); - if (reponse.equals("O")) { - // Show the Petri Net - afficherReseau(); - // Ask the user for the id of the transition to fire - System.out.println("Saisir l'id de la transition à tirer: "); - String id = scanner.nextLine(); - tirer_transition(id); - } - } else { - System.out.println("Transition en cours de validation"); - } - } - - - @Override - public void afficherReseau() { - // TODO Auto-generated method stub - - /** - * L'affichage est sous la forme suivante: - * Réseau de Petri - * 2 places - * 1 transition - * 2 arcs - * Liste des places : - * 1 : place avec 4 jetons, 1 arc simple sortant, 0 arc simple entrant - * 2 : place avec 0 jetons, 0 arc simple sortant, 1 arc simple entrant - * Liste des transitions - * 1 : transition, 1 arc entrant, 1 arc sortant - * Liste des arcs : - * 1 : arc simple poids 1 (place avec 4 jetons vers transition) - * 2 : arc simple poids 1 (transition vers place avec 0 jetons) - */ - - System.out.println("Réseau de Petri"); - System.out.println(this.places.size() + " places"); - System.out.println(this.transitions.size() + " transitions"); - System.out.println(this.arcs.size() + " arcs"); - - System.out.println("Liste des places :"); - // On parcourt la liste des arcs pour afficher les places - // une liste pur les arcs entrants et une liste pour les arcs sortants - for (Place place : this.places) { - List<Arc> arcs_ENTRANTS = new ArrayList<>(); - List<Arc> arcs_SORTANTS = new ArrayList<>(); - for (Arc arc : this.arcs) { - // we verify getting arc.getPlace() == place - if (arc.getPlace().getId() == place.getId()) { - // we verify if it's an arc entrant - if (arc instanceof Arc_ENTRANT) { - arcs_ENTRANTS.add(arc); - } else if (arc instanceof Arc_SORTANT) { - arcs_SORTANTS.add(arc); - } - } - } - // entrants / sortants à la place - System.out.println(place.getId() + " : place avec " + place.get_nombre_jetons() + " jetons, " - + arcs_ENTRANTS.size() + " arc simple sortant, " + arcs_SORTANTS.size() + " arc simple entrant"); - } - - System.out.println("Liste des transitions :"); - for (Transition transition : this.transitions) { - System.out.println(transition.getId() + " : transition " + transition.getNom() + " " + transition.getArcs_ENTRANTS().size() - + " arc entrant, " + transition.getArcs_SORTANTS().size() + " arc sortant"); - } - - System.out.println("Liste des arcs :"); - for (Arc arc : this.arcs) { - if (arc instanceof Arc_ENTRANT) { - System.out.println(arc.getId() + " : arc simple poids " + arc.getPoids() + " (" - + "Place d'Id " + arc.getPlace().getId() + " vers " + arc.getTransition().getNom() + ")"); - } else { - System.out.println(arc.getId() + " : arc simple poids " + arc.getPoids() + " (" - + arc.getTransition().getNom() + " vers " + "Place d'Id " + arc.getPlace().getId() + ")"); - } - } - } - -} diff --git a/src/main/java/org/petriNet/Transition.java b/src/main/java/org/petriNet/Transition.java index 0cb06754917312b8d1cc181469d8e382b0356a5e..6a634ea4f06c9431c2cf011c1fee90065f191ef8 100644 --- a/src/main/java/org/petriNet/Transition.java +++ b/src/main/java/org/petriNet/Transition.java @@ -5,102 +5,92 @@ import java.util.List; public class Transition { - private int id; - String nom; - private List<Arc_SORTANT> arcs_SORTANTS; - private List<Arc_ENTRANT> arcs_ENTRANTS; + private final int id; + String name; + private List<OutgoingArc> outgoingArcs; + private final List<IncomingArc> incomingArcs; - public Transition(String nom, int id) { + public Transition(String name, int id) { this.id = id; - this.nom = nom; - this.arcs_SORTANTS = new ArrayList<Arc_SORTANT>(); - this.arcs_ENTRANTS = new ArrayList<Arc_ENTRANT>(); + this.name = name; + this.outgoingArcs = new ArrayList<>(); + this.incomingArcs = new ArrayList<>(); } /** - * On ajoute deux méthodes pour pouvoir ajouter les arcs entrants et - * sortants à la transition - * c'est une différence par rapport au diagramme de classe soumis + * We add two methods to be able to add incoming and outgoing arcs to the transition. + * This is a difference from the submitted class diagram. */ - public String getNom() { - return nom; + public String getName() { + return name; } public int getId() { return id; } - public void setId(int id) { - this.id = id; - } - - public List<Arc_SORTANT> getArcs_SORTANTS() { - return arcs_SORTANTS; + public List<OutgoingArc> getOutgoingArcs() { + return outgoingArcs; } - public List<Arc_ENTRANT> getArcs_ENTRANTS() { - return arcs_ENTRANTS; + public List<IncomingArc> getIncomingArcs() { + return incomingArcs; } - public void setArcs_SORTANTS(List<Arc_SORTANT> arcs_SORTANTS) { - this.arcs_SORTANTS = arcs_SORTANTS; + public void setOutgoingArcs(List<OutgoingArc> outgoingArcs) { + this.outgoingArcs = outgoingArcs; } - public void ajouterArc_SORTANT(Arc_SORTANT arc_SORTANT) { + public void addOutgoingArc(OutgoingArc outgoingArc) { // verify that the arc doesn't already exist - for (Arc_SORTANT arc_SORTANT1 : arcs_SORTANTS) { - if (arc_SORTANT1.getPlace().getId() == arc_SORTANT.getPlace().getId() && - arc_SORTANT1.getTransition().getId() == arc_SORTANT.getTransition().getId() && - arc_SORTANT1.getClass() == arc_SORTANT.getClass()) { + for (OutgoingArc existingArc : outgoingArcs) { + if (existingArc.getPlace().getId() == outgoingArc.getPlace().getId() && + existingArc.getTransition().getId() == outgoingArc.getTransition().getId() && + existingArc.getClass() == outgoingArc.getClass()) { System.out.println("The arc already exists."); return; } } - this.arcs_SORTANTS.add(arc_SORTANT); + this.outgoingArcs.add(outgoingArc); } - public void ajouterArc_ENTRANT(Arc_ENTRANT arc_ENTRANT) { + public void addIncomingArc(IncomingArc incomingArc) { // verify that the arc doesn't already exist - for (Arc_ENTRANT arc_ENTRANT1 : arcs_ENTRANTS) { - if (arc_ENTRANT1.getPlace().getId() == arc_ENTRANT.getPlace().getId() && - arc_ENTRANT1.getTransition().getId() == arc_ENTRANT.getTransition().getId() && - arc_ENTRANT1.getClass() == arc_ENTRANT.getClass()) { + for (IncomingArc existingArc : incomingArcs) { + if (existingArc.getPlace().getId() == incomingArc.getPlace().getId() && + existingArc.getTransition().getId() == incomingArc.getTransition().getId() && + existingArc.getClass() == incomingArc.getClass()) { System.out.println("The arc already exists."); return; } } - this.arcs_ENTRANTS.add(arc_ENTRANT); + this.incomingArcs.add(incomingArc); } - - public boolean est_tirable() { - - for (Arc_ENTRANT arc_ENTRANT : this.arcs_ENTRANTS) { - if (!arc_ENTRANT.verifier_tirable()) { - System.out.println("La transition n'est pas tirable"); + public boolean canFire() { + for (IncomingArc incomingArc : this.incomingArcs) { + if (!incomingArc.canFire()) { + System.out.println("The transition cannot fire"); return false; } } - System.out.println("La transition est tirable"); + System.out.println("The transition can fire"); return true; - } - public void tirer(){ + public void fire() { + boolean canFire = canFire(); - boolean tirable = est_tirable(); - - if (tirable) { - for (Arc_ENTRANT arc_ENTRANT : this.getArcs_ENTRANTS()) { - arc_ENTRANT.validate(); + if (canFire) { + for (IncomingArc incomingArc : this.getIncomingArcs()) { + incomingArc.validate(); } - for (Arc_SORTANT arc_SORTANT : this.getArcs_SORTANTS()) { - arc_SORTANT.validate(); + for (OutgoingArc outgoingArc : this.getOutgoingArcs()) { + outgoingArc.validate(); } - System.out.println("Transition tirée avec succès"); + System.out.println("Transition fired successfully"); } - } } diff --git a/src/test/java/org/petriNet/PetriNetTest.java b/src/test/java/org/petriNet/PetriNetTest.java new file mode 100644 index 0000000000000000000000000000000000000000..246d302e2ffe5d0c24c79c54da26979a2cf57c29 --- /dev/null +++ b/src/test/java/org/petriNet/PetriNetTest.java @@ -0,0 +1,695 @@ +package org.petriNet; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class PetriNetTest { + + private PetriNet reseauPetri; + + @BeforeEach + public void setUp() { + reseauPetri = new PetriNet(); + } + + @Test + public void testAddToken() { + Place place = new Place(2, reseauPetri.generateId(1)); + place.addTokens(-2); + place.addTokens(2); + assertEquals(4, place.getTokenCount(), "CAJ0, CAJ1"); + } + + @Test + public void testRemoveToken() { + Place place = new Place(2, reseauPetri.generateId(1)); + place.removeTokens(-2); + place.removeTokens(2); + assertEquals(0, place.getTokenCount(), "CEJ0, CEJ1"); + } + + @Test + public void testAddPlace() { + Place place = new Place(2, reseauPetri.generateId(1)); + reseauPetri.addPlace(place); + + // A test for an existing place + reseauPetri.addPlace(place); + assertEquals(1, reseauPetri.getPlaces().size(), "CAP0, CAP1"); + } + + @Test + public void testAddTransition() { + Transition transition = new Transition("transition", reseauPetri.generateId(2)); + reseauPetri.addTransition(transition); + // A test for an existing transition + reseauPetri.addTransition(transition); + assertEquals(1, reseauPetri.getTransitions().size(), "CAT0, CAT1"); + } + + @Test + public void testAddArc() { + Place place = new Place(2, reseauPetri.generateId(1)); + Transition transition = new Transition("transition", reseauPetri.generateId(2)); + OutgoingArc arc_0 = new OutgoingArc(transition, place, 1, reseauPetri.generateId(0)); + + // A test for a negative weight + OutgoingArc arc_1 = new OutgoingArc(transition, place, -1, reseauPetri.generateId(0)); + reseauPetri.addArc(arc_0); + + // A test for an existing arc + reseauPetri.addArc(arc_0); + assertEquals(1, reseauPetri.getArcs().size(), "CAA0, CAA1"); + } + + @Test + @DisplayName("Assemble Petri") + public void testAssemblePetri(){ + + // CR1 + PetriNet Mutex = new PetriNet(); + Arc arc; + + // CP1 + Place P1 = new Place(0, Mutex.generateId(1)); + Mutex.addPlace(P1); + Mutex.addPlace(P1); + assertEquals(1, Mutex.getPlaces().size(), "CP1") ; + + // CT1 + Transition T1 = new Transition("T1", Mutex.generateId(2)); + Mutex.addTransition(T1); + Mutex.addTransition(T1); + assertEquals(1, Mutex.getTransitions().size(), "CT1"); + + // CP2 + Place P2 = new Place(1, Mutex.generateId(1)); + Mutex.addPlace(P2); + assertEquals(2, Mutex.getPlaces().size(), "CP2"); + + // CT2 + Transition T2 = new Transition("T2", Mutex.generateId(2)); + Mutex.addTransition(T2); + assertEquals(2, Mutex.getTransitions().size(), "CT2"); + + // CP5 + Place P3 = new Place(0, Mutex.generateId(1)); + Mutex.addPlace(P3); + assertEquals(3, Mutex.getPlaces().size(), "CP5"); + + // CT3 + Transition T3 = new Transition("T3", Mutex.generateId(2)); + Mutex.addTransition(T3); + assertEquals(3, Mutex.getTransitions().size(), "CT3"); + + // CP4 + Place P4 = new Place(0, Mutex.generateId(1)); + Mutex.addPlace(P4); + assertEquals(4, Mutex.getPlaces().size(), "CP4"); + + // CT4 + Transition T4 = new Transition("T4", Mutex.generateId(2)); + Mutex.addTransition(T4); + assertEquals(4, Mutex.getTransitions().size(), "CT4"); + + // CP5 + Place P5 = new Place(1, Mutex.generateId(1)); + Mutex.addPlace(P5); + assertEquals(5, Mutex.getPlaces().size(), "CP5"); + + // CPT1 + // Create the simple incoming arc to T1 from P1 + arc = new IncomingArc_Simple(T1, P1, 1, Mutex.generateId(0)); + Mutex.addArc(arc); + T1.addIncomingArc((IncomingArc) arc); + T1.addIncomingArc((IncomingArc) arc); + assertEquals(1, T1.getIncomingArcs().size(), "CPT1"); + + // CTP2 + // Create the simple outgoing arc from T2 to P1 + arc = new OutgoingArc(T2, P1, 1, Mutex.generateId(0)); + Mutex.addArc(arc); + T2.addOutgoingArc((OutgoingArc) arc); + assertEquals(1, T2.getOutgoingArcs().size(), "CTP2"); + + // CTP1 + // Create the simple outgoing arc from T1 to P2 + arc = new OutgoingArc(T1, P2, 1, Mutex.generateId(0)); + Mutex.addArc(arc); + T1.addOutgoingArc((OutgoingArc) arc); + assertEquals(1, T1.getOutgoingArcs().size(), "CTP1"); + + // CPT2 + // Create the simple incoming arc to T2 from P2 + arc = new IncomingArc_Simple(T2, P2, 1, Mutex.generateId(0)); + Mutex.addArc(arc); + T2.addIncomingArc((IncomingArc) arc); + assertEquals(1, T2.getIncomingArcs().size(), "CPT2"); + + // CPT5 + // Create the simple incoming arc to T1 from P3 + arc = new IncomingArc_Simple(T1, P3, 1, Mutex.generateId(0)); + Mutex.addArc(arc); + T1.addIncomingArc((IncomingArc) arc); + assertEquals(2, T1.getIncomingArcs().size(), "CPT5"); + + // CTP5 + // Create the simple outgoing arc from T2 to P3 + arc = new OutgoingArc(T2, P3, 1, Mutex.generateId(0)); + Mutex.addArc(arc); + T2.addOutgoingArc((OutgoingArc) arc); + assertEquals(2, T2.getOutgoingArcs().size(), "CTP5"); + + // CPT6 + // Create the simple incoming arc to T3 from P3 + arc = new IncomingArc_Simple(T3, P3, 1, Mutex.generateId(0)); + Mutex.addArc(arc); + T3.addIncomingArc((IncomingArc) arc); + assertEquals(1, T3.getIncomingArcs().size(), "CPT6"); + + // CTP6 + // Create the simple outgoing arc from T4 to P3 + arc = new OutgoingArc(T4, P3, 1, Mutex.generateId(0)); + Mutex.addArc(arc); + T4.addOutgoingArc((OutgoingArc) arc); + assertEquals(1, T4.getOutgoingArcs().size(), "CTP6"); + + // CTP3 + // Create the simple outgoing arc from T3 to P4 + arc = new OutgoingArc(T3, P4, 1, Mutex.generateId(0)); + Mutex.addArc(arc); + T3.addOutgoingArc((OutgoingArc) arc); + assertEquals(1, T3.getOutgoingArcs().size(), "CTP3"); + + // CPT4 + // Create the simple incoming arc to T4 from P4 + arc = new IncomingArc_Simple(T4, P4, 1, Mutex.generateId(0)); + Mutex.addArc(arc); + T4.addIncomingArc((IncomingArc) arc); + assertEquals(1, T4.getIncomingArcs().size(), "CPT4"); + + // CPT3 + // Create the simple incoming arc to T3 from P5 + arc = new IncomingArc_Simple(T3, P5, 1, Mutex.generateId(0)); + Mutex.addArc(arc); + T3.addIncomingArc((IncomingArc) arc); + assertEquals(2, T3.getIncomingArcs().size(), "CPT3"); + + // CTP4 + // Create the simple outgoing arc from T4 to P5 + arc = new OutgoingArc(T4, P5, 1, Mutex.generateId(0)); + Mutex.addArc(arc); + T4.addOutgoingArc((OutgoingArc) arc); + assertEquals(2, T4.getOutgoingArcs().size(), "CTP4"); + + assertEquals(12, Mutex.getArcs().size(), "There should be 12 unique arcs in Mutex"); + assertEquals(5, Mutex.getPlaces().size(), "There should be 5 unique places in Mutex"); + assertEquals(4, Mutex.getTransitions().size(), "There should be 4 unique transitions in Mutex"); + + Mutex.displayNetwork(); + + } + + @Test + @DisplayName("RI") + public void testActivatePetri_1() { + PetriNet Active = new PetriNet(); + + // Active has only one Transition + Transition T1 = new Transition("T1", Active.generateId(2)); + Active.addTransition(T1); + + // Fire T1 + Active.fireTransition(String.valueOf(T1.getId())); + Active.displayNetwork(); + + assertEquals(1, Active.getTransitions().size(), "RI"); + } + + @Test + @DisplayName("RD0") + public void testActivatePetri_2() { + PetriNet active = new PetriNet(); + + Transition t1 = new Transition("T1", active.generateId(2)); + + // Active has only one Place with 0 tokens + Place p1 = new Place(0, active.generateId(1)); + active.addPlace(p1); + + // Create the simple incoming arc to T1 from P1 + IncomingArc arc = new IncomingArc_Simple(t1, p1, 1, active.generateId(0)); + active.addArc(arc); + + t1.addIncomingArc(arc); + active.addTransition(t1); + + // Fire T1 + active.fireTransition(String.valueOf(t1.getId())); + active.displayNetwork(); + + assertEquals(0, p1.getTokenCount(), "RD0"); + } + + @Test + @DisplayName("RD1") + public void testActivatePetri_3() { + PetriNet active = new PetriNet(); + + Transition t1 = new Transition("T1", active.generateId(2)); + + // Active has only one Place with 2 tokens + Place p1 = new Place(2, active.generateId(1)); + active.addPlace(p1); + + // Create the simple incoming arc to T1 from P1 + IncomingArc arc = new IncomingArc_Simple(t1, p1, 1, active.generateId(0)); + active.addArc(arc); + + t1.addIncomingArc(arc); + active.addTransition(t1); + + // Fire T1 + active.fireTransition(String.valueOf(t1.getId())); + active.displayNetwork(); + + assertEquals(1, p1.getTokenCount(), "RD1"); + } + + @Test + @DisplayName("RD2") + public void testActivatePetri_4() { + PetriNet active = new PetriNet(); + + Transition t1 = new Transition("T1", active.generateId(2)); + + // Active has only one Place with 5 tokens + Place p1 = new Place(5, active.generateId(1)); + active.addPlace(p1); + + // Create the simple incoming arc to T1 from P1 + IncomingArc arc = new IncomingArc_Simple(t1, p1, 3, active.generateId(0)); + active.addArc(arc); + + t1.addIncomingArc(arc); + active.addTransition(t1); + + // Fire T1 + active.fireTransition(String.valueOf(t1.getId())); + active.displayNetwork(); + + assertEquals(2, p1.getTokenCount(), "RD2"); + } + + @Test + @DisplayName("RG0") + public void testActivatePetri_5() { + PetriNet active = new PetriNet(); + + Transition t1 = new Transition("T1", active.generateId(2)); + + // Active has one Place with 1 token + Place p1 = new Place(1, active.generateId(1)); + active.addPlace(p1); + + // Create the outgoing arc from T1 to P1 + OutgoingArc arc = new OutgoingArc(t1, p1, 1, active.generateId(0)); + active.addArc(arc); + + t1.addOutgoingArc(arc); + active.addTransition(t1); + + // Fire T1 + active.fireTransition(String.valueOf(t1.getId())); + active.displayNetwork(); + + assertEquals(2, p1.getTokenCount(), "RG0"); + } + + @Test + @DisplayName("RM0") + public void testActivatePetri_6() { + PetriNet active = new PetriNet(); + + Transition t1 = new Transition("T1", active.generateId(2)); + + // Active has two Places with 0 and 2 tokens + Place p1 = new Place(0, active.generateId(1)); + Place p2 = new Place(2, active.generateId(1)); + active.addPlace(p1); + active.addPlace(p2); + + // Create the simple incoming arc to T1 from P1 + IncomingArc arc = new IncomingArc_Simple(t1, p1, 1, active.generateId(0)); + active.addArc(arc); + OutgoingArc arc1 = new OutgoingArc(t1, p2, 1, active.generateId(0)); + active.addArc(arc1); + + t1.addIncomingArc(arc); + t1.addOutgoingArc(arc1); + active.addTransition(t1); + + // Fire T1 + active.fireTransition(String.valueOf(t1.getId())); + active.displayNetwork(); + + assertEquals(0, p1.getTokenCount(), "RM0"); + assertEquals(2, p2.getTokenCount(), "RM0"); + } + + @Test + @DisplayName("RM1") + public void testActivatePetri_7() { + PetriNet active = new PetriNet(); + + Transition t1 = new Transition("T1", active.generateId(2)); + + // Active has two Places with 5 and 0 tokens + Place p1 = new Place(5, active.generateId(1)); + Place p2 = new Place(0, active.generateId(1)); + active.addPlace(p1); + active.addPlace(p2); + + // Create the simple incoming arc to T1 from P1 + IncomingArc arc = new IncomingArc_Simple(t1, p1, 3, active.generateId(0)); + active.addArc(arc); + OutgoingArc arc1 = new OutgoingArc(t1, p2, 1, active.generateId(0)); + active.addArc(arc1); + + t1.addIncomingArc(arc); + t1.addOutgoingArc(arc1); + active.addTransition(t1); + + // Fire T1 + active.fireTransition(String.valueOf(t1.getId())); + active.displayNetwork(); + + assertEquals(2, p1.getTokenCount(), "RM1"); + assertEquals(1, p2.getTokenCount(), "RM1"); + } + + @Test + @DisplayName("RGM") + public void testActivatePetri_8() { + PetriNet active = new PetriNet(); + + Transition t1 = new Transition("T1", active.generateId(2)); + + // Active has two Places with 2 and 1 tokens + Place p1 = new Place(2, active.generateId(1)); + Place p2 = new Place(1, active.generateId(1)); + active.addPlace(p1); + active.addPlace(p2); + + // Create the simple incoming arc to T1 from P1 and P2 + IncomingArc arc = new IncomingArc_Simple(t1, p1, 1, active.generateId(0)); + active.addArc(arc); + IncomingArc arc1 = new IncomingArc_Simple(t1, p2, 1, active.generateId(0)); + active.addArc(arc1); + + t1.addIncomingArc(arc); + t1.addIncomingArc(arc1); + + active.addTransition(t1); + + // Fire T1 + active.fireTransition(String.valueOf(t1.getId())); + active.displayNetwork(); + + assertEquals(1, p1.getTokenCount(), "RGM"); + assertEquals(0, p2.getTokenCount(), "RGM"); + } + + @Test + @DisplayName("RDM") + public void testActivatePetri_9() { + PetriNet active = new PetriNet(); + + Transition t1 = new Transition("T1", active.generateId(2)); + + // Active has two Places with 0 and 1 tokens + Place p1 = new Place(0, active.generateId(1)); + Place p2 = new Place(1, active.generateId(1)); + active.addPlace(p1); + active.addPlace(p2); + + // Create the outgoing arc from T1 to P1 and P2 + OutgoingArc arc = new OutgoingArc(t1, p1, 1, active.generateId(0)); + active.addArc(arc); + OutgoingArc arc1 = new OutgoingArc(t1, p2, 1, active.generateId(0)); + active.addArc(arc1); + + t1.addOutgoingArc(arc); + t1.addOutgoingArc(arc1); + + active.addTransition(t1); + + // Fire T1 + active.fireTransition(String.valueOf(t1.getId())); + active.displayNetwork(); + + assertEquals(1, p1.getTokenCount(), "RDM"); + assertEquals(2, p2.getTokenCount(), "RDM"); + } + + @Test + @DisplayName("RDGM") + public void testActivatePetri_10() { + PetriNet active = new PetriNet(); + + Transition t1 = new Transition("T1", active.generateId(2)); + + // Active has 4 Places with 1 and 1 tokens and 0 and 0 tokens + Place p1 = new Place(1, active.generateId(1)); + Place p2 = new Place(1, active.generateId(1)); + Place p3 = new Place(0, active.generateId(1)); + Place p4 = new Place(0, active.generateId(1)); + active.addPlace(p1); + active.addPlace(p2); + active.addPlace(p3); + active.addPlace(p4); + + // Create the simple incoming arc to T1 from P1 and P2 + IncomingArc arc = new IncomingArc_Simple(t1, p1, 1, active.generateId(0)); + active.addArc(arc); + IncomingArc arc1 = new IncomingArc_Simple(t1, p2, 1, active.generateId(0)); + active.addArc(arc1); + + // Create the outgoing arc from T1 to P3 and P4 + OutgoingArc arc2 = new OutgoingArc(t1, p3, 1, active.generateId(0)); + active.addArc(arc2); + OutgoingArc arc3 = new OutgoingArc(t1, p4, 1, active.generateId(0)); + active.addArc(arc3); + + t1.addIncomingArc(arc); + t1.addIncomingArc(arc1); + t1.addOutgoingArc(arc2); + t1.addOutgoingArc(arc3); + + active.addTransition(t1); + + // Fire T1 + active.fireTransition(String.valueOf(t1.getId())); + active.displayNetwork(); + + assertEquals(0, p1.getTokenCount(), "RDGM"); + assertEquals(0, p2.getTokenCount(), "RDGM"); + assertEquals(1, p3.getTokenCount(), "RDGM"); + assertEquals(1, p4.getTokenCount(), "RDGM"); + } + + @Test + @DisplayName("ST0") + public void testRemoveTransition_0() { + PetriNet supp = new PetriNet(); + + Transition t1 = new Transition("T1", supp.generateId(2)); + supp.addTransition(t1); + supp.removeTransition(t1); + supp.displayNetwork(); + + assertEquals(0, supp.getTransitions().size(), "ST0"); + } + + @Test + @DisplayName("SP1") + public void testRemovePlace() { + PetriNet destruction = new PetriNet(); + + // Create a Petri net similar to Active in testActivatePetri_3 + Place p1 = new Place(2, destruction.generateId(1)); + destruction.addPlace(p1); + + Transition t1 = new Transition("T1", destruction.generateId(2)); + + // Create the simple incoming arc to T1 from P1 + IncomingArc arc = new IncomingArc_Simple(t1, p1, 1, destruction.generateId(0)); + destruction.addArc(arc); + + t1.addIncomingArc(arc); + destruction.addTransition(t1); + + // Remove place P1 + destruction.removePlace(p1); + destruction.displayNetwork(); + + assertEquals(0, destruction.getPlaces().size(), "SP1"); + assertEquals(1, destruction.getTransitions().size(), "SP1"); + assertEquals(0, destruction.getArcs().size(), "SP1"); + } + + @Test + @DisplayName("ST1") + public void testRemoveTransition_1() { + PetriNet destruction = new PetriNet(); + + // Create a Petri net similar to Active in testActivatePetri_3 + Place p1 = new Place(2, destruction.generateId(1)); + destruction.addPlace(p1); + + Transition t1 = new Transition("T1", destruction.generateId(2)); + + // Create the simple incoming arc to T1 from P1 + IncomingArc arc = new IncomingArc_Simple(t1, p1, 1, destruction.generateId(0)); + destruction.addArc(arc); + + t1.addIncomingArc(arc); + destruction.addTransition(t1); + + // Remove transition T1 + destruction.removeTransition(t1); + destruction.displayNetwork(); + + assertEquals(1, destruction.getPlaces().size(), "ST1"); + assertEquals(0, destruction.getTransitions().size(), "ST1"); + assertEquals(0, destruction.getArcs().size(), "ST1"); + } + + @Test + @DisplayName("SA1") + public void testRemoveArc() { + PetriNet destruction = new PetriNet(); + + // Create a Petri net similar to Active in testActivatePetri_3 + Place p1 = new Place(2, destruction.generateId(1)); + destruction.addPlace(p1); + + Transition t1 = new Transition("T1", destruction.generateId(2)); + + // Create the simple incoming arc to T1 from P1 + IncomingArc arc = new IncomingArc_Simple(t1, p1, 1, destruction.generateId(0)); + destruction.addArc(arc); + + t1.addIncomingArc(arc); + destruction.addTransition(t1); + + // Remove the arc + destruction.removeArc(arc); + destruction.displayNetwork(); + + assertEquals(1, destruction.getPlaces().size(), "SA1"); + assertEquals(1, destruction.getTransitions().size(), "SA1"); + assertEquals(0, destruction.getArcs().size(), "SA1"); + } + + @Test + @DisplayName("ARC ZERO") + public void testArcZero() { + // Initialize the Petri network + PetriNet petriNetwork = new PetriNet(); + + // Create places + Place place1 = new Place(3, petriNetwork.generateId(1)); + Place place2 = new Place(0, petriNetwork.generateId(1)); + + // Add places to the network + petriNetwork.addPlace(place1); + petriNetwork.addPlace(place2); + + // Create a transition + Transition transition1 = new Transition("T1", petriNetwork.generateId(2)); + + // Add the transition to the network + petriNetwork.addTransition(transition1); + + // Create arcs and add them to the network + // Incoming arc from place2 to transition1 with weight 1 + IncomingArc_Zero incomingArcZero = new IncomingArc_Zero(transition1, place2, 1, petriNetwork.generateId(0)); + petriNetwork.addArc(incomingArcZero); + transition1.addIncomingArc(incomingArcZero); + + // Outgoing arc from transition1 to place1 with weight 1 + OutgoingArc outgoingArc = new OutgoingArc(transition1, place1, 1, petriNetwork.generateId(0)); + petriNetwork.addArc(outgoingArc); + transition1.addOutgoingArc(outgoingArc); + + // Display the initial state of the Petri network + System.out.println("Initial State of Petri Network:"); + petriNetwork.displayNetwork(); + + // Activate the transition + System.out.println("\nActivating transition T1..."); + petriNetwork.fireTransition(String.valueOf(transition1.getId())); + + // Display the state of the Petri network after activation + System.out.println("\nState of Petri Network after Transition T1 Activation:"); + petriNetwork.displayNetwork(); + + // Assertions to verify the correct state of the Petri network + assertEquals(4, place1.getTokenCount(), "Tokens in Place 1 should be 4"); + assertEquals(0, place2.getTokenCount(), "Tokens in Place 2 should be 0"); + } + + @Test + @DisplayName("ARC Videur") + public void testArcVideur() { + // Initialize the Petri network + PetriNet petriNetwork = new PetriNet(); + + // Create places + Place place1 = new Place(2, petriNetwork.generateId(1)); + Place place2 = new Place(4, petriNetwork.generateId(1)); + + // Add places to the network + petriNetwork.addPlace(place1); + petriNetwork.addPlace(place2); + + // Create a transition + Transition transition1 = new Transition("T1", petriNetwork.generateId(2)); + + // Add the transition to the network + petriNetwork.addTransition(transition1); + + // Create arcs and add them to the network + // Incoming arc from place2 to transition1 with weight 1 + IncomingArc_Videur incomingArcVideur = new IncomingArc_Videur(transition1, place2, 1, petriNetwork.generateId(0)); + petriNetwork.addArc(incomingArcVideur); + transition1.addIncomingArc(incomingArcVideur); + + // Outgoing arc from transition1 to place1 with weight 1 + OutgoingArc outgoingArc = new OutgoingArc(transition1, place1, 1, petriNetwork.generateId(0)); + petriNetwork.addArc(outgoingArc); + transition1.addOutgoingArc(outgoingArc); + + // Display the initial state of the Petri network + System.out.println("Initial State of Petri Network:"); + petriNetwork.displayNetwork(); + + // Activate the transition + System.out.println("\nActivating transition T1..."); + petriNetwork.fireTransition(String.valueOf(transition1.getId())); + + // Display the state of the Petri network after activation + System.out.println("\nState of Petri Network after Transition T1 Activation:"); + petriNetwork.displayNetwork(); + + // Assertions to verify the correct state of the Petri network + assertEquals(3, place1.getTokenCount(), "Tokens in Place 1 should be 3"); + assertEquals(0, place2.getTokenCount(), "Tokens in Place 2 should be 0"); + } + +} + + diff --git a/src/test/java/org/petriNet/ReseauPetriTest.java b/src/test/java/org/petriNet/ReseauPetriTest.java deleted file mode 100644 index f5a8ac18ae790ad895e726c143fdcc7b11ae049b..0000000000000000000000000000000000000000 --- a/src/test/java/org/petriNet/ReseauPetriTest.java +++ /dev/null @@ -1,705 +0,0 @@ -package org.petriNet; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.LinkedList; -import java.util.List; -import static org.junit.jupiter.api.Assertions.*; - -public class ReseauPetriTest { - - private ReseauPetri reseauPetri; - private List<Place> places; - private List<Transition> transitions; - private LinkedList<Arc> arcs; - - @BeforeEach - public void setUp() { - reseauPetri = new ReseauPetri(); - } - - @Test - public void testAjouterJeton() { - Place place = new Place(2, reseauPetri.generateId(1)); - place.ajouter_jeton(-2); - place.ajouter_jeton(2); - assertEquals(4, place.get_nombre_jetons(), "CAJ0, CAJ1"); - } - - @Test - public void testEnleverJeton() { - Place place = new Place(2, reseauPetri.generateId(1)); - place.enlever_jeton(-2); - place.enlever_jeton(2); - assertEquals(0, place.get_nombre_jetons(), "CEJ0, CEJ1"); - } - - @Test - public void testAjouterPlace() { - Place place = new Place(2, reseauPetri.generateId(1)); - reseauPetri.ajouterPlace(place); - - // A test for an existing place - reseauPetri.ajouterPlace(place); - assertEquals(1, reseauPetri.getPlaces().size(), "CAP0, CAP1"); - } - - @Test - public void testAjouterTransition() { - Transition transition = new Transition("transition", reseauPetri.generateId(2)); - reseauPetri.ajouterTransition(transition); - // A test for an existing transition - reseauPetri.ajouterTransition(transition); - assertEquals(1, reseauPetri.getTransitions().size(), "CAT0, CAT1"); - } - - @Test - public void testAjouterArc() { - Place place = new Place(2, reseauPetri.generateId(1)); - Transition transition = new Transition("transition", reseauPetri.generateId(2)); - Arc_SORTANT arc_0 = new Arc_SORTANT(transition, place, 1, reseauPetri.generateId(0)); - - // A test for a negative weight - Arc_SORTANT arc_1 = new Arc_SORTANT(transition, place, -1, reseauPetri.generateId(0)); - reseauPetri.ajouterArc(arc_0); - - // A test for an existing arc - reseauPetri.ajouterArc(arc_0); - assertEquals(1, reseauPetri.getArcs().size(), "CAA0, CAA1"); - } - - @Test - @DisplayName("Assembler un réseau de petri") - public void testAssemblerPetri(){ - - // CR1 - ReseauPetri Mutex = new ReseauPetri(); - Arc arc; - - // CP1 - Place P1 = new Place(0, Mutex.generateId(1)); - Mutex.ajouterPlace(P1); - Mutex.ajouterPlace(P1); - assertEquals(1, Mutex.getPlaces().size(), "CP1") ; - - // CT1 - Transition T1 = new Transition("T1", Mutex.generateId(2)); - Mutex.ajouterTransition(T1); - Mutex.ajouterTransition(T1); - assertEquals(1, Mutex.getTransitions().size(), "CT1"); - - // CP2 - Place P2 = new Place(1, Mutex.generateId(1)); - Mutex.ajouterPlace(P2); - assertEquals(2, Mutex.getPlaces().size(), "CP2"); - - // CT2 - Transition T2 = new Transition("T2", Mutex.generateId(2)); - Mutex.ajouterTransition(T2); - assertEquals(2, Mutex.getTransitions().size(), "CT2"); - - // CP5 - Place P3 = new Place(0, Mutex.generateId(1)); - Mutex.ajouterPlace(P3); - assertEquals(3, Mutex.getPlaces().size(), "CP5"); - - // CT3 - Transition T3 = new Transition("T3", Mutex.generateId(2)); - Mutex.ajouterTransition(T3); - assertEquals(3, Mutex.getTransitions().size(), "CT3"); - - // CP4 - Place P4 = new Place(0, Mutex.generateId(1)); - Mutex.ajouterPlace(P4); - assertEquals(4, Mutex.getPlaces().size(), "CP4"); - - // CT4 - Transition T4 = new Transition("T4", Mutex.generateId(2)); - Mutex.ajouterTransition(T4); - assertEquals(4, Mutex.getTransitions().size(), "CT4"); - - // CP5 - Place P5 = new Place(1, Mutex.generateId(1)); - Mutex.ajouterPlace(P5); - assertEquals(5, Mutex.getPlaces().size(), "CP5"); - - // CPT1 - // Creer l'arc entrant simple à T1 de P1 - arc = new Arc_entrant_simple(T1, P1, 1, Mutex.generateId(0)); - Mutex.ajouterArc(arc); - T1.ajouterArc_ENTRANT((Arc_ENTRANT) arc); - T1.ajouterArc_ENTRANT((Arc_ENTRANT) arc); - assertEquals(1, T1.getArcs_ENTRANTS().size(), "CPT1"); - - // CTP2 - // Creer l'arc sortant de T2 vers P1 - arc = new Arc_SORTANT(T2, P1, 1, Mutex.generateId(0)); - Mutex.ajouterArc(arc); - T2.ajouterArc_SORTANT((Arc_SORTANT) arc); - assertEquals(1, T2.getArcs_SORTANTS().size(), "CTP2"); - - // CTP1 - // Creer l'arc sortant de T1 vers P2 - arc = new Arc_SORTANT(T1, P2, 1, Mutex.generateId(0)); - Mutex.ajouterArc(arc); - T1.ajouterArc_SORTANT((Arc_SORTANT) arc); - assertEquals(1, T1.getArcs_SORTANTS().size(), "CTP1"); - - // CPT2 - // Creer l'arc entrant simple à T2 de P2 - arc = new Arc_entrant_simple(T2, P2, 1, Mutex.generateId(0)); - Mutex.ajouterArc(arc); - T2.ajouterArc_ENTRANT((Arc_ENTRANT) arc); - assertEquals(1, T2.getArcs_ENTRANTS().size(), "CPT2"); - - // CPT5 - // Creer l'arc entrant simple à T1 de P3 - arc = new Arc_entrant_simple(T1, P3, 1, Mutex.generateId(0)); - Mutex.ajouterArc(arc); - T1.ajouterArc_ENTRANT((Arc_ENTRANT) arc); - assertEquals(2, T1.getArcs_ENTRANTS().size(), "CPT5"); - - // CTP5 - // Creer l'arc sortant de T2 vers P3 - arc = new Arc_SORTANT(T2, P3, 1, Mutex.generateId(0)); - Mutex.ajouterArc(arc); - T2.ajouterArc_SORTANT((Arc_SORTANT) arc); - assertEquals(2, T2.getArcs_SORTANTS().size(), "CTP5"); - - // CPT6 - // Creer l'arc entrant simple à T3 de P3 - arc = new Arc_entrant_simple(T3, P3, 1, Mutex.generateId(0)); - Mutex.ajouterArc(arc); - T3.ajouterArc_ENTRANT((Arc_ENTRANT) arc); - assertEquals(1, T3.getArcs_ENTRANTS().size(), "CPT6"); - - // CTP6 - // Creer l'arc sortant de T4 vers P3 - arc = new Arc_SORTANT(T4, P3, 1, Mutex.generateId(0)); - Mutex.ajouterArc(arc); - T4.ajouterArc_SORTANT((Arc_SORTANT) arc); - assertEquals(1, T4.getArcs_SORTANTS().size(), "CTP6"); - - // CTP3 - // Creer l'arc sortant de T3 à P4 - arc = new Arc_SORTANT(T3, P4, 1, Mutex.generateId(0)); - Mutex.ajouterArc(arc); - T3.ajouterArc_SORTANT((Arc_SORTANT) arc); - assertEquals(1, T3.getArcs_SORTANTS().size(), "CTP3"); - - // CPT4 - // Creer l'arc entrant à T4 de P4 - arc = new Arc_entrant_simple(T4, P4, 1, Mutex.generateId(0)); - Mutex.ajouterArc(arc); - T4.ajouterArc_ENTRANT((Arc_ENTRANT) arc); - assertEquals(1, T4.getArcs_ENTRANTS().size(), "CPT4"); - - // CPT3 - // Creer l'arc entrant à T3 de P5 - arc = new Arc_entrant_simple(T3, P5, 1, Mutex.generateId(0)); - Mutex.ajouterArc(arc); - T3.ajouterArc_ENTRANT((Arc_ENTRANT) arc); - assertEquals(2, T3.getArcs_ENTRANTS().size(), "CPT3"); - - // CTP4 - // Creer l'arc sortant de T4 vers P5 - arc = new Arc_SORTANT(T4, P5, 1, Mutex.generateId(0)); - Mutex.ajouterArc(arc); - T4.ajouterArc_SORTANT((Arc_SORTANT) arc); - assertEquals(2, T4.getArcs_SORTANTS().size(), "CTP4"); - - assertEquals(12, Mutex.getArcs().size(), "There should be 12 unique arcs in Mutex"); - assertEquals(5, Mutex.getPlaces().size(), "There should be 5 unique places in Mutex"); - assertEquals(4, Mutex.getTransitions().size(), "There should be 4 unique transitions in Mutex"); - - Mutex.afficherReseau(); - - } - - @Test - @DisplayName("RI") - public void testActiverPetri_1() { - ReseauPetri Active = new ReseauPetri(); - - // Active has only one Transition - Transition T1 = new Transition("T1", Active.generateId(2)); - Active.ajouterTransition(T1); - - // Tirer T1 - Active.tirer_transition(String.valueOf(T1.getId())); - Active.afficherReseau(); - - assertEquals(1, Active.getTransitions().size(), "RI"); - } - - @Test - @DisplayName("RD0") - public void testActiverPetri_2() { - ReseauPetri Active = new ReseauPetri(); - - Transition T1 = new Transition("T1", Active.generateId(2)); - - // Active has only one Place with 0 jetons - Place P1 = new Place(0, Active.generateId(1)); - Active.ajouterPlace(P1); - - // Creer l'arc entrant simple à T1 de P1 - Arc_ENTRANT arc = new Arc_entrant_simple(T1, P1, 1, Active.generateId(0)); - Active.ajouterArc(arc); - - T1.ajouterArc_ENTRANT(arc); - Active.ajouterTransition(T1); - - // Tirer T1 - Active.tirer_transition(String.valueOf(T1.getId())); - Active.afficherReseau(); - - assertEquals(0, P1.get_nombre_jetons(), "RD0"); - } - - @Test - @DisplayName("RD1") - public void testActiverPetri_3() { - ReseauPetri Active = new ReseauPetri(); - - Transition T1 = new Transition("T1", Active.generateId(2)); - - // Active has only one Place with 2 jeton - Place P1 = new Place(2, Active.generateId(1)); - Active.ajouterPlace(P1); - - // Creer l'arc entrant simple à T1 de P1 - Arc_ENTRANT arc = new Arc_entrant_simple(T1, P1, 1, Active.generateId(0)); - Active.ajouterArc(arc); - - T1.ajouterArc_ENTRANT(arc); - Active.ajouterTransition(T1); - - // Tirer T1 - Active.tirer_transition(String.valueOf(T1.getId())); - Active.afficherReseau(); - - assertEquals(1, P1.get_nombre_jetons(), "RD1"); - } - - @Test - @DisplayName("RD2") - public void testActiverPetri_4() { - ReseauPetri Active = new ReseauPetri(); - - Transition T1 = new Transition("T1", Active.generateId(2)); - - // Active has only one Place with 5 jetons - Place P1 = new Place(5, Active.generateId(1)); - Active.ajouterPlace(P1); - - // Creer l'arc entrant simple à T1 de P1 - Arc_ENTRANT arc = new Arc_entrant_simple(T1, P1, 3, Active.generateId(0)); - Active.ajouterArc(arc); - - T1.ajouterArc_ENTRANT(arc); - Active.ajouterTransition(T1); - - // Tirer T1 - Active.tirer_transition(String.valueOf(T1.getId())); - Active.afficherReseau(); - - assertEquals(2, P1.get_nombre_jetons(), "RD2"); - } - - @Test - @DisplayName("RG0") - public void testActiverPetri_5() { - ReseauPetri Active = new ReseauPetri(); - - Transition T1 = new Transition("T1", Active.generateId(2)); - - // Active has one Place with 1 jeton - Place P1 = new Place(1, Active.generateId(1)); - Active.ajouterPlace(P1); - - // Creer l'arc sortant de T1 vers P1 - Arc_SORTANT arc = new Arc_SORTANT(T1, P1, 1, Active.generateId(0)); - Active.ajouterArc(arc); - - T1.ajouterArc_SORTANT(arc); - Active.ajouterTransition(T1); - - // Tirer T1 - Active.tirer_transition(String.valueOf(T1.getId())); - Active.afficherReseau(); - - assertEquals(2, P1.get_nombre_jetons(), "RG0"); - - } - - @Test - @DisplayName("RM0") - public void testActiverPetri_6() { - ReseauPetri Active = new ReseauPetri(); - - Transition T1 = new Transition("T1", Active.generateId(2)); - - // Active has two Places with 0 and 2 jetons - Place P1 = new Place(0, Active.generateId(1)); - Place P2 = new Place(2, Active.generateId(1)); - Active.ajouterPlace(P1); - Active.ajouterPlace(P2); - - // Creer l'arc entrant simple à T1 de P1 - Arc_ENTRANT arc = new Arc_entrant_simple(T1, P1, 1, Active.generateId(0)); - Active.ajouterArc(arc); - Arc_SORTANT arc_1 = new Arc_SORTANT(T1, P2, 1, Active.generateId(0)); - Active.ajouterArc(arc_1); - - T1.ajouterArc_ENTRANT(arc); - T1.ajouterArc_SORTANT(arc_1); - Active.ajouterTransition(T1); - - // Tirer T1 - Active.tirer_transition(String.valueOf(T1.getId())); - Active.afficherReseau(); - - assertEquals(0, P1.get_nombre_jetons(), "RM0"); - assertEquals(2, P2.get_nombre_jetons(), "RM0"); - } - - @Test - @DisplayName("RM1") - public void testActiverPetri_7() { - ReseauPetri Active = new ReseauPetri(); - - Transition T1 = new Transition("T1", Active.generateId(2)); - - // Active has two Places with 5 and 0 jetons - Place P1 = new Place(5, Active.generateId(1)); - Place P2 = new Place(0, Active.generateId(1)); - Active.ajouterPlace(P1); - Active.ajouterPlace(P2); - - // Creer l'arc entrant simple à T1 de P1 - Arc_ENTRANT arc = new Arc_entrant_simple(T1, P1, 3, Active.generateId(0)); - Active.ajouterArc(arc); - Arc_SORTANT arc_1 = new Arc_SORTANT(T1, P2, 1, Active.generateId(0)); - Active.ajouterArc(arc_1); - - T1.ajouterArc_ENTRANT(arc); - T1.ajouterArc_SORTANT(arc_1); - Active.ajouterTransition(T1); - - // Tirer T1 - Active.tirer_transition(String.valueOf(T1.getId())); - Active.afficherReseau(); - - assertEquals(2, P1.get_nombre_jetons(), "RM1"); - assertEquals(1, P2.get_nombre_jetons(), "RM1"); - - } - - @Test - @DisplayName("RGM") - public void testActiverPetri_8() { - ReseauPetri Active = new ReseauPetri(); - - Transition T1 = new Transition("T1", Active.generateId(2)); - - // Active has two Places with 2 and 1 jetons - Place P1 = new Place(2, Active.generateId(1)); - Place P2 = new Place(1, Active.generateId(1)); - Active.ajouterPlace(P1); - Active.ajouterPlace(P2); - - // Creer l'arc entrant simple à T1 de P1 et de P2 - Arc_ENTRANT arc = new Arc_entrant_simple(T1, P1, 1, Active.generateId(0)); - Active.ajouterArc(arc); - Arc_ENTRANT arc_1 = new Arc_entrant_simple(T1, P2, 1, Active.generateId(0)); - Active.ajouterArc(arc_1); - - T1.ajouterArc_ENTRANT(arc); - T1.ajouterArc_ENTRANT(arc_1); - - Active.ajouterTransition(T1); - - // Tirer T1 - Active.tirer_transition(String.valueOf(T1.getId())); - Active.afficherReseau(); - - assertEquals(1, P1.get_nombre_jetons(), "RGM"); - assertEquals(0, P2.get_nombre_jetons(), "RGM"); - } - - @Test - @DisplayName("RDM") - public void testActiverPetri_9() { - ReseauPetri Active = new ReseauPetri(); - - Transition T1 = new Transition("T1", Active.generateId(2)); - - // Active has two Places with 0 and 1 jetons - Place P1 = new Place(0, Active.generateId(1)); - Place P2 = new Place(1, Active.generateId(1)); - Active.ajouterPlace(P1); - Active.ajouterPlace(P2); - - // Creer l'arc sortant de T1 vers P1 et de P2 - Arc_SORTANT arc = new Arc_SORTANT(T1, P1, 1, Active.generateId(0)); - Active.ajouterArc(arc); - Arc_SORTANT arc_1 = new Arc_SORTANT(T1, P2, 1, Active.generateId(0)); - Active.ajouterArc(arc_1); - - T1.ajouterArc_SORTANT(arc); - T1.ajouterArc_SORTANT(arc_1); - - Active.ajouterTransition(T1); - - // Tirer T1 - Active.tirer_transition(String.valueOf(T1.getId())); - Active.afficherReseau(); - - assertEquals(1, P1.get_nombre_jetons(), "RDM"); - assertEquals(2, P2.get_nombre_jetons(), "RDM"); - - } - - @Test - @DisplayName("RDGM") - public void testActiverPetri_10() { - ReseauPetri Active = new ReseauPetri(); - - Transition T1 = new Transition("T1", Active.generateId(2)); - - // Active has 4 Places with 1 and 1 jetons and 0 and 0 jetons - Place P1 = new Place(1, Active.generateId(1)); - Place P2 = new Place(1, Active.generateId(1)); - Place P3 = new Place(0, Active.generateId(1)); - Place P4 = new Place(0, Active.generateId(1)); - Active.ajouterPlace(P1); - Active.ajouterPlace(P2); - Active.ajouterPlace(P3); - Active.ajouterPlace(P4); - - // Creer l'arc entrant simple à T1 de P1 et de P2 - Arc_ENTRANT arc = new Arc_entrant_simple(T1, P1, 1, Active.generateId(0)); - Active.ajouterArc(arc); - Arc_ENTRANT arc_1 = new Arc_entrant_simple(T1, P2, 1, Active.generateId(0)); - Active.ajouterArc(arc_1); - - // Creer l'arc sortant de T1 vers P3 et de P4 - Arc_SORTANT arc_2 = new Arc_SORTANT(T1, P3, 1, Active.generateId(0)); - Active.ajouterArc(arc_2); - Arc_SORTANT arc_3 = new Arc_SORTANT(T1, P4, 1, Active.generateId(0)); - Active.ajouterArc(arc_3); - - T1.ajouterArc_ENTRANT(arc); - T1.ajouterArc_ENTRANT(arc_1); - T1.ajouterArc_SORTANT(arc_2); - T1.ajouterArc_SORTANT(arc_3); - - Active.ajouterTransition(T1); - - // Tirer T1 - Active.tirer_transition(String.valueOf(T1.getId())); - Active.afficherReseau(); - - assertEquals(0, P1.get_nombre_jetons(), "RDGM"); - assertEquals(0, P2.get_nombre_jetons(), "RDGM"); - assertEquals(1, P3.get_nombre_jetons(), "RDGM"); - assertEquals(1, P4.get_nombre_jetons(), "RDGM"); - - } - - // Tests de supression - @Test - @DisplayName("ST0") - public void testSupprimerTransition() { - ReseauPetri Supp = new ReseauPetri(); - - Transition T1 = new Transition("T1", Supp.generateId(2)); - Supp.ajouterTransition(T1); - Supp.supprimerTransition(T1); - Supp.afficherReseau(); - - assertEquals(0, Supp.getTransitions().size(), "ST0"); - } - - @Test - @DisplayName("SP1") - public void testDestruction_Place(){ - ReseauPetri Destruction = new ReseauPetri(); - - // créer un réseau de petri similaire à Active dans testActiverPetri_3 - Place P1 = new Place(2, Destruction.generateId(1)); - Destruction.ajouterPlace(P1); - - Transition T1 = new Transition("T1", Destruction.generateId(2)); - - // Creer l'arc entrant simple à T1 de P1 - Arc_ENTRANT arc = new Arc_entrant_simple(T1, P1, 1, Destruction.generateId(0)); - Destruction.ajouterArc(arc); - - T1.ajouterArc_ENTRANT(arc); - Destruction.ajouterTransition(T1); - - // Supprimer la place P1 - Destruction.supprimerPlace(P1); - Destruction.afficherReseau(); - - assertEquals(0, Destruction.getPlaces().size(), "SP1"); - assertEquals(1, Destruction.getTransitions().size(), "SP1"); - assertEquals(0, Destruction.getArcs().size(), "SP1"); - - } - - @Test - @DisplayName("ST1") - public void testDestruction_Transition() { - ReseauPetri Destruction = new ReseauPetri(); - - // créer un réseau de petri similaire à Active dans testActiverPetri_3 - Place P1 = new Place(2, Destruction.generateId(1)); - Destruction.ajouterPlace(P1); - - Transition T1 = new Transition("T1", Destruction.generateId(2)); - - // Creer l'arc entrant simple à T1 de P1 - Arc_ENTRANT arc = new Arc_entrant_simple(T1, P1, 1, Destruction.generateId(0)); - Destruction.ajouterArc(arc); - - T1.ajouterArc_ENTRANT(arc); - Destruction.ajouterTransition(T1); - - // Supprimer la transition T1 - Destruction.supprimerTransition(T1); - Destruction.afficherReseau(); - - assertEquals(1, Destruction.getPlaces().size(), "ST1"); - assertEquals(0, Destruction.getTransitions().size(), "ST1"); - assertEquals(0, Destruction.getArcs().size(), "ST1"); - } - - @Test - @DisplayName("SA1") - public void testDestruction_Arc() { - ReseauPetri Destruction = new ReseauPetri(); - - // créer un réseau de petri similaire à Active dans testActiverPetri_3 - Place P1 = new Place(2, Destruction.generateId(1)); - Destruction.ajouterPlace(P1); - - Transition T1 = new Transition("T1", Destruction.generateId(2)); - - // Creer l'arc entrant simple à T1 de P1 - Arc_ENTRANT arc = new Arc_entrant_simple(T1, P1, 1, Destruction.generateId(0)); - Destruction.ajouterArc(arc); - - T1.ajouterArc_ENTRANT(arc); - Destruction.ajouterTransition(T1); - - // Supprimer l'arc - Destruction.supprimerArc(arc); - Destruction.afficherReseau(); - - assertEquals(1, Destruction.getPlaces().size(), "SA1"); - assertEquals(1, Destruction.getTransitions().size(), "SA1"); - assertEquals(0, Destruction.getArcs().size(), "SA1"); - } - - @Test - @DisplayName("ARC ZERO") - public void testArczero() { - // Initialize the Petri network - ReseauPetri petriNetwork = new ReseauPetri(); - - // Create places - Place place1 = new Place(3, petriNetwork.generateId(1)); - Place place2 = new Place(0, petriNetwork.generateId(1)); - - // Add places to the network - petriNetwork.ajouterPlace(place1); - petriNetwork.ajouterPlace(place2); - - // Create a transition - Transition transition1 = new Transition("T1", petriNetwork.generateId(2)); - - // Add the transition to the network - petriNetwork.ajouterTransition(transition1); - - // Create arcs and add them to the network - // Incoming arc from place2 to transition1 with weight 1 - Arc_zero incomingArc_zero = new Arc_zero(transition1, place2, 1, petriNetwork.generateId(0)); - petriNetwork.ajouterArc(incomingArc_zero); - transition1.ajouterArc_ENTRANT(incomingArc_zero); - - // Outgoing arc from transition1 to place1 with weight 1 - Arc_SORTANT outgoingArc = new Arc_SORTANT(transition1, place1, 1, petriNetwork.generateId(0)); - petriNetwork.ajouterArc(outgoingArc); - transition1.ajouterArc_SORTANT(outgoingArc); - - // Display the initial state of the Petri network - System.out.println("Initial State of Petri Network:"); - petriNetwork.afficherReseau(); - - // Activate the transition - System.out.println("\nActivating transition T1..."); - petriNetwork.tirer_transition(String.valueOf(transition1.getId())); - - // Display the state of the Petri network after activation - System.out.println("\nState of Petri Network after Transition T1 Activation:"); - petriNetwork.afficherReseau(); - - // Assertions to verify the correct state of the Petri network - assertEquals(4, place1.get_nombre_jetons(), "Tokens in Place 1 should be 4"); - assertEquals(0, place2.get_nombre_jetons(), "Tokens in Place 2 should be 0"); - } - @Test - @DisplayName("ARC Videur") - public void testArcvideur() { - // Initialize the Petri network - ReseauPetri petriNetwork = new ReseauPetri(); - - // Create places - Place place1 = new Place(2, petriNetwork.generateId(1)); - Place place2 = new Place(4, petriNetwork.generateId(1)); - - // Add places to the network - petriNetwork.ajouterPlace(place1); - petriNetwork.ajouterPlace(place2); - - // Create a transition - Transition transition1 = new Transition("T1", petriNetwork.generateId(2)); - - // Add the transition to the network - petriNetwork.ajouterTransition(transition1); - - // Create arcs and add them to the network - // Incoming arc from place2 to transition1 with weight 1 - Arc_videur incomingArc_videur = new Arc_videur(transition1, place2, 1, petriNetwork.generateId(0)); - petriNetwork.ajouterArc(incomingArc_videur); - transition1.ajouterArc_ENTRANT(incomingArc_videur); - - // Outgoing arc from transition1 to place1 with weight 1 - Arc_SORTANT outgoingArc = new Arc_SORTANT(transition1, place1, 1, petriNetwork.generateId(0)); - petriNetwork.ajouterArc(outgoingArc); - transition1.ajouterArc_SORTANT(outgoingArc); - - // Display the initial state of the Petri network - System.out.println("Initial State of Petri Network:"); - petriNetwork.afficherReseau(); - - // Activate the transition - System.out.println("\nActivating transition T1..."); - petriNetwork.tirer_transition(String.valueOf(transition1.getId())); - - // Display the state of the Petri network after activation - System.out.println("\nState of Petri Network after Transition T1 Activation:"); - petriNetwork.afficherReseau(); - - // Assertions to verify the correct state of the Petri network - assertEquals(3, place1.get_nombre_jetons(), "Tokens in Place 1 should be 3"); - assertEquals(0, place2.get_nombre_jetons(), "Tokens in Place 2 should be 0"); - } - -} - -