Search in sources :

Example 1 with DoubleValue

use of org.apache.ratis.examples.arithmetic.expression.DoubleValue in project incubator-ratis by apache.

the class TestAssignCli method createExpression.

@Test
public void createExpression() {
    Assert.assertEquals(new DoubleValue(2.0), new Assign().createExpression("2.0"));
    Assert.assertEquals(new DoubleValue(42.0), new Assign().createExpression("42"));
    Assert.assertEquals(MULT.apply(2.0, new Variable("a")), new Assign().createExpression("2*a"));
    Assert.assertEquals(MULT.apply(new Variable("v1"), 2.0), new Assign().createExpression("v1 * 2"));
    Assert.assertEquals(ADD.apply(2.0, 1.0), new Assign().createExpression("2+1"));
    Assert.assertEquals(SUBTRACT.apply(1.0, 6.0), new Assign().createExpression("1 - 6"));
    Assert.assertEquals(ADD.apply(new Variable("a"), new Variable("v2")), new Assign().createExpression("a+v2"));
    Assert.assertEquals(ADD.apply(new Variable("v1"), new Variable("b")), new Assign().createExpression("v1 + b"));
    Assert.assertEquals(SQRT.apply(new Variable("a")), new Assign().createExpression("√a"));
    Assert.assertEquals(SQRT.apply(new Variable("ABC")), new Assign().createExpression("√ABC"));
    Assert.assertEquals(SQRT.apply(2.0), new Assign().createExpression("√2"));
    Assert.assertEquals(NEG.apply(2.0), new Assign().createExpression("~2.0"));
    Assert.assertEquals(MINUS.apply(6.0), new Assign().createExpression("-6.0"));
}
Also used : Variable(org.apache.ratis.examples.arithmetic.expression.Variable) DoubleValue(org.apache.ratis.examples.arithmetic.expression.DoubleValue) Test(org.junit.Test)

Example 2 with DoubleValue

use of org.apache.ratis.examples.arithmetic.expression.DoubleValue in project incubator-ratis by apache.

the class Assign method createExpression.

@VisibleForTesting
Expression createExpression(String val) {
    if (NUMBER_PATTERN.matcher(val).matches()) {
        return new DoubleValue(Double.parseDouble(val));
    } else if (Variable.PATTERN.matcher(val).matches()) {
        return new Variable(val);
    }
    Matcher binaryMatcher = BINARY_OPERATION_PATTERN.matcher(val);
    Matcher unaryMatcher = UNARY_OPERATION_PATTERN.matcher(val);
    if (binaryMatcher.matches()) {
        return createBinaryExpression(binaryMatcher);
    } else if (unaryMatcher.matches()) {
        return createUnaryExpression(unaryMatcher);
    } else {
        throw new IllegalArgumentException("Invalid expression " + val + " Try something like: 'a+b' or '2'");
    }
}
Also used : Variable(org.apache.ratis.examples.arithmetic.expression.Variable) DoubleValue(org.apache.ratis.examples.arithmetic.expression.DoubleValue) Matcher(java.util.regex.Matcher) VisibleForTesting(org.apache.ratis.thirdparty.com.google.common.annotations.VisibleForTesting)

Aggregations

DoubleValue (org.apache.ratis.examples.arithmetic.expression.DoubleValue)2 Variable (org.apache.ratis.examples.arithmetic.expression.Variable)2 Matcher (java.util.regex.Matcher)1 VisibleForTesting (org.apache.ratis.thirdparty.com.google.common.annotations.VisibleForTesting)1 Test (org.junit.Test)1