Skip to content
Snippets Groups Projects
Commit 1d04805b authored by Sebastian Ebert's avatar Sebastian Ebert
Browse files

bugfixes on export, added more nav utils

parent fb17d71f
Branches
No related tags found
No related merge requests found
package de.tudresden.inf.st.pnml.base.util;
import de.tudresden.inf.st.pnml.jastadd.model.*;
import java.util.*;
public class ServiceUtils {
public static Page treeCopyServicePage(PetriNet petriNet, Page servicePrototypePage, int instanceId){
System.out.println("[ServiceUtils] Copying server page " + servicePrototypePage.getId() + " (" + instanceId + ").");
Page copy = treeCopyWithoutArcs(petriNet, servicePrototypePage);
updateInstanceIds(copy, "-INSTANCE-" + instanceId);
copy.setId(servicePrototypePage.getId() + "-INSTANCE-" + instanceId);
copy.getName().setText(copy.getId());
reintegrateArcs(servicePrototypePage, copy, "-INSTANCE-" + instanceId);
return copy;
}
private static void updateInstanceIds(Page page, String suffix) {
for (PnObject pnObject : page.getObjects()) {
pnObject.setId(pnObject.getId() + suffix);
if (pnObject.hasName()) {
Name name = new Name();
name.setText(pnObject.getId());
pnObject.setName(name);
} else {
pnObject.getName().setText(pnObject.getId());
}
if (pnObject.isPageNode()) {
updateInstanceIds(pnObject.asPage(), suffix);
}
}
}
private static void reintegrateArcs(Page original, Page copy, String suffix) {
Set<Arc> originalArcs = new HashSet<>();
getArcsInPage(original, originalArcs);
for (Arc a : originalArcs) {
Page originalArcPage = Objects.requireNonNull(getObjectInPage(original, a.getId()))._2;
Page arcPage = Objects.requireNonNull(getObjectInPage(copy, originalArcPage.getId() + suffix))._1.asPage();
Pair<PnObject, Page> source = getObjectInPage(copy, a.getSource().getId() + suffix);
Pair<PnObject, Page> target = getObjectInPage(copy, a.getTarget().getId() + suffix);
Arc copyArc = new Arc();
assert source != null;
copyArc.setSource(source._1.asNode());
assert target != null;
copyArc.setTarget(target._1.asNode());
copyArc.setId(a.getId() + suffix);
Name n = new Name();
n.setText(a.getId() + suffix);
copyArc.setName(n);
arcPage.addObject(copyArc);
}
}
private static Pair<PnObject, Page> getObjectInPage(Page page, String id) {
if (page.getId().equals(id)) {
return new Pair<>(page, page);
}
for (PnObject o : page.getObjects()) {
if (o.getId().equals(id)) {
return new Pair<>(o, page);
}
if (o.isPageNode()) {
getObjectInPage(o.asPage(), id);
}
}
return null;
}
private static void getArcsInPage(Page page, Set<Arc> arcs) {
for (PnObject o : page.getObjects()) {
if (o.isArcNode()) {
arcs.add(o.asArc());
}
if (o.isPageNode()) {
getArcsInPage(o.asPage(), arcs);
}
}
}
private static Page treeCopyWithoutArcs(PetriNet petriNet, Page currentPage) {
return treeCopyWithoutArcsInternal(petriNet, currentPage, null);
}
private static Page treeCopyWithoutArcsInternal(PetriNet petriNet, Page currentPage, Page parent) {
Map<String, PnObject> rMap = new HashMap<>();
if (parent == null) {
parent = new Page();
parent.setId(currentPage.getId());
parent.setToolspecificList(currentPage.getToolspecificList().treeCopy());
Name n = new Name();
n.setText(currentPage.getId());
parent.setName(n);
}
for (PnObject pnObject : currentPage.getObjects()) {
if (pnObject.isPageNode()) {
Page copyPage = new Page();
copyPage.setId(pnObject.asPage().getId());
copyPage.setToolspecificList(pnObject.asPage().getToolspecificList().treeCopy());
copyPage.setName(pnObject.getName().treeCopy());
parent.addObject(copyPage);
treeCopyWithoutArcsInternal(petriNet, pnObject.asPage(), copyPage);
}
if (pnObject.isPlaceObject()) {
DinerosPlace copyP = new DinerosPlace();
copyP.setId(pnObject.getId());
copyP.setName(pnObject.getName().treeCopy());
if(pnObject.asDinerosPlace().getInitialMarking() == null){
System.out.println("Defaulting initial marking of " + pnObject.getId() + " to 0.");
PTMarking defaultMarking = new PTMarking();
defaultMarking.setText(0);
pnObject.asDinerosPlace().setInitialMarking(defaultMarking);
} else {
copyP.setInitialMarking(pnObject.asDinerosPlace().getInitialMarking().treeCopy());
}
copyP.setToolspecificList(pnObject.getToolspecificList().treeCopy());
parent.addObject(copyP);
rMap.put(copyP.getId(), copyP);
}
if (pnObject.isTransitionObject()) {
DinerosTransition copyT = new DinerosTransition();
copyT.setId(pnObject.getId());
copyT.setName(pnObject.getName().treeCopy());
copyT.setToolspecificList(pnObject.getToolspecificList().treeCopy());
parent.addObject(copyT);
rMap.put(copyT.getId(), copyT);
}
}
for (PnObject pnObject : currentPage.getObjects()) {
Name n = new Name();
n.setText(pnObject.getId());
if (pnObject.isRefTransitionObject()) {
RefTransition copyRt = new RefTransition();
copyRt.setId(pnObject.getId());
copyRt.setName(n.treeCopy());
copyRt.setRef(Objects.requireNonNull(getReferencedObject(petriNet,
rMap, pnObject.asRefTransitionObject().getRef().getId())).asDinerosTransition());
copyRt.setToolspecificList(pnObject.getToolspecificList().treeCopy());
parent.addObject(copyRt);
}
if (pnObject.isRefPlaceObject()) {
RefPlace copyRp = new RefPlace();
copyRp.setId(pnObject.getId());
copyRp.setName(n.treeCopy());
copyRp.setRef(Objects.requireNonNull(getReferencedObject(petriNet,
rMap, pnObject.asRefPlaceObject().getRef().getId())).asDinerosPlace());
copyRp.setToolspecificList(pnObject.getToolspecificList().treeCopy());
parent.addObject(copyRp);
}
}
return parent;
}
private static PnObject getReferencedObject(PetriNet petriNet, Map<String, PnObject> rMap, String id){
PnObject referencedObject = rMap.get(id);
if(referencedObject == null){
System.out.println("Searching for external referenced object: " + id);
for(PnObject pnObject : petriNet.allObjects()){
if(pnObject.getId().equals(id)){
System.out.println("Found external referenced object: " + id);
return pnObject;
}
}
} else {
return referencedObject;
}
return null;
}
}
\ No newline at end of file
......@@ -96,6 +96,18 @@ aspect Navigation {
syn boolean PlaceNode.isRefPlace() = false;
eq RefPlace.isRefPlace() = true;
syn boolean PnObject.isRefPlaceObject() = false;
eq RefPlace.isRefPlaceObject() = true;
syn RefPlace PnObject.asRefPlaceObject() = null;
eq RefPlace.asRefPlaceObject() = this;
syn boolean PnObject.isRefTransitionObject() = false;
eq RefTransition.isRefTransitionObject() = true;
syn RefTransition PnObject.asRefTransitionObject() = null;
eq RefTransition.asRefTransitionObject() = this;
syn Place PlaceNode.asPlace() = null;
eq Place.asPlace() = this;
......
......@@ -146,6 +146,7 @@ aspect PnmlExporter{
private static Set<RefPlaceHLAPI> convertAndIncludeRefPlaces(PetriNet petriNet, Set<PageHLAPI> convertedPages, Set<PlaceHLAPI> convertedPlaces) {
Set<RefPlaceHLAPI> convertedRefPlaces = new HashSet<>();
Set<java.lang.String> convertedRefPlaceIDs = new HashSet<>();
PlaceHLAPI rootPlace = null;
// get the root transition to construct reference tree
......@@ -159,15 +160,19 @@ aspect PnmlExporter{
for (RefPlace p_one : petriNet.allRefPlaces()) {
assert rootPlace != null;
if (p_one.getRef().getId().equals(rootPlace.getId())) {
RefPlaceHLAPI rph = PrimitiveElementsConverter.convertRefPlaceToPnmlObject(p_one, rootPlace, null);
for (PageHLAPI pageHLAPI : convertedPages) {
if (pageHLAPI.getId().equals(p_one.ContainingPage().getId())) {
assert rph != null;
rph.setContainerPageHLAPI(pageHLAPI);
break;
if(!convertedRefPlaceIDs.contains(p_one.getId())) {
System.out.println("Exporting Ref-Place (l1): " + p_one.getId());
RefPlaceHLAPI rph = PrimitiveElementsConverter.convertRefPlaceToPnmlObject(p_one, rootPlace, null);
for (PageHLAPI pageHLAPI : convertedPages) {
if (pageHLAPI.getId().equals(p_one.ContainingPage().getId())) {
assert rph != null;
rph.setContainerPageHLAPI(pageHLAPI);
break;
}
}
convertedRefPlaces.add(rph);
convertedRefPlaceIDs.add(p_one.getId());
}
convertedRefPlaces.add(rph);
}
}
......@@ -175,15 +180,19 @@ aspect PnmlExporter{
for (RefPlace p_tree : petriNet.allRefPlaces()) {
for (RefPlaceHLAPI convRef : convertedRefPlaces) {
if (convRef.getId().equals(p_tree.getRef().getId())) {
RefPlaceHLAPI rph = PrimitiveElementsConverter.convertRefPlaceToPnmlObject(p_tree, null, convRef);
for (PageHLAPI pageHLAPI : convertedPages) {
if (pageHLAPI.getId().equals(p_tree.ContainingPage().getId())) {
assert rph != null;
rph.setContainerPageHLAPI(pageHLAPI);
break;
if(!convertedRefPlaceIDs.contains(p_tree.getId())) {
System.out.println("Exporting Ref-Place (ln): " + p_tree.getId());
RefPlaceHLAPI rph = PrimitiveElementsConverter.convertRefPlaceToPnmlObject(p_tree, null, convRef);
for (PageHLAPI pageHLAPI : convertedPages) {
if (pageHLAPI.getId().equals(p_tree.ContainingPage().getId())) {
assert rph != null;
rph.setContainerPageHLAPI(pageHLAPI);
break;
}
}
convertedRefPlaces.add(rph);
convertedRefPlaceIDs.add(p_tree.getId());
}
convertedRefPlaces.add(rph);
}
}
}
......@@ -251,6 +260,7 @@ aspect PnmlExporter{
}
for (PlaceHLAPI ph : convertedPlaces) {
if (ph.getId().equals(a.getSource().getId())) {
source = ph;
break;
......@@ -301,6 +311,54 @@ aspect PnmlExporter{
convertedArcs.add(convertedArc);
}
for(ArcHLAPI convertedArc: convertedArcs){
if(convertedArc.getTarget() == null){
System.out.println("[ERROR] Missing target on arc " + convertedArc.getId());
System.out.println(" >> Searching original of arc " + convertedArc.getId() + " ... ");
for(Arc a : petriNet.allArcs()){
if(a.getId().equals(convertedArc.getId())){
System.out.println(" >> Found original arc for " + convertedArc.getId() + " ... ");
System.out.println(" >> Target: " + a.getTarget().getId());
System.out.println(" >> Searching target in places ...");
for (PlaceHLAPI ph : convertedPlaces) {
if(ph.getId().equals(a.getTarget().getId())){
System.out.println(" >> Found it");
break;
}
}
System.out.println(" >> Searching target in transitions ...");
for (TransitionHLAPI th : convertedTransitions) {
if(th.getId().equals(a.getTarget().getId())){
System.out.println(" >> Found it");
break;
}
}
System.out.println(" >> Searching target in ref-places ...");
for (RefPlaceHLAPI rth : convertedRefPlaces) {
if(rth.getId().equals(a.getTarget().getId())){
System.out.println(" >> Found it");
break;
}
}
System.out.println(" >> Searching target in ref-transitions ...");
for (RefTransitionHLAPI tth : convertedRefTransitions) {
if(tth.getId().equals(a.getTarget().getId())){
System.out.println(" >> Found it");
break;
}
}
break;
}
}
}
}
return convertedArcs;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment