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=9f6f46fa6de2efc150e4b962e0d62e265c25e321;hpb=e6378bf9c77ffd2d33d81ab882e6a2243506a0a8;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 9f6f46fa..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.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); 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); @@ -128,4 +148,60 @@ public class DistrictNetworkUtil { 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; + } + }