import org.simantics.scl.compiler.elaboration.chr.plan.PostCommitOp;
import org.simantics.scl.compiler.elaboration.chr.plan.PreCommitOp;
import org.simantics.scl.compiler.elaboration.chr.planning.QueryPlanningContext;
+import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
}
}
- public boolean createQueryPlan(QueryPlanningContext context, Expression inputFact, int activeLiteralId) {
+ public boolean createQueryPlan(QueryPlanningContext context, Expression inputFact, int activeLiteralId, CHRConstraint initConstraint) {
for(int i=0;i<literals.length;++i) {
CHRLiteral literal = literals[i];
if(i == activeLiteralId)
else
context.add(literal, i);
}
+ if(activeLiteralId == -1) {
+ context.addInitFact(initConstraint, inputFact);
+ }
return context.createQueryPlan();
}
Variable activeFact = new Variable("activeFact", constraint.factType);
QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
- if(!head.createQueryPlan(context, new EVariable(activeFact), i))
+ if(!head.createQueryPlan(context, new EVariable(activeFact), i, initConstraint))
return;
body.createEnforcePlan(context, priority);
addPlan(new CHRSearchPlan(constraint, activeFact, context.getPlanOps()));
if(!hasLocalActiveLiteral) {
Variable activeFact = new Variable("activeFact", initConstraint.factType);
QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
- if(!head.createQueryPlan(context, null, -1))
+ if(!head.createQueryPlan(context, new EVariable(activeFact), -1, initConstraint))
return;
body.createEnforcePlan(context, priority);
/*System.out.println(this);
methodWriter.return_(BooleanConstant.TRUE);
}
}
- if(!includes.isEmpty()) {
+ if(!includes.isEmpty() || extensible) {
{
CodeWriter methodWriter = object.createMethod(w.getModuleWriter(), TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {Types.CHRContext});
initializer = methodWriter.getFunction();
new JavaMethod(true, runtimeRulesetClassName, "register", Types.PROC, Types.UNIT, new Type[] {runtimeRulesetType, Types.CHRContext, include.ruleset.runtimeRulesetType}),
object.getTarget(), methodWriter.getParameters()[0], include.storeVar);
}
+ if(extensible)
+ methodWriter.apply(Locations.NO_LOCATION,
+ new JavaMethod(true, runtimeRulesetClassName, "register", Types.PROC, Types.UNIT, new Type[] {runtimeRulesetType, Types.CHRContext}),
+ object.getTarget(), methodWriter.getParameters()[0]);
methodWriter.return_(NoRepConstant.UNIT);
}
{
new JavaMethod(true, runtimeRulesetClassName, "unregister", Types.PROC, Types.UNIT, new Type[] {runtimeRulesetType, Types.CHRContext, include.ruleset.runtimeRulesetType}),
object.getTarget(), methodWriter.getParameters()[0], include.storeVar);
}
+ if(extensible)
+ methodWriter.apply(Locations.NO_LOCATION,
+ new JavaMethod(true, runtimeRulesetClassName, "unregister", Types.PROC, Types.UNIT, new Type[] {runtimeRulesetType, Types.CHRContext}),
+ object.getTarget(), methodWriter.getParameters()[0]);
methodWriter.return_(NoRepConstant.UNIT);
}
}
import org.simantics.scl.compiler.elaboration.expressions.EVariable;
import org.simantics.scl.compiler.elaboration.expressions.Expression;
import org.simantics.scl.compiler.elaboration.expressions.Variable;
+import org.simantics.scl.compiler.errors.Locations;
import gnu.trove.impl.Constants;
import gnu.trove.map.hash.TObjectIntHashMap;
for(int i=0;i<literal.parameters.length;++i)
addOneSidedEquals(literal.parameters[i].location, new EVariable(variables[i]), literal.parameters[i], secondaryPriority);
}
+
+ public void addInitFact(CHRConstraint initConstraint, Expression inputFact) {
+ planOps.add(new AccessFactOp(Locations.NO_LOCATION, inputFact, initConstraint, Variable.EMPTY_ARRAY, false));
+ }
public void claim(QueryPlanningContext context, CHRLiteral literal) {
if(literal.relation instanceof CHRConstraint) {
for(CHRConstraint constraint : ruleset.constraints)
if(constraint.nextContainerFieldName != null)
storeClassBuilder.addField(Opcodes.ACC_PUBLIC, constraint.nextContainerFieldName, CHRPriorityFactContainer);
- if(ruleset.extensible)
- storeClassBuilder.addField(Opcodes.ACC_PUBLIC, "currentId", FACT_ID_TYPE);
+ //if(ruleset.extensible)
+ // storeClassBuilder.addField(Opcodes.ACC_PUBLIC, "currentId", FACT_ID_TYPE);
// Constructors
}
// update context id
+ mb.loadLocal(importedStore);
mb.loadLocal(contextVar);
+ mb.invokeVirtual("org/simantics/scl/runtime/chr/CHRRuntimeRuleset", "register", TypeDesc.VOID, new TypeDesc[] {CHRContext});
+ /*mb.loadLocal(contextVar);
mb.loadLocal(contextVar);
mb.loadField(CHRContext_name, "currentId", FACT_ID_TYPE);
mb.loadLocal(importedStore);
mb.loadField(include.ruleset.runtimeRulesetClassName, "currentId", FACT_ID_TYPE);
mb.invokeStatic("java/lang/Math", "max", FACT_ID_TYPE, new TypeDesc[] {FACT_ID_TYPE, FACT_ID_TYPE});
- mb.storeField(CHRContext_name, "currentId", FACT_ID_TYPE);
+ mb.storeField(CHRContext_name, "currentId", FACT_ID_TYPE);*/
mb.returnVoid();
mb.finish();
// store context id
mb.loadLocal(importedStore);
mb.loadLocal(contextVar);
+ mb.invokeVirtual("org/simantics/scl/runtime/chr/CHRRuntimeRuleset", "unregister", TypeDesc.VOID, new TypeDesc[] {CHRContext});
+ /*mb.loadLocal(importedStore);
+ mb.loadLocal(contextVar);
mb.loadField(CHRContext_name, "currentId", FACT_ID_TYPE);
- mb.storeField(include.ruleset.runtimeRulesetClassName, "currentId", FACT_ID_TYPE);
+ mb.storeField(include.ruleset.runtimeRulesetClassName, "currentId", FACT_ID_TYPE);*/
mb.returnVoid();
mb.finish();
package org.simantics.scl.runtime.chr;
public class CHRRuntimeRuleset {
-
+ int currentId;
+
+ public void register(CHRContext context) {
+ if(context.currentId < currentId)
+ context.currentId = currentId;
+ }
+
+ public void unregister(CHRContext context) {
+ if(context.currentId > currentId)
+ currentId = context.currentId;
+ }
}
*/
//@Test public void Bug6989() { test(); }
-
- @Test public void CHR11() { test(); }
}
@Test public void CHR8() { test(); }
@Test public void CHR9() { test(); }
@Test public void CHR10() { test(); }
+ @Test public void CHR11() { test(); }
@Test public void ClosureRecursion() { test(); }
@Test public void Collaz() { test(); }
@Test public void Compose() { test(); }