projet-mini-genie-logiciel-.../eclipse-workspace/fr.n7.simplepdl/src/simplepdl/manip/simplepdl2petrinet.java

244 lines
7.3 KiB
Java
Raw Normal View History

2021-10-09 16:39:13 +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;
2021-10-09 22:23:48 +00:00
import petrinet.Arc;
import petrinet.Network;
2021-10-10 11:48:27 +00:00
import petrinet.Node;
2021-10-09 22:23:48 +00:00
import petrinet.PetrinetFactory;
2021-10-12 16:49:13 +00:00
import petrinet.PetrinetPackage;
import petrinet.Place;
import petrinet.Transition;
2021-10-09 16:39:13 +00:00
import simplepdl.Process;
2021-10-12 16:49:13 +00:00
import simplepdl.SimplepdlPackage;
2021-10-09 16:39:13 +00:00
import simplepdl.WorkDefinition;
import simplepdl.WorkSequence;
import simplepdl.WorkSequenceType;
2021-10-12 16:49:13 +00:00
import simplepdl.Request;
2021-10-09 16:39:13 +00:00
public class simplepdl2petrinet {
public static void main(String[] args) {
2021-10-09 22:23:48 +00:00
// Charger les package SimplePDL et Petrinet afin de les enregistrer dans le registre d'Eclipse.
2021-10-09 16:39:13 +00:00
SimplepdlPackage packageInstance = SimplepdlPackage.eINSTANCE;
2021-10-09 22:23:48 +00:00
PetrinetPackage packageInstance2 = PetrinetPackage.eINSTANCE;
2021-10-09 16:39:13 +00:00
2021-10-09 22:23:48 +00:00
// Enregistrer l'extension ".xmi" comme devant être ouverte à
2021-10-09 16:39:13 +00:00
// 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-09 22:23:48 +00:00
// Créer un objet resourceSetImpl qui contiendra une ressource EMF (le modèle)
2021-10-09 16:39:13 +00:00
ResourceSet resSet = new ResourceSetImpl();
2021-10-09 22:23:48 +00:00
// Charger la ressource (notre modèle)
2021-10-12 16:49:13 +00:00
URI modelURI = URI.createURI("test.xmi");
2021-10-09 22:23:48 +00:00
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);
2021-10-09 16:39:13 +00:00
2021-10-09 22:23:48 +00:00
// 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 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);
2021-10-10 11:48:27 +00:00
Place started = myFactory.createPlace();
started.setName(name + "_started");
started.setTokens(0);
2021-10-09 22:23:48 +00:00
Place running = myFactory.createPlace();
running.setName(name + "_running");
2021-10-10 11:48:27 +00:00
running.setTokens(0);
2021-10-09 22:23:48 +00:00
Place finished = myFactory.createPlace();
finished.setName(name + "_finished");
2021-10-10 11:48:27 +00:00
finished.setTokens(0);
2021-10-09 22:23:48 +00:00
Arc pause2start = myFactory.createArc();
pause2start.setTarget(idle);
pause2start.setOutgoing(false);
pause2start.setWeight(1);
Arc start2running = myFactory.createArc();
start2running.setTarget(running);
start2running.setOutgoing(true);
start2running.setWeight(1);
2021-10-10 11:48:27 +00:00
Arc start2started = myFactory.createArc();
start2started.setTarget(started);
start2started.setOutgoing(true);
start2started.setWeight(1);
2021-10-09 22:23:48 +00:00
Transition start = myFactory.createTransition();
start.setName(name + "_start");
start.getArcs().add(pause2start);
start.getArcs().add(start2running);
2021-10-10 11:48:27 +00:00
start.getArcs().add(start2started);
2021-10-09 22:23:48 +00:00
Arc running2finish = myFactory.createArc();
running2finish.setTarget(running);
running2finish.setOutgoing(false);
running2finish.setWeight(1);
Arc finish2finished = myFactory.createArc();
finish2finished.setTarget(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-10 11:48:27 +00:00
2021-10-09 22:23:48 +00:00
network.getNodes().add(idle);
network.getNodes().add(start);
2021-10-10 11:48:27 +00:00
network.getNodes().add(started);
2021-10-09 22:23:48 +00:00
network.getNodes().add(running);
network.getNodes().add(finish);
network.getNodes().add(finished);
}
}
2021-10-09 16:39:13 +00:00
2021-10-10 11:48:27 +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();
// 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.setTarget(place);
arc2.setTarget(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-12 16:49:13 +00:00
}
}
// Conversion des Resource et Request en Node et Arcs
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);
res.setTokens(qty);
network.getNodes().add(res);
}
if (o instanceof simplepdl.Request) {
Request r = (Request) o;
simplepdl.Resource target = r.getTarget();
int qty = r.getQuantity();
Arc arc1 = myFactory.createArc();
arc1.setOutgoing(true);
arc1.setWeight(qty);
Arc arc2 = myFactory.createArc();
arc2.setOutgoing(false);
arc2.setWeight(qty);
for (Node node : network.getNodes()) {
if (node instanceof Place) {
Place place = (Place) node;
if (place.getName().equals(target.getName()) {
arc1.setTarget(place);
arc2.setTarget(place);
}
}
}
for (Node node : network.getNodes()) {
if (node instanceof Transition) {
Transition t = (Transition) node;
if (t.getName().equals() {
arc1.setTarget(place);
arc2.setTarget(place);
}
}
}
2021-10-10 11:48:27 +00:00
}
}
2021-10-09 22:23:48 +00:00
// Créer le nouveau xmi (modèle convertit)
2021-10-10 11:48:27 +00:00
URI convURI = URI.createURI("models/Developpement_petrinet.xmi");
2021-10-09 22:23:48 +00:00
Resource conv = resSet.createResource(convURI);
2021-10-09 16:39:13 +00:00
2021-10-09 22:23:48 +00:00
// Ajouter le Network dans le nouveau modèle
conv.getContents().add(network);
2021-10-09 16:39:13 +00:00
// Sauver la ressource
try {
2021-10-09 22:23:48 +00:00
conv.save(Collections.EMPTY_MAP);
2021-10-09 16:39:13 +00:00
} catch (IOException e) {
e.printStackTrace();
}
}
}