Skip to content
Snippets Groups Projects
Commit 3274a411 authored by “Khaoula's avatar “Khaoula
Browse files

One Main with all the arcs

parent 0ad23ee8
No related branches found
No related tags found
No related merge requests found
package org.petriNet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose which simulation to run:");
System.out.println("1. Standard Petri Network");
System.out.println("2. Special Videur Arc Network");
System.out.println("3. Special Zero Arc Petri Network");
System.out.print("Enter your choice (1/2/3): ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
runStandardPetriNetwork();
break;
case 2:
runSpecialVideur();
break;
case 3:
runSpecialZero();
break;
default:
System.out.println("Invalid choice! Please run the program again.");
}
scanner.close();
}
private static void runStandardPetriNetwork() {
System.out.println("Running Standard Petri Network Simulation...");
// 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
// CR1
PetriNet Mutex = new PetriNet();
Arc arc;
petriNetwork.addPlace(place1);
petriNetwork.addPlace(place2);
// Create a transition and add it
// CP1
Place P1 = new Place(0, Mutex.generateId(1));
Mutex.addPlace(P1);
Transition transition1 = new Transition("T1", petriNetwork.generateId(2));
petriNetwork.addTransition(transition1);
// Create arcs and add them to the network
IncomingArc incomingArc = new IncomingArc_Simple(transition1, place1, 1, petriNetwork.generateId(0));
petriNetwork.addArc(incomingArc);
transition1.addIncomingArc(incomingArc);
// CT1
Transition T1 = new Transition("T1", Mutex.generateId(2));
Mutex.addTransition(T1);
OutgoingArc outgoingArc = new OutgoingArc(transition1, place2, 1, petriNetwork.generateId(0));
petriNetwork.addArc(outgoingArc);
transition1.addOutgoingArc(outgoingArc);
// CP2
Place P2 = new Place(1, Mutex.generateId(1));
Mutex.addPlace(P2);
petriNetwork.displayNetwork();
// CT2
Transition T2 = new Transition("T2", Mutex.generateId(2));
Mutex.addTransition(T2);
System.out.println("\nActivating transition T1...");
petriNetwork.fireTransition(String.valueOf(transition1.getId()));
// CP5
Place P3 = new Place(0, Mutex.generateId(1));
Mutex.addPlace(P3);
System.out.println("\nState of Petri Network after Transition T1 Activation:");
petriNetwork.displayNetwork();
// CT3
Transition T3 = new Transition("T3", Mutex.generateId(2));
Mutex.addTransition(T3);
System.out.println("\nVerifying state:");
System.out.println("Tokens in Place 1 (expected 2): " + place1.getTokenCount());
System.out.println("Tokens in Place 2 (expected 1): " + place2.getTokenCount());
}
// CP4
Place P4 = new Place(0, Mutex.generateId(1));
Mutex.addPlace(P4);
private static void runSpecialVideur() {
System.out.println("Running Special Videur Transition Simulation...");
// CT4
Transition T4 = new Transition("T4", Mutex.generateId(2));
Mutex.addTransition(T4);
PetriNet petriNetwork = new PetriNet();
// CP5
Place P5 = new Place(1, Mutex.generateId(1));
Mutex.addPlace(P5);
Place place1 = new Place(2, petriNetwork.generateId(1));
Place place2 = new Place(4, petriNetwork.generateId(1));
// 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);
petriNetwork.addPlace(place1);
petriNetwork.addPlace(place2);
Transition transition1 = new Transition("T1", petriNetwork.generateId(2));
petriNetwork.addTransition(transition1);
// 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);
IncomingArc_Videur incomingArc_videur = new IncomingArc_Videur(transition1, place2, 1, petriNetwork.generateId(0));
petriNetwork.addArc(incomingArc_videur);
transition1.addIncomingArc(incomingArc_videur);
OutgoingArc outgoingArc = new OutgoingArc(transition1, place1, 1, petriNetwork.generateId(0));
petriNetwork.addArc(outgoingArc);
transition1.addOutgoingArc(outgoingArc);
// 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);
petriNetwork.displayNetwork();
System.out.println("\nActivating transition T1...");
petriNetwork.fireTransition(String.valueOf(transition1.getId()));
// 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);
System.out.println("\nState of Petri Network after Transition T1 Activation:");
petriNetwork.displayNetwork();
System.out.println("\nVerifying state:");
System.out.println("Tokens in Place 1 (expected 3): " + place1.getTokenCount());
System.out.println("Tokens in Place 2 (expected 0): " + place2.getTokenCount());
}
// 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);
// 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);
// 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);
private static void runSpecialZero() {
System.out.println("Running Special Zero Transition Simulation...");
// 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);
PetriNet petriNetwork = new PetriNet();
Place place1 = new Place(3, petriNetwork.generateId(1));
Place place2 = new Place(0, petriNetwork.generateId(1));
// 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);
petriNetwork.addPlace(place1);
petriNetwork.addPlace(place2);
Transition transition1 = new Transition("T1", petriNetwork.generateId(2));
petriNetwork.addTransition(transition1);
// 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);
IncomingArc_Zero incomingArc_zero = new IncomingArc_Zero(transition1, place2, 1, petriNetwork.generateId(0));
petriNetwork.addArc(incomingArc_zero);
transition1.addIncomingArc(incomingArc_zero);
OutgoingArc outgoingArc = new OutgoingArc(transition1, place1, 1, petriNetwork.generateId(0));
petriNetwork.addArc(outgoingArc);
transition1.addOutgoingArc(outgoingArc);
// 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);
petriNetwork.displayNetwork();
System.out.println("\nActivating transition T1...");
petriNetwork.fireTransition(String.valueOf(transition1.getId()));
// 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);
System.out.println("\nState of Petri Network after Transition T1 Activation:");
petriNetwork.displayNetwork();
System.out.println("\nVerifying state:");
System.out.println("Tokens in Place 1 (expected 4): " + place1.getTokenCount());
System.out.println("Tokens in Place 2 (expected 0): " + place2.getTokenCount());
Mutex.displayState();
}
}
......@@ -231,7 +231,27 @@
return OutgoingArc;
}
@Override
public Place FindPlaceById(int id) {
for (Place place : this.places) {
if (place.getId() == id) {
return place;
}
}
System.out.println("Place not found");
return null;
}
@Override
public Arc FindArcById(int id) {
for (Arc arc : this.arcs) {
if (arc.getId() == id) {
return arc;
}
}
System.out.println("Arc not found");
return null;
}
/**
* Removing a place results in the removal of all arcs linked to it.
......@@ -291,17 +311,85 @@
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);
while (true) {
System.out.println("Choose an option:");
System.out.println("1. Display the network");
System.out.println("2. Fire a transition");
System.out.println("3. Change the number of tokens in a place");
System.out.println("4. Change the weight of an arc");
System.out.println("5. Change an arc to an IncomingArc_Videur or IncomingArc_Zero");
System.out.println("6. Exit");
String response = scanner.nextLine();
switch (response) {
case "1":
displayNetwork();
break;
case "2":
System.out.println("Enter the id of the transition to fire: ");
int transitionId = getIntInput(scanner);
fireTransition(String.valueOf(transitionId));
break;
case "3":
System.out.println("Enter the id of the place to change tokens: ");
int placeId = getIntInput(scanner);
System.out.println("Enter the new number of tokens: ");
int tokens = getIntInput(scanner);
Place place = this.FindPlaceById(placeId);
if (place == null) {
break;
}
place.setTokenCount(tokens);
break;
case "4":
System.out.println("Enter the id of the arc to change weight: ");
int arcId = getIntInput(scanner);
System.out.println("Enter the new weight: ");
int weight = getIntInput(scanner);
Arc arc = this.FindArcById(arcId);
if (arc == null) {
break;
}
arc.setWeight(weight);
break;
case "5":
System.out.println("Enter the id of the arc to change: ");
int arcToChangeId = getIntInput(scanner);
Arc arcToChange = this.FindArcById(arcToChangeId);
if (arcToChange == null) {
break;
}
if (arcToChange instanceof IncomingArc) {
System.out.println("Choose the type of arc to change to:");
System.out.println("1. IncomingArc_Videur");
System.out.println("2. IncomingArc_Zero");
int arcType = getIntInput(scanner);
Transition transition = arcToChange.getTransition();
Place placeToChange = arcToChange.getPlace();
this.removeArc(arcToChange);
if (arcType == 1) {
IncomingArc_Videur newArc = new IncomingArc_Videur(transition, placeToChange, arcToChange.getWeight(), this.generateId(0));
this.addArc(newArc);
transition.addIncomingArc(newArc);
System.out.println("Arc changed to IncomingArc_Videur.");
} else if (arcType == 2) {
IncomingArc_Zero newArc = new IncomingArc_Zero(transition, placeToChange, arcToChange.getWeight(), this.generateId(0));
this.addArc(newArc);
transition.addIncomingArc(newArc);
System.out.println("Arc changed to IncomingArc_Zero.");
} else {
System.out.println("Invalid arc type.");
}
} else {
System.out.println("Arc is not an IncomingArc.");
}
break;
case "6":
return; // Exit the method
default:
System.out.println("Invalid option. Please try again.");
}
}
} else {
System.out.println("Transition being validated");
......@@ -346,12 +434,24 @@
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() + " ("
System.out.println(arc.getId() + " : "
+ (arc instanceof IncomingArc_Videur ? "videur" : (arc instanceof IncomingArc_Zero ? "zero" : "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() + " ("
System.out.println(arc.getId() + " : outgoing arc with weight " + arc.getWeight() + " ("
+ arc.getTransition().getName() + " to " + "Place with Id " + arc.getPlace().getId() + ")");
}
}
}
private int getIntInput(Scanner scanner) {
while (!scanner.hasNextInt()) {
System.out.println("Invalid input. Please enter an integer.");
scanner.next(); // discard invalid input
}
int input = scanner.nextInt();
scanner.nextLine(); // consume the newline character
return input;
}
}
\ No newline at end of file
......@@ -14,6 +14,10 @@ public interface PetriNetService {
void removeArc(Arc arc);
Place FindPlaceById(int id);
Arc FindArcById(int id);
void displayState();
/**
......
......@@ -49,6 +49,5 @@ public class Place {
this.tokenCount = tokenCount;
}
}
// A function to find the incoming arcs of a place
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment