1 package org.simantics.scl.compiler.elaboration.chr.plan;
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;
12 public class PostCommitOp extends PlanOp {
15 public PostCommitOp(long location, int priority) {
17 this.priority = priority;
21 public void toString(StringBuilder b) {
22 b.append("POST_COMMIT " + priority);
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));
32 for(PartnerFact activeFact : planContext.partnerFacts) {
33 if(activeFact.killAfterMatch) {
34 if(activeFact.nextFact == null)
35 w.jump(activeFact.finishCont);
37 CodeWriter iterateAlive = w.createBlock(activeFact.constraint.factType);
38 w.jump(iterateAlive.getContinuation(), w.apply(location, activeFact.nextFact, activeFact.factVar));
39 iterateUntilLiveFactFound(iterateAlive, activeFact);
43 else if(activeFact.mayBeRemoved) {
44 if(activeFact.nextFact == null) {
45 w.branchAwayUnless(activeFact.isAlive(location, w), activeFact.finishCont);
48 CodeWriter failure = w.createBlock();
49 CodeWriter iterateAlive = w.createBlock(activeFact.constraint.factType);
50 w.branchAwayUnless(activeFact.isAlive(location, w), failure.getContinuation());
51 failure.jump(iterateAlive.getContinuation(), failure.apply(location, activeFact.nextFact, activeFact.factVar));
52 iterateUntilLiveFactFound(iterateAlive, activeFact);
56 // PostCommit does not call nextOp
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(w.apply(location, NullCheck.INSTANCE.createSpecialization(constraint.factType), fact), activeFact.finishCont);
65 w.branchAwayUnless(activeFact.isAlive(location, w, fact), dead.getContinuation());
66 w.jump(activeFact.continueCont, fact);
67 dead.jump(initialContinuation, dead.apply(location, activeFact.nextFact, fact));