From 26416dd8f81ccfd81fe587c84bb6d2d05ce088a5 Mon Sep 17 00:00:00 2001 From: Riccardo Azzolini Date: Tue, 20 Nov 2018 19:07:34 +0100 Subject: [PATCH] Implement ConstantPattern --- .../rules/dsl/patterns/ConstantPattern.java | 36 +++++++++++++++++++ .../warppi/math/rules/dsl/PatternTest.java | 22 ++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/ConstantPattern.java diff --git a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/ConstantPattern.java b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/ConstantPattern.java new file mode 100644 index 00000000..9967c9ea --- /dev/null +++ b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/ConstantPattern.java @@ -0,0 +1,36 @@ +package it.cavallium.warppi.math.rules.dsl.patterns; + +import it.cavallium.warppi.math.Function; +import it.cavallium.warppi.math.MathContext; +import it.cavallium.warppi.math.functions.Variable; +import it.cavallium.warppi.math.rules.dsl.VisitorPattern; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +/** + * Matches and generates a specific symbolic constant. + */ +public class ConstantPattern extends VisitorPattern { + private final char symbol; + + public ConstantPattern(final char symbol) { + this.symbol = symbol; + } + + @Override + public Optional> visit(final Variable variable) { + if (variable.getType().equals(Variable.V_TYPE.CONSTANT) + && variable.getChar() == symbol) { + return Optional.of(new HashMap<>()); + } else { + return Optional.empty(); + } + } + + @Override + public Function replace(final MathContext mathContext, final Map subFunctions) { + return new Variable(mathContext, symbol, Variable.V_TYPE.CONSTANT); + } +} 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 4eb9c89f..d9680844 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,6 +2,7 @@ package it.cavallium.warppi.math.rules.dsl; import it.cavallium.warppi.math.Function; import it.cavallium.warppi.math.MathContext; +import it.cavallium.warppi.math.MathematicalSymbols; import it.cavallium.warppi.math.functions.*; import it.cavallium.warppi.math.functions.Number; import it.cavallium.warppi.math.functions.equations.Equation; @@ -188,6 +189,27 @@ public class PatternTest { assertEquals(rootSquare, replacement); } + @Test + public void constantPattern() { + final Pattern pattern = new ConstantPattern(MathematicalSymbols.PI); + + final Function shouldNotMatch = new Variable( + mathContext, + MathematicalSymbols.EULER_NUMBER, + Variable.V_TYPE.CONSTANT + ); + assertFalse(pattern.match(shouldNotMatch).isPresent()); + + final Function shouldMatch = new Variable( + mathContext, + MathematicalSymbols.PI, + Variable.V_TYPE.CONSTANT + ); + final Optional> subFunctions = pattern.match(shouldMatch); + assertTrue(subFunctions.isPresent()); + assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get())); + } + @Test public void otherBinaryPatterns() { final Number one = new Number(mathContext, 1);