X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2FDistrictNetworkUtil.java;h=64aab473a8a726ba6f4244ea1ce91c3367af9168;hb=37304f4caf1d4252797cbaf7b40a56e212e203b4;hp=6155bf6fade9face371879bcd4ed246094991a3e;hpb=02ecca5e61d2eb17de40cc058be678b414aaad00;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java index 6155bf6f..64aab473 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java +++ b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java @@ -9,38 +9,58 @@ import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.request.PossibleVariable; +import org.simantics.db.layer0.variable.Variable; import org.simantics.diagram.stubs.DiagramResource; -import org.simantics.diagram.synchronization.graph.AddElement; import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; import org.simantics.diagram.synchronization.graph.layer.GraphLayer; import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.layer0.Layer0; +import org.simantics.modeling.ModelingResources; +import org.simantics.operation.Layer0X; public class DistrictNetworkUtil { public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException { + return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping)); + } + + public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + if (mapping == null) { + mapping = graph.getSingleObject(composite, DN.EdgeDefaultMapping); + } Resource edge = graph.newResource(); graph.claim(edge, L0.InstanceOf, DN.Edge); - Resource defaultEdgeMapping = graph.getPossibleObject(composite, DN.EdgeDefaultMapping); - graph.claim(edge, DN.HasMapping, defaultEdgeMapping); + graph.claim(edge, DN.HasMapping, mapping); - OrderedSetUtils.add(graph, composite, edge); + OrderedSetUtils.addFirst(graph, composite, edge); graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge); - AddElement.claimFreshElementName(graph, composite, edge); + claimFreshElementName(graph, composite, edge); + + // We need to put GraphLayer to newLayers so... + for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) { + IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class); + + GraphLayer gl = layerUtil.loadLayer(graph, layer); + gl.forEachTag(tag -> { + DiagramGraphUtil.tag(graph, edge, tag, true); + }); + } + return edge; } - + public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords) throws DatabaseException { Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping); return createVertex(graph, composite, coords, defaultVertexMapping); } - + public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, Resource mapping) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); @@ -54,7 +74,7 @@ public class DistrictNetworkUtil { OrderedSetUtils.add(graph, composite, vertex); graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex); - AddElement.claimFreshElementName(graph, composite, vertex); + claimFreshElementName(graph, composite, vertex); // We need to put GraphLayer to newLayers so... for (Resource layer : graph.getObjects(composite, DiagramResource.getInstance(graph).HasLayer)) { @@ -89,6 +109,8 @@ public class DistrictNetworkUtil { graph.claim(endVertexEdge, DN.HasEndVertex, master); } OrderedSetUtils.remove(graph, composite, slave); + // Remove ConsistsOf statement + graph.deny(composite, Layer0.getInstance(graph).ConsistsOf, slave); } return master; } @@ -109,4 +131,77 @@ public class DistrictNetworkUtil { return crsClass.calculateDistance(startCoords, endCoords); } + + public static final String claimFreshElementName(WriteGraph graph, Resource diagram, Resource element) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); + // Get name prefix from diagram + String namePrefix = graph.getPossibleRelatedValue2(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix); + if (namePrefix == null) + namePrefix = ""; + // Give running name to element and increment the counter attached to the diagram. + Long l = graph.getPossibleRelatedValue(diagram, DIA.HasModCount, Bindings.LONG); + if (l == null) + l = Long.valueOf(0L); + String name = namePrefix + l.toString(); + graph.claimLiteral(element, L0.HasName, name, Bindings.STRING); + graph.claimLiteral(diagram, DIA.HasModCount, ++l, Bindings.LONG); + return name; + } + + public static Resource getDiagramElement(ReadGraph graph, Resource component) throws DatabaseException { + if (component == null) + return null; + DiagramResource DIA = DiagramResource.getInstance(graph); + if (graph.isInstanceOf(component, DIA.Element)) + return component; + ModelingResources MOD = ModelingResources.getInstance(graph); + Resource element = graph.getPossibleObject(component, MOD.ComponentToElement); + return element != null && graph.isInstanceOf(element, DIA.Element) ? element : null; + } + + public static Resource getMappedElement(ReadGraph graph, Resource element) throws DatabaseException { + if (element == null) + return null; + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + return graph.getPossibleObject(element, DN.MappedComponent); + } + + public static Resource getMappedComponent(ReadGraph graph, Resource element) throws DatabaseException { + if (element == null) + return null; + Resource mappedElement = getMappedElement(graph, element); + if (mappedElement == null) + return null; + ModelingResources MOD = ModelingResources.getInstance(graph); + return graph.getPossibleObject(mappedElement, MOD.ElementToComponent); + } + + public static Resource getMappedDNElement(ReadGraph graph, Resource element) throws DatabaseException { + if (element == null) + return null; + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + return graph.getPossibleObject(element, DN.MappedFromElement); + } + + public static Variable toMappedConfigurationModule(ReadGraph graph, Resource input) throws DatabaseException { + if (input == null) + return null; + + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + if (graph.isInstanceOf(input, DN.Element)) { + Resource mappedElement = getMappedElement(graph, input); + if (mappedElement == null) + return null; + + ModelingResources MOD = ModelingResources.getInstance(graph); + Resource mappedComponent = graph.getPossibleObject(mappedElement, MOD.ElementToComponent); + if (mappedComponent == null) + return null; + + return graph.syncRequest(new PossibleVariable(mappedComponent)); + } + return null; + } + }