From e12e3ad357853a07b24923b341c4732962a94623 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Thu, 12 Apr 2018 12:51:23 +0300 Subject: [PATCH] Refactoring CHR handling code refs #7866 Change-Id: If2b2a08c740c93d1a4b01830ab0e6de3c014fbec --- .../compiler/elaboration/chr/CHRRuleset.java | 71 ++++++++++++------- .../chr/relations/CHRConstraint.java | 5 +- .../contexts/TranslationContext.java | 4 +- .../elaboration/expressions/EBlock.java | 2 +- .../elaboration/expressions/ECHRRuleset.java | 5 +- .../expressions/ECHRRulesetConstructor.java | 5 +- .../tests/unit/TestCHRCodeGenerator.java | 2 +- 7 files changed, 57 insertions(+), 37 deletions(-) diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRuleset.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRuleset.java index aa59d4c02..47d57dabc 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRuleset.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRuleset.java @@ -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 list = inverseActiveConstraintSourceMap.get(include); - if(list == null) { - list = new ArrayList(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 list = inverseActiveConstraintSourceMap.get(include); + if(list == null) { + list = new ArrayList(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; diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/relations/CHRConstraint.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/relations/CHRConstraint.java index 8b7c122a9..839f0930e 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/relations/CHRConstraint.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/relations/CHRConstraint.java @@ -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); diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TranslationContext.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TranslationContext.java index 535cc8c8b..757aca8b5 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TranslationContext.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TranslationContext.java @@ -401,7 +401,7 @@ public class TranslationContext extends TypeTranslationContext implements Enviro chrConstraintFrames.add(chrConstraintEntries.size()); } - public void popCHRConstraintFrame(ArrayList 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); } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java index c21732557..c911474eb 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBlock.java @@ -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 diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRuleset.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRuleset.java index da37a4a58..54a3e6bba 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRuleset.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRuleset.java @@ -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; } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRulesetConstructor.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRulesetConstructor.java index 431bd850d..dbba5a539 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRulesetConstructor.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRulesetConstructor.java @@ -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; } diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/unit/TestCHRCodeGenerator.java b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/unit/TestCHRCodeGenerator.java index 2ec8ad5d6..9d9582e5c 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/unit/TestCHRCodeGenerator.java +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/unit/TestCHRCodeGenerator.java @@ -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); -- 2.47.1