]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/ActualConnectionDescriptor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / variables / ActualConnectionDescriptor.java
diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/ActualConnectionDescriptor.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/ActualConnectionDescriptor.java
new file mode 100644 (file)
index 0000000..d066ad2
--- /dev/null
@@ -0,0 +1,153 @@
+package org.simantics.structural2.variables;\r
+\r
+import java.util.Collection;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.util.URIStringUtils;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
+import org.simantics.db.common.request.UnaryRead;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.structural2.Functions;\r
+import org.simantics.structural2.Functions.InterfaceResolution;\r
+import org.simantics.structural2.variables.ConnectionBrowser.IsLeafType;\r
+\r
+/*\r
+ * This connection descriptor variable \r
+ * -has not been lifted into interface\r
+ * -has a structural configuration defined as resources\r
+ * -has not been drilled\r
+ */\r
+class ActualConnectionDescriptor extends AbstractVariableConnectionPointDescriptor {\r
+       \r
+       final private Variable root;\r
+       final private Resource component;\r
+       final private Resource cp;\r
+       \r
+       public ActualConnectionDescriptor(Variable root, Resource component, Resource cp) {\r
+               this.root = root;\r
+               this.component = component;\r
+               this.cp = cp;\r
+       }\r
+       \r
+       static class ComputeInterfaceDescription extends UnaryRead<ActualConnectionDescriptor, Collection<InterfaceResolution>> {\r
+\r
+               public ComputeInterfaceDescription(ActualConnectionDescriptor desc) {\r
+                       super(desc);\r
+               }\r
+\r
+               @Override\r
+               public Collection<InterfaceResolution> perform(ReadGraph graph) throws DatabaseException {\r
+                       \r
+               StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
+               Resource type = graph.getPossibleType(parameter.component, STR.Component);\r
+               if(graph.syncRequest(new IsLeafType(type))) return null;\r
+\r
+                       return Functions.computeInterfacePaths(graph, parameter.getVariable(graph).getParent(graph));\r
+                       \r
+               }\r
+               \r
+       }\r
+       \r
+       @Override\r
+       public Collection<InterfaceResolution> getInterfaceDescription(ReadGraph graph) throws DatabaseException {\r
+               return graph.syncRequest(new ComputeInterfaceDescription(this), TransientCacheAsyncListener.<Collection<InterfaceResolution>>instance());\r
+       }\r
+       \r
+       public Resource getConnectionPointResource(ReadGraph graph) throws DatabaseException {\r
+               return cp;\r
+       }\r
+       \r
+       static class ComputeVariable extends UnaryRead<ActualConnectionDescriptor, Variable> {\r
+\r
+               public ComputeVariable(ActualConnectionDescriptor desc) {\r
+                       super(desc);\r
+               }\r
+\r
+               @Override\r
+               public Variable perform(ReadGraph graph) throws DatabaseException {\r
+                       \r
+               Variable c = ConnectionBrowser.resolve(graph, parameter.root, parameter.component);\r
+               if(c != null) {\r
+                       Variable cnp = c.getPossibleProperty(graph, parameter.cp);\r
+                       if(cnp != null) {\r
+                               return cnp;\r
+                       }\r
+               }\r
+               \r
+               throw new DatabaseException("Unresolved connection point (root=" + parameter.root.getURI(graph) + ", component=" + NameUtils.getURIOrSafeNameInternal(graph, parameter.component) + ", cp=" + NameUtils.getURIOrSafeNameInternal(graph, parameter.cp) + ")");\r
+                       \r
+               }\r
+               \r
+       }\r
+       \r
+       @Override\r
+       public Variable getVariable(ReadGraph graph) throws DatabaseException {\r
+               return graph.syncRequest(new ComputeVariable(this), TransientCacheAsyncListener.<Variable>instance());\r
+       }\r
+\r
+       @Override\r
+       public String getURI(ReadGraph graph) throws DatabaseException {\r
+               \r
+               Variable var = getVariable(graph);\r
+               return var.getURI(graph);\r
+               \r
+       }\r
+\r
+       @Override\r
+       public boolean isFlattenedFrom(ReadGraph graph, Variable possiblyStructuralCp) throws DatabaseException {\r
+\r
+               // This is a top-level configured connection point - we return true if possiblyStructuralCp is actually this connection point\r
+               \r
+               Resource otherCp = possiblyStructuralCp.getPossiblePredicateResource(graph);\r
+               if(!cp.equals(otherCp)) return false;\r
+\r
+               Variable otherComponentVariable = possiblyStructuralCp.getParent(graph);\r
+               \r
+               Resource otherComponent = otherComponentVariable.getPossibleRepresents(graph);\r
+               if(!component.equals(otherComponent)) return false;\r
+\r
+               Variable otherContainer = otherComponentVariable.getParent(graph);\r
+               if(otherContainer.equals(root)) return true;\r
+               \r
+               return getVariable(graph).equals(possiblyStructuralCp);\r
+               \r
+       }\r
+       \r
+       @Override\r
+       public String getRelativeRVI(ReadGraph graph, Variable base) throws DatabaseException {\r
+\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        String cpName = graph.getRelatedValue(cp, L0.HasName, Bindings.STRING);\r
+               \r
+        if(cpName.startsWith("/")) {\r
+            ModelingResources MOD = ModelingResources.getInstance(graph);\r
+            if(graph.isInstanceOf(component, MOD.ReferenceElement)) {\r
+                return "#" + cpName;\r
+            }\r
+           }\r
+           \r
+               if(root.equals(base.getParent(graph))) {\r
+                       \r
+                       StringBuilder b = new StringBuilder();\r
+                       b.append("./");\r
+                       String cName = graph.getRelatedValue(component, L0.HasName, Bindings.STRING);\r
+                       b.append(URIStringUtils.escape(cName));\r
+                       b.append("#");\r
+                       b.append(cpName);\r
+                       \r
+                       return b.toString();\r
+                       \r
+               }\r
+\r
+               return super.getRelativeRVI(graph, base);\r
+\r
+       }\r
+       \r
+}
\ No newline at end of file