]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/FixedConnection.java
7257ca4c5c137a5f8dc88fbc4bf7ca7d0e78c0cc
[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                 LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
103         }
104         return result;
105     }
106
107     @Override
108     public Collection<VariableConnectionPointDescriptor> getConnectionPointDescriptors(ReadGraph graph,
109             Resource relationType) throws DatabaseException {
110         Set<VariableConnectionPointDescriptor> result = new THashSet<VariableConnectionPointDescriptor>();
111         for (Pair<String, Resource> cp : cps) {
112             Variable component = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
113             Variable cp2 = component.getPossibleProperty(graph, cp.second);
114             if (cp2 != null)
115                 result.addAll(ConnectionBrowser.flatten(graph, component, cp.second, relationType));
116             else
117                 LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
118         }
119         return result;
120     }
121
122     @Override
123     public int hashCode() {
124         final int prime = 31;
125         int result = 1;
126         result = prime * result + ((cps == null) ? 0 : cps.hashCode());
127         result = prime * result + ((procedural == null) ? 0 : procedural.hashCode());
128         return result;
129     }
130
131     @Override
132     public boolean equals(Object obj) {
133         if (this == obj)
134             return true;
135         if (obj == null)
136             return false;
137         if (getClass() != obj.getClass())
138             return false;
139         FixedConnection other = (FixedConnection) obj;
140         if (cps == null) {
141             if (other.cps != null)
142                 return false;
143         } else if (!cps.equals(other.cps))
144             return false;
145         if (procedural == null) {
146             if (other.procedural != null)
147                 return false;
148         } else if (!procedural.equals(other.procedural))
149             return false;
150         return true;
151     }
152
153     @Override
154     public Collection<VariableConnectionPointDescriptor> getConnectionPointDescriptors(ReadGraph graph,
155             Variable component, Resource relationType) throws DatabaseException {
156         Set<VariableConnectionPointDescriptor> result = new THashSet<VariableConnectionPointDescriptor>();
157         for (Pair<String, Resource> cp : cps) {
158             Variable base = cp.first == null ? component.getParent(graph) : component;
159             Variable cp2 = base.getPossibleProperty(graph, cp.second);
160             if (cp2 != null)
161                 result.addAll(ConnectionBrowser.flatten(graph, base, cp.second, relationType));
162             else
163                 LOGGER.warn("no cp " + cp.first + " for " + base.getURI(graph));
164         }
165         return result;
166     }
167
168     @Override
169     public Collection<Variable> getConnectionPoints(ReadGraph graph, Variable component, Resource relationType)
170             throws DatabaseException {
171         Set<Variable> result = new THashSet<Variable>();
172         for (Pair<String, Resource> cp : cps) {
173             Variable base = cp.first == null ? component.getParent(graph) : component;
174             Variable cp2 = base.getPossibleProperty(graph, cp.second);
175             if (cp2 != null)
176                 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, base, cp.second,
177                         relationType)) {
178                     result.add(desc.getVariable(graph));
179                 }
180             else
181                 LOGGER.warn("no cp " + cp.first + " for " + base.getURI(graph));
182         }
183         return result;
184     }
185
186     @Override
187     public Collection<String> getConnectionPointURIs(ReadGraph graph, Variable component, Resource relationType)
188             throws DatabaseException {
189         Set<String> result = new THashSet<String>();
190         for (Pair<String, Resource> cp : cps) {
191             Variable base = cp.first == null ? component.getParent(graph) : component;
192             Variable cp2 = base.getPossibleProperty(graph, cp.second);
193             if (cp2 != null)
194                 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, base, cp.second,
195                         relationType)) {
196                     result.add(desc.getURI(graph));
197                 }
198             else
199                 LOGGER.warn("no cp " + cp.first + " for " + base.getURI(graph));
200         }
201         return result;
202     }
203
204     @Override
205     public Connection2 getConnection2() {
206         return this;
207     }
208
209 }