1 package org.simantics.structural2.variables;
\r
3 import java.util.Collection;
\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
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
27 class ActualConnectionDescriptor extends AbstractVariableConnectionPointDescriptor {
\r
29 final private Variable root;
\r
30 final private Resource component;
\r
31 final private Resource cp;
\r
33 public ActualConnectionDescriptor(Variable root, Resource component, Resource cp) {
\r
35 this.component = component;
\r
39 static class ComputeInterfaceDescription extends UnaryRead<ActualConnectionDescriptor, Collection<InterfaceResolution>> {
\r
41 public ComputeInterfaceDescription(ActualConnectionDescriptor desc) {
\r
46 public Collection<InterfaceResolution> perform(ReadGraph graph) throws DatabaseException {
\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
52 return Functions.computeInterfacePaths(graph, parameter.getVariable(graph).getParent(graph));
\r
59 public Collection<InterfaceResolution> getInterfaceDescription(ReadGraph graph) throws DatabaseException {
\r
60 return graph.syncRequest(new ComputeInterfaceDescription(this), TransientCacheAsyncListener.<Collection<InterfaceResolution>>instance());
\r
63 public Resource getConnectionPointResource(ReadGraph graph) throws DatabaseException {
\r
67 static class ComputeVariable extends UnaryRead<ActualConnectionDescriptor, Variable> {
\r
69 public ComputeVariable(ActualConnectionDescriptor desc) {
\r
74 public Variable perform(ReadGraph graph) throws DatabaseException {
\r
76 Variable c = ConnectionBrowser.resolve(graph, parameter.root, parameter.component);
\r
78 Variable cnp = c.getPossibleProperty(graph, parameter.cp);
\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
91 public Variable getVariable(ReadGraph graph) throws DatabaseException {
\r
92 return graph.syncRequest(new ComputeVariable(this), TransientCacheAsyncListener.<Variable>instance());
\r
96 public String getURI(ReadGraph graph) throws DatabaseException {
\r
98 Variable var = getVariable(graph);
\r
99 return var.getURI(graph);
\r
104 public boolean isFlattenedFrom(ReadGraph graph, Variable possiblyStructuralCp) throws DatabaseException {
\r
106 // This is a top-level configured connection point - we return true if possiblyStructuralCp is actually this connection point
\r
108 Resource otherCp = possiblyStructuralCp.getPossiblePredicateResource(graph);
\r
109 if(!cp.equals(otherCp)) return false;
\r
111 Variable otherComponentVariable = possiblyStructuralCp.getParent(graph);
\r
113 Resource otherComponent = otherComponentVariable.getPossibleRepresents(graph);
\r
114 if(!component.equals(otherComponent)) return false;
\r
116 Variable otherContainer = otherComponentVariable.getParent(graph);
\r
117 if(otherContainer.equals(root)) return true;
\r
119 return getVariable(graph).equals(possiblyStructuralCp);
\r
124 public String getRelativeRVI(ReadGraph graph, Variable base) throws DatabaseException {
\r
126 Layer0 L0 = Layer0.getInstance(graph);
\r
127 String cpName = graph.getRelatedValue(cp, L0.HasName, Bindings.STRING);
\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
136 if(root.equals(base.getParent(graph))) {
\r
138 StringBuilder b = new StringBuilder();
\r
140 String cName = graph.getRelatedValue(component, L0.HasName, Bindings.STRING);
\r
141 b.append(URIStringUtils.escape(cName));
\r
145 return b.toString();
\r
149 return super.getRelativeRVI(graph, base);
\r