diff --git a/src/main/java/org/petriNet/PetriNet.java b/src/main/java/org/petriNet/PetriNet.java index 586b5f272ba39ac2e88f0f4544ca4a09d32284e2..46ff4ad79dd8f9363a06561fd5f0490fc1407486 100644 --- a/src/main/java/org/petriNet/PetriNet.java +++ b/src/main/java/org/petriNet/PetriNet.java @@ -1,219 +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; - } + 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); } - 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; - } + // Create a method to generate unique ids + public int generateId(int index) { + int id = idCounters.get(index); + idCounters.set(index, id + 1); + return id; } - 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; - } + + // 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; } - 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; + + 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); } - // 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); + @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; + } } - } 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); - } + 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; } } - // 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)"); + this.arcs.add(arc); } - 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)"); + /** + * 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()); } - System.out.println("List of arcs:"); - for (Arc arc : this.arcs) { + @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) { - System.out.println(arc.getId() + " : simple arc with weight " + arc.getWeight() + " (" - + "Place with Id " + arc.getPlace().getId() + " to " + arc.getTransition().getName() + ")"); + 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(arc.getId() + " : simple arc with weight " + arc.getWeight() + " (" - + arc.getTransition().getName() + " to " + "Place with Id " + arc.getPlace().getId() + ")"); + 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 + } \ No newline at end of file diff --git a/src/test/java/org/petriNet/PetriNetTest.java b/src/test/java/org/petriNet/PetriNetTest.java index 65307a8ef87c6fc8c76763f2fd035bc88afccc48..ea65075c88b750789dceadeef53868f69ac5588d 100644 --- a/src/test/java/org/petriNet/PetriNetTest.java +++ b/src/test/java/org/petriNet/PetriNetTest.java @@ -694,6 +694,7 @@ public class PetriNetTest { assertEquals(3, place1.getTokenCount(), "Tokens in Place 1 should be 3"); assertEquals(0, place2.getTokenCount(), "Tokens in Place 2 should be 0"); } + @Test public void testSetTokenCount() { Place place = new Place(0, 1); @@ -784,6 +785,56 @@ public class PetriNetTest { 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()); + } +