]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/ComponentConnectionDescriptor.java
Merge "Fixes to thread safety problems in SCL compiler"
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / variables / ComponentConnectionDescriptor.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
10 public class ComponentConnectionDescriptor extends AbstractVariableConnectionPointDescriptor {
11         
12         public Variable component;
13         public Resource cp;
14         
15         public ComponentConnectionDescriptor(Variable component, Resource cp) {
16                 this.component = component;
17                 this.cp = cp;
18         }
19         @Override
20         public int hashCode() {
21                 return component.hashCode() + 31*cp.hashCode();
22         }
23         @Override
24         public boolean equals(Object object) {
25         if (this == object)
26             return true;
27         else if (object == null)
28             return false;
29         else if (!(object instanceof ComponentConnectionDescriptor))
30             return false;
31         ComponentConnectionDescriptor r = (ComponentConnectionDescriptor)object;
32         if(!r.cp.equals(cp)) return false;
33         if(!r.component.equals(component)) return false;
34         return true;
35         }
36
37         static class ComputeVariable extends UnaryRead<ComponentConnectionDescriptor, Variable> {
38
39                 public ComputeVariable(ComponentConnectionDescriptor desc) {
40                         super(desc);
41                 }
42
43                 @Override
44                 public Variable perform(ReadGraph graph) throws DatabaseException {
45                         return parameter.component.getProperty(graph, parameter.cp);
46                 }
47                 
48         }
49         @Override
50         public Variable getVariable(ReadGraph graph) throws DatabaseException {
51                 return graph.syncRequest(new ComputeVariable(this), TransientCacheAsyncListener.<Variable>instance());
52         }
53         public String getURI(ReadGraph graph) throws DatabaseException {
54                 return getVariable(graph).getURI(graph);
55         }
56         @Override
57         public Resource getConnectionPointResource(ReadGraph graph) throws DatabaseException {
58                 return getVariable(graph).getPossiblePredicateResource(graph);
59         }
60         
61         @Override
62         public boolean isFlattenedFrom(ReadGraph graph, Variable possiblyStructuralCp) throws DatabaseException {
63
64                 Variable parent = possiblyStructuralCp.getParent(graph);
65                 
66                 String descURI = getURI(graph);
67                 String parentURI = parent.getURI(graph);
68                 if(descURI.startsWith(parentURI)) {
69                         // Children are in substructure
70                         if('/' == descURI.charAt(parentURI.length())) return true;
71                         // Otherwise require exact match
72                         return descURI.endsWith(possiblyStructuralCp.getName(graph));
73                 } else {
74                         return false;
75                 }
76                 
77         }
78         
79 }