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