From c433e87787fb162d3f5d966678aaedf491266080 Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Wed, 5 Feb 2020 15:47:13 +0200 Subject: [PATCH] Non-constant external value configuration via procedural UCs. gitlab #461 Change-Id: I764644ea60a421e6c807486c925ccd9e33aa6928 --- .../StandardProceduralChildVariable.java | 9 +++ .../StandardProdeduralPropertyVariable.java | 71 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 bundles/org.simantics.structural2/src/org/simantics/structural2/variables/StandardProdeduralPropertyVariable.java diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/StandardProceduralChildVariable.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/StandardProceduralChildVariable.java index e96fd050a..e7ba5ab78 100644 --- a/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/StandardProceduralChildVariable.java +++ b/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/StandardProceduralChildVariable.java @@ -7,6 +7,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import org.simantics.databoard.Bindings; @@ -115,6 +116,14 @@ public class StandardProceduralChildVariable extends AbstractChildVariable { } else if (p.value instanceof Expression) { Expression expression = (Expression)p.value; this.properties.put(pn, new StructuralProceduralExpressionPropertyVariable(graph, this, p.relation, expression.text) ); + } else if (this.properties.containsKey(pn)) { + // The property overrides the value of an asserted property variable + StandardAssertedGraphPropertyVariable assertedValue = (StandardAssertedGraphPropertyVariable) this.properties.get(pn); + if (Objects.equals(assertedValue.property.predicate, p.relation)) { + this.properties.put(pn, new StandardProdeduralPropertyVariable(graph, this, assertedValue, p.value)); + } else { + LOGGER.warn("Ignored attempt to override asserted property {}/{}#{} with a different relation", parent.getURI(graph), name, pn); + } } else { this.properties.put(pn, new StandardConstantGraphPropertyVariable(graph, this, p.relation, p.value) ); } diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/StandardProdeduralPropertyVariable.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/StandardProdeduralPropertyVariable.java new file mode 100644 index 000000000..a277f5142 --- /dev/null +++ b/bundles/org.simantics.structural2/src/org/simantics/structural2/variables/StandardProdeduralPropertyVariable.java @@ -0,0 +1,71 @@ +package org.simantics.structural2.variables; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.type.Datatype; +import org.simantics.db.ReadGraph; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.StandardAssertedGraphPropertyVariable; +import org.simantics.db.layer0.variable.ValueAccessor; +import org.simantics.db.layer0.variable.Variable; + +public class StandardProdeduralPropertyVariable extends StandardAssertedGraphPropertyVariable { + + private Object defaultValue; + + public StandardProdeduralPropertyVariable(ReadGraph graph, + StandardProceduralChildVariable parent, + StandardAssertedGraphPropertyVariable assertedVariable, Object value) throws DatabaseException { + super(graph, parent, assertedVariable.node, assertedVariable.parentResource, assertedVariable.property.predicate, assertedVariable.object); + + this.defaultValue = value; + } + + @Override + protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException { + final ValueAccessor valueAccessor = super.getValueAccessor(graph); + return new ValueAccessor() { + @Override + public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException { + valueAccessor.setValue(graph, context, value, binding); + } + + @Override + public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException { + valueAccessor.setValue(graph, context, value); + } + + @Override + public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException { + if (node != null) { + return valueAccessor.getValue(graph, context, binding); + } else { + if (binding.isInstance(defaultValue)) + return defaultValue; + else + try { + return Bindings.adapt(defaultValue, Bindings.OBJECT, binding); + } catch (AdaptException e) { + throw new DatabaseException("Failed to adapt object " + defaultValue + " to " + binding); + } + } + } + + @Override + public Object getValue(ReadGraph graph, Variable context) throws DatabaseException { + if (node != null) { + return valueAccessor.getValue(graph, context); + } else { + return defaultValue; + } + } + + @Override + public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException { + return valueAccessor.getDatatype(graph, context); + } + }; + } +} -- 2.43.2