]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/scl/GraphPropertyRelation.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / scl / GraphPropertyRelation.java
1 package org.simantics.modeling.scl;
2
3 import org.simantics.db.Resource;
4 import org.simantics.scl.compiler.common.names.Name;
5 import org.simantics.scl.compiler.common.names.Names;
6 import org.simantics.scl.compiler.elaboration.chr.plan.PlanContext;
7 import org.simantics.scl.compiler.elaboration.expressions.EApply;
8 import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant;
9 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
10 import org.simantics.scl.compiler.elaboration.expressions.Expression;
11 import org.simantics.scl.compiler.elaboration.expressions.Variable;
12 import org.simantics.scl.compiler.elaboration.query.compilation.EnforcingContext;
13 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext;
14 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
15 import org.simantics.scl.compiler.environment.Environment;
16 import org.simantics.scl.compiler.errors.Locations;
17 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
18 import org.simantics.scl.compiler.types.TVar;
19 import org.simantics.scl.compiler.types.Type;
20 import org.simantics.scl.compiler.types.Types;
21
22 public class GraphPropertyRelation implements SCLRelation {
23
24     Resource propertyRelation;
25     Type valueType;
26     Type[] parameterTypes;
27
28     private static final Type RESOURCE = Types.con("Simantics/DB", "Resource");
29     
30     public GraphPropertyRelation(Resource propertyRelation, Type valueType) {
31         this.propertyRelation = propertyRelation;
32         this.valueType = valueType;
33         this.parameterTypes = new Type[] { RESOURCE, valueType };
34     }
35
36     @Override
37     public TVar[] getTypeVariables() {
38         return TVar.EMPTY_ARRAY;
39     }
40     
41     @Override
42     public Type[] getParameterTypes() {
43         return parameterTypes;
44     }
45
46     @Override
47     public double getSelectivity(int boundVariables) {
48         switch(boundVariables) {
49         case FF:
50         case FB: return Double.POSITIVE_INFINITY;
51         case BF: return 1.0;
52         case BB: return 0.01;
53         default: throw new IllegalArgumentException();
54         }
55     }
56     
57     @Override
58     public int getRequiredVariablesMask() {
59         return BF;
60     }
61     
62     private static final Name POSSIBLE_RELATED_VALUE = Name.create("Simantics/DB", "possibleRelatedValue");
63     
64     @Override
65     public void generate(long location, QueryCompilationContext context,
66             Type[] typeParameters, Variable[] parameters, int boundVariables) {
67         Expression possibleValue = new EApply(
68                 Locations.NO_LOCATION,
69                 Types.READ_GRAPH,
70                 context.getCompilationContext().getConstant(POSSIBLE_RELATED_VALUE, valueType),
71                 context.getEvidence(location, Types.pred(Types.SERIALIZABLE, valueType)),
72                 new EVariable(parameters[0]),
73                 new EExternalConstant(propertyRelation, RESOURCE)
74                 );
75         switch(boundVariables) {
76         case BB: {
77             Variable temp = new Variable("temp", valueType);
78             context.condition(new EApply(
79                     context.getCompilationContext().getConstant(Names.Builtin_equals, valueType),
80                     new Expression[] {
81                         new EVariable(temp),
82                         new EVariable(parameters[1])
83                     }
84                     ));
85             context.iterateMaybe(temp, possibleValue);
86         } break;
87         case BF: context.iterateMaybe(parameters[1], possibleValue);
88         break;
89         default: throw new IllegalArgumentException();
90         }
91     }
92
93     private static final Name CLAIM_RELATED_VALUE = Name.create("Simantics/DB", "claimRelatedValue");
94     
95     @Override
96     public Expression generateEnforce(long location, EnforcingContext context,
97             Type[] typeParameters, Variable[] parameters) {
98         return new EApply(
99                 Locations.NO_LOCATION,
100                 Types.WRITE_GRAPH,
101                 context.getCompilationContext().getConstant(CLAIM_RELATED_VALUE, valueType),
102                 context.getEvidence(location, Types.pred(Types.SERIALIZABLE, valueType)),
103                 new EVariable(parameters[0]),
104                 new EExternalConstant(propertyRelation, RESOURCE),
105                 new EVariable(parameters[1])
106                 );
107     }
108
109     @Override
110     public int getPhase() {
111         return 0;
112     }
113     
114     @Override
115     public void generateEnforce(PlanContext context, CodeWriter w, long location, Expression[] parameters) {
116         throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support enforce.");
117     }
118     
119     @Override
120     public void generateIterate(PlanContext context, CodeWriter w, long location, int boundMask, Variable[] variables,
121             Expression[] expressions) {
122         throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support iterate.");
123     }
124 }