import org.simantics.scl.compiler.types.Types;
import gnu.trove.map.hash.TIntObjectHashMap;
+import gnu.trove.set.hash.THashSet;
public class CHRConstraint extends Symbol implements CHRRelation {
public final String name;
public final Type[] parameterTypes;
+ public String[] fieldNames;
public boolean implicitlyDeclared;
// Analysis
- public int firstPriorityAdded;
+ //public int firstPriorityAdded;
public int lastPriorityAdded;
- public int firstPriorityRemoved;
+ //public int firstPriorityRemoved;
public int lastPriorityRemoved;
// Transient info
public Constant addProcedure;
public Constant removeProcedure;
- public TIntObjectHashMap<IndexInfo> indices;
+ public String nextContainerFieldName;
- // Query plans
- public int minimumPriority = Integer.MAX_VALUE;
- public int nextPriority = Integer.MAX_VALUE; // used in code generation
+ public TIntObjectHashMap<IndexInfo> indices;
public static class IndexInfo {
public final int indexMask;
this.name = name;
this.parameterTypes = parameterTypes;
}
+
+ public void setParent(CHRRuleset parentRuleset) {
+ this.parentRuleset = parentRuleset;
+ }
public void initializeCodeGeneration(CompilationContext context, CHRRuleset parentRuleset) {
JavaTypeTranslator jtt = context.javaTypeTranslator;
- this.parentRuleset = parentRuleset;
- this.factClassName = parentRuleset.runtimeRulesetName + "$" + name;
+ this.factClassName = parentRuleset.runtimeRulesetClassName + "$" + name;
TCon factTypeConstructor = Types.con(parentRuleset.runtimeRulesetType.module, parentRuleset.runtimeRulesetType.name + "$" + name);
this.factType = Types.apply(factTypeConstructor, TVar.EMPTY_ARRAY);
this.factTypeDesc = TypeDesc.forClass(factClassName);
if(context.module != null) // for unit testing
context.module.addTypeDescriptor(factTypeConstructor.name, new StandardTypeConstructor(factTypeConstructor, TVar.EMPTY_ARRAY, factTypeDesc));
+
+ // next container
+ if(parentRuleset.extensible) {
+ nextContainerFieldName = CHRCodeGenerationConstants.nextContainerName(name);
+ }
}
@Override
Constant accessIndex;
if(indexMask == 0) {
accessIndex = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, factType, new Type[] {parentRuleset.runtimeRulesetType},
- null, new FieldRef(parentRuleset.runtimeRulesetName, name + "$" + indexName, factTypeDesc), null);
+ null, new FieldRef(parentRuleset.runtimeRulesetClassName, name + "$" + indexName, factTypeDesc), null);
}
else {
Type[] keyTypes = keyTypeList.toArray(new Type[keyTypeList.size()]);
- accessIndex = new JavaMethod(true, parentRuleset.runtimeRulesetName, name + "$" + indexName, Types.PROC, factType, keyTypes);
+ accessIndex = new JavaMethod(true, parentRuleset.runtimeRulesetClassName, name + "$" + indexName, Types.PROC, factType, keyTypes);
}
return new IndexInfo(
indexMask,
);
}
- public IVal fetchFromIndex(CompilationContext context, int boundMask) {
+ public IndexInfo getOrCreateIndex(CompilationContext context, int boundMask) {
IndexInfo indexInfo = indices.get(boundMask);
if(indexInfo == null) {
indexInfo = createIndexInfo(context, boundMask);
indices.put(boundMask, indexInfo);
}
- return indexInfo.firstFact;
+ return indexInfo;
+ }
+
+ public IVal fetchFromIndex(CompilationContext context, int boundMask) {
+ return getOrCreateIndex(context, boundMask).firstFact;
}
public Constant nextElement(CompilationContext context, int boundMask) {
indexInfo = createIndexInfo(context, boundMask);
indices.put(boundMask, indexInfo);
}
- return indexInfo.nextFact;
+ return getOrCreateIndex(context, boundMask).nextFact;
}
else
return w.apply(location, accessor, fact);
}
+
+ @Override
+ public String[] getFieldNames() {
+ return fieldNames;
+ }
+
+ @Override
+ public void collectEnforceEffects(THashSet<Type> effects) {
+ effects.add(Types.PROC);
+ }
+
+ @Override
+ public void collectQueryEffects(THashSet<Type> effects) {
+ effects.add(Types.PROC);
+ }
}