From 5450d67497aea8a74db431665174526e1287cb01 Mon Sep 17 00:00:00 2001 From: Riccardo Azzolini Date: Sat, 6 Oct 2018 11:51:33 +0200 Subject: [PATCH] Implement SumPattern --- .../math/rules/dsl/patterns/SumPattern.java | 39 ++++++++++++++ .../warppi/math/rules/dsl/PatternTest.java | 51 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/SumPattern.java diff --git a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/SumPattern.java b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/SumPattern.java new file mode 100644 index 00000000..951bd4ce --- /dev/null +++ b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/SumPattern.java @@ -0,0 +1,39 @@ +package it.cavallium.warppi.math.rules.dsl.patterns; + +import it.cavallium.warppi.math.Function; +import it.cavallium.warppi.math.functions.Sum; +import it.cavallium.warppi.math.rules.dsl.Pattern; +import it.cavallium.warppi.math.rules.dsl.VisitorPattern; + +import java.util.Map; +import java.util.Optional; + +/** + * Matches and generates a sum of two other patterns. + */ +public class SumPattern extends VisitorPattern { + private final Pattern left; + private final Pattern right; + + public SumPattern(Pattern left, Pattern right) { + this.left = left; + this.right = right; + } + + @Override + public Optional> visit(Sum sum) { + return left.match(sum.getParameter1()) + .flatMap(leftMatch -> right.match(sum.getParameter2()) + .flatMap(rightMatch -> mergeMatches(leftMatch, rightMatch)) + ); + } + + @Override + public Function replace(Map subFunctions) { + return new Sum( + null, + left.replace(subFunctions), + right.replace(subFunctions) + ); + } +} diff --git a/core/src/test/java/it/cavallium/warppi/math/rules/dsl/PatternTest.java b/core/src/test/java/it/cavallium/warppi/math/rules/dsl/PatternTest.java index 0f7c1651..448154ca 100644 --- a/core/src/test/java/it/cavallium/warppi/math/rules/dsl/PatternTest.java +++ b/core/src/test/java/it/cavallium/warppi/math/rules/dsl/PatternTest.java @@ -2,8 +2,11 @@ package it.cavallium.warppi.math.rules.dsl; import it.cavallium.warppi.math.Function; import it.cavallium.warppi.math.functions.Number; +import it.cavallium.warppi.math.functions.Subtraction; import it.cavallium.warppi.math.functions.Sum; +import it.cavallium.warppi.math.functions.Variable; import it.cavallium.warppi.math.rules.dsl.patterns.SubFunctionPattern; +import it.cavallium.warppi.math.rules.dsl.patterns.SumPattern; import org.junit.Test; import java.util.Map; @@ -27,4 +30,52 @@ public class PatternTest { assertEquals(func, pattern.replace(subFunctions.get())); } + + @Test + public void sumPattern() { + final Pattern pattern = new SumPattern( + new SubFunctionPattern("x"), + new SubFunctionPattern("y") + ); + + final Function shouldNotMatch = new Subtraction( + null, + new Number(null, 1), + new Number(null, 2) + ); + assertFalse(pattern.match(shouldNotMatch).isPresent()); + + final Function shouldMatch = new Sum( + null, + new Number(null, 1), + new Number(null, 2) + ); + final Optional> subFunctions = pattern.match(shouldMatch); + assertTrue(subFunctions.isPresent()); + assertEquals(shouldMatch, pattern.replace(subFunctions.get())); + } + + @Test + public void repeatedSubFunction() { + final Pattern pattern = new SumPattern( + new SubFunctionPattern("x"), + new SubFunctionPattern("x") + ); + + final Function shouldMatch = new Sum( + null, + new Number(null, 1), + new Number(null, 1) + ); + final Optional> subFunctions = pattern.match(shouldMatch); + assertTrue(subFunctions.isPresent()); + assertEquals(shouldMatch, pattern.replace(subFunctions.get())); + + final Function shouldNotMatch = new Sum( + null, + new Number(null, 1), + new Number(null, 2) + ); + assertFalse(pattern.match(shouldNotMatch).isPresent()); + } } \ No newline at end of file