From 53e2416426846fa1e57007a0029da90186411294 Mon Sep 17 00:00:00 2001 From: Riccardo Azzolini Date: Tue, 16 Oct 2018 18:46:49 +0200 Subject: [PATCH] Implement EquationPattern --- .../rules/dsl/patterns/EquationPattern.java | 39 +++++++++++++++++++ .../warppi/math/rules/dsl/PatternTest.java | 5 +++ 2 files changed, 44 insertions(+) create mode 100644 core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/EquationPattern.java diff --git a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/EquationPattern.java b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/EquationPattern.java new file mode 100644 index 00000000..21350831 --- /dev/null +++ b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/EquationPattern.java @@ -0,0 +1,39 @@ +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.Sum; +import it.cavallium.warppi.math.functions.equations.Equation; +import it.cavallium.warppi.math.rules.dsl.Pattern; +import it.cavallium.warppi.math.rules.dsl.PatternUtils; +import it.cavallium.warppi.math.rules.dsl.VisitorPattern; + +import java.util.Map; +import java.util.Optional; + +/** + * Matches and generates an equation of two other patterns. + */ +public class EquationPattern extends VisitorPattern { + private final Pattern left; + private final Pattern right; + + public EquationPattern(final Pattern left, final Pattern right) { + this.left = left; + this.right = right; + } + + @Override + public Optional> visit(final Equation equation) { + return PatternUtils.matchFunctionOperatorParameters(equation, left, right); + } + + @Override + public Function replace(final MathContext mathContext, final Map subFunctions) { + return new Equation( + mathContext, + left.replace(mathContext, subFunctions), + right.replace(mathContext, 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 a8f76b19..c2d81c5f 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 @@ -4,6 +4,7 @@ import it.cavallium.warppi.math.Function; import it.cavallium.warppi.math.MathContext; import it.cavallium.warppi.math.functions.*; import it.cavallium.warppi.math.functions.Number; +import it.cavallium.warppi.math.functions.equations.Equation; import it.cavallium.warppi.math.functions.trigonometry.*; import it.cavallium.warppi.math.rules.dsl.patterns.*; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -142,6 +143,10 @@ public class PatternTest { new DivisionPattern(x, y), new Division(mathContext, one, two) ), + new ImmutablePair<>( + new EquationPattern(x, y), + new Equation(mathContext, one, two) + ), new ImmutablePair<>( new LogarithmPattern(x, y), new Logarithm(mathContext, one, two)