X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.structural2%2Fsrc%2Forg%2Fsimantics%2Fstructural2%2Fvariables%2FFixedConnection.java;fp=bundles%2Forg.simantics.structural2%2Fsrc%2Forg%2Fsimantics%2Fstructural2%2Fvariables%2FFixedConnection.java;h=7257ca4c5c137a5f8dc88fbc4bf7ca7d0e78c0cc;hb=2d97029aeaaf5d6a965eae98c1646eef29ae2f8b;hp=0000000000000000000000000000000000000000;hpb=25ff14b9cb52ccec8a7d6117f089d9ccbbb08ce2;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/FixedConnection.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/FixedConnection.java new file mode 100644 index 000000000..7257ca4c5 --- /dev/null +++ b/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/FixedConnection.java @@ -0,0 +1,209 @@ +/******************************************************************************* + * Copyright (c) 2018 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.structural2.variables; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Set; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.utils.datastructures.Pair; +import org.slf4j.LoggerFactory; + +import gnu.trove.set.hash.THashSet; + +/** + * @author Antti Villberg + * @since 1.36.0 + */ +public class FixedConnection implements Connection, Connection2 { + + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(FixedConnection.class); + + /* + * This is the parent of the component to be connected + */ + private final Variable procedural; + + private final Collection> cps = new ArrayList>(); + + public FixedConnection(Variable procedural) { + this.procedural = procedural; + } + + public void addAll(List> cps) throws DatabaseException { + /* + * For interface connections the name is null + */ + for (Pair cp : cps) { + this.cps.add(cp); + } + } + + public int size() { + return cps.size(); + } + + public void addConnectionDescriptors(ReadGraph graph, Variable curConfiguration, + Collection result) throws DatabaseException { + for (Pair cpzz : cps) { + // This is a connection to an interface terminal. It is handled by + // ConnectionBrowser in separate logic. We should never have gotten this far + if (cpzz.first == null) { + String message = "Lifted connection was not resolved. Child = " + procedural.getURI(graph); + throw new DatabaseException(message); + } + result.add(new PairConnectionDescriptor(curConfiguration, cpzz)); + } + } + + @Override + public Collection getConnectionPoints(ReadGraph graph, Resource relationType) throws DatabaseException { + Set result = new THashSet(); + for (Pair cp : cps) { + Variable component = cp.first == null ? procedural : procedural.getChild(graph, cp.first); + Variable cp2 = component.getPossibleProperty(graph, cp.second); + if (cp2 != null) + for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second, + relationType)) { + result.add(desc.getVariable(graph)); + } + else + LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph)); + } + return result; + } + + @Override + public Collection getConnectionPointURIs(ReadGraph graph, Resource relationType) throws DatabaseException { + Set result = new THashSet(); + for (Pair cp : cps) { + Variable component = cp.first == null ? procedural : procedural.getChild(graph, cp.first); + Variable cp2 = component.getPossibleProperty(graph, cp.second); + if (cp2 != null) + for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second, + relationType)) { + result.add(desc.getURI(graph)); + } + else + LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph)); + } + return result; + } + + @Override + public Collection getConnectionPointDescriptors(ReadGraph graph, + Resource relationType) throws DatabaseException { + Set result = new THashSet(); + for (Pair cp : cps) { + Variable component = cp.first == null ? procedural : procedural.getChild(graph, cp.first); + Variable cp2 = component.getPossibleProperty(graph, cp.second); + if (cp2 != null) + result.addAll(ConnectionBrowser.flatten(graph, component, cp.second, relationType)); + else + LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph)); + } + return result; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((cps == null) ? 0 : cps.hashCode()); + result = prime * result + ((procedural == null) ? 0 : procedural.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + FixedConnection other = (FixedConnection) obj; + if (cps == null) { + if (other.cps != null) + return false; + } else if (!cps.equals(other.cps)) + return false; + if (procedural == null) { + if (other.procedural != null) + return false; + } else if (!procedural.equals(other.procedural)) + return false; + return true; + } + + @Override + public Collection getConnectionPointDescriptors(ReadGraph graph, + Variable component, Resource relationType) throws DatabaseException { + Set result = new THashSet(); + for (Pair cp : cps) { + Variable base = cp.first == null ? component.getParent(graph) : component; + Variable cp2 = base.getPossibleProperty(graph, cp.second); + if (cp2 != null) + result.addAll(ConnectionBrowser.flatten(graph, base, cp.second, relationType)); + else + LOGGER.warn("no cp " + cp.first + " for " + base.getURI(graph)); + } + return result; + } + + @Override + public Collection getConnectionPoints(ReadGraph graph, Variable component, Resource relationType) + throws DatabaseException { + Set result = new THashSet(); + for (Pair cp : cps) { + Variable base = cp.first == null ? component.getParent(graph) : component; + Variable cp2 = base.getPossibleProperty(graph, cp.second); + if (cp2 != null) + for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, base, cp.second, + relationType)) { + result.add(desc.getVariable(graph)); + } + else + LOGGER.warn("no cp " + cp.first + " for " + base.getURI(graph)); + } + return result; + } + + @Override + public Collection getConnectionPointURIs(ReadGraph graph, Variable component, Resource relationType) + throws DatabaseException { + Set result = new THashSet(); + for (Pair cp : cps) { + Variable base = cp.first == null ? component.getParent(graph) : component; + Variable cp2 = base.getPossibleProperty(graph, cp.second); + if (cp2 != null) + for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, base, cp.second, + relationType)) { + result.add(desc.getURI(graph)); + } + else + LOGGER.warn("no cp " + cp.first + " for " + base.getURI(graph)); + } + return result; + } + + @Override + public Connection2 getConnection2() { + return this; + } + +} \ No newline at end of file