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;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Compiles an SCL expression that is attached to a literal
34 * whose parent is a component that is a part of a component type.
36 * @author Antti Villberg
38 public abstract class AbstractCompileStructuralValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
40 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractCompileStructuralValueRequest.class);
42 protected final Resource relation;
44 public static class CompilationContext extends AbstractExpressionCompilationContext {
45 public final Map<String, ComponentTypeProperty> propertyMap;
47 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
48 Map<String, ComponentTypeProperty> propertyMap) {
49 super(runtimeEnvironment);
50 this.propertyMap = propertyMap;
54 public AbstractCompileStructuralValueRequest(Resource relation) {
55 this.relation = relation;
59 abstract protected String getExpressionText(ReadGraph graph) throws DatabaseException;
62 * @throws DatabaseException is an index root could not be found
64 abstract protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException;
66 * Does not throw if the graph is valid.
68 * @return null if component type was not availble
71 abstract protected Resource getComponentType(ReadGraph graph) throws DatabaseException;
73 protected Resource getPossibleIndexRoot(ReadGraph graph) throws DatabaseException {
75 return getIndexRoot(graph);
76 } catch (DatabaseException e) {
77 LOGGER.error("Error while resolving index root during structural value compilation", e);
83 protected Type getExpectedType(ReadGraph graph, CompilationContext context)
84 throws DatabaseException {
85 Layer0 L0 = Layer0.getInstance(graph);
86 String valueType = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
87 if(valueType != null) {
88 Resource relationIndexRoot = graph.syncRequest(new PossibleIndexRoot(relation));
89 RuntimeEnvironment relationRuntimeEnvironment = relationIndexRoot != null ? graph.syncRequest(new RuntimeEnvironmentRequest(relationIndexRoot)) : context.runtimeEnvironment;
91 return Environments.getType(relationRuntimeEnvironment.getEnvironment(), valueType);
92 } catch (SCLExpressionCompilationException e) {
96 return super.getExpectedType(graph, context);
102 protected CompilationContext getCompilationContext(ReadGraph graph)
103 throws DatabaseException {
104 Resource indexRoot = getIndexRoot(graph);
105 Resource componentType = getComponentType(graph);
106 if(componentType == null) {
107 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
108 return new CompilationContext(runtimeEnvironment, Collections.<String,ComponentTypeProperty>emptyMap());
110 return graph.syncRequest(new ResourceRead2<CompilationContext>(componentType, indexRoot) {
112 public CompilationContext perform(ReadGraph graph)
113 throws DatabaseException {
114 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest2(resource, resource2));
115 Map<String, ComponentTypeProperty> propertyMap =
116 graph.syncRequest(new ReadComponentTypeInterfaceRequest(resource, runtimeEnvironment.getEnvironment()),
117 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
118 return new CompilationContext(runtimeEnvironment, propertyMap);
125 protected Type getContextVariableType() {
129 protected static Expression accessInputVariable(Environment environment,
130 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
131 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
132 return new EApply(new EConstant(variableParentFunction),
133 new EApply(new EConstant(variableParentFunction),
134 new EVariable(contextVariable)));
138 protected Expression getVariableAccessExpression(
140 CompilationContext context,
141 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
142 String name) throws DatabaseException {
143 ComponentTypeProperty property = context.propertyMap.get(name);
144 if(property != null) {
145 Environment environment = context.runtimeEnvironment.getEnvironment();
146 return getPropertyFlexible(environment,
147 accessInputVariable(environment, contextVariable),
151 else if(name.equals("input")) {
152 Environment environment = context.runtimeEnvironment.getEnvironment();
153 return accessInputVariable(environment, contextVariable);
155 else if(name.equals("self"))
156 return new EVariable(contextVariable);
162 protected String getContextDescription(ReadGraph graph) throws DatabaseException {
163 Resource componentType = getComponentType(graph);
164 if(componentType != null) {
165 String uri = graph.getPossibleURI(componentType);
169 // OK, no component type - report index root then
170 Resource indexRoot = getPossibleIndexRoot(graph);
171 if(indexRoot != null) {
172 String uri = graph.getPossibleURI(componentType);
176 // OK, nothing - report default
177 return super.getContextDescription(graph);