]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/plan/PostCommitOp.java
A utility class for recursive search in cyclic graph
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / chr / plan / PostCommitOp.java
1 package org.simantics.scl.compiler.elaboration.chr.plan;
2
3 import org.simantics.scl.compiler.compilation.CompilationContext;
4 import org.simantics.scl.compiler.constants.IntegerConstant;
5 import org.simantics.scl.compiler.constants.singletons.NullCheck;
6 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
7 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
8 import org.simantics.scl.compiler.internal.codegen.continuations.ICont;
9 import org.simantics.scl.compiler.internal.codegen.references.IVal;
10 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
11
12 public class PostCommitOp extends PlanOp {
13     int priority;
14     
15     public PostCommitOp(long location, int priority) {
16         super(location);
17         this.priority = priority;
18     }
19
20     @Override
21     public void toString(StringBuilder b) {
22         b.append("POST_COMMIT " + priority);
23     }
24
25     @Override
26     public void generateCode(CompilationContext context, PlanContext planContext, CodeWriter w) {
27         if(planContext.currentId != null) {
28             w.apply(location, CHRRuleset.WRITE_CURRENT_ID, planContext.contextVar, planContext.currentId);
29             planContext.currentId = null;
30             w.apply(location, CHRRuleset.ACTIVATE, planContext.contextVar, new IntegerConstant(priority+planContext.ruleset.initialPriorityNumber));
31         }
32         for(PartnerFact activeFact : planContext.partnerFacts) {
33             if(activeFact.killAfterMatch) {
34                 if(activeFact.nextFact == null)
35                     w.jump(location, activeFact.finishCont);
36                 else {
37                     CodeWriter iterateAlive = w.createBlock(activeFact.constraint.factType);
38                     w.jump(location, iterateAlive.getContinuation(), w.apply(location, activeFact.nextFact, activeFact.factVar));
39                     iterateUntilLiveFactFound(iterateAlive, activeFact);
40                 }
41                 break;
42             }
43             else if(activeFact.mayBeRemoved) {
44                 if(activeFact.nextFact == null) {
45                     w.branchAwayUnless(location, activeFact.isAlive(location, w), activeFact.finishCont);
46                 }
47                 else {
48                     CodeWriter failure = w.createBlock();
49                     CodeWriter iterateAlive = w.createBlock(activeFact.constraint.factType);
50                     w.branchAwayUnless(location, activeFact.isAlive(location, w), failure.getContinuation());
51                     failure.jump(location, iterateAlive.getContinuation(), failure.apply(location, activeFact.nextFact, activeFact.factVar));
52                     iterateUntilLiveFactFound(iterateAlive, activeFact);     
53                 }
54             }
55         }
56         // PostCommit does not call nextOp
57     }
58     
59     private void iterateUntilLiveFactFound(CodeWriter w, PartnerFact activeFact) {
60         ICont initialContinuation = w.getContinuation();
61         CHRConstraint constraint = activeFact.constraint;
62         IVal fact = w.getParameters()[0];
63         CodeWriter dead = w.createBlock();
64         w.branchAwayIf(location, w.apply(location, NullCheck.INSTANCE.createSpecialization(constraint.factType), fact), activeFact.finishCont);
65         w.branchAwayUnless(location, activeFact.isAlive(location, w, fact), dead.getContinuation());
66         w.jump(location, activeFact.continueCont, fact);
67         dead.jump(location, initialContinuation, dead.apply(location, activeFact.nextFact, fact));
68     }
69
70 }