]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/procedural/CompileProceduralComponentTypeRequest.java
compile method in CompileProceduralComponentTypeRequest
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / scl / procedural / CompileProceduralComponentTypeRequest.java
1 package org.simantics.structural2.scl.procedural;
2
3 import java.util.List;
4
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;
29
30 public class CompileProceduralComponentTypeRequest extends AbstractExpressionCompilationRequest<ProceduralComponentTypeCompilationContext, Variable> {
31
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");
35     
36     private final Resource componentType;
37     
38     public CompileProceduralComponentTypeRequest(Resource componentType) {
39         this.componentType = componentType;
40     }
41     
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");
46         try {
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);
56         } finally {
57             sclContext.put("graph", oldGraph);
58         }
59     }
60
61     public static Function1<Variable, Object> compile(ReadGraph graph, Resource componentType) throws DatabaseException {
62         return graph.syncRequest(new CompileProceduralComponentTypeRequest(componentType), TransientCacheListener.instance());
63     }
64     
65     @Override
66     protected String getExpressionText(ReadGraph graph)
67             throws DatabaseException {
68         StructuralResource2 STR = StructuralResource2.getInstance(graph);
69         return graph.getRelatedValue(componentType, STR.ProceduralComponentType_code, Bindings.STRING);
70     }
71
72     @Override
73     protected ProceduralComponentTypeCompilationContext getCompilationContext(
74             ReadGraph graph) throws DatabaseException {
75         return graph.syncRequest(new ProceduralComponentTypeCompilationContextRequest(componentType));
76     }
77
78     @Override
79     protected Type getContextVariableType() {
80         return VARIABLE;
81     }
82
83     @Override
84     protected Expression getVariableAccessExpression(
85             ReadGraph graph,
86             ProceduralComponentTypeCompilationContext context,
87             org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
88             String name) throws DatabaseException {
89         ComponentTypeProperty property = context.propertyMap.get(name);
90         if(property != null) {
91             Environment environment = context.runtimeEnvironment.getEnvironment();
92             return new EApply(
93                     new EConstant(environment.getValue(FROM_DYNAMIC), property.type),
94                     new EApply(
95                             new EConstant(environment.getValue(PROPERTY_VALUE), Types.DYNAMIC),
96                             new EVariable(contextVariable),
97                             new ELiteral(new StringConstant(name))));
98         }
99         
100         Resource connectionPoint = context.connectionPointMap.get(name);
101         if(connectionPoint != null)
102             return new EExternalConstant(new Interface(connectionPoint), CONNECTION_POINT);
103         
104         else if(name.equals("input"))
105             return new EVariable(contextVariable);
106         else if(name.equals("self"))
107             return new EVariable(contextVariable);
108         else
109             return null;
110     }
111     
112     public static Type getExpectedType() {
113         return EXPECTED_TYPE;
114     }
115     
116     @Override
117     protected boolean parseAsBlock() {
118         return true;
119     }
120     
121     @Override
122     public boolean equals(Object obj) {
123         if(obj == this)
124             return true;
125         if(obj == null || obj.getClass() != getClass())
126             return false;
127         CompileProceduralComponentTypeRequest other = (CompileProceduralComponentTypeRequest)obj;
128         return componentType.equals(other.componentType);
129     }
130     
131     @Override
132     public int hashCode() {
133         return componentType.hashCode();
134     }
135 }