]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/ActualConnectionDescriptor.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / variables / ActualConnectionDescriptor.java
1 package org.simantics.structural2.variables;
2
3 import java.util.Collection;
4
5 import org.simantics.databoard.Bindings;
6 import org.simantics.databoard.util.URIStringUtils;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
10 import org.simantics.db.common.request.UnaryRead;
11 import org.simantics.db.common.utils.NameUtils;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.variable.Variable;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.modeling.ModelingResources;
16 import org.simantics.structural.stubs.StructuralResource2;
17 import org.simantics.structural2.DefinedUCInterfaceMap;
18 import org.simantics.structural2.Functions;
19 import org.simantics.structural2.Functions.InterfaceResolution;
20 import org.simantics.structural2.utils.StructuralUtils.StructuralComponentClass;
21 import org.simantics.structural2.variables.ConnectionBrowser.IsLeafType;
22
23 /*
24  * This connection descriptor
25  * 
26  * -has not been lifted into interface
27  * -has a structural configuration defined as resources
28  * -might not be drilled
29  * 
30  * Note: these are constructed after climb (before drill) phase of the connection browser. This means that
31  * the component can still contain sub structure during connection browsing. This means that some descriptors
32  * are not final and are replaced by correct descriptors during drill phase.
33  * 
34  */
35 class ActualConnectionDescriptor extends AbstractVariableConnectionPointDescriptor {
36         
37         /*
38          * This is the nearest known ancestor variable
39          */
40         final Variable root;
41         /*
42          * A component on variable path under the root variable.
43          */
44         final Resource component;
45         /*
46          * TODO
47          */
48         final Resource componentType;
49         /*
50          * The connection point that has type of the component as its domain
51          */
52         final Resource cp;
53         
54         public ActualConnectionDescriptor(Variable root, Resource component, Resource componentType, Resource cp) {
55                 this.root = root;
56                 this.component = component;
57                 this.componentType = componentType;
58                 this.cp = cp;
59         }
60         
61         static class ActualConnectionDescriptorInterfaceDescription extends UnaryRead<ActualConnectionDescriptor, Collection<InterfaceResolution>> {
62
63                 public ActualConnectionDescriptorInterfaceDescription(ActualConnectionDescriptor desc) {
64                         super(desc);
65                 }
66
67                 @Override
68                 public Collection<InterfaceResolution> perform(ReadGraph graph) throws DatabaseException {
69                         
70 //                      ConnectionBrowser.reportDescriptor(graph, parameter);
71                 
72                 /*
73                  * The componentType is the possibly replaced (STR.ReplaceableDefinedComponentType) type
74                  */
75                 StructuralComponentClass clazz = StructuralComponentClass.get(graph, parameter.componentType);
76                 if(StructuralComponentClass.PRIMITIVE.equals(clazz)) {
77                         return null;
78                 } else if(StructuralComponentClass.DEFINED.equals(clazz)) {
79                         final Collection<InterfaceResolution> interfaces = graph.syncRequest(new DefinedUCInterfaceMap(parameter.componentType));
80                         if(interfaces != null) return interfaces;
81                         else return Functions.BUILTIN_STRUCTURAL_CPS;
82                 } else if(StructuralComponentClass.REPLACEABLE.equals(clazz)) {
83                         throw new DatabaseException("ConnectionBrowser does not support nested replaceable defined structural types.");
84                 } else {
85                         return Functions.computeInterfacePaths(graph, parameter.getVariable(graph).getParent(graph));   
86                 }
87                         
88                 }
89                 
90         }
91         
92         @Override
93         public boolean isLeaf(ReadGraph graph) throws DatabaseException {
94                 
95                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
96                 Resource type = graph.getPossibleType(component, STR.Component);
97                 return type != null ? graph.syncRequest(new IsLeafType(type)) : false;
98                 
99         }
100         
101         @Override
102         public Collection<InterfaceResolution> getInterfaceDescription(ReadGraph graph) throws DatabaseException {
103                 return graph.syncRequest(new ActualConnectionDescriptorInterfaceDescription(this), TransientCacheAsyncListener.<Collection<InterfaceResolution>>instance());
104         }
105         
106         public Resource getConnectionPointResource(ReadGraph graph) throws DatabaseException {
107                 return cp;
108         }
109         
110         static class ComputeVariable extends UnaryRead<ActualConnectionDescriptor, Variable> {
111
112                 public ComputeVariable(ActualConnectionDescriptor desc) {
113                         super(desc);
114                 }
115
116                 @Override
117                 public Variable perform(ReadGraph graph) throws DatabaseException {
118                         
119                 Variable c = ConnectionBrowser.resolve(graph, parameter.root, parameter.component);
120                 if(c != null) {
121                         Variable cnp = c.getPossibleProperty(graph, parameter.cp);
122                         if(cnp != null) {
123                                 return cnp;
124                         }
125                 }
126                 
127                 throw new DatabaseException("Unresolved connection point (root=" + parameter.root.getURI(graph) + ", component=" + NameUtils.getURIOrSafeNameInternal(graph, parameter.component) + ", cp=" + NameUtils.getURIOrSafeNameInternal(graph, parameter.cp) + ")");
128                         
129                 }
130                 
131         }
132         
133         @Override
134         public Variable getVariable(ReadGraph graph) throws DatabaseException {
135                 return graph.syncRequest(new ComputeVariable(this), TransientCacheAsyncListener.<Variable>instance());
136         }
137
138         @Override
139         public String getURI(ReadGraph graph) throws DatabaseException {
140                 
141                 Variable var = getVariable(graph);
142                 return var.getURI(graph);
143                 
144         }
145
146         @Override
147         public boolean isFlattenedFrom(ReadGraph graph, Variable possiblyStructuralCp) throws DatabaseException {
148
149                 // This is a top-level configured connection point - we return true if possiblyStructuralCp is actually this connection point
150                 
151                 Resource otherCp = possiblyStructuralCp.getPossiblePredicateResource(graph);
152                 if(!cp.equals(otherCp)) return false;
153
154                 Variable otherComponentVariable = possiblyStructuralCp.getParent(graph);
155                 
156                 Resource otherComponent = otherComponentVariable.getPossibleRepresents(graph);
157                 if(!component.equals(otherComponent)) return false;
158
159                 Variable otherContainer = otherComponentVariable.getParent(graph);
160                 if(otherContainer.equals(root)) return true;
161                 
162                 return getVariable(graph).equals(possiblyStructuralCp);
163                 
164         }
165         
166         @Override
167         public String getRelativeRVI(ReadGraph graph, Variable base) throws DatabaseException {
168
169         Layer0 L0 = Layer0.getInstance(graph);
170         String cpName = graph.getRelatedValue(cp, L0.HasName, Bindings.STRING);
171                 
172         if(cpName.startsWith("/")) {
173             ModelingResources MOD = ModelingResources.getInstance(graph);
174             if(graph.isInstanceOf(component, MOD.ReferenceElement)) {
175                 return "#" + cpName;
176             }
177             }
178             
179                 if(root.equals(base.getParent(graph))) {
180                         
181                         StringBuilder b = new StringBuilder();
182                         b.append("./");
183                         String cName = graph.getRelatedValue(component, L0.HasName, Bindings.STRING);
184                         b.append(URIStringUtils.escape(cName));
185                         b.append("#");
186                         b.append(cpName);
187                         
188                         return b.toString();
189                         
190                 }
191
192                 return super.getRelativeRVI(graph, base);
193
194         }
195         
196 }