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.scl.runtime.SCLContext;
9 import org.simantics.scl.runtime.function.Function1;
10 import org.simantics.scl.runtime.function.Function2;
11 import org.simantics.scl.runtime.function.Function3;
13 public class SCLValueAccessor implements ValueAccessor {
15 private final Function1<Variable,Object> getValue1;
16 private final Function2<Variable,Binding,Object> getValue2;
17 private final Function2<Variable,Object,Object> setValue2;
18 private final Function3<Variable,Object,Binding,Object> setValue3;
19 private final Function1<Variable,Datatype> getDatatype;
21 public SCLValueAccessor(Function1<Variable, Object> getValue1, Function2<Variable, Binding, Object> getValue2,
22 Function2<Variable, Object, Object> setValue2, Function3<Variable, Object, Binding, Object> setValue3,
23 Function1<Variable, Datatype> getDatatype) {
24 this.getValue1 = getValue1;
25 this.getValue2 = getValue2;
26 this.setValue2 = setValue2;
27 this.setValue3 = setValue3;
28 this.getDatatype = getDatatype;
32 public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
33 SCLContext sclContext = SCLContext.getCurrent();
34 Object oldGraph = sclContext.put("graph", graph);
36 return getValue1.apply(context);
37 } catch (Throwable t) {
38 throw new DatabaseException(t);
40 sclContext.put("graph", oldGraph);
45 public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException {
46 SCLContext sclContext = SCLContext.getCurrent();
47 Object oldGraph = sclContext.put("graph", graph);
49 return getValue2.apply(context, binding);
50 } catch (Throwable t) {
51 throw new DatabaseException(t);
53 sclContext.put("graph", oldGraph);
58 public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
59 SCLContext sclContext = SCLContext.getCurrent();
60 Object oldGraph = sclContext.put("graph", graph);
62 setValue2.apply(context, value);
63 } catch (Throwable t) {
64 throw new DatabaseException(t);
66 sclContext.put("graph", oldGraph);
71 public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException {
72 SCLContext sclContext = SCLContext.getCurrent();
73 Object oldGraph = sclContext.put("graph", graph);
75 setValue3.apply(context, value, binding);
76 } catch (Throwable t) {
77 throw new DatabaseException(t);
79 sclContext.put("graph", oldGraph);
84 public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException {
85 SCLContext sclContext = SCLContext.getCurrent();
86 Object oldGraph = sclContext.put("graph", graph);
88 return getDatatype.apply(context);
89 } catch (Throwable t) {
90 throw new DatabaseException(t);
92 sclContext.put("graph", oldGraph);