Use actual MathContext instead of null in Pattern replace

This commit is contained in:
Riccardo Azzolini 2018-10-06 14:48:45 +02:00
parent 0afacf7ddc
commit 21de6349f4
6 changed files with 46 additions and 36 deletions

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.math.rules.dsl; package it.cavallium.warppi.math.rules.dsl;
import it.cavallium.warppi.math.Function; import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.MathContext;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -21,8 +22,10 @@ public interface Pattern {
/** /**
* Creates a new function by filling in sub-functions within this pattern. * Creates a new function by filling in sub-functions within this pattern.
* *
*
* @param mathContext The <code>MathContext</code> used to construct <code>Function</code>s.
* @param subFunctions A map of named sub-functions to be inserted into this pattern. * @param subFunctions A map of named sub-functions to be inserted into this pattern.
* @return The resulting function. * @return The resulting function.
*/ */
Function replace(Map<String, Function> subFunctions); Function replace(MathContext mathContext, Map<String, Function> subFunctions);
} }

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.math.rules.dsl.patterns; package it.cavallium.warppi.math.rules.dsl.patterns;
import it.cavallium.warppi.math.Function; import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.functions.Negative; import it.cavallium.warppi.math.functions.Negative;
import it.cavallium.warppi.math.rules.dsl.Pattern; import it.cavallium.warppi.math.rules.dsl.Pattern;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern; import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
@ -24,10 +25,10 @@ public class NegativePattern extends VisitorPattern {
} }
@Override @Override
public Function replace(Map<String, Function> subFunctions) { public Function replace(MathContext mathContext, Map<String, Function> subFunctions) {
return new Negative( return new Negative(
null, mathContext,
inner.replace(subFunctions) inner.replace(mathContext, subFunctions)
); );
} }
} }

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.math.rules.dsl.patterns; package it.cavallium.warppi.math.rules.dsl.patterns;
import it.cavallium.warppi.math.Function; import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.functions.Number; import it.cavallium.warppi.math.functions.Number;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern; import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
@ -29,7 +30,7 @@ public class NumberPattern extends VisitorPattern {
} }
@Override @Override
public Function replace(Map<String, Function> subFunctions) { public Function replace(MathContext mathContext, Map<String, Function> subFunctions) {
return new Number(null, value); return new Number(mathContext, value);
} }
} }

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.math.rules.dsl.patterns; package it.cavallium.warppi.math.rules.dsl.patterns;
import it.cavallium.warppi.math.Function; import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.rules.dsl.Pattern; import it.cavallium.warppi.math.rules.dsl.Pattern;
import java.util.HashMap; import java.util.HashMap;
@ -25,7 +26,7 @@ public class SubFunctionPattern implements Pattern {
} }
@Override @Override
public Function replace(Map<String, Function> subFunctions) { public Function replace(MathContext mathContext, Map<String, Function> subFunctions) {
return subFunctions.get(name); return subFunctions.get(name);
} }
} }

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.math.rules.dsl.patterns; package it.cavallium.warppi.math.rules.dsl.patterns;
import it.cavallium.warppi.math.Function; 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.Sum;
import it.cavallium.warppi.math.rules.dsl.Pattern; import it.cavallium.warppi.math.rules.dsl.Pattern;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern; import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
@ -29,11 +30,11 @@ public class SumPattern extends VisitorPattern {
} }
@Override @Override
public Function replace(Map<String, Function> subFunctions) { public Function replace(MathContext mathContext, Map<String, Function> subFunctions) {
return new Sum( return new Sum(
null, mathContext,
left.replace(subFunctions), left.replace(mathContext, subFunctions),
right.replace(subFunctions) right.replace(mathContext, subFunctions)
); );
} }
} }

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.math.rules.dsl; package it.cavallium.warppi.math.rules.dsl;
import it.cavallium.warppi.math.Function; import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.functions.Negative; import it.cavallium.warppi.math.functions.Negative;
import it.cavallium.warppi.math.functions.Number; import it.cavallium.warppi.math.functions.Number;
import it.cavallium.warppi.math.functions.Subtraction; import it.cavallium.warppi.math.functions.Subtraction;
@ -18,20 +19,22 @@ import java.util.Optional;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class PatternTest { public class PatternTest {
private final MathContext mathContext = new MathContext();
@Test @Test
public void subFunctionPattern() { public void subFunctionPattern() {
final Pattern pattern = new SubFunctionPattern("x"); final Pattern pattern = new SubFunctionPattern("x");
final Function func = new Sum( final Function func = new Sum(
null, mathContext,
new Number(null, 1), new Number(mathContext, 1),
new Number(null, 2) new Number(mathContext, 2)
); );
final Optional<Map<String, Function>> subFunctions = pattern.match(func); final Optional<Map<String, Function>> subFunctions = pattern.match(func);
assertTrue(subFunctions.isPresent()); assertTrue(subFunctions.isPresent());
assertEquals(func, pattern.replace(subFunctions.get())); assertEquals(func, pattern.replace(mathContext, subFunctions.get()));
} }
@Test @Test
@ -42,20 +45,20 @@ public class PatternTest {
); );
final Function shouldNotMatch = new Subtraction( final Function shouldNotMatch = new Subtraction(
null, mathContext,
new Number(null, 1), new Number(mathContext, 1),
new Number(null, 2) new Number(mathContext, 2)
); );
assertFalse(pattern.match(shouldNotMatch).isPresent()); assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldMatch = new Sum( final Function shouldMatch = new Sum(
null, mathContext,
new Number(null, 1), new Number(mathContext, 1),
new Number(null, 2) new Number(mathContext, 2)
); );
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch); final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent()); assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(subFunctions.get())); assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
} }
@Test @Test
@ -66,18 +69,18 @@ public class PatternTest {
); );
final Function shouldMatch = new Sum( final Function shouldMatch = new Sum(
null, mathContext,
new Number(null, 1), new Number(mathContext, 1),
new Number(null, 1) new Number(mathContext, 1)
); );
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch); final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent()); assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(subFunctions.get())); assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
final Function shouldNotMatch = new Sum( final Function shouldNotMatch = new Sum(
null, mathContext,
new Number(null, 1), new Number(mathContext, 1),
new Number(null, 2) new Number(mathContext, 2)
); );
assertFalse(pattern.match(shouldNotMatch).isPresent()); assertFalse(pattern.match(shouldNotMatch).isPresent());
} }
@ -86,13 +89,13 @@ public class PatternTest {
public void numberPattern() { public void numberPattern() {
final Pattern pattern = new NumberPattern(BigDecimal.valueOf(Math.PI)); final Pattern pattern = new NumberPattern(BigDecimal.valueOf(Math.PI));
final Function shouldNotMatch = new Number(null, 2); final Function shouldNotMatch = new Number(mathContext, 2);
assertFalse(pattern.match(shouldNotMatch).isPresent()); assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldMatch = new Number(null, Math.PI); final Function shouldMatch = new Number(mathContext, Math.PI);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch); final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent()); assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(subFunctions.get())); assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
} }
@Test @Test
@ -101,15 +104,15 @@ public class PatternTest {
new SubFunctionPattern("x") new SubFunctionPattern("x")
); );
final Function shouldNotMatch = new Number(null, 1); final Function shouldNotMatch = new Number(mathContext, 1);
assertFalse(pattern.match(shouldNotMatch).isPresent()); assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldMatch = new Negative( final Function shouldMatch = new Negative(
null, mathContext,
new Number(null, 2) new Number(mathContext, 2)
); );
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch); final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent()); assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(subFunctions.get())); assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
} }
} }