import org.simantics.scl.compiler.constants.BooleanConstant;
import org.simantics.scl.compiler.constants.Constant;
import org.simantics.scl.compiler.constants.IntegerConstant;
+import org.simantics.scl.compiler.constants.JavaConstructor;
import org.simantics.scl.compiler.constants.JavaMethod;
import org.simantics.scl.compiler.constants.generic.CallJava;
import org.simantics.scl.compiler.constants.generic.MethodRef.FieldRef;
import org.simantics.scl.compiler.constants.generic.MethodRef.SetFieldRef;
import org.simantics.scl.compiler.elaboration.chr.analysis.UsageAnalysis;
+import org.simantics.scl.compiler.elaboration.chr.plan.CHRSearchPlan;
import org.simantics.scl.compiler.elaboration.chr.plan.PlanRealizer;
-import org.simantics.scl.compiler.elaboration.chr.plan.PrioritizedPlan;
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.expressions.Variable;
import org.simantics.scl.compiler.elaboration.expressions.VariableProcedure;
import org.simantics.scl.compiler.errors.Locations;
-import org.simantics.scl.compiler.internal.codegen.chr.CHRCodeGenerator;
+import org.simantics.scl.compiler.internal.codegen.chr.CHRCodeGenerationConstants;
+import org.simantics.scl.compiler.internal.codegen.chr.CHRRuntimeRulesetCodeGenerator;
import org.simantics.scl.compiler.internal.codegen.references.BoundVar;
import org.simantics.scl.compiler.internal.codegen.references.IVal;
import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor;
public CHRConstraint initConstraint;
public int priorityCount;
- public String storeClassName;
- public TCon storeType;
- public BoundVar storeVariable;
- public TypeDesc storeTypeDesc;
- public Constant activateProcedure;
+ public String runtimeRulesetName;
+ public TCon runtimeRulesetType;
+ public BoundVar runtimeRulesetVariable;
+ public TypeDesc runtimeRulesetTypeDesc;
public Constant readCurrentId;
public Constant writeCurrentId;
for(CHRRule rule : rules)
rule.compile(context.getCompilationContext(), initConstraint);
// remove init constraint if it is not useful
- if(initConstraint.plans.isEmpty()) {
+ if(initConstraint.minimumPriority == Integer.MAX_VALUE) {
constraints.remove(0);
initConstraint = null;
}
- for(CHRConstraint constraint : constraints) {
- constraint.plans.sort((PrioritizedPlan a, PrioritizedPlan b) -> {
- return Integer.compare(a.priority, b.priority);
- });
- /*System.out.println(constraint.name);
- for(PrioritizedPlan plan : constraint.plans) {
- System.out.println(" priority " + plan.priority);
- for(PlanOp op : plan.ops)
- System.out.println(" " + op);
- }*/
- }
}
public void simplify(SimplificationContext context) {
cachedContext = context; // FIXME remove
String suffix = context.namingPolicy.getFreshClosureClassNameSuffix();
- storeType = Types.con(context.namingPolicy.getModuleName(), "CHR" + suffix);
- storeClassName = context.namingPolicy.getModuleClassName() + suffix;
- storeTypeDesc = TypeDesc.forClass(storeClassName);
- storeVariable = new BoundVar(storeType);
+ runtimeRulesetType = Types.con(context.namingPolicy.getModuleName(), "CHR" + suffix);
+ runtimeRulesetName = context.namingPolicy.getModuleClassName() + suffix;
+ runtimeRulesetTypeDesc = TypeDesc.forClass(runtimeRulesetName);
+ runtimeRulesetVariable = new BoundVar(runtimeRulesetType);
for(CHRConstraint constraint : constraints)
constraint.initializeCodeGeneration(context, this);
- activateProcedure = new JavaMethod(true, storeClassName, "activate", Types.PROC, Types.UNIT, storeType, Types.INTEGER);
- readCurrentId = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.INTEGER, new Type[] {storeType},
- null, new FieldRef(storeClassName, "currentId", CHRCodeGenerator.FACT_ID_TYPE), null);
- writeCurrentId = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {storeType, Types.INTEGER},
- null, new SetFieldRef(storeClassName, "currentId", CHRCodeGenerator.FACT_ID_TYPE), null);
+ readCurrentId = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.INTEGER, new Type[] {Types.CHRContext},
+ null, new FieldRef(CHRCodeGenerationConstants.CHRContext_name, "currentId", CHRRuntimeRulesetCodeGenerator.FACT_ID_TYPE), null);
+ writeCurrentId = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {Types.CHRContext, Types.INTEGER},
+ null, new SetFieldRef(CHRCodeGenerationConstants.CHRContext_name, "currentId", CHRRuntimeRulesetCodeGenerator.FACT_ID_TYPE), null);
if(context.module != null) // for unit testing
- context.module.addTypeDescriptor(storeType.name, new StandardTypeConstructor(storeType, TVar.EMPTY_ARRAY, storeTypeDesc));
+ context.module.addTypeDescriptor(runtimeRulesetType.name, new StandardTypeConstructor(runtimeRulesetType, TVar.EMPTY_ARRAY, runtimeRulesetTypeDesc));
}
+
+ public static final Constant ACTIVATE = new JavaMethod(true, CHRCodeGenerationConstants.CHRContext_name, "activate", Types.PROC, Types.UNIT, Types.CHRContext, Types.INTEGER);
+ private static final Constant CREATE_CHR_CONTEXT = new JavaConstructor("org/simantics/scl/runtime/chr/CHRContext", Types.PROC, Types.CHRContext);
public void generateCode(CodeWriter w) {
- CHRRulesetObject object = new CHRRulesetObject(storeVariable, this);
+ CHRRulesetObject object = new CHRRulesetObject(runtimeRulesetVariable, this);
w.defineObject(object);
- for(CHRConstraint constraint : constraints) {
- //System.out.println(constraint);
- for(PrioritizedPlan plan : constraint.plans) {
+ for(CHRRule rule : rules) {
+ for(CHRSearchPlan plan : rule.plans) {
/*System.out.println(" plan " + plan.priority);
for(PlanOp planOp : plan.ops)
System.out.println(" " + planOp);*/
- PlanRealizer realizer = new PlanRealizer(cachedContext, this, storeVariable, plan.ops);
- CodeWriter methodWriter = object.createMethod(w.getModuleWriter(), TVar.EMPTY_ARRAY, Types.PROC, Types.BOOLEAN, new Type[] {constraint.factType});
+ CodeWriter methodWriter = object.createMethod(w.getModuleWriter(), TVar.EMPTY_ARRAY, Types.PROC, Types.BOOLEAN, new Type[] {Types.CHRContext, plan.constraint.factType});
plan.implementation = methodWriter.getFunction();
- plan.activeFact.setVal(methodWriter.getParameters()[0]);
+ IVal[] implementationParameters = methodWriter.getParameters();
+ plan.activeFact.setVal(implementationParameters[1]);
+ PlanRealizer realizer = new PlanRealizer(cachedContext, this, runtimeRulesetVariable, implementationParameters[0], plan.ops);
realizer.nextOp(methodWriter);
if(methodWriter.isUnfinished())
methodWriter.return_(BooleanConstant.TRUE);
}
}
if(initConstraint != null) {
+ IVal chrContext = w.apply(location, CREATE_CHR_CONTEXT);
IVal initFact = w.apply(location, initConstraint.constructor, IntegerConstant.ZERO);
- w.apply(location, initConstraint.addProcedure, storeVariable, initFact);
- w.apply(location, activateProcedure, storeVariable, new IntegerConstant(Integer.MAX_VALUE));
+ w.apply(location, initConstraint.addProcedure, runtimeRulesetVariable, chrContext, initFact);
+ w.apply(location, ACTIVATE, chrContext, new IntegerConstant(Integer.MAX_VALUE));
}
}