Implement EquationPattern

This commit is contained in:
Riccardo Azzolini 2018-10-16 18:46:49 +02:00
parent 2aeb396b53
commit 53e2416426
2 changed files with 44 additions and 0 deletions

View File

@ -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<Map<String, Function>> visit(final Equation equation) {
return PatternUtils.matchFunctionOperatorParameters(equation, left, right);
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
return new Equation(
mathContext,
left.replace(mathContext, subFunctions),
right.replace(mathContext, subFunctions)
);
}
}

View File

@ -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)