From abc8bc508ca376228516bae0049bd81c35b3e1ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CKhaoula?= <“khaoula.arouissi@imt-atlantique.net”> Date: Mon, 18 Nov 2024 11:34:36 +0100 Subject: [PATCH] =?UTF-8?q?Test=20des=20set=20et=20l'impl=C3=A9mentation?= =?UTF-8?q?=20de=20l'ajout/cr=C3=A9ation=20de=20plusieurs=20composants=20d?= =?UTF-8?q?'un=20seul=20coup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/petriNet/Arc.java | 8 +- src/main/java/org/petriNet/PetriNet.java | 137 ++++++- src/main/java/org/petriNet/Transition.java | 15 +- src/test/java/org/petriNet/PetriNetTest.java | 355 +++++++++++-------- 4 files changed, 360 insertions(+), 155 deletions(-) diff --git a/src/main/java/org/petriNet/Arc.java b/src/main/java/org/petriNet/Arc.java index 167e340..246ec93 100644 --- a/src/main/java/org/petriNet/Arc.java +++ b/src/main/java/org/petriNet/Arc.java @@ -37,12 +37,16 @@ public abstract class Arc { return this.place; } + public Transition getTransition() { + return this.transition; + } + public void setPlace(Place place) { this.place = place; } - public Transition getTransition() { - return this.transition; + public void setWeight(int weight) { + this.weight = weight; } } diff --git a/src/main/java/org/petriNet/PetriNet.java b/src/main/java/org/petriNet/PetriNet.java index 46ff4ad..c21f224 100644 --- a/src/main/java/org/petriNet/PetriNet.java +++ b/src/main/java/org/petriNet/PetriNet.java @@ -53,9 +53,9 @@ return this.arcs; } -// public void setArcs(LinkedList<Arc> arcs) { -// this.arcs = arcs; -// } + public void setArcs(LinkedList<Arc> arcs) { + this.arcs = arcs; + } @Override public void addPlace(Place place) { @@ -95,6 +95,137 @@ this.arcs.add(arc); } + /** + * We add a method to create many transitions at once. + * This is an enhancement to avoid many lines of code. + * @param numberOfTransitions the number of transitions to create + * @return List<Transition> + */ + + public LinkedList<Transition> createTransitions(int numberOfTransitions) { + LinkedList<Transition> transitions = new LinkedList<>(); + // verify that the number of transitions is positive + if (numberOfTransitions <= 0) { + System.out.println("The number of transitions must be positive"); + return transitions; + } + for (int i = 0; i < numberOfTransitions; i++) { + int id = this.generateId(2); + Transition transition = new Transition("Transition " + id, id); + transitions.add(transition); + } + return transitions; + } + + /** + * We add a method to create many places at once. + * This is an enhancement to avoid many lines of code. + * @param numberOfPlaces the number of places to create + * @param numberOfTokens List<int> of tokens for each place + * @return List<Place> + */ + + public LinkedList<Place> createPlaces(int numberOfPlaces, List<Integer> numberOfTokens) { + LinkedList<Place> places = new LinkedList<>(); + + /* Verify that the number of tokens is equal to the number of places + * Verify that the number of tokens is positive + * verify that the number of places is positive + */ + + if (numberOfPlaces <= 0 || numberOfTokens.size() != numberOfPlaces || numberOfTokens.stream().anyMatch(token -> token < 0)) { + System.out.println("Error: The number of places must be positive, the number of tokens must be positive and equal to the number of places"); + return places; + } + + for (int i = 0; i < numberOfPlaces; i++) { + places.add(new Place( numberOfTokens.get(i), this.generateId(1))); + }; + return places; + } + + /** + * We add a method to create many Incoming arcs at once. + * This is an enhancement to avoid many lines of code. + * If one of the types of arcs is not valid, the method will return null. + * + * @param numberOfArcs the number of Incoming Arcs to create + * @param weights List<int> of weights for each Incoming Arc + * @param places List<Place> of places for each Incoming Arc + * @param transitions List<Transition> of transitions for each Incoming Arc + * @param types_of_arcs List<String> of types for each Incoming Arc + * @return List<IncomingArc> + */ + + public LinkedList<Arc> createIncomingArcs(int numberOfArcs, List<String> types_of_arcs, List<Integer> weights, List<Place> places, List<Transition> transitions) { + LinkedList<Arc> IncomingArc = new LinkedList<>(); + + /* Verify that the number of IncomingArc is positive + * Verify that the number of weights is equal to the number of IncomingArc + * Verify that the number of weights is positive + * Verify that the number of places is equal to the number of IncomingArc + * Verify that the number of transitions is equal to the number of IncomingArc + */ + + if (numberOfArcs <= 0 || weights.size() != numberOfArcs || weights.stream().anyMatch(weight -> weight < 0) || + places.size() != numberOfArcs || transitions.size() != numberOfArcs) { + System.out.println("Error: The number of weights, places, and transitions must be positive and equal to the number of IncomingArc"); + return IncomingArc; + } + + for (int i = 0; i < numberOfArcs; i++) { + switch (types_of_arcs.get(i)) { + case "Simple": + IncomingArc.add(new IncomingArc_Simple(transitions.get(i), places.get(i), weights.get(i), this.generateId(0))); + break; + case "Videur": + IncomingArc.add(new IncomingArc_Videur(transitions.get(i), places.get(i), weights.get(i), this.generateId(0))); + break; + case "Zero": + IncomingArc.add(new IncomingArc_Zero(transitions.get(i), places.get(i), weights.get(i), this.generateId(0))); + break; + default: + System.out.println("Error: one of he type of arc is not valid"); + return null; + } + } + return IncomingArc; + } + + /** + * We add a method to create many Outgoing arcs at once. + * This is an enhancement to avoid many lines of code. + * @param numberOfArcs the number of Outgoing Arcs to create + * @param weights List<int> of weights for each Outgoing Arc + * @param places List<Place> of places for each Outgoing Arc + * @param transitions List<Transition> of transitions for each Outgoing Arc + * @return List<Arc> + */ + + public LinkedList<Arc> createOutgoingArcs(int numberOfArcs, List<Integer> weights, List<Place> places, List<Transition> transitions) { + LinkedList<Arc> OutgoingArc = new LinkedList<>(); + + /* Verify that the number of OutgoingArc is positive + * Verify that the number of weights is equal to the number of OutgoingArc + * Verify that the number of weights is positive + * Verify that the number of places is equal to the number of OutgoingArc + * Verify that the number of transitions is equal to the number of OutgoingArc + */ + + if (numberOfArcs <= 0 || weights.size() != numberOfArcs || weights.stream().anyMatch(weight -> weight < 0) || + places.size() != numberOfArcs || transitions.size() != numberOfArcs) { + System.out.println("Error: The number of weights, places, and transitions must be positive and equal to the number of OutgoingArc"); + return OutgoingArc; + } + + for (int i = 0; i < numberOfArcs; i++) { + OutgoingArc.add(new OutgoingArc(transitions.get(i), places.get(i), weights.get(i), this.generateId(0))); + } + return OutgoingArc; + } + + + /** * 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. diff --git a/src/main/java/org/petriNet/Transition.java b/src/main/java/org/petriNet/Transition.java index 6a634ea..8fc574c 100644 --- a/src/main/java/org/petriNet/Transition.java +++ b/src/main/java/org/petriNet/Transition.java @@ -1,20 +1,21 @@ package org.petriNet; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; public class Transition { private final int id; String name; - private List<OutgoingArc> outgoingArcs; - private final List<IncomingArc> incomingArcs; + private LinkedList<OutgoingArc> outgoingArcs; + private LinkedList<IncomingArc> incomingArcs; public Transition(String name, int id) { this.id = id; this.name = name; - this.outgoingArcs = new ArrayList<>(); - this.incomingArcs = new ArrayList<>(); + this.outgoingArcs = new LinkedList<>(); + this.incomingArcs = new LinkedList<>(); } /** @@ -38,10 +39,14 @@ public class Transition { return incomingArcs; } - public void setOutgoingArcs(List<OutgoingArc> outgoingArcs) { + public void setOutgoingArcs(LinkedList<OutgoingArc> outgoingArcs) { this.outgoingArcs = outgoingArcs; } + public void setIncomingArcs(LinkedList<IncomingArc> incomingArcs) { + this.incomingArcs = incomingArcs; + } + public void addOutgoingArc(OutgoingArc outgoingArc) { // verify that the arc doesn't already exist for (OutgoingArc existingArc : outgoingArcs) { diff --git a/src/test/java/org/petriNet/PetriNetTest.java b/src/test/java/org/petriNet/PetriNetTest.java index ea65075..1adcf46 100644 --- a/src/test/java/org/petriNet/PetriNetTest.java +++ b/src/test/java/org/petriNet/PetriNetTest.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; +import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.*; @@ -69,6 +70,93 @@ public class PetriNetTest { assertEquals(1, reseauPetri.getArcs().size(), "CAA0, CAA1"); } + @Test + public void testSetWeight() { + Place place = new Place(2, reseauPetri.generateId(1)); + Transition transition = new Transition("transition", reseauPetri.generateId(2)); + OutgoingArc arc = new OutgoingArc(transition, place, 1, reseauPetri.generateId(0)); + arc.setWeight(2); + assertEquals(2, arc.getWeight(), "CSW0, CSW1"); + } + + @Test + public void testSetTokenCount() { + Place place = new Place(0, 1); + + // Set the token count to a positive value + place.setTokenCount(5); + assertEquals(5, place.getTokenCount()); + + // Set the token count to zero + place.setTokenCount(0); + assertEquals(0, place.getTokenCount()); + + // Set the token count to a negative value (should be clamped to 0) + place.setTokenCount(-2); + assertEquals(0, place.getTokenCount()); + } + + @Test + public void testSetPlaces() { + PetriNet petriNet = new PetriNet(); + + // Create a list of places + List<Place> places = new ArrayList<>(); + Place place1 = new Place(2, petriNet.generateId(1)); + Place place2 = new Place(3, petriNet.generateId(2)); + places.add(place1); + places.add(place2); + + // Set the places in the Petri net + petriNet.setPlaces(places); + + // Assert that the places were correctly set + assertEquals(2, petriNet.getPlaces().size()); + assertEquals(place1, petriNet.getPlaces().get(0)); + assertEquals(place2, petriNet.getPlaces().get(1)); + } + + @Test + public void testSetTransitions() { + PetriNet petriNet = new PetriNet(); + + // Create a list of transitions + List<Transition> transitions = new ArrayList<>(); + Transition transition1 = new Transition("T1", petriNet.generateId(1)); + Transition transition2 = new Transition("T2", petriNet.generateId(2)); + transitions.add(transition1); + transitions.add(transition2); + + // Set the transitions in the Petri net + petriNet.setTransitions(transitions); + + // Assert that the transitions were correctly set + assertEquals(2, petriNet.getTransitions().size()); + assertEquals(transition1, petriNet.getTransitions().get(0)); + assertEquals(transition2, petriNet.getTransitions().get(1)); + } + + @Test + public void testSetPlace() { + Transition transition = new Transition("T1", 1); + Place initialPlace = new Place(2, 2); + Place newPlace = new Place(3, 3); + int weight = 1; + int id = 4; + + // Create an OutgoingArc + OutgoingArc arc = new OutgoingArc(transition, initialPlace, weight, id); + + // Assert initial place + assertEquals(initialPlace, arc.getPlace()); + + // Set a new place + arc.setPlace(newPlace); + + // Assert the place is updated + assertEquals(newPlace, arc.getPlace()); + } + @Test @DisplayName("Assemble Petri") public void testAssemblePetri(){ @@ -460,7 +548,7 @@ public class PetriNetTest { } @Test - @DisplayName("RDGM") + @DisplayName("RDGM0") public void testActivatePetri_10() { PetriNet active = new PetriNet(); @@ -505,6 +593,127 @@ public class PetriNetTest { assertEquals(1, p4.getTokenCount(), "RDGM"); } + @Test + @DisplayName("RDGM1") + public void testActivatePetri_11(){ + PetriNet active = new PetriNet(); + + Transition t1 = new Transition("T1", active.generateId(2)); + + /* We test PetriNet.setPlaces(List<Place> places), + PetriNet.setArcs(List<Arc> arcs), + Transition.setIncomingArcs(List<IncomingArc> incomingArcs), + Transition.setOutgoingArcs(List<OutgoingArc> outgoingArcs), + PetriNet.setTransitions(List<Transition> transitions) + */ + + // 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)); + // Add places through a list + List<Place> places = Arrays.asList(p1, p2, p3, p4); + active.setPlaces(places); + + // Create the simple incoming arc to T1 from P1 and P2 + IncomingArc arc = new IncomingArc_Simple(t1, p1, 1, active.generateId(0)); + IncomingArc arc1 = new IncomingArc_Simple(t1, p2, 1, active.generateId(0)); + + // Create the outgoing arc from T1 to P3 and P4 + OutgoingArc arc2 = new OutgoingArc(t1, p3, 1, active.generateId(0)); + OutgoingArc arc3 = new OutgoingArc(t1, p4, 1, active.generateId(0)); + + // Add arcs to Petri through a list + List<Arc> arcs = Arrays.asList(arc, arc1, arc2, arc3); + active.setArcs(new LinkedList<>(arcs)); + + // Add arcs to a transition through a list + LinkedList<IncomingArc> incomingArcs = new LinkedList<>(Arrays.asList(arc, arc1)); + LinkedList<OutgoingArc> outgoingArcs = new LinkedList<>(Arrays.asList(arc2, arc3)); + t1.setIncomingArcs(incomingArcs); + t1.setOutgoingArcs(outgoingArcs); + + // Create other transitions to test PetriNet.setTransitions + Transition t2 = new Transition("T2", active.generateId(2)); + Transition t3 = new Transition("T3", active.generateId(2)); + Transition t4 = new Transition("T4", active.generateId(2)); + + // Add transitions through a list + List<Transition> transitions = Arrays.asList(t1, t2, t3, t4); + + active.setTransitions(transitions); + + // Fire T1 + active.fireTransition(String.valueOf(t1.getId())); + active.displayNetwork(); + + assertEquals(4, active.getTransitions().size(), "RDGM"); + assertEquals(4, active.getPlaces().size(), "RDGM"); + assertEquals(4, active.getArcs().size(), "RDGM"); + assertEquals(0, p1.getTokenCount(), "RDGM"); + assertEquals(0, p2.getTokenCount(), "RDGM"); + assertEquals(1, p3.getTokenCount(), "RDGM"); + assertEquals(1, p4.getTokenCount(), "RDGM"); + } + + @Test + @DisplayName("RDGM2") + public void testActivatePetri_12(){ + 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 + // Create places using the PetriNet.createPlaces(int tokenCount) method + // The list of number of tokens is {1, 1, 0, 0} + List<Integer> tokenCounts = Arrays.asList(1, 1, 0, 0); + List<Place> places = active.createPlaces(4, tokenCounts); + active.setPlaces(places); + + // Create the simple incoming arcs to T1 from P1 and P2 + // Create arcs using the PetriNet.createIncomingArcs(int numberOfArcs, List<String> types_of_arcs, List<Integer> weights, + // List<Place> places, List<Transition> transitions) + // The list of types of arcs is {"simple", "simple"} + // The list of weights is {1, 1} + // The list of places is {P1, P2} + // The list of transitions is {T1, T1} + List<String> types_of_arcs = Arrays.asList("Simple", "Simple"); + List<Integer> weights = Arrays.asList(1, 1); + LinkedList<Arc> incomingArcs = active.createIncomingArcs(2, types_of_arcs, weights, places.subList(0, 2), Arrays.asList(t1, t1)); + + // Create the outgoing arc from T1 to P3 and P4 + // Create arcs using the PetriNet.createOutgoingArcs(int numberOfArcs, List<Integer> weights, List<Place> places, List<Transition> transitions) + // The list of weights is {1, 1} + // The list of places is {P3, P4} + // The list of transitions is {T1, T1} + LinkedList<Arc> outgoingArcs = active.createOutgoingArcs(2, weights, places.subList(2, 4), Arrays.asList(t1, t1)); + + // Merge the incoming and outgoing arcs + LinkedList<Arc> arcs = new LinkedList<>(incomingArcs); + arcs.addAll(outgoingArcs); + active.setArcs(arcs); + + LinkedList<IncomingArc> incomingArcsList = incomingArcs.stream().map(arc -> (IncomingArc) arc).collect(Collectors.toCollection(LinkedList::new)); + LinkedList<OutgoingArc> outgoingArcsList = outgoingArcs.stream().map(arc -> (OutgoingArc) arc).collect(Collectors.toCollection(LinkedList::new)); + t1.setIncomingArcs(incomingArcsList); + t1.setOutgoingArcs(outgoingArcsList); + + active.addTransition(t1); + + // Fire T1 + active.fireTransition(String.valueOf(t1.getId())); + active.displayNetwork(); + + assertEquals(1, active.getTransitions().size(), "RDGM : number of transitions"); + assertEquals(4, active.getPlaces().size(), "RDGM : number of places"); + assertEquals(4, active.getArcs().size(), "RDGM : number of arcs"); + assertEquals(0, places.get(0).getTokenCount(), "RDGM : P1"); + assertEquals(0, places.get(1).getTokenCount(), "RDGM : P2"); + assertEquals(1, places.get(2).getTokenCount(), "RDGM : P3"); + assertEquals(1, places.get(3).getTokenCount(), "RDGM : P4"); + } + @Test @DisplayName("ST0") public void testRemoveTransition_0() { @@ -695,150 +904,6 @@ public class PetriNetTest { assertEquals(0, place2.getTokenCount(), "Tokens in Place 2 should be 0"); } - @Test - public void testSetTokenCount() { - Place place = new Place(0, 1); - - // Set the token count to a positive value - place.setTokenCount(5); - assertEquals(5, place.getTokenCount()); - - // Set the token count to zero - place.setTokenCount(0); - assertEquals(0, place.getTokenCount()); - - // Set the token count to a negative value (should be clamped to 0) - place.setTokenCount(-2); - assertEquals(0, place.getTokenCount()); - } - @Test - public void testAddTokens() { - Place place = new Place(0, reseauPetri.generateId(1)); - - // Test with positive tokens - place.addTokens(5); - assertEquals(5, place.getTokenCount()); - - // Test with zero tokens - place.addTokens(0); - assertEquals(5, place.getTokenCount()); - - // Test with negative tokens (should not change token count) - place.addTokens(-2); - assertEquals(5, place.getTokenCount()); - } - @Test - public void testRemoveTokens() { - Place place = new Place(10, reseauPetri.generateId(1)); - - // Test with positive tokens less than the current count - place.removeTokens(5); - assertEquals(5, place.getTokenCount()); - - // Test with positive tokens equal to the current count - place.removeTokens(5); - assertEquals(0, place.getTokenCount()); - - // Test with positive tokens greater than the current count (should not go below 0) - place.removeTokens(10); - assertEquals(0, place.getTokenCount()); - - // Test with negative tokens (should not change token count) - place.removeTokens(-2); - assertEquals(0, place.getTokenCount()); - } - @Test - public void testSetPlaces() { - PetriNet petriNet = new PetriNet(); - - // Create a list of places - List<Place> places = new ArrayList<>(); - Place place1 = new Place(2, petriNet.generateId(1)); - Place place2 = new Place(3, petriNet.generateId(2)); - places.add(place1); - places.add(place2); - - // Set the places in the Petri net - petriNet.setPlaces(places); - - // Assert that the places were correctly set - assertEquals(2, petriNet.getPlaces().size()); - assertEquals(place1, petriNet.getPlaces().get(0)); - assertEquals(place2, petriNet.getPlaces().get(1)); - } - @Test - public void testSetTransitions() { - PetriNet petriNet = new PetriNet(); - - // Create a list of transitions - List<Transition> transitions = new ArrayList<>(); - Transition transition1 = new Transition("T1", petriNet.generateId(1)); - Transition transition2 = new Transition("T2", petriNet.generateId(2)); - transitions.add(transition1); - transitions.add(transition2); - - // Set the transitions in the Petri net - petriNet.setTransitions(transitions); - - // Assert that the transitions were correctly set - assertEquals(2, petriNet.getTransitions().size()); - assertEquals(transition1, petriNet.getTransitions().get(0)); - assertEquals(transition2, petriNet.getTransitions().get(1)); - } - @Test - public void testTransitionMethods() { - Transition transition = new Transition("T1", 1); - - // Test setOutgoingArcs - List<OutgoingArc> outgoingArcs = new ArrayList<>(); - Place place1 = new Place(2, 2); - Place place2 = new Place(3, 3); - OutgoingArc arc1 = new OutgoingArc(transition, place1, 1, 4); - OutgoingArc arc2 = new OutgoingArc(transition, place2, 2, 5); - outgoingArcs.add(arc1); - outgoingArcs.add(arc2); - - transition.setOutgoingArcs(outgoingArcs); - assertEquals(2, transition.getOutgoingArcs().size()); - assertEquals(arc1, transition.getOutgoingArcs().get(0)); - assertEquals(arc2, transition.getOutgoingArcs().get(1)); - - // Test addOutgoingArc (duplicate) - OutgoingArc duplicateArc = new OutgoingArc(transition, place1, 1, 6); // Same place and weight - transition.addOutgoingArc(duplicateArc); - assertEquals(2, transition.getOutgoingArcs().size()); // Should not add duplicate - - // Test addOutgoingArc (unique) - OutgoingArc uniqueArc = new OutgoingArc(transition, new Place(4, 7), 3, 8); - transition.addOutgoingArc(uniqueArc); - assertEquals(3, transition.getOutgoingArcs().size()); - assertTrue(transition.getOutgoingArcs().contains(uniqueArc)); - } - @Test - public void testSetPlace() { - Transition transition = new Transition("T1", 1); - Place initialPlace = new Place(2, 2); - Place newPlace = new Place(3, 3); - int weight = 1; - int id = 4; - - // Create an OutgoingArc - OutgoingArc arc = new OutgoingArc(transition, initialPlace, weight, id); - - // Assert initial place - assertEquals(initialPlace, arc.getPlace()); - - // Set a new place - arc.setPlace(newPlace); - - // Assert the place is updated - assertEquals(newPlace, arc.getPlace()); - } - - - - - } -- GitLab