1 package org.simantics.db.layer0.variable;
3 import org.simantics.databoard.binding.Binding;
4 import org.simantics.databoard.type.Datatype;
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.WriteGraph;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.exception.DatatypeNotFoundException;
9 import org.simantics.db.layer0.exception.MissingVariableValueException;
10 import org.simantics.db.layer0.exception.NonWritableVariableException;
11 import org.simantics.scl.runtime.SCLContext;
12 import org.simantics.scl.runtime.function.Function1;
13 import org.simantics.scl.runtime.function.Function2;
14 import org.simantics.scl.runtime.function.Function3;
16 public class SCLValueAccessor implements ValueAccessor {
18 private final Function1<Variable,Object> getValue1;
19 private final Function2<Variable,Binding,Object> getValue2;
20 private final Function2<Variable,Object,Object> setValue2;
21 private final Function3<Variable,Object,Binding,Object> setValue3;
22 private final Function1<Variable,Datatype> getDatatype;
24 public SCLValueAccessor(Function1<Variable, Object> getValue1, Function2<Variable, Binding, Object> getValue2,
25 Function2<Variable, Object, Object> setValue2, Function3<Variable, Object, Binding, Object> setValue3,
26 Function1<Variable, Datatype> getDatatype) {
27 this.getValue1 = getValue1;
28 this.getValue2 = getValue2;
29 this.setValue2 = setValue2;
30 this.setValue3 = setValue3;
31 this.getDatatype = getDatatype;
35 public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
36 SCLContext sclContext = SCLContext.getCurrent();
37 Object oldGraph = sclContext.put("graph", graph);
39 return getValue1.apply(context);
40 } catch (Throwable t) {
41 throw new MissingVariableValueException("Could not get value for " + String.valueOf(context.getRepresents(graph)), t);
43 sclContext.put("graph", oldGraph);
48 public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException {
49 SCLContext sclContext = SCLContext.getCurrent();
50 Object oldGraph = sclContext.put("graph", graph);
52 return getValue2.apply(context, binding);
53 } catch (Throwable t) {
54 throw new MissingVariableValueException("Could not get value for " + String.valueOf(context.getRepresents(graph)) + " with binding " + binding, t);
56 sclContext.put("graph", oldGraph);
61 public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
62 SCLContext sclContext = SCLContext.getCurrent();
63 Object oldGraph = sclContext.put("graph", graph);
65 setValue2.apply(context, value);
66 } catch (Throwable t) {
67 throw new NonWritableVariableException("Could not write value " + String.valueOf(value) + " for " + String.valueOf(context.getRepresents(graph)), t);
69 sclContext.put("graph", oldGraph);
74 public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException {
75 SCLContext sclContext = SCLContext.getCurrent();
76 Object oldGraph = sclContext.put("graph", graph);
78 setValue3.apply(context, value, binding);
79 } catch (Throwable t) {
80 throw new NonWritableVariableException("Could not write value " + String.valueOf(value) + " for " + String.valueOf(context.getRepresents(graph)) + " with binding " + binding, t);
82 sclContext.put("graph", oldGraph);
87 public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException {
88 SCLContext sclContext = SCLContext.getCurrent();
89 Object oldGraph = sclContext.put("graph", graph);
91 return getDatatype.apply(context);
92 } catch (Throwable t) {
93 throw new DatatypeNotFoundException("Could not find datatype for " + String.valueOf(context.getRepresents(graph)), t);
95 sclContext.put("graph", oldGraph);