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

125 lines
4.1 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.PetrinetPackage;
import petrinet.Place;
import petrinet.Transition;
import petrinet.Arc;
import petrinet.Network;
import petrinet.PetrinetFactory;
2021-10-09 16:39:13 +00:00
import simplepdl.Process;
import simplepdl.WorkDefinition;
import simplepdl.WorkSequence;
import simplepdl.WorkSequenceType;
import simplepdl.SimplepdlPackage;
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)
URI modelURI = URI.createURI("pdl-sujet.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);
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();
System.out.println(" - " + name);
Place idle = myFactory.createPlace();
idle.setName(name + "_idle");
idle.setTokens(1);
Place running = myFactory.createPlace();
running.setName(name + "_running");
running.setTokens(1);
Place finished = myFactory.createPlace();
finished.setName(name + "_finished");
finished.setTokens(1);
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);
Transition start = myFactory.createTransition();
start.setName(name + "_start");
start.getArcs().add(pause2start);
start.getArcs().add(start2running);
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);
System.out.println(" + " + idle.getName() + " (" + idle.getTokens() + ")");
network.getNodes().add(idle);
network.getNodes().add(start);
network.getNodes().add(running);
network.getNodes().add(finish);
network.getNodes().add(finished);
}
}
2021-10-09 16:39:13 +00:00
2021-10-09 22:23:48 +00:00
// Créer le nouveau xmi (modèle convertit)
URI convURI = URI.createURI("models/test.xmi");
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();
}
}
}