Make NegativePattern match and produce negative numbers

This commit is contained in:
Riccardo Azzolini 2019-02-09 19:35:45 +01:00
parent d18e2d3708
commit df5de92931
2 changed files with 38 additions and 5 deletions

View File

@ -3,9 +3,11 @@ 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.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.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;
import java.math.BigDecimal;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@ -26,12 +28,28 @@ public class NegativePattern extends VisitorPattern {
return inner.match(negative.getParameter()); return inner.match(negative.getParameter());
} }
@Override
public Optional<Map<String, Function>> visit(final Number number) {
final BigDecimal value = number.getTerm();
if (value.signum() < 0) {
return inner.match(new Number(number.getMathContext(), value.abs()));
} else {
return Optional.empty();
}
}
@Override @Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) { public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Negative( final Function newInner = inner.replace(mathContext, subFunctions);
mathContext,
inner.replace(mathContext, subFunctions) if (newInner instanceof Number) {
); return ((Number) newInner).multiply(new Number(mathContext, -1));
} else {
return new Negative(
mathContext,
inner.replace(mathContext, subFunctions)
);
}
} }
@Override @Override

View File

@ -111,13 +111,28 @@ public class PatternTest {
final Function shouldMatch = new Negative( final Function shouldMatch = new Negative(
mathContext, mathContext,
new Number(mathContext, 2) new Variable(mathContext, 'x', Variable.V_TYPE.VARIABLE)
); );
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(mathContext, subFunctions.get())); assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
} }
@Test
public void negativePatternForNumber() {
final Pattern pattern = new NegativePattern(
new NumberPattern(new BigDecimal(1))
);
final Function shouldNotMatch = new Number(mathContext, 1);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldMatch = new Number(mathContext, -1);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
@Test @Test
public void undefinedPattern() { public void undefinedPattern() {
final Pattern pattern = new UndefinedPattern(); final Pattern pattern = new UndefinedPattern();