]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/FixedConnection.java
3f5219e6176aa9aed61b5f8278cf21a52e939697
[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     final private Variable parent;
19     
20     final private Collection<Pair<String,Resource>> cps = new ArrayList<Pair<String,Resource>>();
21     
22     public FixedConnection(Variable parent) {
23         this.parent = parent;
24     }
25     
26     public void addAll(List<Pair<String,Resource>> cps) throws DatabaseException {
27         for(Pair<String,Resource> cp : cps) {
28             this.cps.add(cp);
29         }
30     }
31     
32     public int size() {
33         return cps.size();
34     }
35     
36     public void addConnectionDescriptors(ReadGraph graph, Variable curConfiguration, Collection<VariableConnectionPointDescriptor> result) throws DatabaseException {
37         for(Pair<String,Resource> cpzz : cps) {
38             // This is a connection to an interface terminal. It is handled by ConnectionBrowser in separate logic. Do not include it here.
39             if(cpzz.first == null) continue;
40             /*if(cpzz.first == null) {
41                 String message = "Lifted connection was not resolved. Child = " + parent.getURI(graph);
42                 throw new DatabaseException(message);
43             }*/
44             result.add(new PairConnectionDescriptor(curConfiguration, cpzz));
45         }
46     }
47     
48     @Override
49     public Collection<Variable> getConnectionPoints(ReadGraph graph, Resource relationType) throws DatabaseException {
50         return getConnectionPoints(graph, parent, relationType);
51     }
52     
53         @Override
54         public Collection<String> getConnectionPointURIs(ReadGraph graph, Resource relationType) throws DatabaseException {
55             return getConnectionPointURIs(graph, parent, relationType);
56                 
57         }
58         
59         @Override
60         public Collection<VariableConnectionPointDescriptor> getConnectionPointDescriptors(ReadGraph graph, Resource relationType) throws DatabaseException {
61             return getConnectionPointDescriptors(graph, parent, relationType);
62         }
63
64     @Override
65         public int hashCode() {
66                 final int prime = 31;
67                 int result = 1;
68                 result = prime * result + ((cps == null) ? 0 : cps.hashCode());
69                 result = prime * result
70                                 + ((parent == null) ? 0 : parent.hashCode());
71                 return result;
72         }
73
74         @Override
75         public boolean equals(Object obj) {
76                 if (this == obj)
77                         return true;
78                 if (obj == null)
79                         return false;
80                 if (getClass() != obj.getClass())
81                         return false;
82                 FixedConnection other = (FixedConnection) obj;
83                 if (cps == null) {
84                         if (other.cps != null)
85                                 return false;
86                 } else if (!cps.equals(other.cps))
87                         return false;
88                 if (parent == null) {
89                         if (other.parent != null)
90                                 return false;
91                 } else if (!parent.equals(other.parent))
92                         return false;
93                 return true;
94         }
95
96         @Override
97         public Collection<VariableConnectionPointDescriptor> getConnectionPointDescriptors(ReadGraph graph,
98                 Variable parent, Resource relationType) throws DatabaseException {
99         Set<VariableConnectionPointDescriptor> result = new THashSet<VariableConnectionPointDescriptor>();
100         for(Pair<String,Resource> cp : cps) {
101             Variable component = cp.first == null ? parent : parent.getChild(graph, cp.first); 
102             Variable cp2 = component.getPossibleProperty(graph, cp.second);
103             if(cp2 != null)
104                 result.addAll(ConnectionBrowser.flatten(graph, component, cp.second, relationType));
105             else
106                 StandardProceduralChildVariable.LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
107         }
108         return result;
109         }
110         
111         @Override
112         public Collection<Variable> getConnectionPoints(ReadGraph graph, Variable parent, Resource relationType)
113                 throws DatabaseException {
114         Set<Variable> result = new THashSet<Variable>();
115         for(Pair<String,Resource> cp : cps) {
116             Variable component = cp.first == null ? parent : parent.getChild(graph, cp.first); 
117             Variable cp2 = component.getPossibleProperty(graph, cp.second);
118             if(cp2 != null)
119                 for(VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second, relationType)) {
120                     result.add(desc.getVariable(graph));
121                 }
122             else
123                 StandardProceduralChildVariable.LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
124         }
125         return result;
126         }
127         
128         @Override
129         public Collection<String> getConnectionPointURIs(ReadGraph graph, Variable parent, Resource relationType)
130                 throws DatabaseException {
131         Set<String> result = new THashSet<String>();
132         for(Pair<String,Resource> cp : cps) {
133             Variable component = cp.first == null ? parent : parent.getChild(graph, cp.first);
134             Variable cp2 = component.getPossibleProperty(graph, cp.second);
135             if(cp2 != null)
136                 for(VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second, relationType)) {
137                     result.add(desc.getURI(graph));
138                 }
139             else
140                 StandardProceduralChildVariable.LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
141         }
142         return result;
143         }
144
145     @Override
146     public Connection2 getConnection2() {
147         return this;
148     }
149         
150 }