--- /dev/null
+package org.simantics.structural2.scl;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.PossibleIndexRoot;
+import org.simantics.db.common.request.ResourceRead2;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.scl.AbstractExpressionAnalysisRequest;
+import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
+import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
+import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.layer0.Layer0;
+import org.simantics.scl.compiler.elaboration.expressions.EApply;
+import org.simantics.scl.compiler.elaboration.expressions.EConstant;
+import org.simantics.scl.compiler.elaboration.expressions.EVariable;
+import org.simantics.scl.compiler.elaboration.expressions.Expression;
+import org.simantics.scl.compiler.elaboration.modules.SCLValue;
+import org.simantics.scl.compiler.environment.Environment;
+import org.simantics.scl.compiler.environment.Environments;
+import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
+import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
+import org.simantics.scl.compiler.types.Type;
+import org.simantics.scl.runtime.function.Function1;
+import org.simantics.structural2.scl.AbstractAnalyseStructuralValueRequest.AnalysisCompilationContext;
+
+/**
+ * Compiles an SCL expression that is attached to a literal
+ * whose parent is a component that is a part of a component type.
+ *
+ * @author Antti Villberg
+ */
+abstract public class AbstractAnalyseStructuralValueRequest extends AbstractExpressionAnalysisRequest<AnalysisCompilationContext, Variable> {
+
+ protected final Resource relation;
+
+ public static class AnalysisCompilationContext extends AbstractExpressionCompilationContext {
+
+ final private Map<String, ComponentTypeProperty> propertyMap;
+ final private Set<String> consumed = new HashSet<>();
+
+ public AnalysisCompilationContext(RuntimeEnvironment runtimeEnvironment,
+ Map<String, ComponentTypeProperty> propertyMap) {
+ super(runtimeEnvironment);
+ this.propertyMap = propertyMap;
+ }
+
+ public ComponentTypeProperty consume(String name) {
+ consumed.add(name);
+ return propertyMap.get(name);
+ }
+
+ public Set<String> getConsumed() {
+ return consumed;
+ }
+
+ }
+
+ public AbstractAnalyseStructuralValueRequest(Resource relation) {
+ this.relation = relation;
+ }
+
+ @Override
+ abstract protected String getExpressionText(ReadGraph graph) throws DatabaseException;
+ abstract protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException;
+ abstract protected Resource getComponentType(ReadGraph graph) throws DatabaseException;
+
+ @Override
+ protected Type getExpectedType(ReadGraph graph, AnalysisCompilationContext context)
+ throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ String valueType = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
+ if(valueType != null) {
+ Resource relationIndexRoot = graph.syncRequest(new PossibleIndexRoot(relation));
+ RuntimeEnvironment relationRuntimeEnvironment = relationIndexRoot != null ? graph.syncRequest(new RuntimeEnvironmentRequest(relationIndexRoot)) : context.runtimeEnvironment;
+ try {
+ return Environments.getType(relationRuntimeEnvironment.getEnvironment(), valueType);
+ } catch (SCLExpressionCompilationException e) {
+ e.printStackTrace();
+ }
+ }
+ return super.getExpectedType(graph, context);
+ }
+
+
+
+ @Override
+ protected AnalysisCompilationContext getCompilationContext(ReadGraph graph)
+ throws DatabaseException {
+ Resource indexRoot = getIndexRoot(graph);
+ Resource componentType = getComponentType(graph);
+ if(componentType == null) {
+ RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
+ return new AnalysisCompilationContext(runtimeEnvironment, Collections.<String,ComponentTypeProperty>emptyMap());
+ } else {
+ return graph.syncRequest(new ResourceRead2<AnalysisCompilationContext>(componentType, indexRoot) {
+ @Override
+ public AnalysisCompilationContext perform(ReadGraph graph)
+ throws DatabaseException {
+ RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest2(resource, resource2));
+ Map<String, ComponentTypeProperty> propertyMap =
+ graph.syncRequest(new ReadComponentTypeInterfaceRequest(resource, runtimeEnvironment.getEnvironment()),
+ TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
+ return new AnalysisCompilationContext(runtimeEnvironment, propertyMap);
+ }
+ });
+ }
+ }
+
+ @Override
+ protected Type getContextVariableType() {
+ return VARIABLE;
+ }
+
+ protected static Expression accessInputVariable(Environment environment,
+ org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
+ SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
+ return new EApply(new EConstant(variableParentFunction),
+ new EApply(new EConstant(variableParentFunction),
+ new EVariable(contextVariable)));
+ }
+
+ @Override
+ protected Expression getVariableAccessExpression(
+ ReadGraph graph,
+ AnalysisCompilationContext context,
+ org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
+ String name) throws DatabaseException {
+ ComponentTypeProperty property = context.consume(name);
+ if(property != null) {
+ Environment environment = context.runtimeEnvironment.getEnvironment();
+ return getPropertyFlexible(environment,
+ accessInputVariable(environment, contextVariable),
+ name,
+ property.type);
+ }
+ else if(name.equals("input")) {
+ Environment environment = context.runtimeEnvironment.getEnvironment();
+ return accessInputVariable(environment, contextVariable);
+ }
+ else if(name.equals("self"))
+ return new EVariable(contextVariable);
+ else
+ return null;
+ }
+
+}