(refs #7365) Fixed the bug in the test CHR11.scl 16/716/1
authorHannu Niemistö <hannu.niemisto@iki.fi>
Sun, 16 Jul 2017 10:32:05 +0000 (13:32 +0300)
committerHannu Niemistö <hannu.niemisto@iki.fi>
Sun, 16 Jul 2017 10:32:05 +0000 (13:32 +0300)
Change-Id: I961c3ae92013ec4085fe61eee9021a3cc0aff6e4

bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRQuery.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRule.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRuleset.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/planning/QueryPlanningContext.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/chr/CHRRuntimeRulesetCodeGenerator.java
bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/chr/CHRRuntimeRuleset.java
tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ActiveTests.java
tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java

index 9e2bf6f343c026637ff5dfd0db3ae2af6663c512..5cb0f2aee230acb6f79838fd358612c81edc024f 100644 (file)
@@ -3,6 +3,7 @@ package org.simantics.scl.compiler.elaboration.chr;
 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;
@@ -62,7 +63,7 @@ public class CHRQuery extends Symbol {
         }
     }
     
-    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)
@@ -70,6 +71,9 @@ public class CHRQuery extends Symbol {
             else
                 context.add(literal, i);
         }
+        if(activeLiteralId == -1) {
+            context.addInitFact(initConstraint, inputFact);
+        }      
         return context.createQueryPlan();
     }
     
index 0c81daafd8d3ddfa3b36b5ec327b5ca73f0c8a43..f714497c4212c76c1ccef6e217a06c40f2d71a39 100644 (file)
@@ -104,7 +104,7 @@ public class CHRRule extends Symbol {
             
             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()));
@@ -115,7 +115,7 @@ public class CHRRule extends Symbol {
         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);
index d670300cbe1a4253758ee5731a766ddc42cac0aa..63a8344f220d93fdc63296eb05abcc6fe0b267d5 100644 (file)
@@ -283,7 +283,7 @@ public class CHRRuleset extends Symbol {
                     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();
@@ -292,6 +292,10 @@ public class CHRRuleset extends Symbol {
                             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);
             }
             {
@@ -302,6 +306,10 @@ public class CHRRuleset extends Symbol {
                             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);
             }
         }
index 537a2aa9dca73a0495b7eb794d8f7c5abf16bef0..61503458cd541ef4d84d2aabe0b06220b13d0813 100644 (file)
@@ -22,6 +22,7 @@ import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
 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;
@@ -223,6 +224,10 @@ public class QueryPlanningContext {
         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) {
index 9ff56eda4cc45eb27dcf29b00c0f9789816215c8..341ba34a796b18ebdd7dbd0850e7649b1328f60f 100644 (file)
@@ -46,8 +46,8 @@ public class CHRRuntimeRulesetCodeGenerator implements CHRCodeGenerationConstant
             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
 
@@ -106,13 +106,16 @@ public class CHRRuntimeRulesetCodeGenerator implements CHRCodeGenerationConstant
                     }
                 
                 // 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();
@@ -137,8 +140,11 @@ public class CHRRuntimeRulesetCodeGenerator implements CHRCodeGenerationConstant
                 // 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();
index 8f6ca3ee9f08ad4c03dde93c895d7070ef621b22..21f3b65ad69f26fdb620f97ed8e77b590ede3fc1 100644 (file)
@@ -1,5 +1,15 @@
 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;
+       }
 }
index 4b9a3ffd2b48d8a5b20072c508926bfe324c6bfe..c4d1c6f235f40f8758cc6570812b909b450e793a 100644 (file)
@@ -19,6 +19,4 @@ public class ActiveTests extends TestBase {
   */  
     
     //@Test public void Bug6989() { test(); }
-    
-    @Test public void CHR11() { test(); }
 }
index a5975932b2fdc142ab952229c29f93c9ade7c7bf..d8f703afc344d209cd18f4a7373a8a9b3caef69f 100644 (file)
@@ -32,6 +32,7 @@ public class ModuleRegressionTests extends TestBase {
     @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(); }