1 package org.simantics.modeling.scl;
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;
22 public class GraphPropertyRelation implements SCLRelation {
24 Resource propertyRelation;
26 Type[] parameterTypes;
28 private static final Type RESOURCE = Types.con("Simantics/DB", "Resource");
30 public GraphPropertyRelation(Resource propertyRelation, Type valueType) {
31 this.propertyRelation = propertyRelation;
32 this.valueType = valueType;
33 this.parameterTypes = new Type[] { RESOURCE, valueType };
37 public TVar[] getTypeVariables() {
38 return TVar.EMPTY_ARRAY;
42 public Type[] getParameterTypes() {
43 return parameterTypes;
47 public double getSelectivity(int boundVariables) {
48 switch(boundVariables) {
50 case FB: return Double.POSITIVE_INFINITY;
53 default: throw new IllegalArgumentException();
58 public int getRequiredVariablesMask() {
62 private static final Name POSSIBLE_RELATED_VALUE = Name.create("Simantics/DB", "possibleRelatedValue");
65 public void generate(long location, QueryCompilationContext context,
66 Type[] typeParameters, Variable[] parameters, int boundVariables) {
67 Expression possibleValue = new EApply(
68 Locations.NO_LOCATION,
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)
75 switch(boundVariables) {
77 Variable temp = new Variable("temp", valueType);
78 context.condition(new EApply(
79 context.getCompilationContext().getConstant(Names.Builtin_equals, valueType),
82 new EVariable(parameters[1])
85 context.iterateMaybe(temp, possibleValue);
87 case BF: context.iterateMaybe(parameters[1], possibleValue);
89 default: throw new IllegalArgumentException();
93 private static final Name CLAIM_RELATED_VALUE = Name.create("Simantics/DB", "claimRelatedValue");
96 public Expression generateEnforce(long location, EnforcingContext context,
97 Type[] typeParameters, Variable[] parameters) {
99 Locations.NO_LOCATION,
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])
110 public int getPhase() {
115 public void generateEnforce(PlanContext context, CodeWriter w, long location, Expression[] parameters) {
116 throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support enforce.");
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.");