]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/FixedConnection.java
ecafac42f6eba7e7c4b78b18419753f93ea717ba
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / variables / FixedConnection.java
1 /*******************************************************************************
2  * Copyright (c) 2018 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.structural2.variables;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Set;
18
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.variable.Variable;
23 import org.simantics.utils.datastructures.Pair;
24 import org.slf4j.LoggerFactory;
25
26 import gnu.trove.set.hash.THashSet;
27
28 /**
29  * @author Antti Villberg
30  * @since 1.36.0
31  */
32 public class FixedConnection implements Connection, Connection2 {
33
34     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(FixedConnection.class);
35
36     /*
37      * This is the parent of the component to be connected
38      */
39     private final Variable procedural;
40
41     private final Collection<Pair<String, Resource>> cps = new ArrayList<Pair<String, Resource>>();
42
43     public FixedConnection(Variable procedural) {
44         this.procedural = procedural;
45     }
46
47     public void addAll(List<Pair<String, Resource>> cps) throws DatabaseException {
48         /*
49          * For interface connections the name is null
50          */
51         for (Pair<String, Resource> cp : cps) {
52             this.cps.add(cp);
53         }
54     }
55
56     public int size() {
57         return cps.size();
58     }
59
60     public void addConnectionDescriptors(ReadGraph graph, Variable curConfiguration,
61             Collection<VariableConnectionPointDescriptor> result) throws DatabaseException {
62         for (Pair<String, Resource> cpzz : cps) {
63             // This is a connection to an interface terminal. It is handled by
64             // ConnectionBrowser in separate logic. We should never have gotten this far
65             if (cpzz.first == null) {
66                 String message = "Lifted connection was not resolved. Child = " + procedural.getURI(graph);
67                 throw new DatabaseException(message);
68             }
69             result.add(new PairConnectionDescriptor(curConfiguration, cpzz));
70         }
71     }
72
73     @Override
74     public Collection<Variable> getConnectionPoints(ReadGraph graph, Resource relationType) throws DatabaseException {
75         Set<Variable> result = new THashSet<Variable>();
76         for (Pair<String, Resource> cp : cps) {
77             Variable component = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
78             Variable cp2 = component.getPossibleProperty(graph, cp.second);
79             if (cp2 != null)
80                 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second,
81                         relationType)) {
82                     result.add(desc.getVariable(graph));
83                 }
84             else
85                 LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
86         }
87         return result;
88     }
89
90     @Override
91     public Collection<String> getConnectionPointURIs(ReadGraph graph, Resource relationType) throws DatabaseException {
92         Set<String> result = new THashSet<String>();
93         for (Pair<String, Resource> cp : cps) {
94             Variable component = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
95             Variable cp2 = component.getPossibleProperty(graph, cp.second);
96             if (cp2 != null) {
97                 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second,
98                         relationType)) {
99                     result.add(desc.getURI(graph));
100                 }
101             } else {
102                 logWarn(graph, cp, component, procedural);
103             }
104         }
105         return result;
106     }
107
108     @Override
109     public Collection<VariableConnectionPointDescriptor> getConnectionPointDescriptors(ReadGraph graph,
110             Resource relationType) throws DatabaseException {
111         Set<VariableConnectionPointDescriptor> result = new THashSet<>();
112         for (Pair<String, Resource> cp : cps) {
113             Variable component = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
114             Variable cp2 = component.getPossibleProperty(graph, cp.second);
115             if (cp2 != null) {
116                 result.addAll(ConnectionBrowser.flatten(graph, component, cp.second, relationType));
117             } else {
118                 logWarn(graph, cp, component, procedural);
119             }
120         }
121         return result;
122     }
123
124     @Override
125     public int hashCode() {
126         final int prime = 31;
127         int result = 1;
128         result = prime * result + ((cps == null) ? 0 : cps.hashCode());
129         result = prime * result + ((procedural == null) ? 0 : procedural.hashCode());
130         return result;
131     }
132
133     @Override
134     public boolean equals(Object obj) {
135         if (this == obj)
136             return true;
137         if (obj == null)
138             return false;
139         if (getClass() != obj.getClass())
140             return false;
141         FixedConnection other = (FixedConnection) obj;
142         if (cps == null) {
143             if (other.cps != null)
144                 return false;
145         } else if (!cps.equals(other.cps))
146             return false;
147         if (procedural == null) {
148             if (other.procedural != null)
149                 return false;
150         } else if (!procedural.equals(other.procedural))
151             return false;
152         return true;
153     }
154
155     @Override
156     public Collection<VariableConnectionPointDescriptor> getConnectionPointDescriptors(ReadGraph graph,
157             Variable component, Resource relationType) throws DatabaseException {
158         Set<VariableConnectionPointDescriptor> result = new THashSet<>();
159         Variable procedural = component.getParent(graph);
160         for (Pair<String, Resource> cp : cps) {
161             Variable base = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
162             Variable cp2 = base.getPossibleProperty(graph, cp.second);
163             if (cp2 != null) {
164                 result.addAll(ConnectionBrowser.flatten(graph, base, cp.second, relationType));
165             } else {
166                 logWarn(graph, cp, base, procedural);
167             }
168         }
169         return result;
170     }
171
172     @Override
173     public Collection<Variable> getConnectionPoints(ReadGraph graph, Variable component, Resource relationType)
174             throws DatabaseException {
175         Set<Variable> result = new THashSet<>();
176         Variable procedural = component.getParent(graph);
177         for (Pair<String, Resource> cp : cps) {
178             Variable base = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
179             Variable cp2 = base.getPossibleProperty(graph, cp.second);
180             if (cp2 != null) {
181                 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, base, cp.second,
182                         relationType)) {
183                     result.add(desc.getVariable(graph));
184                 }
185             } else {
186                 logWarn(graph, cp, base, procedural);
187             }
188         }
189         return result;
190     }
191
192     @Override
193     public Collection<String> getConnectionPointURIs(ReadGraph graph, Variable component, Resource relationType)
194             throws DatabaseException {
195         Set<String> result = new THashSet<>();
196         Variable procedural = component.getParent(graph);
197         for (Pair<String, Resource> cp : cps) {
198             Variable base = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
199             Variable cp2 = base.getPossibleProperty(graph, cp.second);
200             if (cp2 != null) {
201                 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, base, cp.second,
202                         relationType)) {
203                     result.add(desc.getURI(graph));
204                 }
205             } else {
206                 logWarn(graph, cp, base, procedural);
207             }
208         }
209         return result;
210     }
211
212     @Override
213     public Connection2 getConnection2() {
214         return this;
215     }
216
217     private static void logWarn(ReadGraph graph, Pair<String, Resource> cp, Variable base, Variable procedural) throws DatabaseException {
218         LOGGER.warn("no cp " + cp.first + " for " + base.getURI(graph));
219         LOGGER.warn("    proc: " + procedural.getURI(graph));
220         LOGGER.warn("    rel: " + graph.getURI(cp.second));
221         LOGGER.warn("    base: " + base.getURI(graph));
222     }
223
224 }