From 95560e12b70e2aefd7c94c000a6b3cad5afd7563 Mon Sep 17 00:00:00 2001 From: Riccardo Azzolini Date: Sat, 6 Oct 2018 19:14:24 +0200 Subject: [PATCH] Implement UndefinedPattern --- .../rules/dsl/patterns/UndefinedPattern.java | 25 +++++++++++++++++++ .../warppi/math/rules/dsl/PatternTest.java | 13 ++++++++++ 2 files changed, 38 insertions(+) create mode 100644 core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/UndefinedPattern.java diff --git a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/UndefinedPattern.java b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/UndefinedPattern.java new file mode 100644 index 00000000..ae965de3 --- /dev/null +++ b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/patterns/UndefinedPattern.java @@ -0,0 +1,25 @@ +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.Undefined; +import it.cavallium.warppi.math.rules.dsl.VisitorPattern; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +/** + * Matches and generates Undefined. + */ +public class UndefinedPattern extends VisitorPattern { + @Override + public Optional> visit(Undefined undefined) { + return Optional.of(new HashMap<>()); + } + + @Override + public Function replace(MathContext mathContext, Map subFunctions) { + return new Undefined(mathContext); + } +} 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 481e4bd2..a8f76b19 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 @@ -115,6 +115,19 @@ public class PatternTest { assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get())); } + @Test + public void undefinedPattern() { + final Pattern pattern = new UndefinedPattern(); + + final Function shouldNotMatch = new Number(mathContext, 0); + assertFalse(pattern.match(shouldNotMatch).isPresent()); + + final Function shouldMatch = new Undefined(mathContext); + final Optional> subFunctions = pattern.match(shouldMatch); + assertTrue(subFunctions.isPresent()); + assertTrue(pattern.replace(mathContext, subFunctions.get()) instanceof Undefined); + } + @Test public void otherBinaryPatterns() { final Number one = new Number(mathContext, 1);