import org.simantics.db.layer0.util.Layer0Utils;
import org.simantics.diagram.profile.StyleBase;
import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.district.network.DistrictNetworkUtil;
import org.simantics.district.network.ontology.DistrictNetworkResource;
import org.simantics.layer0.Layer0;
import org.simantics.modeling.ModelingResources;
import org.simantics.scenegraph.profile.EvaluationContext;
import org.simantics.scenegraph.profile.common.ProfileVariables;
import org.simantics.scenegraph.utils.GeometryUtils;
+import org.simantics.scl.compiler.top.ValueNotFound;
+import org.simantics.scl.osgi.SCLOsgi;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.function.Function1;
import org.simantics.structural.stubs.StructuralResource2;
public class ConnectionLineStyle extends StyleBase<List<Point2D>> {
@Override
public List<Point2D> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem)
throws DatabaseException {
- return graph.syncRequest(new ElementConnectionRequest(groupItem), TransientCacheListener.instance());
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ ModelingResources MOD = ModelingResources.getInstance(graph);
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+
+ Resource vertex = groupItem;
+ if (!graph.isInstanceOf(vertex, DN.Vertex))
+ return Collections.emptyList();
+
+ double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation);
+
+ Resource component = DistrictNetworkUtil.getMappedComponentCached(graph, vertex);
+ if (component == null)
+ return Collections.emptyList();
+
+ Resource componentType = graph.getPossibleType(component, STR.Component);
+ if (componentType == null)
+ return Collections.emptyList();
+
+ Function1<Resource, List<Resource>> fun = getConnectedComponentsFunctionCached(graph, componentType);
+ if (fun == null)
+ return Collections.emptyList();
+
+ List<Resource> components = Simantics.applySCLRead(graph, fun, component);
+
+ if (components == null || components.isEmpty())
+ return Collections.emptyList();
+
+ List<Point2D> result = new ArrayList<>(components.size() + 1);
+ result.add(new Point2D.Double(coords[0], coords[1]));
+ for (Resource comp : components) {
+ Resource e = comp != null ? graph.getPossibleObject(comp, MOD.ComponentToElement) : null;
+ Resource mappingElement = e != null ? graph.getPossibleObject(e, DN.MappedFromElement) : null;
+ if (mappingElement != null) {
+ double[] coords2 = graph.getRelatedValue(mappingElement, DiagramResource.getInstance(graph).HasLocation);
+ result.add(new Point2D.Double(coords2[0], coords2[1]));
+ }
+ else {
+ result.add(null);
+ }
+ }
+
+ return result;
}
@Override
ProfileVariables.denyChild(parent, "*", "districtNetworkConnection");
}
- private static final class ElementConnectionRequest extends ResourceRead<List<Point2D>> {
- private ElementConnectionRequest(Resource resource) {
+ private static Function1<Resource, List<Resource>> getConnectedComponentsFunctionCached(ReadGraph graph, Resource componentType)
+ throws DatabaseException {
+ return graph.syncRequest(new ConnectedComponentsFunctionRequest(componentType), TransientCacheListener.instance());
+ }
+
+ private static final class ConnectedComponentsFunctionRequest
+ extends ResourceRead<Function1<Resource, List<Resource>>> {
+ public ConnectedComponentsFunctionRequest(Resource resource) {
super(resource);
}
-
+
+ @SuppressWarnings("unchecked")
@Override
- public List<Point2D> perform(ReadGraph graph) throws DatabaseException {
- DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
- ModelingResources MOD = ModelingResources.getInstance(graph);
- StructuralResource2 STR = StructuralResource2.getInstance(graph);
-
- Resource vertex = resource;
- if (!graph.isInstanceOf(vertex, DN.Vertex))
- return Collections.emptyList();
-
- double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation);
-
- Resource element = graph.getPossibleObject(vertex, DN.MappedComponent);
- if (element == null)
- return Collections.emptyList();
- Resource component = graph.getPossibleObject(element, MOD.ElementToComponent);
- if (component == null)
- return Collections.emptyList();
- Resource componentType = graph.getPossibleType(component, STR.Component);
- if (componentType == null)
- return Collections.emptyList();
-
- Resource actionsModule = Layer0Utils.getPossibleChild(graph, componentType, "Actions");
+ public Function1<Resource, List<Resource>> perform(ReadGraph graph) throws DatabaseException {
+ Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, "Actions");
if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
- return Collections.emptyList();
+ return null;
- List<Resource> components;
+ String uri = graph.getURI(actionsModule);
+ SCLContext sclContext = SCLContext.getCurrent();
+ Object oldGraph = sclContext.get("graph");
try {
- components = Simantics.applySCL(graph.getURI(actionsModule), "getConnectedComponents", graph, component);
+ sclContext.put("graph", graph);
+ return (Function1<Resource, List<Resource>>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "getConnectedComponents");
+ } catch (ValueNotFound e1) {
+ return null;
+ } finally {
+ sclContext.put("graph", oldGraph);
}
- catch (DatabaseException e) {
- return Collections.emptyList();
- }
-
- if (components == null || components.isEmpty())
- return Collections.emptyList();
-
- List<Point2D> result = new ArrayList<>(components.size() + 1);
- result.add(new Point2D.Double(coords[0], coords[1]));
- for (Resource comp : components) {
- Resource e = comp != null ? graph.getPossibleObject(comp, MOD.ComponentToElement) : null;
- Resource mappingElement = e != null ? graph.getPossibleObject(e, DN.MappedFromElement) : null;
- if (mappingElement != null) {
- double[] coords2 = graph.getRelatedValue(mappingElement, DiagramResource.getInstance(graph).HasLocation);
- result.add(new Point2D.Double(coords2[0], coords2[1]));
- }
- else {
- result.add(null);
- }
- }
-
- return result;
}
}
}