1 package org.simantics.structural2.scl;
3 import java.util.Collections;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
10 import org.simantics.db.common.request.PossibleIndexRoot;
11 import org.simantics.db.common.request.ResourceRead2;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
14 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
15 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
16 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.layer0.Layer0;
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.EVariable;
22 import org.simantics.scl.compiler.elaboration.expressions.Expression;
23 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
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.structural2.scl.AbstractCompileStructuralValueRequest.CompilationContext;
32 * Compiles an SCL expression that is attached to a literal
33 * whose parent is a component that is a part of a component type.
35 * @author Antti Villberg
37 abstract public class AbstractCompileStructuralValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
39 protected final Resource relation;
41 public static class CompilationContext extends AbstractExpressionCompilationContext {
42 public final Map<String, ComponentTypeProperty> propertyMap;
44 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
45 Map<String, ComponentTypeProperty> propertyMap) {
46 super(runtimeEnvironment);
47 this.propertyMap = propertyMap;
51 public AbstractCompileStructuralValueRequest(Resource relation) {
52 this.relation = relation;
56 abstract protected String getExpressionText(ReadGraph graph) throws DatabaseException;
57 abstract protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException;
58 abstract protected Resource getComponentType(ReadGraph graph) throws DatabaseException;
61 protected Type getExpectedType(ReadGraph graph, CompilationContext context)
62 throws DatabaseException {
63 Layer0 L0 = Layer0.getInstance(graph);
64 String valueType = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
65 if(valueType != null) {
66 Resource relationIndexRoot = graph.syncRequest(new PossibleIndexRoot(relation));
67 RuntimeEnvironment relationRuntimeEnvironment = relationIndexRoot != null ? graph.syncRequest(new RuntimeEnvironmentRequest(relationIndexRoot)) : context.runtimeEnvironment;
69 return Environments.getType(relationRuntimeEnvironment.getEnvironment(), valueType);
70 } catch (SCLExpressionCompilationException e) {
74 return super.getExpectedType(graph, context);
80 protected CompilationContext getCompilationContext(ReadGraph graph)
81 throws DatabaseException {
82 Resource indexRoot = getIndexRoot(graph);
83 Resource componentType = getComponentType(graph);
84 if(componentType == null) {
85 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
86 return new CompilationContext(runtimeEnvironment, Collections.<String,ComponentTypeProperty>emptyMap());
88 return graph.syncRequest(new ResourceRead2<CompilationContext>(componentType, indexRoot) {
90 public CompilationContext perform(ReadGraph graph)
91 throws DatabaseException {
92 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest2(resource, resource2));
93 Map<String, ComponentTypeProperty> propertyMap =
94 graph.syncRequest(new ReadComponentTypeInterfaceRequest(resource, runtimeEnvironment.getEnvironment()),
95 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
96 return new CompilationContext(runtimeEnvironment, propertyMap);
103 protected Type getContextVariableType() {
107 protected static Expression accessInputVariable(Environment environment,
108 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
109 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
110 return new EApply(new EConstant(variableParentFunction),
111 new EApply(new EConstant(variableParentFunction),
112 new EVariable(contextVariable)));
116 protected Expression getVariableAccessExpression(
118 CompilationContext context,
119 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
120 String name) throws DatabaseException {
121 ComponentTypeProperty property = context.propertyMap.get(name);
122 if(property != null) {
123 Environment environment = context.runtimeEnvironment.getEnvironment();
124 return getPropertyFlexible(environment,
125 accessInputVariable(environment, contextVariable),
129 else if(name.equals("input")) {
130 Environment environment = context.runtimeEnvironment.getEnvironment();
131 return accessInputVariable(environment, contextVariable);
133 else if(name.equals("self"))
134 return new EVariable(contextVariable);