projet-mini-genie-logiciel-.../livrables/SimplePDL2PetriNet.java

237 lines
7.2 KiB
Java
Raw Normal View History

2021-10-20 14:36:15 +00:00
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;
2021-10-20 14:57:21 +00:00
public class SimplePDL2PetriNet {
2021-10-20 14:36:15 +00:00
public static void main(String[] args) {
2021-10-20 14:57:21 +00:00
// Charger les package SimplePDL et Petrinet afin de les enregistrer dans le
// registre d'Eclipse.
2021-10-20 14:36:15 +00:00
SimplepdlPackage packageInstance = SimplepdlPackage.eINSTANCE;
PetrinetPackage packageInstance2 = PetrinetPackage.eINSTANCE;
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
// 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());
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
// 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);
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
// Récupérer le premier élément du modèle (élément racine)
Process process = (Process) resource.getContents().get(0);
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
// La fabrique pour fabriquer les éléments de PetriNET
2021-10-20 14:57:21 +00:00
PetrinetFactory myFactory = PetrinetFactory.eINSTANCE;
2021-10-20 14:36:15 +00:00
// 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) {
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
simplepdl.Resource r = (simplepdl.Resource) o;
String name = r.getName();
int qty = r.getQuantity();
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
Place res = myFactory.createPlace();
res.setName(name + "_resource");
res.setTokens(qty);
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
network.getNodes().add(res);
}
}
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
// Conversion des WorkDefinition en Node et Transition
for (Object o : process.getProcessElements()) {
if (o instanceof WorkDefinition) {
WorkDefinition wd = (WorkDefinition) o;
String name = wd.getName();
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
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);
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
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);
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
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);
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
network.getNodes().add(idle);
network.getNodes().add(start);
network.getNodes().add(started);
network.getNodes().add(running);
network.getNodes().add(finish);
network.getNodes().add(finished);
2021-10-20 14:57:21 +00:00
// Conversion des Requests s'il y en a
2021-10-20 14:36:15 +00:00
for (Request req : wd.getRequests()) {
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
int qty = req.getQuantity();
Place target = null;
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
for (Node node : network.getNodes()) {
if (node instanceof Place) {
Place place = (Place) node;
if (place.getName().equals(req.getTarget().getName() + "_resource")) {
target = place;
}
}
}
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
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);
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
start.getArcs().add(res2start);
finish.getArcs().add(finish2res);
}
}
}
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
// 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();
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
// creation des suffixs permettant la recherche des noeuds
String predecessor_suffix = new String();
2021-10-20 14:57:21 +00:00
String successor_suffix = new String();
2021-10-20 14:36:15 +00:00
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;
}
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
// creation du read-arc
Arc arc1 = myFactory.createArc();
arc1.setOutgoing(false);
arc1.setWeight(1);
Arc arc2 = myFactory.createArc();
arc2.setOutgoing(true);
arc2.setWeight(1);
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
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);
}
}
}
2021-10-20 14:57:21 +00:00
}
2021-10-20 14:36:15 +00:00
}
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
// 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);
2021-10-20 14:57:21 +00:00
2021-10-20 14:36:15 +00:00
// Sauver la ressource
2021-10-20 14:57:21 +00:00
try {
conv.save(Collections.EMPTY_MAP);
2021-10-20 14:36:15 +00:00
} catch (IOException e) {
e.printStackTrace();
}
}
}