-package org.simantics.modeling.scl;\r
-\r
-import org.simantics.db.Resource;\r
-import org.simantics.scl.compiler.common.names.Name;\r
-import org.simantics.scl.compiler.elaboration.chr.plan.PlanContext;\r
-import org.simantics.scl.compiler.elaboration.expressions.EApply;\r
-import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant;\r
-import org.simantics.scl.compiler.elaboration.expressions.EVariable;\r
-import org.simantics.scl.compiler.elaboration.expressions.Expression;\r
-import org.simantics.scl.compiler.elaboration.expressions.Variable;\r
-import org.simantics.scl.compiler.elaboration.query.compilation.EnforcingContext;\r
-import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext;\r
-import org.simantics.scl.compiler.elaboration.relations.SCLRelation;\r
-import org.simantics.scl.compiler.environment.Environment;\r
-import org.simantics.scl.compiler.errors.Locations;\r
-import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;\r
-import org.simantics.scl.compiler.types.TVar;\r
-import org.simantics.scl.compiler.types.Type;\r
-import org.simantics.scl.compiler.types.Types;\r
-\r
-public class GraphRelation implements SCLRelation {\r
-\r
- Resource relation;\r
- double relationSelectivity;\r
- Resource inverseRelation;\r
- double inverseRelationSelectivity;\r
- \r
- public GraphRelation(Resource relation, double relationSelectivity,\r
- Resource inverseRelation, double inverseRelationSelectivity) {\r
- this.relation = relation;\r
- this.relationSelectivity = relationSelectivity;\r
- this.inverseRelation = inverseRelation;\r
- this.inverseRelationSelectivity = inverseRelationSelectivity;\r
- }\r
-\r
- @Override\r
- public TVar[] getTypeVariables() {\r
- return TVar.EMPTY_ARRAY;\r
- }\r
- \r
- private static final Type[] PARAMETER_TYPES = new Type[] { Types.RESOURCE, Types.RESOURCE };\r
- \r
- @Override\r
- public Type[] getParameterTypes() {\r
- return PARAMETER_TYPES;\r
- }\r
-\r
- @Override\r
- public double getSelectivity(int boundVariables) {\r
- switch(boundVariables) {\r
- case FF: return Double.POSITIVE_INFINITY;\r
- case BF: return relationSelectivity;\r
- case FB: return inverseRelation == null ? Double.POSITIVE_INFINITY : inverseRelationSelectivity;\r
- case BB: return 0.1;\r
- default: throw new IllegalArgumentException();\r
- }\r
- }\r
- \r
- @Override\r
- public int getRequiredVariablesMask() {\r
- return inverseRelation == null ? BF : FF;\r
- }\r
- \r
- private static final Name GET_OBJECTS = Name.create("Simantics/DB", "#");\r
- private static final Name HAS_STATEMENT = Name.create("Simantics/DB", "existsStatement3");\r
- \r
- @Override\r
- public void generate(long location, QueryCompilationContext context,\r
- Type[] typeParameters, Variable[] parameters, int boundVariables) {\r
- switch(boundVariables) {\r
- case BF: \r
- context.iterateList(parameters[1], new EApply(\r
- Locations.NO_LOCATION,\r
- Types.READ_GRAPH,\r
- context.getCompilationContext().getConstant(GET_OBJECTS),\r
- new EVariable(parameters[0]),\r
- new EExternalConstant(relation, Types.RESOURCE)\r
- ));\r
- break;\r
- case FB:\r
- if(inverseRelation == null)\r
- throw new IllegalArgumentException();\r
- context.iterateList(parameters[0], new EApply(\r
- Locations.NO_LOCATION,\r
- Types.READ_GRAPH,\r
- context.getCompilationContext().getConstant(GET_OBJECTS),\r
- new EVariable(parameters[1]),\r
- new EExternalConstant(inverseRelation, Types.RESOURCE)\r
- ));\r
- break;\r
- case BB:\r
- context.condition(\r
- inverseRelation == null || relationSelectivity <= inverseRelationSelectivity\r
- ? new EApply(\r
- Locations.NO_LOCATION,\r
- Types.READ_GRAPH,\r
- context.getCompilationContext().getConstant(HAS_STATEMENT),\r
- new Expression[] {\r
- new EVariable(parameters[0]),\r
- new EExternalConstant(relation, Types.RESOURCE),\r
- new EVariable(parameters[1])\r
- }\r
- )\r
- : new EApply(\r
- Locations.NO_LOCATION,\r
- Types.READ_GRAPH,\r
- context.getCompilationContext().getConstant(HAS_STATEMENT),\r
- new Expression[] {\r
- new EVariable(parameters[1]),\r
- new EExternalConstant(inverseRelation, Types.RESOURCE),\r
- new EVariable(parameters[0])\r
- }\r
- ));\r
- break;\r
- default: throw new IllegalArgumentException();\r
- }\r
- }\r
-\r
- private static final Name CLAIM = Name.create("Simantics/DB", "claim");\r
- \r
- @Override\r
- public Expression generateEnforce(long location, EnforcingContext context,\r
- Type[] typeParameters, Variable[] parameters) {\r
- return new EApply(\r
- Locations.NO_LOCATION,\r
- Types.WRITE_GRAPH,\r
- context.getCompilationContext().getConstant(CLAIM),\r
- new EVariable(parameters[0]),\r
- new EExternalConstant(relation, Types.RESOURCE),\r
- new EVariable(parameters[1])\r
- );\r
- }\r
-\r
- @Override\r
- public int getPhase() {\r
- return 0;\r
- }\r
-\r
- @Override\r
- public void generateIterate(PlanContext context, CodeWriter w, long location, int boundMask, Variable[] variables,\r
- Expression[] expressions) {\r
- Environment env = context.context.environment;\r
- switch(boundMask) {\r
- case BF:\r
- context.iterateList(location, w, variables[1],\r
- w.apply(location,\r
- env.getValue(GET_OBJECTS).getValue(),\r
- expressions[0].toVal(env, w),\r
- w.getModuleWriter().getExternalConstant(relation, Types.RESOURCE)));\r
- break;\r
- case FB:\r
- if(inverseRelation == null)\r
- throw new IllegalArgumentException();\r
- context.iterateList(location, w, variables[0],\r
- w.apply(location,\r
- env.getValue(GET_OBJECTS).getValue(),\r
- expressions[1].toVal(env, w),\r
- w.getModuleWriter().getExternalConstant(inverseRelation, Types.RESOURCE)));\r
- break;\r
- case BB:\r
- context.check(location, w, \r
- inverseRelation == null || relationSelectivity <= inverseRelationSelectivity\r
- ? w.apply(location, env.getValue(HAS_STATEMENT).getValue(), \r
- expressions[0].toVal(env, w),\r
- w.getModuleWriter().getExternalConstant(relation, Types.RESOURCE),\r
- expressions[1].toVal(env, w))\r
- : w.apply(location, env.getValue(HAS_STATEMENT).getValue(), \r
- expressions[1].toVal(env, w),\r
- w.getModuleWriter().getExternalConstant(inverseRelation, Types.RESOURCE),\r
- expressions[0].toVal(env, w)));\r
- break;\r
- default: throw new IllegalArgumentException();\r
- }\r
- }\r
-\r
- @Override\r
- public void generateEnforce(PlanContext context, CodeWriter w, long location, Expression[] parameters) {\r
- Environment env = context.context.environment;\r
- w.apply(location,\r
- env.getValue(CLAIM).getValue(),\r
- parameters[0].toVal(env, w),\r
- w.getModuleWriter().getExternalConstant(relation, Types.RESOURCE),\r
- parameters[1].toVal(env, w));\r
- }\r
-}\r
+package org.simantics.modeling.scl;
+
+import org.simantics.db.Resource;
+import org.simantics.scl.compiler.common.names.Name;
+import org.simantics.scl.compiler.compilation.CompilationContext;
+import org.simantics.scl.compiler.elaboration.chr.plan.PlanContext;
+import org.simantics.scl.compiler.elaboration.expressions.EApply;
+import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant;
+import org.simantics.scl.compiler.elaboration.expressions.EVariable;
+import org.simantics.scl.compiler.elaboration.expressions.Expression;
+import org.simantics.scl.compiler.elaboration.expressions.Variable;
+import org.simantics.scl.compiler.elaboration.query.compilation.EnforcingContext;
+import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext;
+import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
+import org.simantics.scl.compiler.errors.Locations;
+import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
+import org.simantics.scl.compiler.types.TVar;
+import org.simantics.scl.compiler.types.Type;
+import org.simantics.scl.compiler.types.Types;
+
+public class GraphRelation implements SCLRelation {
+
+ Resource relation;
+ double relationSelectivity;
+ Resource inverseRelation;
+ double inverseRelationSelectivity;
+
+ public GraphRelation(Resource relation, double relationSelectivity,
+ Resource inverseRelation, double inverseRelationSelectivity) {
+ this.relation = relation;
+ this.relationSelectivity = relationSelectivity;
+ this.inverseRelation = inverseRelation;
+ this.inverseRelationSelectivity = inverseRelationSelectivity;
+ }
+
+ @Override
+ public TVar[] getTypeVariables() {
+ return TVar.EMPTY_ARRAY;
+ }
+
+ private static final Type[] PARAMETER_TYPES = new Type[] { Types.RESOURCE, Types.RESOURCE };
+
+ @Override
+ public Type[] getParameterTypes() {
+ return PARAMETER_TYPES;
+ }
+
+ @Override
+ public double getSelectivity(int boundVariables) {
+ switch(boundVariables) {
+ case FF: return Double.POSITIVE_INFINITY;
+ case BF: return relationSelectivity;
+ case FB: return inverseRelation == null ? Double.POSITIVE_INFINITY : inverseRelationSelectivity;
+ case BB: return 0.1;
+ default: throw new IllegalArgumentException();
+ }
+ }
+
+ @Override
+ public int getRequiredVariablesMask() {
+ return inverseRelation == null ? BF : FF;
+ }
+
+ private static final Name GET_OBJECTS = Name.create("Simantics/DB", "#");
+ private static final Name HAS_STATEMENT = Name.create("Simantics/DB", "existsStatement3");
+
+ @Override
+ public void generate(long location, QueryCompilationContext context,
+ Type[] typeParameters, Variable[] parameters, int boundVariables) {
+ switch(boundVariables) {
+ case BF:
+ context.iterateList(parameters[1], new EApply(
+ Locations.NO_LOCATION,
+ Types.READ_GRAPH,
+ context.getCompilationContext().getConstant(GET_OBJECTS),
+ new EVariable(parameters[0]),
+ new EExternalConstant(relation, Types.RESOURCE)
+ ));
+ break;
+ case FB:
+ if(inverseRelation == null)
+ throw new IllegalArgumentException();
+ context.iterateList(parameters[0], new EApply(
+ Locations.NO_LOCATION,
+ Types.READ_GRAPH,
+ context.getCompilationContext().getConstant(GET_OBJECTS),
+ new EVariable(parameters[1]),
+ new EExternalConstant(inverseRelation, Types.RESOURCE)
+ ));
+ break;
+ case BB:
+ context.condition(
+ inverseRelation == null || relationSelectivity <= inverseRelationSelectivity
+ ? new EApply(
+ Locations.NO_LOCATION,
+ Types.READ_GRAPH,
+ context.getCompilationContext().getConstant(HAS_STATEMENT),
+ new Expression[] {
+ new EVariable(parameters[0]),
+ new EExternalConstant(relation, Types.RESOURCE),
+ new EVariable(parameters[1])
+ }
+ )
+ : new EApply(
+ Locations.NO_LOCATION,
+ Types.READ_GRAPH,
+ context.getCompilationContext().getConstant(HAS_STATEMENT),
+ new Expression[] {
+ new EVariable(parameters[1]),
+ new EExternalConstant(inverseRelation, Types.RESOURCE),
+ new EVariable(parameters[0])
+ }
+ ));
+ break;
+ default: throw new IllegalArgumentException();
+ }
+ }
+
+ private static final Name CLAIM = Name.create("Simantics/DB", "claim");
+
+ @Override
+ public Expression generateEnforce(long location, EnforcingContext context,
+ Type[] typeParameters, Variable[] parameters) {
+ return new EApply(
+ Locations.NO_LOCATION,
+ Types.WRITE_GRAPH,
+ context.getCompilationContext().getConstant(CLAIM),
+ new EVariable(parameters[0]),
+ new EExternalConstant(relation, Types.RESOURCE),
+ new EVariable(parameters[1])
+ );
+ }
+
+ @Override
+ public int getPhase() {
+ return 0;
+ }
+
+ @Override
+ public void generateIterate(PlanContext context, CodeWriter w, long location, int boundMask, Variable[] variables,
+ Expression[] expressions, Expression[] typeConstraintEvidenceParameters) {
+ CompilationContext compilationContext = context.context;
+ switch(boundMask) {
+ case BF:
+ context.iterateList(location, w, variables[1],
+ w.apply(location,
+ compilationContext.environment.getValue(GET_OBJECTS).getValue(),
+ expressions[0].toVal(compilationContext, w),
+ w.getModuleWriter().getExternalConstant(relation, Types.RESOURCE)));
+ break;
+ case FB:
+ if(inverseRelation == null)
+ throw new IllegalArgumentException();
+ context.iterateList(location, w, variables[0],
+ w.apply(location,
+ compilationContext.environment.getValue(GET_OBJECTS).getValue(),
+ expressions[1].toVal(compilationContext, w),
+ w.getModuleWriter().getExternalConstant(inverseRelation, Types.RESOURCE)));
+ break;
+ case BB:
+ context.check(location, w,
+ inverseRelation == null || relationSelectivity <= inverseRelationSelectivity
+ ? w.apply(location, compilationContext.environment.getValue(HAS_STATEMENT).getValue(),
+ expressions[0].toVal(compilationContext, w),
+ w.getModuleWriter().getExternalConstant(relation, Types.RESOURCE),
+ expressions[1].toVal(compilationContext, w))
+ : w.apply(location, compilationContext.environment.getValue(HAS_STATEMENT).getValue(),
+ expressions[1].toVal(compilationContext, w),
+ w.getModuleWriter().getExternalConstant(inverseRelation, Types.RESOURCE),
+ expressions[0].toVal(compilationContext, w)));
+ break;
+ default: throw new IllegalArgumentException();
+ }
+ }
+
+ @Override
+ public void generateEnforce(PlanContext context, CodeWriter w, long location, Expression[] parameters,
+ Expression[] typeConstraintEvidenceParameters) {
+ CompilationContext compilationContext = context.context;
+ w.apply(location,
+ compilationContext.environment.getValue(CLAIM).getValue(),
+ parameters[0].toVal(compilationContext, w),
+ w.getModuleWriter().getExternalConstant(relation, Types.RESOURCE),
+ parameters[1].toVal(compilationContext, w));
+ }
+}