]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/FixedConnection.java
Procedural connectivity was broken
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / variables / FixedConnection.java
1 package org.simantics.structural2.variables;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6 import java.util.Set;
7
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.variable.Variable;
12 import org.simantics.utils.datastructures.Pair;
13
14 import gnu.trove.set.hash.THashSet;
15
16 public class FixedConnection implements Connection, Connection2 {
17
18     /*
19      * This is the parent of the component to be connected
20      */
21     final private Variable procedural;
22     
23     final private Collection<Pair<String,Resource>> cps = new ArrayList<Pair<String,Resource>>();
24     
25     public FixedConnection(Variable procedural) {
26         this.procedural = procedural;
27     }
28     
29     public void addAll(List<Pair<String,Resource>> cps) throws DatabaseException {
30         /*
31          * For interface connections the name is null
32          */
33         for(Pair<String,Resource> cp : cps) {
34             this.cps.add(cp);
35         }
36     }
37     
38     public int size() {
39         return cps.size();
40     }
41     
42     public void addConnectionDescriptors(ReadGraph graph, Variable curConfiguration, Collection<VariableConnectionPointDescriptor> result) throws DatabaseException {
43         for(Pair<String,Resource> cpzz : cps) {
44             // This is a connection to an interface terminal. It is handled by ConnectionBrowser in separate logic. We should never have gotten this far
45             if(cpzz.first == null) {
46                 String message = "Lifted connection was not resolved. Child = " + procedural.getURI(graph);
47                 throw new DatabaseException(message);
48             }
49             result.add(new PairConnectionDescriptor(curConfiguration, cpzz));
50         }
51     }
52     
53     @Override
54     public Collection<Variable> getConnectionPoints(ReadGraph graph, Resource relationType) throws DatabaseException {
55         Set<Variable> result = new THashSet<Variable>();
56         for(Pair<String,Resource> cp : cps) {
57             Variable component = cp.first == null ? procedural : procedural.getChild(graph, cp.first); 
58             Variable cp2 = component.getPossibleProperty(graph, cp.second);
59             if(cp2 != null)
60                 for(VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second, relationType)) {
61                     result.add(desc.getVariable(graph));
62                 }
63             else
64                 StandardProceduralChildVariable.LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
65         }
66         return result;
67     }
68     
69         @Override
70         public Collection<String> getConnectionPointURIs(ReadGraph graph, Resource relationType) throws DatabaseException {
71         Set<String> result = new THashSet<String>();
72         for(Pair<String,Resource> cp : cps) {
73             Variable component = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
74             Variable cp2 = component.getPossibleProperty(graph, cp.second);
75             if(cp2 != null)
76                 for(VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second, relationType)) {
77                     result.add(desc.getURI(graph));
78                 }
79             else
80                 StandardProceduralChildVariable.LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
81         }
82         return result;
83         }
84         
85         @Override
86         public Collection<VariableConnectionPointDescriptor> getConnectionPointDescriptors(ReadGraph graph, Resource relationType) throws DatabaseException {
87         Set<VariableConnectionPointDescriptor> result = new THashSet<VariableConnectionPointDescriptor>();
88         for(Pair<String,Resource> cp : cps) {
89             Variable component = cp.first == null ? procedural : procedural.getChild(graph, cp.first); 
90             Variable cp2 = component.getPossibleProperty(graph, cp.second);
91             if(cp2 != null)
92                 result.addAll(ConnectionBrowser.flatten(graph, component, cp.second, relationType));
93             else
94                 StandardProceduralChildVariable.LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
95         }
96         return result;
97         }
98
99     @Override
100         public int hashCode() {
101                 final int prime = 31;
102                 int result = 1;
103                 result = prime * result + ((cps == null) ? 0 : cps.hashCode());
104                 result = prime * result
105                                 + ((procedural == null) ? 0 : procedural.hashCode());
106                 return result;
107         }
108
109         @Override
110         public boolean equals(Object obj) {
111                 if (this == obj)
112                         return true;
113                 if (obj == null)
114                         return false;
115                 if (getClass() != obj.getClass())
116                         return false;
117                 FixedConnection other = (FixedConnection) obj;
118                 if (cps == null) {
119                         if (other.cps != null)
120                                 return false;
121                 } else if (!cps.equals(other.cps))
122                         return false;
123                 if (procedural == null) {
124                         if (other.procedural != null)
125                                 return false;
126                 } else if (!procedural.equals(other.procedural))
127                         return false;
128                 return true;
129         }
130
131         @Override
132         public Collection<VariableConnectionPointDescriptor> getConnectionPointDescriptors(ReadGraph graph,
133                 Variable component_, Resource relationType) throws DatabaseException {
134         Set<VariableConnectionPointDescriptor> result = new THashSet<VariableConnectionPointDescriptor>();
135         for(Pair<String,Resource> cp : cps) {
136             Variable component = cp.first == null ? component_.getParent(graph) : component_;
137             Variable cp2 = component.getPossibleProperty(graph, cp.second);
138             if(cp2 != null)
139                 result.addAll(ConnectionBrowser.flatten(graph, component, cp.second, relationType));
140             else
141                 StandardProceduralChildVariable.LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
142         }
143         return result;
144         }
145         
146         @Override
147         public Collection<Variable> getConnectionPoints(ReadGraph graph, Variable component_, Resource relationType)
148                 throws DatabaseException {
149         Set<Variable> result = new THashSet<Variable>();
150         for(Pair<String,Resource> cp : cps) {
151             Variable component = cp.first == null ? component_.getParent(graph) : component_;
152             Variable cp2 = component.getPossibleProperty(graph, cp.second);
153             if(cp2 != null)
154                 for(VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second, relationType)) {
155                     result.add(desc.getVariable(graph));
156                 }
157             else
158                 StandardProceduralChildVariable.LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
159         }
160         return result;
161         }
162         
163         @Override
164         public Collection<String> getConnectionPointURIs(ReadGraph graph, Variable component_, Resource relationType)
165                 throws DatabaseException {
166         Set<String> result = new THashSet<String>();
167         for(Pair<String,Resource> cp : cps) {
168             Variable component = cp.first == null ? component_.getParent(graph) : component_;
169             Variable cp2 = component.getPossibleProperty(graph, cp.second);
170             if(cp2 != null)
171                 for(VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second, relationType)) {
172                     result.add(desc.getURI(graph));
173                 }
174             else
175                 StandardProceduralChildVariable.LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
176         }
177         return result;
178         }
179
180     @Override
181     public Connection2 getConnection2() {
182         return this;
183     }
184         
185 }