]> gerrit.simantics Code Review - simantics/platform.git/blob
4c7964513f8f38b171c92b9607a1de6e4dd68bad
[simantics/platform.git] /
1 package org.simantics.modeling.scl;
2
3 import gnu.trove.map.hash.THashMap;
4
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;
32
33 public class CompileProceduralSCLMonitorRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
34     
35     protected static Name BROWSE = Name.create("Simantics/Variables", "browse");
36     protected static Name VALUE = Name.create("Simantics/Variables", "value");
37     
38     private final Resource componentType;
39     private final Resource literal;
40     private final Variable componentVariable;
41     
42     public static class CompilationContext extends AbstractExpressionCompilationContext {
43         public final THashMap<String, Pair<String,Type>> propertyMap;
44         
45         public CompilationContext(RuntimeEnvironment runtimeEnvironment,
46                 THashMap<String, Pair<String,Type>> propertyMap) {
47             super(runtimeEnvironment);
48             this.propertyMap = propertyMap;
49         }
50     }
51     
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);
57     }
58
59     public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
60         SCLContext sclContext = SCLContext.getCurrent();
61         Object oldGraph = sclContext.get("graph");
62         try {
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);
71         } finally {
72             sclContext.put("graph", oldGraph);
73         }
74     }
75
76     @Override
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);
81     }
82
83     @Override
84     protected CompilationContext getCompilationContext(ReadGraph graph)
85             throws DatabaseException {
86         return graph.syncRequest(new VariableRead<CompilationContext>(componentVariable) {
87             @Override
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);
95             }
96         });
97     }
98
99     @Override
100     protected Type getContextVariableType() {
101         return VARIABLE;
102     }
103
104     @Override
105     protected Expression getVariableAccessExpression(
106             ReadGraph graph,
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);
111         if(entry == null)
112             return null;
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))
118                 );
119         return new EApply(
120                 new EConstant(environment.getValue(VALUE), entry.second),
121                 propertyVariable
122                 );
123     }
124     
125     @Override
126     public int hashCode() {
127         return 31*componentVariable.hashCode() + literal.hashCode();
128     }
129     
130     @Override
131     public boolean equals(Object obj) {
132         if(this == obj)
133             return true;
134         if(obj == null || obj.getClass() != getClass())
135             return false;
136         CompileProceduralSCLMonitorRequest other = (CompileProceduralSCLMonitorRequest)obj;
137         return literal.equals(other.literal) && componentVariable.equals(other.componentVariable);
138     }
139 }
140