48aefbdac541459f783c7becb5d013bd488d6a9b
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / chr / CHRRule.java
1 package org.simantics.scl.compiler.elaboration.chr;
2
3 import org.simantics.scl.compiler.compilation.CompilationContext;
4 import org.simantics.scl.compiler.elaboration.chr.plan.PlanOp;
5 import org.simantics.scl.compiler.elaboration.chr.plan.PrioritizedPlan;
6 import org.simantics.scl.compiler.elaboration.chr.planning.QueryPlanningContext;
7 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
8 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
9 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
10 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
11 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
12 import org.simantics.scl.compiler.elaboration.expressions.Variable;
13 import org.simantics.scl.compiler.elaboration.expressions.VariableProcedure;
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;
19
20 import gnu.trove.map.hash.TObjectIntHashMap;
21 import gnu.trove.set.hash.THashSet;
22 import gnu.trove.set.hash.TIntHashSet;
23
24 public class CHRRule extends Symbol {
25     public int priority;
26     public CHRQuery head;
27     public CHRQuery body;
28     public Variable[] existentialVariables;
29     
30     // Analysis
31     public int firstPriorityExecuted;
32     public int lastPriorityExecuted;
33     
34     public CHRRule(long location, CHRQuery head, CHRQuery body, Variable[] existentialVariables) {
35         this.location = location;
36         this.head = head;
37         this.body = body;
38         this.existentialVariables = existentialVariables;
39     }
40
41     public void resolve(TranslationContext context) {
42         context.pushExistentialFrame();
43         head.resolve(context);
44         body.resolve(context);
45         existentialVariables = context.popExistentialFrame();
46     }
47
48     public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
49         head.collectRefs(allRefs, refs);
50         body.collectRefs(allRefs, refs);
51     }
52
53     public void checkType(TypingContext context) {
54         for(Variable variable : existentialVariables)
55             variable.setType(Types.metaVar(Kinds.STAR));
56         head.checkType(context);
57         body.checkType(context);
58     }
59     
60     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
61         head.collectVars(allVars, vars);
62         body.collectVars(allVars, vars);
63     }
64
65     public void forVariables(VariableProcedure procedure) {
66         head.forVariables(procedure);
67         body.forVariables(procedure);
68     }
69
70     public void collectFreeVariables(THashSet<Variable> vars) {
71         head.collectFreeVariables(vars);
72         body.collectFreeVariables(vars);
73     }
74
75     public void setLocationDeep(long loc) {
76         if(location == Locations.NO_LOCATION) {
77             this.location = loc;
78             head.setLocationDeep(loc);
79             body.setLocationDeep(loc);
80         }
81     }
82     
83     public void simplify(SimplificationContext context) {
84         head.simplify(context);
85         body.simplify(context);
86     }
87
88     public void compile(CompilationContext compilationContext, CHRConstraint initConstraint) {
89         boolean hasActiveLiteral = false;
90         for(int i=0;i<head.literals.length;++i) {
91             CHRLiteral literal = head.literals[i];
92             if(literal.passive)
93                 continue;
94             CHRConstraint constraint = (CHRConstraint)literal.relation;
95             
96             Variable activeFact = new Variable("activeFact", constraint.factType);
97             QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
98             if(!head.createQueryPlan(context, new EVariable(activeFact), i))
99                 return;
100             body.createEnforcePlan(context, priority);
101             constraint.plans.add(new PrioritizedPlan(priority, activeFact, context.getPlanOps()));
102             
103             hasActiveLiteral = true;
104         }
105         if(!hasActiveLiteral) {
106             Variable activeFact = new Variable("activeFact", initConstraint.factType);
107             QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
108             if(!head.createQueryPlan(context, null, -1))
109                 return;
110             body.createEnforcePlan(context, priority);
111             /*System.out.println(this);
112             for(PlanOp planOp : context.getPlanOps())
113                 System.out.println("    " + planOp);*/
114             initConstraint.plans.add(new PrioritizedPlan(priority, activeFact, context.getPlanOps()));
115         }
116     }
117     
118     public String toString() {
119         StringBuilder b = new StringBuilder();
120         ExpressionToStringVisitor visitor = new ExpressionToStringVisitor(b);
121         visitor.visit(this);
122         return b.toString();
123     }
124     
125 }