From 00119bc6c509134e985c11ad4f6aff37801adc3f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Fri, 2 Jun 2017 19:29:13 +0300 Subject: [PATCH] (refs #7250) CHR bugfixes, cleaning up unit tests Change-Id: If0f255ce74938029ab91975a2db7a09c517473aa --- .../scl/compiler/elaboration/chr/CHRRule.java | 9 +++---- .../compiler/elaboration/chr/CHRRuleset.java | 23 +++++++++++------- .../elaboration/chr/plan/PlanContext.java | 2 +- .../elaboration/chr/plan/PostCommitOp.java | 2 +- .../elaboration/expressions/EBlock.java | 2 +- .../options/ModuleCompilationOptions.java | 8 ++++++- .../ModuleCompilationOptionsAdvisor.java | 1 + .../compiler/source/TextualModuleSource.java | 7 ++++-- .../simantics/scl/runtime/chr/CHRContext.java | 6 ++++- .../reporting/WriterSCLReportingHandler.java | 22 +++++++++++++++++ .../scl/compiler/tests/TestBase.java | 22 +++++++++++++---- .../simantics/scl/compiler/tests/scl/CHR1.scl | 5 ++++ .../simantics/scl/compiler/tests/scl/CHR3.scl | 5 ++-- .../simantics/scl/compiler/tests/scl/CHR5.scl | 8 ++++++- .../simantics/scl/compiler/tests/scl/CHR6.scl | 6 ++++- .../scl/compiler/tests/scl/Formula.scl | 4 ++++ .../compiler/tests/scl/Transformation2.scl | 24 ++++++++++++++++++- .../compiler/tests/scl/Transformation3.scl | 23 +++++++++++++++++- .../compiler/tests/scl/Transformation4.scl | 14 ++++++++++- .../compiler/tests/scl/Transformation7.scl | 3 ++- .../tests/scl/TransformationOrder.scl | 5 +++- 21 files changed, 168 insertions(+), 33 deletions(-) create mode 100644 bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/reporting/WriterSCLReportingHandler.java diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRule.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRule.java index b30021c1c..20dda072c 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRule.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRRule.java @@ -2,7 +2,6 @@ package org.simantics.scl.compiler.elaboration.chr; import java.util.ArrayList; -import org.junit.runners.ParentRunner; import org.simantics.scl.compiler.compilation.CompilationContext; import org.simantics.scl.compiler.elaboration.chr.plan.CHRSearchPlan; import org.simantics.scl.compiler.elaboration.chr.planning.QueryPlanningContext; @@ -24,6 +23,7 @@ import gnu.trove.set.hash.THashSet; import gnu.trove.set.hash.TIntHashSet; public class CHRRule extends Symbol { + public CHRRuleset parentRuleset; public int priority; public CHRQuery head; public CHRQuery body; @@ -94,7 +94,7 @@ public class CHRRule extends Symbol { } public void compile(CompilationContext compilationContext, CHRConstraint initConstraint) { - boolean hasActiveLiteral = false; + boolean hasLocalActiveLiteral = false; for(int i=0;i(compiler.getModule()); else { - LOGGER.error("While compiling " + getModuleName() + ":"); - LOGGER.error(CompilationErrorFormatter.toString(getSourceReader(null), compiler.getErrorLog().getErrors())); + if(!options.silent) + LOGGER.error("While compiling " + getModuleName() + ":\n " + + CompilationErrorFormatter.toString(getSourceReader(null), compiler.getErrorLog().getErrors()).replaceAll("\n", "\n ")); return new Failure(compiler.getErrorLog().getErrors()); } } catch (IOException e) { + if(!options.silent) + LOGGER.error("Compilation of module " + moduleName + " failed.", e); return new Failure(e); } } diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/chr/CHRContext.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/chr/CHRContext.java index 59fe191f7..b79cf6d7d 100644 --- a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/chr/CHRContext.java +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/chr/CHRContext.java @@ -2,7 +2,7 @@ package org.simantics.scl.runtime.chr; public class CHRContext { public CHRPriority topPriority; - public int currentId = 1; + public int currentId = 0; public void activate(int maxPriority) { //System.out.println("--- ACTIVATE " + maxPriority + "---------------------------------------------"); @@ -14,4 +14,8 @@ public class CHRContext { } //System.out.println("--- FINISHED " + maxPriority + "---------------------------------------------"); } + + public int generateId() { + return currentId++; + } } diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/reporting/WriterSCLReportingHandler.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/reporting/WriterSCLReportingHandler.java new file mode 100644 index 000000000..3cc3f4b58 --- /dev/null +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/reporting/WriterSCLReportingHandler.java @@ -0,0 +1,22 @@ +package org.simantics.scl.runtime.reporting; + +import java.io.IOException; +import java.io.Writer; + +public class WriterSCLReportingHandler extends AbstractSCLReportingHandler { + private final Writer writer; + + public WriterSCLReportingHandler(Writer writer) { + this.writer = writer; + } + + @Override + public void print(String text) { + try { + writer.write(text); + writer.write('\n'); + } catch(IOException e) { + e.printStackTrace(); + } + } +} diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/TestBase.java b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/TestBase.java index 2dfaafee3..fa90f524c 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/TestBase.java +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/TestBase.java @@ -2,6 +2,7 @@ package org.simantics.scl.compiler.tests; import java.io.IOException; import java.io.InputStream; +import java.io.StringWriter; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; @@ -12,12 +13,16 @@ import org.simantics.scl.compiler.errors.Failable; import org.simantics.scl.compiler.errors.Failure; import org.simantics.scl.compiler.module.ImportDeclaration; import org.simantics.scl.compiler.module.Module; +import org.simantics.scl.compiler.module.options.ModuleCompilationOptions; import org.simantics.scl.compiler.module.repository.ModuleRepository; import org.simantics.scl.compiler.module.repository.UpdateListener; import org.simantics.scl.compiler.source.ModuleSource; import org.simantics.scl.compiler.source.StringModuleSource; import org.simantics.scl.compiler.source.repository.MapModuleSourceRepository; import org.simantics.scl.compiler.top.ValueNotFound; +import org.simantics.scl.runtime.SCLContext; +import org.simantics.scl.runtime.reporting.SCLReportingHandler; +import org.simantics.scl.runtime.reporting.WriterSCLReportingHandler; public class TestBase { @@ -93,16 +98,25 @@ public class TestBase { return ImportDeclaration.ONLY_BUILTINS; } }; - ModuleRepository testEnvironment = new ModuleRepository( + ModuleRepository testRepository = new ModuleRepository( PRELUDE_MODULE_REPOSITORY, new MapModuleSourceRepository(moduleSources)); + testRepository.setAdvisor(moduleName -> ModuleCompilationOptions.SILENT); int lastId = moduleNames.length-1; - Failable result = testEnvironment.getModule(moduleNames[lastId]); + Failable result = testRepository.getModule(moduleNames[lastId]); if(!result.didSucceed()) return ((Failure)result).toString(moduleTexts[lastId]); else { - Object main = testEnvironment.getRuntimeModule(moduleNames[lastId]).getResult().getValue("main"); - return String.valueOf(main); + SCLContext context = SCLContext.getCurrent(); + StringWriter writer = new StringWriter(); + Object oldReportingHandler = context.put(SCLReportingHandler.REPORTING_HANDLER, new WriterSCLReportingHandler(writer)); + try { + Object main = testRepository.getRuntimeModule(moduleNames[lastId]).getResult().getValue("main"); + writer.write(String.valueOf(main)); + return writer.toString(); + } finally { + context.put(SCLReportingHandler.REPORTING_HANDLER, oldReportingHandler); + } } } diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR1.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR1.scl index d305a2a83..c1e3402af 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR1.scl +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR1.scl @@ -140,6 +140,11 @@ main = sort (MList.freeze answer) Edge ?x ?y => MList.add answer (?x, ?y) -- +Remove loop (5,5) +Remove dangling edge (3,5) +Remove node 5 +Simplify path (1,2,3) +Simplify path (1,3,4) [(1,4), (1,4)] -- import "StandardLibrary" diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR3.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR3.scl index 03df6896f..1d8451439 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR3.scl +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR3.scl @@ -6,6 +6,5 @@ main = () A ?x, not A (?x+1) => A (?x-1) True => A 0 -- -() - - +0 +() \ No newline at end of file diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR5.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR5.scl index 00ca32bec..edefae634 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR5.scl +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR5.scl @@ -31,4 +31,10 @@ main = () addSet set 1 printSet set -- -() +added 1 +added 2 +added 1 +removed duplicate 1 +printing 2 +printing 1 +() \ No newline at end of file diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR6.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR6.scl index c18a13a5a..325af5270 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR6.scl +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR6.scl @@ -20,4 +20,8 @@ main = () True => X 2 True => X 1 -- -() +A 1 +B 1 +A 2 +B 2 +() \ No newline at end of file diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Formula.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Formula.scl index 10eafb061..d19263a53 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Formula.scl +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Formula.scl @@ -128,4 +128,8 @@ main = do x := True print (evalV f) -- +("x" `UntilF` "y") +false +false +("x" `UntilF` "y") () \ No newline at end of file diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation2.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation2.scl index 45ad0658a..84e0c0b4f 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation2.scl +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation2.scl @@ -22,4 +22,26 @@ main = transformation OneShotForward where Fib 0 1 Fib 1 1 -- -() +21 -> 11 +20 -> 11 +19 -> 10 +18 -> 10 +17 -> 9 +16 -> 9 +15 -> 8 +14 -> 8 +13 -> 7 +12 -> 7 +11 -> 6 +10 -> 6 +9 -> 5 +8 -> 5 +7 -> 4 +6 -> 4 +5 -> 3 +4 -> 3 +3 -> 2 +2 -> 2 +1 -> 1 +0 -> 1 +() \ No newline at end of file diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation3.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation3.scl index f9b995cee..20c32335c 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation3.scl +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation3.scl @@ -29,4 +29,25 @@ rule PrintIt where main = transformation OneShotForward where -- -() +20 -> 10946 +19 -> 6765 +18 -> 4181 +17 -> 2584 +16 -> 1597 +15 -> 987 +14 -> 610 +13 -> 377 +12 -> 233 +11 -> 144 +10 -> 89 +9 -> 55 +8 -> 34 +7 -> 21 +6 -> 13 +5 -> 8 +4 -> 5 +3 -> 3 +2 -> 2 +1 -> 1 +0 -> 1 +() \ No newline at end of file diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation4.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation4.scl index 5a889e1e0..92789e687 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation4.scl +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation4.scl @@ -24,4 +24,16 @@ rule Seed where main = transformation OneShotForward where -- -() +11 -> 2, 1 +10 -> 1, 2 +9 -> 2, 1 +8 -> 1, 2 +7 -> 2, 1 +6 -> 1, 2 +5 -> 2, 1 +4 -> 1, 2 +3 -> 2, 1 +2 -> 1, 2 +1 -> 2, 1 +0 -> 1, 2 +() \ No newline at end of file diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation7.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation7.scl index cce48f6ed..88cd3d1f0 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation7.scl +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Transformation7.scl @@ -11,4 +11,5 @@ rule DoIt where main = transformation OneShotForward where -- -() +Hello world! +() \ No newline at end of file diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/TransformationOrder.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/TransformationOrder.scl index 36c08c7b7..a8a657d2a 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/TransformationOrder.scl +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/TransformationOrder.scl @@ -14,4 +14,7 @@ rule C where main = transformation OneShotForward where -- -() +A +B +C +() \ No newline at end of file -- 2.43.2