projet-mini-genie-logiciel-.../livrables/SimplePDL2PetriNet.java
Laureηt 52aacc4288
xD
2021-10-20 16:57:21 +02:00

237 lines
7.2 KiB
Java

package simplepdl.manip;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import petrinet.Arc;
import petrinet.Network;
import petrinet.Node;
import petrinet.PetrinetFactory;
import petrinet.PetrinetPackage;
import petrinet.Place;
import petrinet.Transition;
import simplepdl.Process;
import simplepdl.SimplepdlPackage;
import simplepdl.WorkDefinition;
import simplepdl.WorkSequence;
import simplepdl.WorkSequenceType;
import simplepdl.Request;
public class SimplePDL2PetriNet {
public static void main(String[] args) {
// Charger les package SimplePDL et Petrinet afin de les enregistrer dans le
// registre d'Eclipse.
SimplepdlPackage packageInstance = SimplepdlPackage.eINSTANCE;
PetrinetPackage packageInstance2 = PetrinetPackage.eINSTANCE;
// Enregistrer l'extension ".xmi" comme devant être ouverte à
// l'aide d'un objet "XMIResourceFactoryImpl"
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
m.put("xmi", new XMIResourceFactoryImpl());
// Créer un objet resourceSetImpl qui contiendra une ressource EMF (le modèle)
ResourceSet resSet = new ResourceSetImpl();
// Charger la ressource (notre modèle)
URI modelURI = URI.createURI("models/developpement.xmi");
Resource resource = resSet.getResource(modelURI, true);
// Récupérer le premier élément du modèle (élément racine)
Process process = (Process) resource.getContents().get(0);
// La fabrique pour fabriquer les éléments de PetriNET
PetrinetFactory myFactory = PetrinetFactory.eINSTANCE;
// Créer un élément Network
Network network = myFactory.createNetwork();
network.setName(process.getName());
// Conversion des Resource en Places
for (Object o : process.getProcessElements()) {
if (o instanceof simplepdl.Resource) {
simplepdl.Resource r = (simplepdl.Resource) o;
String name = r.getName();
int qty = r.getQuantity();
Place res = myFactory.createPlace();
res.setName(name + "_resource");
res.setTokens(qty);
network.getNodes().add(res);
}
}
// Conversion des WorkDefinition en Node et Transition
for (Object o : process.getProcessElements()) {
if (o instanceof WorkDefinition) {
WorkDefinition wd = (WorkDefinition) o;
String name = wd.getName();
Place idle = myFactory.createPlace();
idle.setName(name + "_idle");
idle.setTokens(1);
Place started = myFactory.createPlace();
started.setName(name + "_started");
started.setTokens(0);
Place running = myFactory.createPlace();
running.setName(name + "_running");
running.setTokens(0);
Place finished = myFactory.createPlace();
finished.setName(name + "_finished");
finished.setTokens(0);
Arc pause2start = myFactory.createArc();
pause2start.setPlace(idle);
pause2start.setOutgoing(false);
pause2start.setWeight(1);
Arc start2running = myFactory.createArc();
start2running.setPlace(running);
start2running.setOutgoing(true);
start2running.setWeight(1);
Arc start2started = myFactory.createArc();
start2started.setPlace(started);
start2started.setOutgoing(true);
start2started.setWeight(1);
Transition start = myFactory.createTransition();
start.setName(name + "_start");
start.getArcs().add(pause2start);
start.getArcs().add(start2running);
start.getArcs().add(start2started);
Arc running2finish = myFactory.createArc();
running2finish.setPlace(running);
running2finish.setOutgoing(false);
running2finish.setWeight(1);
Arc finish2finished = myFactory.createArc();
finish2finished.setPlace(finished);
finish2finished.setOutgoing(true);
finish2finished.setWeight(1);
Transition finish = myFactory.createTransition();
finish.setName(name + "_finish");
finish.getArcs().add(running2finish);
finish.getArcs().add(finish2finished);
network.getNodes().add(idle);
network.getNodes().add(start);
network.getNodes().add(started);
network.getNodes().add(running);
network.getNodes().add(finish);
network.getNodes().add(finished);
// Conversion des Requests s'il y en a
for (Request req : wd.getRequests()) {
int qty = req.getQuantity();
Place target = null;
for (Node node : network.getNodes()) {
if (node instanceof Place) {
Place place = (Place) node;
if (place.getName().equals(req.getTarget().getName() + "_resource")) {
target = place;
}
}
}
Arc res2start = myFactory.createArc();
res2start.setPlace(target);
res2start.setOutgoing(false);
res2start.setWeight(qty);
Arc finish2res = myFactory.createArc();
finish2res.setPlace(target);
finish2res.setOutgoing(true);
finish2res.setWeight(qty);
start.getArcs().add(res2start);
finish.getArcs().add(finish2res);
}
}
}
// Conversion des WorkSequence en Node et Transition
for (Object o : process.getProcessElements()) {
if (o instanceof WorkSequence) {
WorkSequence ws = (WorkSequence) o;
WorkSequenceType type = ws.getLinkType();
WorkDefinition predecessor = ws.getPredecessor();
WorkDefinition successor = ws.getSuccessor();
// creation des suffixs permettant la recherche des noeuds
String predecessor_suffix = new String();
String successor_suffix = new String();
switch (type) {
case START_TO_START:
predecessor_suffix = "_started";
successor_suffix = "_start";
break;
case START_TO_FINISH:
predecessor_suffix = "_started";
successor_suffix = "_finished";
break;
case FINISH_TO_START:
predecessor_suffix = "_finished";
successor_suffix = "_start";
break;
case FINISH_TO_FINISH:
predecessor_suffix = "_finished";
successor_suffix = "_finish";
break;
default:
System.out.print("the fuck ?");
break;
}
// creation du read-arc
Arc arc1 = myFactory.createArc();
arc1.setOutgoing(false);
arc1.setWeight(1);
Arc arc2 = myFactory.createArc();
arc2.setOutgoing(true);
arc2.setWeight(1);
for (Node node : network.getNodes()) {
if (node instanceof Place) {
Place place = (Place) node;
if (place.getName().equals(predecessor.getName() + predecessor_suffix)) {
arc1.setPlace(place);
arc2.setPlace(place);
}
}
if (node instanceof Transition) {
Transition transition = (Transition) node;
if (transition.getName().equals(successor.getName() + successor_suffix)) {
transition.getArcs().add(arc1);
transition.getArcs().add(arc2);
}
}
}
}
}
// Créer le nouveau xmi (modèle convertit)
URI convURI = URI.createURI("../fr.n7.petrinet/models/gen/developpement_java.xmi");
Resource conv = resSet.createResource(convURI);
// Ajouter le Network dans le nouveau modèle
conv.getContents().add(network);
// Sauver la ressource
try {
conv.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
}
}