8bba6697d9bb3832782ee20ab4dd5af493219ab1
[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.chr.relations.SpecialCHRRelation;
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.Expression;
12 import org.simantics.scl.compiler.elaboration.expressions.Variable;
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 import org.simantics.scl.compiler.types.Type;
17
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
20 import gnu.trove.set.hash.TIntHashSet;
21
22 public class CHRQuery extends Symbol {
23     public CHRLiteral[] literals;
24
25     public CHRQuery(CHRLiteral[] literals) {
26         this.literals = literals;
27     }
28
29     public void resolve(TranslationContext context) {
30         for(CHRLiteral literal : literals)
31             literal.resolve(context);
32     }
33
34     public void checkType(TypingContext context) {
35         for(CHRLiteral literal : literals)
36             literal.checkType(context);
37     }
38     
39     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
40         for(CHRLiteral literal : literals)
41             literal.collectVars(allVars, vars);
42     }
43
44     public void collectFreeVariables(THashSet<Variable> vars) {
45         for(CHRLiteral literal : literals)
46             literal.collectFreeVariables(vars);
47         for(CHRLiteral literal : literals)
48             if(literal.relation == SpecialCHRRelation.ASSIGN)
49                 literal.parameters[0].removeFreeVariables(vars);
50     }
51
52     public void setLocationDeep(long loc) {
53         if(location == Locations.NO_LOCATION) {
54             this.location = loc;
55             for(CHRLiteral literal : literals)
56                 literal.setLocationDeep(loc);
57         }
58     }
59     
60     public boolean createQueryPlan(QueryPlanningContext context, Expression inputFact, int activeLiteralId, CHRConstraint initConstraint) {
61         for(int i=0;i<literals.length;++i) {
62             CHRLiteral literal = literals[i];
63             if(i == activeLiteralId)
64                 context.activate(literal, inputFact, i);
65             else
66                 context.add(literal, i);
67         }
68         if(activeLiteralId == -1 && inputFact != null) {
69             context.addInitFact(initConstraint, inputFact);
70         }       
71         return context.createQueryPlan();
72     }
73     
74     public void simplify(SimplificationContext context) {
75         for(CHRLiteral literal : literals)
76             literal.simplify(context);
77     }
78
79     public void createEnforcePlan(QueryPlanningContext context, int priority) {
80         context.addPlanOp(new PreCommitOp(location));
81         for(CHRLiteral literal : literals)
82             context.claim(context, literal);
83         context.addPlanOp(new PostCommitOp(location, priority));
84     }
85     
86     public String toString() {
87         StringBuilder b = new StringBuilder();
88         ExpressionToStringVisitor visitor = new ExpressionToStringVisitor(b);
89         visitor.visit(this);
90         return b.toString();
91     }
92
93     public void collectQueryEffects(THashSet<Type> effects) {
94         for(CHRLiteral literal : literals)
95             literal.collectQueryEffects(effects);
96     }
97     
98     public void collectEnforceEffects(THashSet<Type> effects) {
99         for(CHRLiteral literal : literals)
100             literal.collectEnforceEffects(effects);
101     }
102 }