]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/relations/CHRConstraint.java
(refs #7250) Refactoring CHR implementation
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / chr / relations / CHRConstraint.java
index 868d70a6f1b51faa2765fc14961d87c1007cd5e5..8612fd766ade4410ad9b1706f887e2504e29273b 100644 (file)
@@ -16,9 +16,8 @@ import org.simantics.scl.compiler.constants.generic.ParameterStackItem;
 import org.simantics.scl.compiler.constants.generic.StackItem;
 import org.simantics.scl.compiler.elaboration.chr.CHRRelation;
 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
-import org.simantics.scl.compiler.elaboration.chr.plan.PrioritizedPlan;
-import org.simantics.scl.compiler.internal.codegen.chr.CHRCodeGenerator;
-import org.simantics.scl.compiler.internal.codegen.chr.CHRFactCodeGenerator;
+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.IVal;
 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
 import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor;
@@ -56,12 +55,12 @@ public class CHRConstraint extends Symbol implements CHRRelation {
     public Constant[] accessors;
     public Constant addProcedure;
     public Constant removeProcedure;
-    public Constant isAlive;
     
     public TIntObjectHashMap<IndexInfo> indices;
     
     // Query plans
-    public ArrayList<PrioritizedPlan> plans = new ArrayList<PrioritizedPlan>();
+    public int minimumPriority = Integer.MAX_VALUE;
+    public int nextPriority = Integer.MAX_VALUE; // used in code generation
     
     public static class IndexInfo {
         public final int indexMask;
@@ -87,8 +86,8 @@ public class CHRConstraint extends Symbol implements CHRRelation {
         JavaTypeTranslator jtt = context.javaTypeTranslator;
         
         this.parentRuleset = parentRuleset;
-        this.factClassName = parentRuleset.storeClassName + "$" + name;
-        TCon factTypeConstructor = Types.con(parentRuleset.storeType.module, parentRuleset.storeType.name + "$" + name); 
+        this.factClassName = parentRuleset.runtimeRulesetName + "$" + 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);
         
@@ -109,18 +108,18 @@ public class CHRConstraint extends Symbol implements CHRRelation {
                 null);
         //this.constructor = new JavaConstructor(factClassName, Types.PROC, factType, constructorTypes);
         this.accessId = new CallJava(TVar.EMPTY_ARRAY, Types.NO_EFFECTS, Types.INTEGER, new Type[] {factType},
-                null, new FieldRef(factClassName, "id", CHRCodeGenerator.FACT_ID_TYPE), null);
+                null, new FieldRef(CHRCodeGenerationConstants.CHRFact_name, "id", CHRRuntimeRulesetCodeGenerator.FACT_ID_TYPE), null);
         this.accessors = new Constant[parameterTypes.length];
         for(int i=0;i<parameterTypes.length;++i) {
             TypeDesc typeDesc = jtt.toTypeDesc(parameterTypes[i]);
             if(typeDesc.equals(TypeDesc.VOID))
                 continue;
             this.accessors[i] = new CallJava(TVar.EMPTY_ARRAY, Types.NO_EFFECTS, parameterTypes[i], new Type[] {factType},
-                    null, new FieldRef(factClassName, CHRFactCodeGenerator.fieldName(i), jtt.toTypeDesc(parameterTypes[i])), null);
+                    null, new FieldRef(factClassName, CHRCodeGenerationConstants.fieldName(i), jtt.toTypeDesc(parameterTypes[i])), null);
         }
-        this.addProcedure = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {parentRuleset.storeType, factType},
-                new StackItem[] {new ParameterStackItem(1, factType), new ParameterStackItem(0, parentRuleset.storeType)},
-                new ObjectMethodRef(false, factClassName, "add", TypeDesc.VOID, new TypeDesc[] {parentRuleset.storeTypeDesc}),
+        this.addProcedure = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {parentRuleset.runtimeRulesetType, Types.CHRContext, factType},
+                new StackItem[] {new ParameterStackItem(2, factType), new ParameterStackItem(0, parentRuleset.runtimeRulesetType), new ParameterStackItem(1, Types.CHRContext)},
+                new ObjectMethodRef(false, factClassName, "add", TypeDesc.VOID, new TypeDesc[] {parentRuleset.runtimeRulesetTypeDesc, CHRCodeGenerationConstants.CHRContext}),
                 null);
         
         this.indices = new TIntObjectHashMap<IndexInfo>(Math.min(10, 1 << parameterTypes.length));
@@ -154,19 +153,19 @@ public class CHRConstraint extends Symbol implements CHRRelation {
 
     private IndexInfo createIndexInfo(CompilationContext context, int indexMask) {
         ArrayList<Type> keyTypeList = new ArrayList<Type>(parameterTypes.length+1);
-        keyTypeList.add(parentRuleset.storeType);
+        keyTypeList.add(parentRuleset.runtimeRulesetType);
         for(int i=0;i<parameterTypes.length;++i)
             if(((indexMask>>i)&1)==1)
                 keyTypeList.add(parameterTypes[i]);
         String indexName = nameOfIndex(indexMask, parameterTypes.length);
         Constant accessIndex;
         if(indexMask == 0) {
-            accessIndex = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, factType, new Type[] {parentRuleset.storeType},
-                    null, new FieldRef(parentRuleset.storeClassName, name + "$" + indexName, factTypeDesc), null);
+            accessIndex = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, factType, new Type[] {parentRuleset.runtimeRulesetType},
+                    null, new FieldRef(parentRuleset.runtimeRulesetName, name + "$" + indexName, factTypeDesc), null);
         }
         else {
             Type[] keyTypes = keyTypeList.toArray(new Type[keyTypeList.size()]);
-            accessIndex = new JavaMethod(true, parentRuleset.storeClassName, name + "$" + indexName, Types.PROC, factType, keyTypes);
+            accessIndex = new JavaMethod(true, parentRuleset.runtimeRulesetName, name + "$" + indexName, Types.PROC, factType, keyTypes);
         }
         return new IndexInfo(
                 indexMask,
@@ -205,22 +204,13 @@ public class CHRConstraint extends Symbol implements CHRRelation {
 
     public void setMayBeRemoved() {
         if(removeProcedure == null) {
-            removeProcedure = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {parentRuleset.storeType, factType},
-                    new StackItem[] {new ParameterStackItem(1, factType), new ParameterStackItem(0, parentRuleset.storeType)},
-                    new ObjectMethodRef(false, factClassName, "remove", TypeDesc.VOID, new TypeDesc[] {parentRuleset.storeTypeDesc}),
+            removeProcedure = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {parentRuleset.runtimeRulesetType, factType},
+                    new StackItem[] {new ParameterStackItem(1, factType), new ParameterStackItem(0, parentRuleset.runtimeRulesetType)},
+                    new ObjectMethodRef(false, factClassName, "remove", TypeDesc.VOID, new TypeDesc[] {parentRuleset.runtimeRulesetTypeDesc}),
                     null);
-            isAlive = new JavaMethod(true, factClassName, "isAlive", Types.PROC, Types.BOOLEAN, factType);
         }
     }
 
-    public int getMinimumPriority() {
-        return plans.get(0).priority;
-    }
-    
-    public boolean isPassive() {
-        return plans.isEmpty();
-    }
-
     public TPred[] getTypeConstraints() {
         return TPred.EMPTY_ARRAY;
     }