]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/SCLValueAccessor.java
Merge "Revert "Prime SCL BindingRegistry to shave ~0.5s from startup""
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / SCLValueAccessor.java
1 package org.simantics.db.layer0.variable;
2
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;
15
16 public class SCLValueAccessor implements ValueAccessor {
17
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;
23         
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;
32         }
33
34         @Override
35         public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
36         SCLContext sclContext = SCLContext.getCurrent();
37         Object oldGraph = sclContext.put("graph", graph);
38         try {
39             return getValue1.apply(context);
40         } catch (Throwable t) {
41             throw new MissingVariableValueException("Could not get value for " + String.valueOf(context.getRepresents(graph)), t);
42         } finally {
43             sclContext.put("graph", oldGraph);
44         }
45         }
46
47         @Override
48         public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException {
49         SCLContext sclContext = SCLContext.getCurrent();
50         Object oldGraph = sclContext.put("graph", graph);
51         try {
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);
55         } finally {
56             sclContext.put("graph", oldGraph);
57         }
58         }
59
60         @Override
61         public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
62         SCLContext sclContext = SCLContext.getCurrent();
63         Object oldGraph = sclContext.put("graph", graph);
64         try {
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);
68         } finally {
69             sclContext.put("graph", oldGraph);
70         }
71         }
72
73         @Override
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);
77         try {
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);
81         } finally {
82             sclContext.put("graph", oldGraph);
83         }
84         }
85
86         @Override
87         public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException {
88         SCLContext sclContext = SCLContext.getCurrent();
89         Object oldGraph = sclContext.put("graph", graph);
90         try {
91             return getDatatype.apply(context);
92         } catch (Throwable t) {
93             throw new DatatypeNotFoundException("Could not find datatype for " + String.valueOf(context.getRepresents(graph)), t);
94         } finally {
95             sclContext.put("graph", oldGraph);
96         }
97         }
98
99 }