(refs #7365) Fixed the bug in the test CHR11.scl
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / chr / CHRQuery.java
1 package org.simantics.scl.compiler.elaboration.chr;
2
3 import org.simantics.scl.compiler.elaboration.chr.plan.PostCommitOp;
4 import org.simantics.scl.compiler.elaboration.chr.plan.PreCommitOp;
5 import org.simantics.scl.compiler.elaboration.chr.planning.QueryPlanningContext;
6 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
7 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
8 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
9 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
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.expressions.VariableProcedure;
13 import org.simantics.scl.compiler.elaboration.expressions.printing.ExpressionToStringVisitor;
14 import org.simantics.scl.compiler.errors.Locations;
15 import org.simantics.scl.compiler.internal.parsing.Symbol;
16
17 import gnu.trove.map.hash.TObjectIntHashMap;
18 import gnu.trove.set.hash.THashSet;
19 import gnu.trove.set.hash.TIntHashSet;
20
21 public class CHRQuery extends Symbol {
22     public CHRLiteral[] literals;
23
24     public CHRQuery(CHRLiteral[] literals) {
25         this.literals = literals;
26     }
27
28     public void resolve(TranslationContext context) {
29         for(CHRLiteral literal : literals)
30             literal.resolve(context);
31     }
32
33     public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
34         for(CHRLiteral literal : literals)
35             literal.collectRefs(allRefs, refs);
36     }
37
38     public void checkType(TypingContext context) {
39         for(CHRLiteral literal : literals)
40             literal.checkType(context);
41     }
42     
43     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
44         for(CHRLiteral literal : literals)
45             literal.collectVars(allVars, vars);
46     }
47
48     public void forVariables(VariableProcedure procedure) {
49         for(CHRLiteral literal : literals)
50             literal.forVariables(procedure);
51     }
52
53     public void collectFreeVariables(THashSet<Variable> vars) {
54         for(CHRLiteral literal : literals)
55             literal.collectFreeVariables(vars);
56     }
57
58     public void setLocationDeep(long loc) {
59         if(location == Locations.NO_LOCATION) {
60             this.location = loc;
61             for(CHRLiteral literal : literals)
62                 literal.setLocationDeep(loc);
63         }
64     }
65     
66     public boolean createQueryPlan(QueryPlanningContext context, Expression inputFact, int activeLiteralId, CHRConstraint initConstraint) {
67         for(int i=0;i<literals.length;++i) {
68             CHRLiteral literal = literals[i];
69             if(i == activeLiteralId)
70                 context.activate(literal, inputFact, i);
71             else
72                 context.add(literal, i);
73         }
74         if(activeLiteralId == -1) {
75             context.addInitFact(initConstraint, inputFact);
76         }       
77         return context.createQueryPlan();
78     }
79     
80     public void simplify(SimplificationContext context) {
81         for(CHRLiteral literal : literals)
82             literal.simplify(context);
83     }
84
85     public void createEnforcePlan(QueryPlanningContext context, int priority) {
86         context.addPlanOp(new PreCommitOp(location));
87         for(CHRLiteral literal : literals)
88             context.claim(context, literal);
89         context.addPlanOp(new PostCommitOp(location, priority));
90     }
91     
92     public String toString() {
93         StringBuilder b = new StringBuilder();
94         ExpressionToStringVisitor visitor = new ExpressionToStringVisitor(b);
95         visitor.visit(this);
96         return b.toString();
97     }
98 }