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.layer0.Layer0;
18 import org.simantics.scl.compiler.elaboration.expressions.EApply;
19 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
20 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
21 import org.simantics.scl.compiler.elaboration.expressions.Expression;
22 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
23 import org.simantics.scl.compiler.environment.Environment;
24 import org.simantics.scl.compiler.environment.Environments;
25 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
26 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
27 import org.simantics.scl.compiler.types.Type;
28 import org.simantics.structural2.scl.AbstractCompileStructuralValueRequest.CompilationContext;
31 * Compiles an SCL expression that is attached to a literal
32 * whose parent is a component that is a part of a component type.
34 * @author Antti Villberg
36 public abstract class AbstractCompileStructuralValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
38 protected final Resource relation;
40 public static class CompilationContext extends AbstractExpressionCompilationContext {
41 public final Map<String, ComponentTypeProperty> propertyMap;
43 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
44 Map<String, ComponentTypeProperty> propertyMap) {
45 super(runtimeEnvironment);
46 this.propertyMap = propertyMap;
50 public AbstractCompileStructuralValueRequest(Resource relation) {
51 this.relation = relation;
55 abstract protected String getExpressionText(ReadGraph graph) throws DatabaseException;
56 abstract protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException;
57 abstract protected Resource getComponentType(ReadGraph graph) throws DatabaseException;
60 protected Type getExpectedType(ReadGraph graph, CompilationContext context)
61 throws DatabaseException {
62 Layer0 L0 = Layer0.getInstance(graph);
63 String valueType = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
64 if(valueType != null) {
65 Resource relationIndexRoot = graph.syncRequest(new PossibleIndexRoot(relation));
66 RuntimeEnvironment relationRuntimeEnvironment = relationIndexRoot != null ? graph.syncRequest(new RuntimeEnvironmentRequest(relationIndexRoot)) : context.runtimeEnvironment;
68 return Environments.getType(relationRuntimeEnvironment.getEnvironment(), valueType);
69 } catch (SCLExpressionCompilationException e) {
73 return super.getExpectedType(graph, context);
79 protected CompilationContext getCompilationContext(ReadGraph graph)
80 throws DatabaseException {
81 Resource indexRoot = getIndexRoot(graph);
82 Resource componentType = getComponentType(graph);
83 if(componentType == null) {
84 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
85 return new CompilationContext(runtimeEnvironment, Collections.<String,ComponentTypeProperty>emptyMap());
87 return graph.syncRequest(new ResourceRead2<CompilationContext>(componentType, indexRoot) {
89 public CompilationContext perform(ReadGraph graph)
90 throws DatabaseException {
91 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest2(resource, resource2));
92 Map<String, ComponentTypeProperty> propertyMap =
93 graph.syncRequest(new ReadComponentTypeInterfaceRequest(resource, runtimeEnvironment.getEnvironment()),
94 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
95 return new CompilationContext(runtimeEnvironment, propertyMap);
102 protected Type getContextVariableType() {
106 protected static Expression accessInputVariable(Environment environment,
107 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
108 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
109 return new EApply(new EConstant(variableParentFunction),
110 new EApply(new EConstant(variableParentFunction),
111 new EVariable(contextVariable)));
115 protected Expression getVariableAccessExpression(
117 CompilationContext context,
118 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
119 String name) throws DatabaseException {
120 ComponentTypeProperty property = context.propertyMap.get(name);
121 if(property != null) {
122 Environment environment = context.runtimeEnvironment.getEnvironment();
123 return getPropertyFlexible(environment,
124 accessInputVariable(environment, contextVariable),
128 else if(name.equals("input")) {
129 Environment environment = context.runtimeEnvironment.getEnvironment();
130 return accessInputVariable(environment, contextVariable);
132 else if(name.equals("self"))
133 return new EVariable(contextVariable);