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