]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/scl/CompileSCLMonitorRequest.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / scl / CompileSCLMonitorRequest.java
1 package org.simantics.modeling.scl;
2
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;
32
33
34 public class CompileSCLMonitorRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
35     
36     protected static Name BROWSE = Name.create("Simantics/Variables", "browse");
37     protected static Name VALUE = Name.create("Simantics/Variables", "value");
38     
39     private final Resource componentType;
40     private final Resource literal;
41     private final Resource relation;
42     
43     public static class CompilationContext extends AbstractExpressionCompilationContext {
44         public final ComponentTypeSubstructure substructure;
45         
46         public CompilationContext(RuntimeEnvironment runtimeEnvironment,
47                 ComponentTypeSubstructure substructure) {
48             super(runtimeEnvironment);
49             this.substructure = substructure;
50         }
51     }
52     
53     private CompileSCLMonitorRequest(Resource componentType, Resource literal, Resource relation) {
54         this.componentType = componentType;
55         this.literal = literal;
56         this.relation = relation;
57     }
58     
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));
64     }
65
66     public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
67         SCLContext sclContext = SCLContext.getCurrent();
68         Object oldGraph = sclContext.get("graph");
69         try {
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) {
75             e.printStackTrace();
76             throw (DatabaseException)e;
77         } catch (Throwable t) {
78             t.printStackTrace();
79             throw new DatabaseException(t);
80         } finally {
81             sclContext.put("graph", oldGraph);
82         }
83     }
84
85     @Override
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);
90     }
91     
92     @Override
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) {
98             try {
99                 return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);
100             } catch (SCLExpressionCompilationException e) {
101                 e.printStackTrace();
102             }
103         }
104         return super.getExpectedType(graph, context);
105     }
106     
107     @Override
108     protected CompilationContext getCompilationContext(ReadGraph graph)
109             throws DatabaseException {
110         return graph.syncRequest(new ResourceRead<CompilationContext>(componentType) {
111             @Override
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));
117             }
118         });
119     }
120
121     @Override
122     protected Type getContextVariableType() {
123         return VARIABLE;
124     }
125
126     @Override
127     protected Expression getVariableAccessExpression(
128             ReadGraph graph,
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);
133         if(entry == null)
134             return null;
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))
140                 );
141         return makeTypeFlexible(environment, new EApply(
142                 new EConstant(environment.getValue(VALUE), entry.second),
143                 propertyVariable
144                 ), entry.second);
145     }
146     
147     @Override
148     public int hashCode() {
149         return 31*(31*getClass().hashCode() + literal.hashCode()) + componentType.hashCode();
150     }
151     
152     @Override
153     public boolean equals(Object obj) {
154         if(this == obj)
155             return true;
156         if(obj == null || obj.getClass() != getClass())
157             return false;
158         CompileSCLMonitorRequest other = (CompileSCLMonitorRequest)obj;
159         return literal.equals(other.literal) && componentType.equals(other.componentType);
160     }
161 }