Implement patterns for most FunctionSingles
This commit is contained in:
parent
54fc13b211
commit
0ec3974f41
@ -0,0 +1,34 @@
|
||||
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.trigonometry.ArcCosine;
|
||||
import it.cavallium.warppi.math.rules.dsl.Pattern;
|
||||
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Matches and generates the arccosine of another pattern.
|
||||
*/
|
||||
public class ArcCosinePattern extends VisitorPattern {
|
||||
private final Pattern argument;
|
||||
|
||||
public ArcCosinePattern(final Pattern argument) {
|
||||
this.argument = argument;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Map<String, Function>> visit(final ArcCosine arcCosine) {
|
||||
return argument.match(arcCosine.getParameter());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
|
||||
return new ArcCosine(
|
||||
mathContext,
|
||||
argument.replace(mathContext, subFunctions)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
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.trigonometry.ArcSine;
|
||||
import it.cavallium.warppi.math.rules.dsl.Pattern;
|
||||
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Matches and generates the arcsine of another pattern.
|
||||
*/
|
||||
public class ArcSinePattern extends VisitorPattern {
|
||||
private final Pattern argument;
|
||||
|
||||
public ArcSinePattern(final Pattern argument) {
|
||||
this.argument = argument;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Map<String, Function>> visit(final ArcSine arcSine) {
|
||||
return argument.match(arcSine.getParameter());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
|
||||
return new ArcSine(
|
||||
mathContext,
|
||||
argument.replace(mathContext, subFunctions)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
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.trigonometry.ArcTangent;
|
||||
import it.cavallium.warppi.math.rules.dsl.Pattern;
|
||||
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Matches and generates the arctangent of another pattern.
|
||||
*/
|
||||
public class ArcTangentPattern extends VisitorPattern {
|
||||
private final Pattern argument;
|
||||
|
||||
public ArcTangentPattern(final Pattern argument) {
|
||||
this.argument = argument;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Map<String, Function>> visit(final ArcTangent arcTangent) {
|
||||
return argument.match(arcTangent.getParameter());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
|
||||
return new ArcTangent(
|
||||
mathContext,
|
||||
argument.replace(mathContext, subFunctions)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
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.trigonometry.Cosine;
|
||||
import it.cavallium.warppi.math.rules.dsl.Pattern;
|
||||
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Matches and generates the cosine of another pattern.
|
||||
*/
|
||||
public class CosinePattern extends VisitorPattern {
|
||||
private final Pattern argument;
|
||||
|
||||
public CosinePattern(final Pattern argument) {
|
||||
this.argument = argument;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Map<String, Function>> visit(final Cosine cosine) {
|
||||
return argument.match(cosine.getParameter());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
|
||||
return new Cosine(
|
||||
mathContext,
|
||||
argument.replace(mathContext, subFunctions)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
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.trigonometry.Sine;
|
||||
import it.cavallium.warppi.math.rules.dsl.Pattern;
|
||||
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Matches and generates the sine of another pattern.
|
||||
*/
|
||||
public class SinePattern extends VisitorPattern {
|
||||
private final Pattern argument;
|
||||
|
||||
public SinePattern(final Pattern argument) {
|
||||
this.argument = argument;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Map<String, Function>> visit(final Sine sine) {
|
||||
return argument.match(sine.getParameter());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
|
||||
return new Sine(
|
||||
mathContext,
|
||||
argument.replace(mathContext, subFunctions)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
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.trigonometry.Tangent;
|
||||
import it.cavallium.warppi.math.rules.dsl.Pattern;
|
||||
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Matches and generates the tangent of another pattern.
|
||||
*/
|
||||
public class TangentPattern extends VisitorPattern {
|
||||
private final Pattern argument;
|
||||
|
||||
public TangentPattern(final Pattern argument) {
|
||||
this.argument = argument;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Map<String, Function>> visit(final Tangent tangent) {
|
||||
return argument.match(tangent.getParameter());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
|
||||
return new Tangent(
|
||||
mathContext,
|
||||
argument.replace(mathContext, subFunctions)
|
||||
);
|
||||
}
|
||||
}
|
@ -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.trigonometry.*;
|
||||
import it.cavallium.warppi.math.rules.dsl.patterns.*;
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.junit.Test;
|
||||
@ -165,4 +166,50 @@ public class PatternTest {
|
||||
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void otherUnaryPatterns() {
|
||||
final Number one = new Number(mathContext, 1);
|
||||
final SubFunctionPattern x = new SubFunctionPattern("x");
|
||||
|
||||
final Function shouldNotMatch = new Negative(mathContext, one);
|
||||
|
||||
final List<ImmutablePair<Pattern, Function>> patternsAndMatchingFunctions = Arrays.asList(
|
||||
new ImmutablePair<>(
|
||||
new ArcCosinePattern(x),
|
||||
new ArcCosine(mathContext, one)
|
||||
),
|
||||
new ImmutablePair<>(
|
||||
new ArcSinePattern(x),
|
||||
new ArcSine(mathContext, one)
|
||||
),
|
||||
new ImmutablePair<>(
|
||||
new ArcTangentPattern(x),
|
||||
new ArcTangent(mathContext, one)
|
||||
),
|
||||
new ImmutablePair<>(
|
||||
new CosinePattern(x),
|
||||
new Cosine(mathContext, one)
|
||||
),
|
||||
new ImmutablePair<>(
|
||||
new SinePattern(x),
|
||||
new Sine(mathContext, one)
|
||||
),
|
||||
new ImmutablePair<>(
|
||||
new TangentPattern(x),
|
||||
new Tangent(mathContext, one)
|
||||
)
|
||||
);
|
||||
|
||||
for (final ImmutablePair<Pattern, Function> patternAndMatchingFunction : patternsAndMatchingFunctions) {
|
||||
final Pattern pattern = patternAndMatchingFunction.getLeft();
|
||||
final Function shouldMatch = patternAndMatchingFunction.getRight();
|
||||
|
||||
assertFalse(pattern.match(shouldNotMatch).isPresent());
|
||||
|
||||
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
|
||||
assertTrue(subFunctions.isPresent());
|
||||
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user