1 /*******************************************************************************
2 * Copyright (c) 2018 Association for Decentralized Information Management in
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.structural2.variables;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
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;
26 import gnu.trove.set.hash.THashSet;
29 * @author Antti Villberg
32 public class FixedConnection implements Connection, Connection2 {
34 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(FixedConnection.class);
37 * This is the parent of the component to be connected
39 private final Variable procedural;
41 private final Collection<Pair<String, Resource>> cps = new ArrayList<Pair<String, Resource>>();
43 public FixedConnection(Variable procedural) {
44 this.procedural = procedural;
47 public void addAll(List<Pair<String, Resource>> cps) throws DatabaseException {
49 * For interface connections the name is null
51 for (Pair<String, Resource> cp : cps) {
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);
69 result.add(new PairConnectionDescriptor(curConfiguration, cpzz));
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);
80 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second,
82 result.add(desc.getVariable(graph));
85 LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
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);
97 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second,
99 result.add(desc.getURI(graph));
102 logWarn(graph, cp, component, procedural);
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);
116 result.addAll(ConnectionBrowser.flatten(graph, component, cp.second, relationType));
118 logWarn(graph, cp, component, procedural);
125 public int hashCode() {
126 final int prime = 31;
128 result = prime * result + ((cps == null) ? 0 : cps.hashCode());
129 result = prime * result + ((procedural == null) ? 0 : procedural.hashCode());
134 public boolean equals(Object obj) {
139 if (getClass() != obj.getClass())
141 FixedConnection other = (FixedConnection) obj;
143 if (other.cps != null)
145 } else if (!cps.equals(other.cps))
147 if (procedural == null) {
148 if (other.procedural != null)
150 } else if (!procedural.equals(other.procedural))
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);
164 result.addAll(ConnectionBrowser.flatten(graph, base, cp.second, relationType));
166 logWarn(graph, cp, base, procedural);
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);
181 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, base, cp.second,
183 result.add(desc.getVariable(graph));
186 logWarn(graph, cp, base, procedural);
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);
201 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, base, cp.second,
203 result.add(desc.getURI(graph));
206 logWarn(graph, cp, base, procedural);
213 public Connection2 getConnection2() {
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));