]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Refactoring CHR handling code 12/1712/1
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Thu, 12 Apr 2018 09:51:23 +0000 (12:51 +0300)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Thu, 12 Apr 2018 09:51:23 +0000 (12:51 +0300)
refs #7866

Change-Id: If2b2a08c740c93d1a4b01830ab0e6de3c014fbec

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/relations/CHRConstraint.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TranslationContext.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRuleset.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRulesetConstructor.java
tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/unit/TestCHRCodeGenerator.java

index aa59d4c0239159b604d396be91710fb66d69cfda..47d57dabca1efb45e9fc42e8648c7244a2603a64 100644 (file)
@@ -3,6 +3,7 @@ package org.simantics.scl.compiler.elaboration.chr;
 import java.util.ArrayList;
 
 import org.cojen.classfile.TypeDesc;
+import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
 import org.simantics.scl.compiler.compilation.CompilationContext;
 import org.simantics.scl.compiler.constants.BooleanConstant;
 import org.simantics.scl.compiler.constants.Constant;
@@ -84,7 +85,12 @@ public class CHRRuleset extends Symbol {
     
     public CHRRuleset() {
         initConstraint = new CHRConstraint(Locations.NO_LOCATION, INIT_CONSTRAINT, Type.EMPTY_ARRAY);
-        constraints.add(initConstraint);
+        addConstraint(initConstraint);
+    }
+    
+    public void addConstraint(CHRConstraint constraint) {
+        constraints.add(constraint);
+        constraint.setParent(this);
     }
     
     public void resolve(TranslationContext context) {
@@ -162,33 +168,37 @@ public class CHRRuleset extends Symbol {
         }
     }
 
-    public void compile(SimplificationContext context) {
-        initializeCodeGeneration(context.getCompilationContext());
-        if(extensible)
-            applyExtensibleDefaults();
-        UsageAnalysis.analyzeUsage(this);
-        for(CHRRule rule : rules) {
-            rule.compile(context.getCompilationContext(), initConstraint);
-            for(CHRSearchPlan plan : rule.plans) {
-                CHRConstraint constraint = plan.constraint;
-                if(!activeConstraintGGInfo.containsKey(constraint)) {
-                    activeConstraintGGInfo.put(constraint, new CHRConstraintGGInfo(rule.priority));
-                    IncludeStatement include = constraintSourceMap.get(constraint);
-                    if(include != null) {
-                        ArrayList<CHRConstraint> list = inverseActiveConstraintSourceMap.get(include);
-                        if(list == null) {
-                            list = new ArrayList<CHRConstraint>(4);
-                            inverseActiveConstraintSourceMap.put(include, list);
+    private void compile(SimplificationContext context) {
+        try {
+            initializeCodeGeneration(context.getCompilationContext());
+            if(extensible)
+                applyExtensibleDefaults();
+            UsageAnalysis.analyzeUsage(this);
+            for(CHRRule rule : rules) {
+                rule.compile(context.getCompilationContext(), initConstraint);
+                for(CHRSearchPlan plan : rule.plans) {
+                    CHRConstraint constraint = plan.constraint;
+                    if(!activeConstraintGGInfo.containsKey(constraint)) {
+                        activeConstraintGGInfo.put(constraint, new CHRConstraintGGInfo(rule.priority));
+                        IncludeStatement include = constraintSourceMap.get(constraint);
+                        if(include != null) {
+                            ArrayList<CHRConstraint> list = inverseActiveConstraintSourceMap.get(include);
+                            if(list == null) {
+                                list = new ArrayList<CHRConstraint>(4);
+                                inverseActiveConstraintSourceMap.put(include, list);
+                            }
+                            list.add(constraint);
                         }
-                        list.add(constraint);
                     }
                 }
             }
-        }
-        // remove init constraint if it is not useful
-        if(getMinimumPriority(initConstraint) == Integer.MAX_VALUE) {
-            constraints.remove(0);
-            initConstraint = null;
+            // remove init constraint if it is not useful
+            if(getMinimumPriority(initConstraint) == Integer.MAX_VALUE) {
+                constraints.remove(0);
+                initConstraint = null;
+            }
+        } catch(Exception e) {
+            throw InternalCompilerError.injectLocation(location, e);
         }
     }
 
@@ -206,13 +216,22 @@ public class CHRRuleset extends Symbol {
         }
     }
 
-    public void simplify(SimplificationContext context) {
-        for(IncludeStatement include : includes)
+    private void simplify(SimplificationContext context) {
+        for(IncludeStatement include : includes) {
+            //include.ruleset.simplifyAndCompileIfNeeded(context);
             include.value = include.value.simplify(context);
+        }
         for(CHRRule rule : rules)
             rule.simplify(context);
     }
     
+    public void simplifyAndCompileIfNeeded(SimplificationContext context) {
+        if(runtimeRulesetTypeDesc == null) {
+            simplify(context);
+            compile(context);
+        }
+    }
+    
     public void setRulesetType(TCon type, String className) {
         this.runtimeRulesetType = type;
         this.runtimeRulesetClassName = className;
index 8b7c122a98458b1a8fa5fbac7e745ae36edb72cf..839f0930e23cc276d1538a57e25217555739b130 100644 (file)
@@ -81,11 +81,14 @@ public class CHRConstraint extends Symbol implements CHRRelation {
         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.runtimeRulesetClassName + "$" + name;
         TCon factTypeConstructor = Types.con(parentRuleset.runtimeRulesetType.module, parentRuleset.runtimeRulesetType.name + "$" + name); 
         this.factType = Types.apply(factTypeConstructor, TVar.EMPTY_ARRAY);
index 535cc8c8b6cc1f17311945523447bc2f01c2da3b..757aca8b5e272880e7c6526048f1b20837709f82 100644 (file)
@@ -401,7 +401,7 @@ public class TranslationContext extends TypeTranslationContext implements Enviro
         chrConstraintFrames.add(chrConstraintEntries.size());
     }
     
-    public void popCHRConstraintFrame(ArrayList<CHRConstraint> constraints) {
+    public void popCHRConstraintFrame(CHRRuleset ruleset) {
         int frame = chrConstraintFrames.removeAt(chrConstraintFrames.size()-1);
         int i = chrConstraintEntries.size();
         while(i > frame) {
@@ -413,7 +413,7 @@ public class TranslationContext extends TypeTranslationContext implements Enviro
             else
                 newConstraint = chrConstraints.put(entry.name, entry.constraint);
             if(newConstraint.implicitlyDeclared)
-                constraints.add(newConstraint);
+                ruleset.addConstraint(newConstraint);
         }
     }
     
index c21732557ba555f5ce950058e0eddaaab468182a..c911474eba8b39c41004b10cff3d7b824b73a8a4 100644 (file)
@@ -107,7 +107,7 @@ public class EBlock extends ASTExpression {
                         chrStatement.body.translate(context, CHRQueryTranslationMode.RULE_BODY)));
             }
             else if(statement instanceof ConstraintStatement)
-                ruleset.constraints.add(CHRTranslation.convertConstraintStatement(context, (ConstraintStatement)statement));
+                ruleset.addConstraint(CHRTranslation.convertConstraintStatement(context, (ConstraintStatement)statement));
             else if(statement instanceof IncludeStatement)
                 ruleset.includes.add((IncludeStatement)statement);
             else
index da37a4a58f228b5cd8c15af94c2fa80b709eb2bb..54a3e6bba3a57a35eb73f46a885d429b295c6725 100644 (file)
@@ -45,7 +45,7 @@ public class ECHRRuleset extends Expression {
         context.pushCHRConstraintFrame();
         ruleset.resolve(context);
         in = in.resolve(context);
-        context.popCHRConstraintFrame(ruleset.constraints);
+        context.popCHRConstraintFrame(ruleset);
         context.popFrame();
         
         context.currentRuleset = null;
@@ -82,8 +82,7 @@ public class ECHRRuleset extends Expression {
     
     @Override
     public Expression simplify(SimplificationContext context) {
-        ruleset.simplify(context);
-        ruleset.compile(context);
+        ruleset.simplifyAndCompileIfNeeded(context);
         in = in.simplify(context);
         return this;
     }
index 431bd850dd4e8225f74d3c13284073a60b9d588a..dbba5a5390d93a99a5a010ea5baad5bc23bcb627 100644 (file)
@@ -35,7 +35,7 @@ public class ECHRRulesetConstructor extends Expression {
         context.pushFrame();
         context.pushCHRConstraintFrame();
         ruleset.resolve(context);
-        context.popCHRConstraintFrame(ruleset.constraints);
+        context.popCHRConstraintFrame(ruleset);
         context.popFrame();
         return this;
     }
@@ -59,8 +59,7 @@ public class ECHRRulesetConstructor extends Expression {
     
     @Override
     public Expression simplify(SimplificationContext context) {
-        ruleset.simplify(context);
-        ruleset.compile(context);
+        ruleset.simplifyAndCompileIfNeeded(context);
         return this;
     }
     
index 2ec8ad5d6e128960f201e2b7fe65c8b86c19787d..9d9582e5ce064ad092fea63b25ee5d74b4d8b904 100644 (file)
@@ -41,7 +41,7 @@ public class TestCHRCodeGenerator {
             
             CHRRuleset ruleset = new CHRRuleset(); 
             CHRConstraint exampleFact = new CHRConstraint(Locations.NO_LOCATION, "ExampleFact", new Type[] { Types.INTEGER, Types.INTEGER });
-            ruleset.constraints.add(exampleFact);
+            ruleset.addConstraint(exampleFact);
     
             System.out.println("==============================================================================================");
             ruleset.initializeCodeGeneration(compilationContext);