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

Petri Net corrigé sans Test Main, sans Arcs videurs ...

parent 36f7080e
Branches
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CheckStyle-IDEA" serialisationVersion="2">
<checkstyleVersion>10.18.1</checkstyleVersion>
<scanScope>JavaOnly</scanScope>
<option name="thirdPartyClasspath" />
<option name="activeLocationIds" />
<option name="locations">
<list>
<ConfigurationLocation id="bundled-sun-checks" type="BUNDLED" scope="All" description="Sun Checks">(bundled)</ConfigurationLocation>
<ConfigurationLocation id="bundled-google-checks" type="BUNDLED" scope="All" description="Google Checks">(bundled)</ConfigurationLocation>
</list>
</option>
</component>
</project>
\ No newline at end of file
......@@ -11,5 +11,5 @@
<component name="PDMPlugin">
<option name="skipTestSources" value="false" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK" />
</project>
\ No newline at end of file
package org.petriNet;
public class Arc {
protected int poid;
protected int id;
public Arc(int poid,int id) {
this.poid = poid;
public abstract class Arc {
private int id;
private Place place;
private Transition transition;
private int poids;
public Arc(Place place, Transition transition, int poids, int id) {
this.place = place;
this.transition = transition;
this.poids = poids;
this.id = id;
}
public void modifier_poid(int nouveauPoid) {
this.poid = nouveauPoid;
}
public boolean valider() {
return true;
}
public abstract void modifierPoids(int poids);
public int getPoid() {
return poid;
}
public abstract void valider();
public void setPoid(int poid) {
this.poid = poid;
}
public abstract int getPoids() ;
public abstract int getId();
public abstract void setId(int id);
public abstract void setPoid(int poids);
public abstract Place getPlace();
public abstract void setPlace(Place place);
public abstract Transition getTransition();
}
......@@ -2,6 +2,10 @@ package org.petriNet;
public class Arc_ENTRANT extends Arc {
private Place place;
private Transition transition;
private int poids;
public Arc_ENTRANT(Place place, Transition transition, int poids) {
super(place, transition, poids);
}
......@@ -19,13 +23,49 @@ public class Arc_ENTRANT extends Arc {
public void enleverJetons() {
// On retire le nombre de jetons du poids de l'arc
this.place.enleverJetons(this.poids);
this.place.enlever_jeton(this.poids);
}
public boolean verifier_tirable() {
if (this.place.get_nombre_jetons() >= this.poids) {
return true;
}
return false;
}
@Override
public void valider() {
// On retire le nombre de jetons du poids de l'arc
this.place.enleverJetons(this.poids);
// prendre en cond la place choisie
this.place.enlever_jeton(this.poids);
}
@Override
public int getPoids() {
return 0;
}
@Override
public void setPoid(int poids) {
}
@Override
public Place getPlace() {
return null;
}
@Override
public void setPlace(Place place) {
}
@Override
public Transition getTransition() {
return null;
}
}
\ No newline at end of file
package org.petriNet;
public abstract class Arc_SORTANT extends Arc {
public Arc_SORTANT(Place place, Transition transition, int poids) {
super(place, transition, poids);
private Place place;
private Transition transition;
private int poids;
private int id;
public Arc_SORTANT(Place place, Transition transition, int poids, int id) {
super(place, transition, poids, id);
}
@Override
......@@ -10,10 +16,12 @@ public abstract class Arc_SORTANT extends Arc {
this.poids = poids;
}
@Override
public void valider() {
// On ajoute le nombre de jetons du poids de l'arc
this.place.ajouterJetons(this.poids);
this.place.ajouter_jeton(this.poids);
}
/**
......@@ -25,7 +33,7 @@ public abstract class Arc_SORTANT extends Arc {
public void ajouterJetons() {
// On ajoute le nombre de jetons du poids de l'arc
this.place.ajouterJetons(this.poids);
this.place.ajouter_jeton(this.poids);
}
......
package org.petriNet;
public class Arc_sortant_simple {
}
package org.petriNet;
public class Arc_videur {
}
package org.petriNet;
public class Arc_zero {
}
......@@ -8,8 +8,6 @@ public interface PetriNetService {
public void ajouterArc(Arc arc);
public void tirerTransition(Transition transition);
public void afficherEtat();
/**
......@@ -19,4 +17,5 @@ public interface PetriNetService {
*/
public void afficherReseau();
public void tirer_transition(String id);
}
......@@ -9,13 +9,13 @@ public class Place {
this.nombre_jeton = nombre_jeton;
}
public void ajouter_jeton() {
this.nombre_jeton++;
public void ajouter_jeton(int jetons) {
this.nombre_jeton += jetons;
}
public void enlever_jeton() {
public void enlever_jeton(int jetons) {
if (this.nombre_jeton > 0) {
this.nombre_jeton--;
this.nombre_jeton -= jetons;
} else {
System.out.println("No more tokens to remove.");
}
......
package org.petriNet;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReseauPerti implements PetriNetService {
// Creat attributes as Lists of Places, Transitions and Arcs
......@@ -7,15 +11,40 @@ public class ReseauPerti implements PetriNetService {
private List<Place> places;
private List<Transition> transitions;
private List<Arc> arcs;
private String etat_reseau = "Pas de transition tirée";
// Create a constructor
public ReseauPerti() {
// initialiser les listes vide
this.places = new ArrayList<Place>();
this.transitions = new ArrayList<Transition>();
this.arcs = new ArrayList<Arc>();
this.places = new ArrayList<>();
this.transitions = new ArrayList<>();
this.arcs = new ArrayList<>();
}
// 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 List<Arc> getArcs() {
return this.arcs;
}
public void setArcs(List<Arc> arcs) {
this.arcs = arcs;
}
@Override
......@@ -37,28 +66,71 @@ public class ReseauPerti implements PetriNetService {
}
@Override
public void tirerTransition(Transition transition) {
// TODO Auto-generated method stub
// si la transition est tirable
if (transition.est_tirable()) {
// tirer la transition
transition.tirer();
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 (transition.getId() == Integer.parseInt(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 {
// Check if the transition is tirable
transition_choisie.est_tirable();
}
// Ask if the user wants to validate the firing of the transition
Scanner scanner = new Scanner(System.in);
System.out.println("Voulez-vous valider le tirage de la transition? (O/N)");
String reponse = scanner.nextLine();
if (reponse.equals("O")) {
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
// afficher le nombre de jetons dans chaque place
for (Place place : places) {
System.out.println("Place " + place.getId() + " : " + place.getNbrJetons());
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 afficherReseauPetri() {
public void afficherReseau() {
// TODO Auto-generated method stub
System.out.println(this.etat_reseau);
/**
* afficher les places, les transitions et les arcs
* comme un dessin du réseau de petri
......@@ -78,5 +150,6 @@ public class ReseauPerti implements PetriNetService {
}
}
}
package org.petriNet;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Transition {
private int id;
private List<Arc_SORTANT> arcs_SORTANTS;
private List<Arc_ENTRANT> arcs_ENTRANTS;
private List<Arc_ENTRANT> arcs_ENTRANTS_TIRABLE = new ArrayList<Arc_ENTRANT>();
public Transition(int id, List<Arc_SORTANT> arcs_SORTANTS, List<Arc_ENTRANT> arcs_ENTRANTS) {
this.id = id;
......@@ -17,6 +22,22 @@ public class Transition {
* c'est une différence par rapport au diagramme de classe soumis
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Arc_SORTANT> getArcs_SORTANTS() {
return arcs_SORTANTS;
}
public void setArcs_SORTANTS(List<Arc_SORTANT> arcs_SORTANTS) {
this.arcs_SORTANTS = arcs_SORTANTS;
}
public void ajouterArc_SORTANT(Arc_SORTANT arc_SORTANT) {
this.arcs_SORTANTS.add(arc_SORTANT);
}
......@@ -25,41 +46,52 @@ public class Transition {
this.arcs_ENTRANTS.add(arc_ENTRANT);
}
public boolean est_tirable() {
// verifier si la transition est tirable à travers l'ARC_SORTANT
int n_jetons_necessaires;
int n_jetons_existant;
// pour chaque arc sortant de la transition
// n_jetons_necessaires += arc_SORTANT.getPoids();
for (Arc_SORTANT arc_SORTANT : arcs_SORTANTS) {
n_jetons_necessaires += arc_SORTANT.getPoids();
}
public void est_tirable() {
// creer liste vide d'arcs
this.arcs_ENTRANTS_TIRABLE = new ArrayList<Arc_ENTRANT>();
// pour chaque arc entrant de la transition
// n_jetons_existant += arc_ENTRANT.getPoids();
for (Arc_ENTRANT arc_ENTRANT : arcs_ENTRANTS) {
n_jetons_existant += arc_ENTRANT.getPoids();
if (arc_ENTRANT.verifier_tirable()) {
arcs_ENTRANTS_TIRABLE.add(arc_ENTRANT);
}
}
// si n_jetons_necessaires <= n_jetons_existant
// return true;
if (n_jetons_necessaires <= n_jetons_existant) {
return true;
if(arcs_ENTRANTS_TIRABLE.isEmpty()){
System.out.println("La transition n'est pas tirable");
}
else {
System.out.println("La transition est tirable");
}
}
public void tirer(){
// fait appel à valider() dans les classes Arc_SORTANT et
// Arc_ENTRANT pour modifier les jetons
for (Arc_ENTRANT arc_ENTRANT : arcs_ENTRANTS) {
arc_ENTRANT.valider();
est_tirable();
// print the list of arcs entrants tirables
System.out.println("Arcs entrants tirable: " + this.arcs_ENTRANTS_TIRABLE
+ "Saisir l'arc entrant à tirer: ");
// copilot get input from user of the id of the arc entrant to tirer (java.util.Scanner)
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
// find the Arc_ENTRANT by id
Arc_ENTRANT arc_ENTRANT_toTirer = null;
for (Arc_ENTRANT arc_ENTRANT : this.arcs_ENTRANTS_TIRABLE) {
if (arc_ENTRANT.getId() == id) {
arc_ENTRANT_toTirer = arc_ENTRANT;
}
}
// valider l'arc entrant
arc_ENTRANT_toTirer.valider();
for (Arc_SORTANT arc_SORTANT : arcs_SORTANTS) {
arc_SORTANT.valider();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment