1 package org.simantics.scl.compiler.elaboration.chr;
3 import java.util.ArrayList;
5 import org.simantics.scl.compiler.compilation.CompilationContext;
6 import org.simantics.scl.compiler.elaboration.chr.plan.CHRSearchPlan;
7 import org.simantics.scl.compiler.elaboration.chr.planning.QueryPlanningContext;
8 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
9 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
10 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
11 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
12 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
13 import org.simantics.scl.compiler.elaboration.expressions.Variable;
14 import org.simantics.scl.compiler.elaboration.expressions.printing.ExpressionToStringVisitor;
15 import org.simantics.scl.compiler.errors.Locations;
16 import org.simantics.scl.compiler.internal.parsing.Symbol;
17 import org.simantics.scl.compiler.types.Types;
18 import org.simantics.scl.compiler.types.kinds.Kinds;
20 import gnu.trove.map.hash.TObjectIntHashMap;
21 import gnu.trove.set.hash.TIntHashSet;
23 public class CHRRule extends Symbol {
24 public CHRRuleset parentRuleset;
28 public Variable[] existentialVariables;
31 //public int firstPriorityExecuted;
32 public int lastPriorityExecuted;
35 public ArrayList<CHRSearchPlan> plans = new ArrayList<CHRSearchPlan>();
37 // Code generation, move to CHRPriority
38 public String containerClassName;
40 public CHRRule(long location, CHRQuery head, CHRQuery body, Variable[] existentialVariables) {
41 this.location = location;
44 this.existentialVariables = existentialVariables;
47 public CHRRule(long location, CHRQuery head, CHRQuery body) {
48 this(location, head, body, null);
51 public void resolve(TranslationContext context) {
52 context.pushExistentialFrame();
53 head.resolve(context);
54 context.disallowNewExistentials();
55 body.resolve(context);
56 existentialVariables = context.popExistentialFrame();
59 public void checkType(TypingContext context) {
60 for(Variable variable : existentialVariables)
61 variable.setType(Types.metaVar(Kinds.STAR));
62 head.checkType(context);
63 body.checkType(context);
66 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
67 head.collectVars(allVars, vars);
68 body.collectVars(allVars, vars);
71 public void setLocationDeep(long loc) {
72 if(location == Locations.NO_LOCATION) {
74 head.setLocationDeep(loc);
75 body.setLocationDeep(loc);
79 public void simplify(SimplificationContext context) {
80 head.simplify(context);
81 body.simplify(context);
84 public void compile(CompilationContext compilationContext, CHRConstraint initConstraint) {
85 boolean hasLocalActiveLiteral = false;
86 for(int i=0;i<head.literals.length;++i) {
87 CHRLiteral literal = head.literals[i];
90 CHRConstraint constraint = (CHRConstraint)literal.relation;
92 Variable activeFact = new Variable("activeFact", constraint.factType);
93 QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
94 if(!head.createQueryPlan(context, new EVariable(activeFact), i, initConstraint))
96 body.createEnforcePlan(context, priority);
97 addPlan(new CHRSearchPlan(constraint, activeFact, context.getPlanOps()));
99 if(constraint.parentRuleset == parentRuleset)
100 hasLocalActiveLiteral = true;
102 if(!hasLocalActiveLiteral) {
103 Variable activeFact = new Variable("activeFact", initConstraint.factType);
104 QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
105 if(!head.createQueryPlan(context, new EVariable(activeFact), -1, initConstraint))
107 body.createEnforcePlan(context, priority);
108 /*System.out.println(this);
109 for(PlanOp planOp : context.getPlanOps())
110 System.out.println(" " + planOp);*/
111 addPlan(new CHRSearchPlan(initConstraint, activeFact, context.getPlanOps()));
115 private void addPlan(CHRSearchPlan plan) {
119 public String toString() {
120 StringBuilder b = new StringBuilder();
121 ExpressionToStringVisitor visitor = new ExpressionToStringVisitor(b);