1 package org.simantics.modeling.scl;
3 import gnu.trove.map.hash.THashMap;
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
9 import org.simantics.db.common.request.IndexRoot;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.request.VariableRead;
12 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
13 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
14 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
15 import org.simantics.db.layer0.variable.Variable;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.modeling.ProceduralSubstructureMapRequest;
18 import org.simantics.modeling.scl.CompileProceduralSCLMonitorRequest.CompilationContext;
19 import org.simantics.scl.compiler.common.names.Name;
20 import org.simantics.scl.compiler.constants.StringConstant;
21 import org.simantics.scl.compiler.elaboration.expressions.EApply;
22 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
23 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
24 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
25 import org.simantics.scl.compiler.elaboration.expressions.Expression;
26 import org.simantics.scl.compiler.environment.Environment;
27 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
28 import org.simantics.scl.compiler.types.Type;
29 import org.simantics.scl.runtime.SCLContext;
30 import org.simantics.scl.runtime.function.Function1;
31 import org.simantics.utils.datastructures.Pair;
33 public class CompileProceduralSCLMonitorRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
35 protected static Name BROWSE = Name.create("Simantics/Variables", "browse");
36 protected static Name VALUE = Name.create("Simantics/Variables", "value");
38 private final Resource componentType;
39 private final Resource literal;
40 private final Variable componentVariable;
42 public static class CompilationContext extends AbstractExpressionCompilationContext {
43 public final THashMap<String, Pair<String,Type>> propertyMap;
45 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
46 THashMap<String, Pair<String,Type>> propertyMap) {
47 super(runtimeEnvironment);
48 this.propertyMap = propertyMap;
52 public CompileProceduralSCLMonitorRequest(ReadGraph graph, Variable context)
53 throws DatabaseException {
54 this.componentType = context.getParent(graph).getType(graph);
55 this.literal = context.getRepresents(graph);
56 this.componentVariable = context.getParent(graph);
59 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
60 SCLContext sclContext = SCLContext.getCurrent();
61 Object oldGraph = sclContext.get("graph");
63 Function1<Variable,Object> exp = graph.syncRequest(new CompileProceduralSCLMonitorRequest(graph, context),
64 TransientCacheListener.<Function1<Variable,Object>>instance());
65 sclContext.put("graph", graph);
66 return exp.apply(context.getParent(graph));
67 } catch (DatabaseException e) {
68 throw (DatabaseException)e;
69 } catch (Throwable t) {
70 throw new DatabaseException(t);
72 sclContext.put("graph", oldGraph);
77 protected String getExpressionText(ReadGraph graph)
78 throws DatabaseException {
79 Layer0 L0 = Layer0.getInstance(graph);
80 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
84 protected CompilationContext getCompilationContext(ReadGraph graph)
85 throws DatabaseException {
86 return graph.syncRequest(new VariableRead<CompilationContext>(componentVariable) {
88 public CompilationContext perform(ReadGraph graph)
89 throws DatabaseException {
90 Resource indexRoot = graph.syncRequest(new IndexRoot(componentType));
91 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
92 THashMap<String, Pair<String,Type>> propertyMap =
93 graph.sync(new ProceduralSubstructureMapRequest(componentVariable));
94 return new CompilationContext(runtimeEnvironment, propertyMap);
100 protected Type getContextVariableType() {
105 protected Expression getVariableAccessExpression(
107 CompilationContext context,
108 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
109 String name) throws DatabaseException {
110 Pair<String,Type> entry = context.propertyMap.get(name);
113 Environment environment = context.runtimeEnvironment.getEnvironment();
114 Expression propertyVariable = new EApply(
115 new EConstant(environment.getValue(BROWSE)),
116 new EVariable(contextVariable),
117 new ELiteral(new StringConstant(entry.first))
120 new EConstant(environment.getValue(VALUE), entry.second),
126 public int hashCode() {
127 return 31*componentVariable.hashCode() + literal.hashCode();
131 public boolean equals(Object obj) {
134 if(obj == null || obj.getClass() != getClass())
136 CompileProceduralSCLMonitorRequest other = (CompileProceduralSCLMonitorRequest)obj;
137 return literal.equals(other.literal) && componentVariable.equals(other.componentVariable);