1 package org.simantics.structural2.scl.procedural;
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
11 import org.simantics.db.layer0.variable.Variable;
12 import org.simantics.scl.compiler.common.names.Name;
13 import org.simantics.scl.compiler.constants.StringConstant;
14 import org.simantics.scl.compiler.elaboration.expressions.EApply;
15 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
16 import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant;
17 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
18 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
19 import org.simantics.scl.compiler.elaboration.expressions.Expression;
20 import org.simantics.scl.compiler.environment.Environment;
21 import org.simantics.scl.compiler.types.Type;
22 import org.simantics.scl.compiler.types.Types;
23 import org.simantics.scl.runtime.SCLContext;
24 import org.simantics.scl.runtime.function.Function1;
25 import org.simantics.structural.stubs.StructuralResource2;
26 import org.simantics.structural2.procedural.Interface;
27 import org.simantics.structural2.procedural.SubstructureElement;
28 import org.simantics.structural2.scl.ComponentTypeProperty;
30 public class CompileProceduralComponentTypeRequest extends AbstractExpressionCompilationRequest<ProceduralComponentTypeCompilationContext, Variable> {
32 private static final Type EXPECTED_TYPE = Types.list(Types.con("Structural/Procedural", "SubstructureElement"));
33 private static Name PROPERTY_VALUE = Name.create("Simantics/Variables", "propertyValue");
34 private static final Type CONNECTION_POINT = Types.con("Structural/Procedural", "ConnectionPoint");
36 private final Resource componentType;
38 public CompileProceduralComponentTypeRequest(Resource componentType) {
39 this.componentType = componentType;
42 @SuppressWarnings("unchecked")
43 public static List<SubstructureElement> compileAndEvaluate(ReadGraph graph, Resource componentType, Variable context) throws DatabaseException {
44 SCLContext sclContext = SCLContext.getCurrent();
45 Object oldGraph = sclContext.get("graph");
47 Function1<Variable,Object> exp = graph.syncRequest(
48 new CompileProceduralComponentTypeRequest(componentType),
49 TransientCacheListener.<Function1<Variable,Object>>instance());
50 sclContext.put("graph", graph);
51 return (List<SubstructureElement>)exp.apply(context);
52 } catch (DatabaseException e) {
53 throw (DatabaseException)e;
54 } catch (Throwable t) {
55 throw new DatabaseException(t);
57 sclContext.put("graph", oldGraph);
62 protected String getExpressionText(ReadGraph graph)
63 throws DatabaseException {
64 StructuralResource2 STR = StructuralResource2.getInstance(graph);
65 return graph.getRelatedValue(componentType, STR.ProceduralComponentType_code, Bindings.STRING);
69 protected ProceduralComponentTypeCompilationContext getCompilationContext(
70 ReadGraph graph) throws DatabaseException {
71 return graph.syncRequest(new ProceduralComponentTypeCompilationContextRequest(componentType));
75 protected Type getContextVariableType() {
80 protected Expression getVariableAccessExpression(
82 ProceduralComponentTypeCompilationContext context,
83 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
84 String name) throws DatabaseException {
85 ComponentTypeProperty property = context.propertyMap.get(name);
86 if(property != null) {
87 Environment environment = context.runtimeEnvironment.getEnvironment();
89 new EConstant(environment.getValue(FROM_DYNAMIC), property.type),
91 new EConstant(environment.getValue(PROPERTY_VALUE), Types.DYNAMIC),
92 new EVariable(contextVariable),
93 new ELiteral(new StringConstant(name))));
96 Resource connectionPoint = context.connectionPointMap.get(name);
97 if(connectionPoint != null)
98 return new EExternalConstant(new Interface(connectionPoint), CONNECTION_POINT);
100 else if(name.equals("input"))
101 return new EVariable(contextVariable);
102 else if(name.equals("self"))
103 return new EVariable(contextVariable);
108 public static Type getExpectedType() {
109 return EXPECTED_TYPE;
113 protected boolean parseAsBlock() {
118 public boolean equals(Object obj) {
121 if(obj == null || obj.getClass() != getClass())
123 CompileProceduralComponentTypeRequest other = (CompileProceduralComponentTypeRequest)obj;
124 return componentType.equals(other.componentType);
128 public int hashCode() {
129 return componentType.hashCode();