0c81daafd8d3ddfa3b36b5ec327b5ca73f0c8a43
[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 java.util.ArrayList;
4
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.VariableProcedure;
15 import org.simantics.scl.compiler.elaboration.expressions.printing.ExpressionToStringVisitor;
16 import org.simantics.scl.compiler.errors.Locations;
17 import org.simantics.scl.compiler.internal.parsing.Symbol;
18 import org.simantics.scl.compiler.types.Types;
19 import org.simantics.scl.compiler.types.kinds.Kinds;
20
21 import gnu.trove.map.hash.TObjectIntHashMap;
22 import gnu.trove.set.hash.THashSet;
23 import gnu.trove.set.hash.TIntHashSet;
24
25 public class CHRRule extends Symbol {
26     public CHRRuleset parentRuleset;
27     public int priority;
28     public CHRQuery head;
29     public CHRQuery body;
30     public Variable[] existentialVariables;
31     
32     // Analysis
33     //public int firstPriorityExecuted;
34     public int lastPriorityExecuted;
35     
36     // Plans
37     public ArrayList<CHRSearchPlan> plans = new ArrayList<CHRSearchPlan>();
38     
39     // Code generation, move to CHRPriority
40     public String containerClassName;
41     
42     public CHRRule(long location, CHRQuery head, CHRQuery body, Variable[] existentialVariables) {
43         this.location = location;
44         this.head = head;
45         this.body = body;
46         this.existentialVariables = existentialVariables;
47     }
48
49     public void resolve(TranslationContext context) {
50         context.pushExistentialFrame();
51         head.resolve(context);
52         context.disallowNewExistentials();
53         body.resolve(context);
54         existentialVariables = context.popExistentialFrame();
55     }
56
57     public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
58         head.collectRefs(allRefs, refs);
59         body.collectRefs(allRefs, refs);
60     }
61
62     public void checkType(TypingContext context) {
63         for(Variable variable : existentialVariables)
64             variable.setType(Types.metaVar(Kinds.STAR));
65         head.checkType(context);
66         body.checkType(context);
67     }
68     
69     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
70         head.collectVars(allVars, vars);
71         body.collectVars(allVars, vars);
72     }
73
74     public void forVariables(VariableProcedure procedure) {
75         head.forVariables(procedure);
76         body.forVariables(procedure);
77     }
78
79     public void collectFreeVariables(THashSet<Variable> vars) {
80         head.collectFreeVariables(vars);
81         body.collectFreeVariables(vars);
82     }
83
84     public void setLocationDeep(long loc) {
85         if(location == Locations.NO_LOCATION) {
86             this.location = loc;
87             head.setLocationDeep(loc);
88             body.setLocationDeep(loc);
89         }
90     }
91     
92     public void simplify(SimplificationContext context) {
93         head.simplify(context);
94         body.simplify(context);
95     }
96
97     public void compile(CompilationContext compilationContext, CHRConstraint initConstraint) {
98         boolean hasLocalActiveLiteral = false;
99         for(int i=0;i<head.literals.length;++i) {
100             CHRLiteral literal = head.literals[i];
101             if(literal.passive)
102                 continue;
103             CHRConstraint constraint = (CHRConstraint)literal.relation;
104             
105             Variable activeFact = new Variable("activeFact", constraint.factType);
106             QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
107             if(!head.createQueryPlan(context, new EVariable(activeFact), i))
108                 return;
109             body.createEnforcePlan(context, priority);
110             addPlan(new CHRSearchPlan(constraint, activeFact, context.getPlanOps()));
111             
112             if(constraint.parentRuleset == parentRuleset)
113                 hasLocalActiveLiteral = true;
114         }
115         if(!hasLocalActiveLiteral) {
116             Variable activeFact = new Variable("activeFact", initConstraint.factType);
117             QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
118             if(!head.createQueryPlan(context, null, -1))
119                 return;
120             body.createEnforcePlan(context, priority);
121             /*System.out.println(this);
122             for(PlanOp planOp : context.getPlanOps())
123                 System.out.println("    " + planOp);*/
124             addPlan(new CHRSearchPlan(initConstraint, activeFact, context.getPlanOps()));
125         }
126     }
127     
128     private void addPlan(CHRSearchPlan plan) {
129         plans.add(plan);
130     }
131
132     public String toString() {
133         StringBuilder b = new StringBuilder();
134         ExpressionToStringVisitor visitor = new ExpressionToStringVisitor(b);
135         visitor.visit(this);
136         return b.toString();
137     }
138     
139 }