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.RuntimeEnvironmentRequest2;
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;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 public class CompileSCLMonitorRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
38 private static Logger LOGGER = LoggerFactory.getLogger(CompileSCLMonitorRequest.class);
40 protected static Name BROWSE = Name.create("Simantics/Variables", "browse");
41 protected static Name VALUE = Name.create("Simantics/Variables", "value");
43 private final Resource componentType;
44 private final Resource literal;
45 private final Resource relation;
47 public static class CompilationContext extends AbstractExpressionCompilationContext {
48 public final ComponentTypeSubstructure substructure;
50 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
51 ComponentTypeSubstructure substructure) {
52 super(runtimeEnvironment);
53 this.substructure = substructure;
57 private CompileSCLMonitorRequest(Resource componentType, Resource literal, Resource relation) {
58 this.componentType = componentType;
59 this.literal = literal;
60 this.relation = relation;
63 public CompileSCLMonitorRequest(ReadGraph graph, Variable context)
64 throws DatabaseException {
65 this(context.getParent(graph).getType(graph),
66 context.getRepresents(graph),
67 context.getPredicateResource(graph));
70 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
71 SCLContext sclContext = SCLContext.getCurrent();
72 Object oldGraph = sclContext.get("graph");
74 Function1<Variable,Object> exp = graph.syncRequest(new CompileSCLMonitorRequest(graph, context),
75 TransientCacheListener.<Function1<Variable,Object>>instance());
76 sclContext.put("graph", graph);
77 return exp.apply(context.getParent(graph));
78 } catch (DatabaseException e) {
79 throw (DatabaseException)e;
80 } catch (Throwable t) {
81 throw new DatabaseException(t);
83 sclContext.put("graph", oldGraph);
88 protected String getExpressionText(ReadGraph graph)
89 throws DatabaseException {
90 Layer0 L0 = Layer0.getInstance(graph);
91 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
95 protected Type getExpectedType(ReadGraph graph, CompilationContext context)
96 throws DatabaseException {
97 Layer0 L0 = Layer0.getInstance(graph);
98 String valueType = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
99 if(valueType != null) {
101 return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);
102 } catch (SCLExpressionCompilationException e) {
103 LOGGER.warn("getExpectedType failed for valueType: " + valueType, e);
106 return super.getExpectedType(graph, context);
110 protected CompilationContext getCompilationContext(ReadGraph graph)
111 throws DatabaseException {
112 return graph.syncRequest(new ResourceRead<CompilationContext>(componentType) {
114 public CompilationContext perform(ReadGraph graph)
115 throws DatabaseException {
116 Resource indexRoot = graph.syncRequest(new IndexRoot(resource));
117 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest2(resource, indexRoot));
118 return new CompilationContext(runtimeEnvironment, ComponentTypeSubstructure.forType(graph, resource));
124 protected Type getContextVariableType() {
129 protected Expression getVariableAccessExpression(
131 CompilationContext context,
132 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
133 String name) throws DatabaseException {
134 Pair<String,Type> entry = context.substructure.possibleTypedRVI(name);
137 Environment environment = context.runtimeEnvironment.getEnvironment();
138 Expression propertyVariable = new EApply(
139 new EConstant(environment.getValue(BROWSE)),
140 new EVariable(contextVariable),
141 new ELiteral(new StringConstant(entry.first))
143 return makeTypeFlexible(environment, new EApply(
144 new EConstant(environment.getValue(VALUE), entry.second),
150 public int hashCode() {
151 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentType.hashCode();
155 public boolean equals(Object obj) {
158 if(obj == null || obj.getClass() != getClass())
160 CompileSCLMonitorRequest other = (CompileSCLMonitorRequest)obj;
161 return literal.equals(other.literal) && componentType.equals(other.componentType);