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 // ConnectionBrowser.reportDescriptor(graph, parameter);
73 * The componentType is the possibly replaced (STR.ReplaceableDefinedComponentType) type
75 StructuralComponentClass clazz = StructuralComponentClass.get(graph, parameter.componentType);
76 if(StructuralComponentClass.PRIMITIVE.equals(clazz)) {
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.");
85 return Functions.computeInterfacePaths(graph, parameter.getVariable(graph).getParent(graph));
93 public boolean isLeaf(ReadGraph graph) throws DatabaseException {
95 StructuralResource2 STR = StructuralResource2.getInstance(graph);
96 Resource type = graph.getPossibleType(component, STR.Component);
97 return graph.syncRequest(new IsLeafType(type));
102 public Collection<InterfaceResolution> getInterfaceDescription(ReadGraph graph) throws DatabaseException {
103 return graph.syncRequest(new ActualConnectionDescriptorInterfaceDescription(this), TransientCacheAsyncListener.<Collection<InterfaceResolution>>instance());
106 public Resource getConnectionPointResource(ReadGraph graph) throws DatabaseException {
110 static class ComputeVariable extends UnaryRead<ActualConnectionDescriptor, Variable> {
112 public ComputeVariable(ActualConnectionDescriptor desc) {
117 public Variable perform(ReadGraph graph) throws DatabaseException {
119 Variable c = ConnectionBrowser.resolve(graph, parameter.root, parameter.component);
121 Variable cnp = c.getPossibleProperty(graph, parameter.cp);
127 throw new DatabaseException("Unresolved connection point (root=" + parameter.root.getURI(graph) + ", component=" + NameUtils.getURIOrSafeNameInternal(graph, parameter.component) + ", cp=" + NameUtils.getURIOrSafeNameInternal(graph, parameter.cp) + ")");
134 public Variable getVariable(ReadGraph graph) throws DatabaseException {
135 return graph.syncRequest(new ComputeVariable(this), TransientCacheAsyncListener.<Variable>instance());
139 public String getURI(ReadGraph graph) throws DatabaseException {
141 Variable var = getVariable(graph);
142 return var.getURI(graph);
147 public boolean isFlattenedFrom(ReadGraph graph, Variable possiblyStructuralCp) throws DatabaseException {
149 // This is a top-level configured connection point - we return true if possiblyStructuralCp is actually this connection point
151 Resource otherCp = possiblyStructuralCp.getPossiblePredicateResource(graph);
152 if(!cp.equals(otherCp)) return false;
154 Variable otherComponentVariable = possiblyStructuralCp.getParent(graph);
156 Resource otherComponent = otherComponentVariable.getPossibleRepresents(graph);
157 if(!component.equals(otherComponent)) return false;
159 Variable otherContainer = otherComponentVariable.getParent(graph);
160 if(otherContainer.equals(root)) return true;
162 return getVariable(graph).equals(possiblyStructuralCp);
167 public String getRelativeRVI(ReadGraph graph, Variable base) throws DatabaseException {
169 Layer0 L0 = Layer0.getInstance(graph);
170 String cpName = graph.getRelatedValue(cp, L0.HasName, Bindings.STRING);
172 if(cpName.startsWith("/")) {
173 ModelingResources MOD = ModelingResources.getInstance(graph);
174 if(graph.isInstanceOf(component, MOD.ReferenceElement)) {
179 if(root.equals(base.getParent(graph))) {
181 StringBuilder b = new StringBuilder();
183 String cName = graph.getRelatedValue(component, L0.HasName, Bindings.STRING);
184 b.append(URIStringUtils.escape(cName));
192 return super.getRelativeRVI(graph, base);