]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/procedural/CompileProceduralComponentTypeRequest.java
Bumped master version to 1.43.0
[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     @Override
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);
66     }
67
68     @Override
69     protected ProceduralComponentTypeCompilationContext getCompilationContext(
70             ReadGraph graph) throws DatabaseException {
71         return graph.syncRequest(new ProceduralComponentTypeCompilationContextRequest(componentType));
72     }
73
74     @Override
75     protected Type getContextVariableType() {
76         return VARIABLE;
77     }
78
79     @Override
80     protected Expression getVariableAccessExpression(
81             ReadGraph graph,
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();
88             return new EApply(
89                     new EConstant(environment.getValue(FROM_DYNAMIC), property.type),
90                     new EApply(
91                             new EConstant(environment.getValue(PROPERTY_VALUE), Types.DYNAMIC),
92                             new EVariable(contextVariable),
93                             new ELiteral(new StringConstant(name))));
94         }
95         
96         Resource connectionPoint = context.connectionPointMap.get(name);
97         if(connectionPoint != null)
98             return new EExternalConstant(new Interface(connectionPoint), CONNECTION_POINT);
99         
100         else if(name.equals("input"))
101             return new EVariable(contextVariable);
102         else if(name.equals("self"))
103             return new EVariable(contextVariable);
104         else
105             return null;
106     }
107     
108     public static Type getExpectedType() {
109         return EXPECTED_TYPE;
110     }
111     
112     @Override
113     protected boolean parseAsBlock() {
114         return true;
115     }
116     
117     @Override
118     public boolean equals(Object obj) {
119         if(obj == this)
120             return true;
121         if(obj == null || obj.getClass() != getClass())
122             return false;
123         CompileProceduralComponentTypeRequest other = (CompileProceduralComponentTypeRequest)obj;
124         return componentType.equals(other.componentType);
125     }
126     
127     @Override
128     public int hashCode() {
129         return componentType.hashCode();
130     }
131 }