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