1 package org.simantics.modeling.scl;
3 import org.simantics.databoard.Bindings;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
7 import org.simantics.db.common.request.IndexRoot;
8 import org.simantics.db.common.request.ResourceRead;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
11 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
12 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
13 import org.simantics.db.layer0.variable.Variable;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.modeling.ComponentTypeSubstructure;
16 import org.simantics.modeling.scl.CompileSCLMonitorRequest.CompilationContext;
17 import org.simantics.scl.compiler.common.names.Name;
18 import org.simantics.scl.compiler.constants.StringConstant;
19 import org.simantics.scl.compiler.elaboration.expressions.EApply;
20 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
21 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
22 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
23 import org.simantics.scl.compiler.elaboration.expressions.Expression;
24 import org.simantics.scl.compiler.environment.Environment;
25 import org.simantics.scl.compiler.environment.Environments;
26 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
27 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
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;
34 public class CompileSCLMonitorRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
36 protected static Name BROWSE = Name.create("Simantics/Variables", "browse");
37 protected static Name VALUE = Name.create("Simantics/Variables", "value");
39 private final Resource componentType;
40 private final Resource literal;
41 private final Resource relation;
43 public static class CompilationContext extends AbstractExpressionCompilationContext {
44 public final ComponentTypeSubstructure substructure;
46 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
47 ComponentTypeSubstructure substructure) {
48 super(runtimeEnvironment);
49 this.substructure = substructure;
53 private CompileSCLMonitorRequest(Resource componentType, Resource literal, Resource relation) {
54 this.componentType = componentType;
55 this.literal = literal;
56 this.relation = relation;
59 public CompileSCLMonitorRequest(ReadGraph graph, Variable context)
60 throws DatabaseException {
61 this(context.getParent(graph).getType(graph),
62 context.getRepresents(graph),
63 context.getPredicateResource(graph));
66 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
67 SCLContext sclContext = SCLContext.getCurrent();
68 Object oldGraph = sclContext.get("graph");
70 Function1<Variable,Object> exp = graph.syncRequest(new CompileSCLMonitorRequest(graph, context),
71 TransientCacheListener.<Function1<Variable,Object>>instance());
72 sclContext.put("graph", graph);
73 return exp.apply(context.getParent(graph));
74 } catch (DatabaseException e) {
76 throw (DatabaseException)e;
77 } catch (Throwable t) {
79 throw new DatabaseException(t);
81 sclContext.put("graph", oldGraph);
86 protected String getExpressionText(ReadGraph graph)
87 throws DatabaseException {
88 Layer0 L0 = Layer0.getInstance(graph);
89 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
93 protected Type getExpectedType(ReadGraph graph, CompilationContext context)
94 throws DatabaseException {
95 Layer0 L0 = Layer0.getInstance(graph);
96 String valueType = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
97 if(valueType != null) {
99 return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);
100 } catch (SCLExpressionCompilationException e) {
104 return super.getExpectedType(graph, context);
108 protected CompilationContext getCompilationContext(ReadGraph graph)
109 throws DatabaseException {
110 return graph.syncRequest(new ResourceRead<CompilationContext>(componentType) {
112 public CompilationContext perform(ReadGraph graph)
113 throws DatabaseException {
114 Resource indexRoot = graph.syncRequest(new IndexRoot(resource));
115 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
116 return new CompilationContext(runtimeEnvironment, ComponentTypeSubstructure.forType(graph, resource));
122 protected Type getContextVariableType() {
127 protected Expression getVariableAccessExpression(
129 CompilationContext context,
130 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
131 String name) throws DatabaseException {
132 Pair<String,Type> entry = context.substructure.possibleTypedRVI(name);
135 Environment environment = context.runtimeEnvironment.getEnvironment();
136 Expression propertyVariable = new EApply(
137 new EConstant(environment.getValue(BROWSE)),
138 new EVariable(contextVariable),
139 new ELiteral(new StringConstant(entry.first))
141 return makeTypeFlexible(environment, new EApply(
142 new EConstant(environment.getValue(VALUE), entry.second),
148 public int hashCode() {
149 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentType.hashCode();
153 public boolean equals(Object obj) {
156 if(obj == null || obj.getClass() != getClass())
158 CompileSCLMonitorRequest other = (CompileSCLMonitorRequest)obj;
159 return literal.equals(other.literal) && componentType.equals(other.componentType);