Non-constant external value configuration via procedural UCs. 31/3831/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Wed, 5 Feb 2020 13:47:13 +0000 (15:47 +0200)
committerReino Ruusu <reino.ruusu@semantum.fi>
Wed, 5 Feb 2020 13:48:22 +0000 (15:48 +0200)
gitlab #461

Change-Id: I764644ea60a421e6c807486c925ccd9e33aa6928

bundles/org.simantics.structural2/src/org/simantics/structural2/variables/StandardProceduralChildVariable.java
bundles/org.simantics.structural2/src/org/simantics/structural2/variables/StandardProdeduralPropertyVariable.java [new file with mode: 0644]

index e96fd050a021ee1c9451efe19c1ee88d112d5855..e7ba5ab7879c8749ffc527597e47c205629539f6 100644 (file)
@@ -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 (file)
index 0000000..a277f51
--- /dev/null
@@ -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);
+                       }
+               };
+       }
+}