1 package org.simantics.structural2.variables;
3 import java.util.Collection;
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;
24 * This connection descriptor
26 * -has not been lifted into interface
27 * -has a structural configuration defined as resources
28 * -might not be drilled
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.
35 class ActualConnectionDescriptor extends AbstractVariableConnectionPointDescriptor {
38 * This is the nearest known ancestor variable
42 * A component on variable path under the root variable.
44 final Resource component;
48 final Resource componentType;
50 * The connection point that has type of the component as its domain
54 public ActualConnectionDescriptor(Variable root, Resource component, Resource componentType, Resource cp) {
56 this.component = component;
57 this.componentType = componentType;
61 static class ActualConnectionDescriptorInterfaceDescription extends UnaryRead<ActualConnectionDescriptor, Collection<InterfaceResolution>> {
63 public ActualConnectionDescriptorInterfaceDescription(ActualConnectionDescriptor desc) {
68 public Collection<InterfaceResolution> perform(ReadGraph graph) throws DatabaseException {
70 System.err.println("ActualConnectionDescriptorInterfaceDescription");
72 ConnectionBrowser.reportDescriptor(graph, parameter);
75 * The componentType is the possibly replaced (STR.ReplaceableDefinedComponentType) type
77 StructuralComponentClass clazz = StructuralComponentClass.get(graph, parameter.componentType);
78 if(StructuralComponentClass.PRIMITIVE.equals(clazz)) {
80 } else if(StructuralComponentClass.DEFINED.equals(clazz)) {
81 final Collection<InterfaceResolution> interfaces = graph.syncRequest(new DefinedUCInterfaceMap(parameter.componentType));
82 if(interfaces != null) return interfaces;
83 else return Functions.BUILTIN_STRUCTURAL_CPS;
84 } else if(StructuralComponentClass.REPLACEABLE.equals(clazz)) {
85 throw new DatabaseException("ConnectionBrowser does not support nested replaceable defined structural types.");
87 return Functions.computeInterfacePaths(graph, parameter.getVariable(graph).getParent(graph));
95 public boolean isLeaf(ReadGraph graph) throws DatabaseException {
97 StructuralResource2 STR = StructuralResource2.getInstance(graph);
98 Resource type = graph.getPossibleType(component, STR.Component);
99 return graph.syncRequest(new IsLeafType(type));
104 public Collection<InterfaceResolution> getInterfaceDescription(ReadGraph graph) throws DatabaseException {
105 return graph.syncRequest(new ActualConnectionDescriptorInterfaceDescription(this), TransientCacheAsyncListener.<Collection<InterfaceResolution>>instance());
108 public Resource getConnectionPointResource(ReadGraph graph) throws DatabaseException {
112 static class ComputeVariable extends UnaryRead<ActualConnectionDescriptor, Variable> {
114 public ComputeVariable(ActualConnectionDescriptor desc) {
119 public Variable perform(ReadGraph graph) throws DatabaseException {
121 Variable c = ConnectionBrowser.resolve(graph, parameter.root, parameter.component);
123 Variable cnp = c.getPossibleProperty(graph, parameter.cp);
129 throw new DatabaseException("Unresolved connection point (root=" + parameter.root.getURI(graph) + ", component=" + NameUtils.getURIOrSafeNameInternal(graph, parameter.component) + ", cp=" + NameUtils.getURIOrSafeNameInternal(graph, parameter.cp) + ")");
136 public Variable getVariable(ReadGraph graph) throws DatabaseException {
137 return graph.syncRequest(new ComputeVariable(this), TransientCacheAsyncListener.<Variable>instance());
141 public String getURI(ReadGraph graph) throws DatabaseException {
143 Variable var = getVariable(graph);
144 return var.getURI(graph);
149 public boolean isFlattenedFrom(ReadGraph graph, Variable possiblyStructuralCp) throws DatabaseException {
151 // This is a top-level configured connection point - we return true if possiblyStructuralCp is actually this connection point
153 Resource otherCp = possiblyStructuralCp.getPossiblePredicateResource(graph);
154 if(!cp.equals(otherCp)) return false;
156 Variable otherComponentVariable = possiblyStructuralCp.getParent(graph);
158 Resource otherComponent = otherComponentVariable.getPossibleRepresents(graph);
159 if(!component.equals(otherComponent)) return false;
161 Variable otherContainer = otherComponentVariable.getParent(graph);
162 if(otherContainer.equals(root)) return true;
164 return getVariable(graph).equals(possiblyStructuralCp);
169 public String getRelativeRVI(ReadGraph graph, Variable base) throws DatabaseException {
171 Layer0 L0 = Layer0.getInstance(graph);
172 String cpName = graph.getRelatedValue(cp, L0.HasName, Bindings.STRING);
174 if(cpName.startsWith("/")) {
175 ModelingResources MOD = ModelingResources.getInstance(graph);
176 if(graph.isInstanceOf(component, MOD.ReferenceElement)) {
181 if(root.equals(base.getParent(graph))) {
183 StringBuilder b = new StringBuilder();
185 String cName = graph.getRelatedValue(component, L0.HasName, Bindings.STRING);
186 b.append(URIStringUtils.escape(cName));
194 return super.getRelativeRVI(graph, base);