From b5d9e2433f9e974560aca81013f526af256b0409 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Wed, 21 Nov 2018 10:31:37 +0200 Subject: [PATCH] Fix errors with procedural user components for computational values gitlab #169 Merged cherry pick from master. Change-Id: I1d95429ea1c49ca2f8bd20d447500513a3e6f87a --- .../db/layer0/scl/CompileValueRequest.java | 30 +++- .../util/RuntimeEnvironmentRequest2.java | 13 +- .../ReadComponentTypeInterfaceRequest.java | 10 +- .../variables/FixedConnection.java | 162 ++++++++++++++++++ 4 files changed, 199 insertions(+), 16 deletions(-) create mode 100644 bundles/org.simantics.structural2/src/org/simantics/structural2/variables/FixedConnection.java diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/CompileValueRequest.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/CompileValueRequest.java index e1804d790..11f3ad185 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/CompileValueRequest.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/CompileValueRequest.java @@ -25,7 +25,7 @@ import org.simantics.scl.runtime.function.Function1; * * @author Tuukka Lehtonen */ -public class CompileValueRequest extends AbstractExpressionCompilationRequest { +public class CompileValueRequest extends AbstractExpressionCompilationRequest { public static class CompilationContext extends AbstractExpressionCompilationContext { public CompilationContext(RuntimeEnvironment runtimeEnvironment) { @@ -34,27 +34,23 @@ public class CompileValueRequest extends AbstractExpressionCompilationRequest exp = graph.syncRequest(new CompileValueRequest(graph, context), - TransientCacheListener.>instance()); + Function1 exp = graph.syncRequest(new CompileValueRequest(graph, context), + TransientCacheListener.instance()); sclContext.put("graph", graph); return exp.apply(context); } catch (DatabaseException e) { @@ -66,6 +62,22 @@ public class CompileValueRequest extends AbstractExpressionCompilationRequest compile(ReadGraph graph, Resource literal, Resource predicate) throws DatabaseException { + SCLContext sclContext = SCLContext.getCurrent(); + Object oldGraph = sclContext.get("graph"); + try { + Function1 exp = graph.syncRequest(new CompileValueRequest(literal, predicate), TransientCacheListener.instance()); + sclContext.put("graph", graph); + return exp; + } catch (DatabaseException e) { + throw (DatabaseException)e; + } catch (Throwable t) { + throw new DatabaseException(t); + } finally { + sclContext.put("graph", oldGraph); + } + } + @Override protected String getExpressionText(ReadGraph graph) throws DatabaseException { diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java index 4bbd48002..2948379be 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java @@ -1,8 +1,6 @@ package org.simantics.db.layer0.util; import java.util.Collection; -import java.util.HashMap; -import java.util.Map; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -111,9 +109,14 @@ public class RuntimeEnvironmentRequest2 extends BinaryRead sclModules = graph.syncRequest(new ObjectsWithType(parameter, L0.ConsistsOf, L0.SCLModule)); - for (Resource sclModule : sclModules) - environmentSpecification.importModule(graph.getURI(sclModule), ""); + if (parameter != null) { + Collection sclModules = graph.syncRequest(new ObjectsWithType(parameter, L0.ConsistsOf, L0.SCLModule)); + for (Resource sclModule : sclModules) { + environmentSpecification.importModule(graph.getURI(sclModule), ""); + } + } else { + // `parameter` is optional and can be null for e.g. procedural user components + } Resource mainModule = Layer0Utils.getPossibleChild(graph, parameter2, "SCLMain"); if(mainModule != null) diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/scl/ReadComponentTypeInterfaceRequest.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/scl/ReadComponentTypeInterfaceRequest.java index a93cbe8d4..6ac607e4f 100644 --- a/bundles/org.simantics.structural2/src/org/simantics/structural2/scl/ReadComponentTypeInterfaceRequest.java +++ b/bundles/org.simantics.structural2/src/org/simantics/structural2/scl/ReadComponentTypeInterfaceRequest.java @@ -2,6 +2,7 @@ package org.simantics.structural2.scl; import gnu.trove.map.hash.THashMap; +import java.util.Collections; import java.util.Map; import org.simantics.databoard.Bindings; @@ -50,11 +51,16 @@ public class ReadComponentTypeInterfaceRequest extends ResourceRead perform(ReadGraph graph) throws DatabaseException { - + if (resource == null) + return Collections.emptyMap(); + THashMap result = new THashMap(); - collect(graph, resource, result); + // TODO: For Antti to consider + // resource == null with procedural user components + if (resource != null) + collect(graph, resource, result); //for(Resource t : graph.getSupertypes(resource)) collect(graph, t, result); return result; 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..e162c6719 --- /dev/null +++ b/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/FixedConnection.java @@ -0,0 +1,162 @@ +/******************************************************************************* + * 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 { + + 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 { + logWarn(graph, cp, component, procedural); + } + } + 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 { + logWarn(graph, cp, component, procedural); + } + } + 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; + } + + private static void logWarn(ReadGraph graph, Pair cp, Variable base, Variable procedural) throws DatabaseException { + LOGGER.warn("no cp " + cp.first + " for " + base.getURI(graph)); + LOGGER.warn(" proc: " + procedural.getURI(graph)); + LOGGER.warn(" rel: " + graph.getURI(cp.second)); + LOGGER.warn(" base: " + base.getURI(graph)); + } + +} -- 2.43.2