]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.structural2.variables;\r
2 \r
3 import java.util.Collection;\r
4 \r
5 import org.simantics.databoard.Bindings;\r
6 import org.simantics.databoard.util.URIStringUtils;\r
7 import org.simantics.db.ReadGraph;\r
8 import org.simantics.db.Resource;\r
9 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
10 import org.simantics.db.common.request.UnaryRead;\r
11 import org.simantics.db.common.utils.NameUtils;\r
12 import org.simantics.db.exception.DatabaseException;\r
13 import org.simantics.db.layer0.variable.Variable;\r
14 import org.simantics.layer0.Layer0;\r
15 import org.simantics.modeling.ModelingResources;\r
16 import org.simantics.structural.stubs.StructuralResource2;\r
17 import org.simantics.structural2.Functions;\r
18 import org.simantics.structural2.Functions.InterfaceResolution;\r
19 import org.simantics.structural2.variables.ConnectionBrowser.IsLeafType;\r
20 \r
21 /*\r
22  * This connection descriptor variable \r
23  * -has not been lifted into interface\r
24  * -has a structural configuration defined as resources\r
25  * -has not been drilled\r
26  */\r
27 class ActualConnectionDescriptor extends AbstractVariableConnectionPointDescriptor {\r
28         \r
29         final private Variable root;\r
30         final private Resource component;\r
31         final private Resource cp;\r
32         \r
33         public ActualConnectionDescriptor(Variable root, Resource component, Resource cp) {\r
34                 this.root = root;\r
35                 this.component = component;\r
36                 this.cp = cp;\r
37         }\r
38         \r
39         static class ComputeInterfaceDescription extends UnaryRead<ActualConnectionDescriptor, Collection<InterfaceResolution>> {\r
40 \r
41                 public ComputeInterfaceDescription(ActualConnectionDescriptor desc) {\r
42                         super(desc);\r
43                 }\r
44 \r
45                 @Override\r
46                 public Collection<InterfaceResolution> perform(ReadGraph graph) throws DatabaseException {\r
47                         \r
48                 StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
49                 Resource type = graph.getPossibleType(parameter.component, STR.Component);\r
50                 if(graph.syncRequest(new IsLeafType(type))) return null;\r
51 \r
52                         return Functions.computeInterfacePaths(graph, parameter.getVariable(graph).getParent(graph));\r
53                         \r
54                 }\r
55                 \r
56         }\r
57         \r
58         @Override\r
59         public Collection<InterfaceResolution> getInterfaceDescription(ReadGraph graph) throws DatabaseException {\r
60                 return graph.syncRequest(new ComputeInterfaceDescription(this), TransientCacheAsyncListener.<Collection<InterfaceResolution>>instance());\r
61         }\r
62         \r
63         public Resource getConnectionPointResource(ReadGraph graph) throws DatabaseException {\r
64                 return cp;\r
65         }\r
66         \r
67         static class ComputeVariable extends UnaryRead<ActualConnectionDescriptor, Variable> {\r
68 \r
69                 public ComputeVariable(ActualConnectionDescriptor desc) {\r
70                         super(desc);\r
71                 }\r
72 \r
73                 @Override\r
74                 public Variable perform(ReadGraph graph) throws DatabaseException {\r
75                         \r
76                 Variable c = ConnectionBrowser.resolve(graph, parameter.root, parameter.component);\r
77                 if(c != null) {\r
78                         Variable cnp = c.getPossibleProperty(graph, parameter.cp);\r
79                         if(cnp != null) {\r
80                                 return cnp;\r
81                         }\r
82                 }\r
83                 \r
84                 throw new DatabaseException("Unresolved connection point (root=" + parameter.root.getURI(graph) + ", component=" + NameUtils.getURIOrSafeNameInternal(graph, parameter.component) + ", cp=" + NameUtils.getURIOrSafeNameInternal(graph, parameter.cp) + ")");\r
85                         \r
86                 }\r
87                 \r
88         }\r
89         \r
90         @Override\r
91         public Variable getVariable(ReadGraph graph) throws DatabaseException {\r
92                 return graph.syncRequest(new ComputeVariable(this), TransientCacheAsyncListener.<Variable>instance());\r
93         }\r
94 \r
95         @Override\r
96         public String getURI(ReadGraph graph) throws DatabaseException {\r
97                 \r
98                 Variable var = getVariable(graph);\r
99                 return var.getURI(graph);\r
100                 \r
101         }\r
102 \r
103         @Override\r
104         public boolean isFlattenedFrom(ReadGraph graph, Variable possiblyStructuralCp) throws DatabaseException {\r
105 \r
106                 // This is a top-level configured connection point - we return true if possiblyStructuralCp is actually this connection point\r
107                 \r
108                 Resource otherCp = possiblyStructuralCp.getPossiblePredicateResource(graph);\r
109                 if(!cp.equals(otherCp)) return false;\r
110 \r
111                 Variable otherComponentVariable = possiblyStructuralCp.getParent(graph);\r
112                 \r
113                 Resource otherComponent = otherComponentVariable.getPossibleRepresents(graph);\r
114                 if(!component.equals(otherComponent)) return false;\r
115 \r
116                 Variable otherContainer = otherComponentVariable.getParent(graph);\r
117                 if(otherContainer.equals(root)) return true;\r
118                 \r
119                 return getVariable(graph).equals(possiblyStructuralCp);\r
120                 \r
121         }\r
122         \r
123         @Override\r
124         public String getRelativeRVI(ReadGraph graph, Variable base) throws DatabaseException {\r
125 \r
126         Layer0 L0 = Layer0.getInstance(graph);\r
127         String cpName = graph.getRelatedValue(cp, L0.HasName, Bindings.STRING);\r
128                 \r
129         if(cpName.startsWith("/")) {\r
130             ModelingResources MOD = ModelingResources.getInstance(graph);\r
131             if(graph.isInstanceOf(component, MOD.ReferenceElement)) {\r
132                 return "#" + cpName;\r
133             }\r
134             }\r
135             \r
136                 if(root.equals(base.getParent(graph))) {\r
137                         \r
138                         StringBuilder b = new StringBuilder();\r
139                         b.append("./");\r
140                         String cName = graph.getRelatedValue(component, L0.HasName, Bindings.STRING);\r
141                         b.append(URIStringUtils.escape(cName));\r
142                         b.append("#");\r
143                         b.append(cpName);\r
144                         \r
145                         return b.toString();\r
146                         \r
147                 }\r
148 \r
149                 return super.getRelativeRVI(graph, base);\r
150 \r
151         }\r
152         \r
153 }