]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/PairConnectionDescriptor.java
IConnectionPoint and canBeConnected-checking util to SCL
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / variables / PairConnectionDescriptor.java
1 package org.simantics.structural2.variables;
2
3 import org.simantics.db.ReadGraph;
4 import org.simantics.db.Resource;
5 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
6 import org.simantics.db.common.request.UnaryRead;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.layer0.variable.Variable;
9 import org.simantics.utils.datastructures.Pair;
10
11 public class PairConnectionDescriptor extends AbstractVariableConnectionPointDescriptor {
12         
13         public Variable base;
14         public Pair<String,Resource> pair;
15         
16         public PairConnectionDescriptor(Variable base, Pair<String,Resource> pair) {
17                 this.base = base;
18                 this.pair = pair;
19         }
20         @Override
21         public int hashCode() {
22                 return base.hashCode() + 31*pair.hashCode();
23         }
24         @Override
25         public boolean equals(Object object) {
26         if (this == object)
27             return true;
28         else if (object == null)
29             return false;
30         else if (!(object instanceof PairConnectionDescriptor))
31             return false;
32         PairConnectionDescriptor r = (PairConnectionDescriptor)object;
33         if(!r.pair.equals(pair)) return false;
34         if(!r.base.equals(base)) return false;
35         return true;
36         }
37         
38         static class ComputeVariable extends UnaryRead<PairConnectionDescriptor, Variable> {
39
40                 public ComputeVariable(PairConnectionDescriptor desc) {
41                         super(desc);
42                 }
43
44                 @Override
45                 public Variable perform(ReadGraph graph) throws DatabaseException {
46                         return parameter.base.getChild(graph, parameter.pair.first).getProperty(graph, parameter.pair.second);
47                 }
48                 
49         }
50         @Override
51         public Variable getVariable(ReadGraph graph) throws DatabaseException {
52                 return graph.syncRequest(new ComputeVariable(this), TransientCacheAsyncListener.<Variable>instance());
53         }
54         public String getURI(ReadGraph graph) throws DatabaseException {
55                 return getVariable(graph).getURI(graph);
56         }
57         @Override
58         public Resource getConnectionPointResource(ReadGraph graph) throws DatabaseException {
59                 return getVariable(graph).getPossiblePredicateResource(graph);
60         }
61         
62         @Override
63         public boolean isFlattenedFrom(ReadGraph graph, Variable possiblyStructuralCp) throws DatabaseException {
64                 
65                 Variable parent = possiblyStructuralCp.getParent(graph);
66                 
67                 String descURI = getURI(graph);
68                 String parentURI = parent.getURI(graph);
69                 if(descURI.startsWith(parentURI)) {
70                         // Children are in substructure
71                         if('/' == descURI.charAt(parentURI.length())) return true;
72                         // Otherwise require exact match
73                         return descURI.endsWith(possiblyStructuralCp.getName(graph));
74                 } else {
75                         return false;
76                 }
77                 
78         }
79         
80 }