use of org.apache.ratis.examples.arithmetic.expression.Expression in project incubator-ratis by apache.
the class ArithmeticStateMachine method applyTransaction.
@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
final LogEntryProto entry = trx.getLogEntry();
final AssignmentMessage assignment = new AssignmentMessage(entry.getSmLogEntry().getData());
final long index = entry.getIndex();
final Double result;
try (final AutoCloseableLock writeLock = writeLock()) {
result = assignment.evaluate(variables);
updateLastAppliedTermIndex(entry.getTerm(), index);
}
final Expression r = Expression.Utils.double2Expression(result);
LOG.debug("{}-{}: {} = {}", getId(), index, assignment, r);
if (LOG.isTraceEnabled()) {
LOG.trace("{}-{}: variables={}", getId(), index, variables);
}
return CompletableFuture.completedFuture(Expression.Utils.toMessage(r));
}
use of org.apache.ratis.examples.arithmetic.expression.Expression in project incubator-ratis by apache.
the class ArithmeticStateMachine method query.
@Override
public CompletableFuture<Message> query(Message request) {
final Expression q = Expression.Utils.bytes2Expression(request.getContent().toByteArray(), 0);
final Double result;
try (final AutoCloseableLock readLock = readLock()) {
result = q.evaluate(variables);
}
final Expression r = Expression.Utils.double2Expression(result);
LOG.debug("QUERY: {} = {}", q, r);
return CompletableFuture.completedFuture(Expression.Utils.toMessage(r));
}
use of org.apache.ratis.examples.arithmetic.expression.Expression in project incubator-ratis by apache.
the class TestArithmetic method assertRaftClientReply.
static Expression assertRaftClientReply(RaftClientReply reply, Double expected) {
Assert.assertTrue(reply.isSuccess());
final Expression e = Expression.Utils.bytes2Expression(reply.getMessage().getContent().toByteArray(), 0);
if (expected != null) {
Assert.assertEquals(expected, e.evaluate(null));
}
return e;
}
use of org.apache.ratis.examples.arithmetic.expression.Expression in project incubator-ratis by apache.
the class TestArithmetic method assignNull.
static void assignNull(RaftClient client, Variable x) throws IOException {
final Expression e = assign(client, x, NullValue.getInstance());
Assert.assertEquals(NullValue.getInstance(), e);
}
use of org.apache.ratis.examples.arithmetic.expression.Expression in project incubator-ratis by apache.
the class TestArithmetic method runTestPythagorean.
public static void runTestPythagorean(RaftClient client, int start, int count) throws IOException {
Preconditions.assertTrue(count > 0, () -> "count = " + count + " <= 0");
Preconditions.assertTrue(start >= 2, () -> "start = " + start + " < 2");
final Variable a = new Variable("a");
final Variable b = new Variable("b");
final Variable c = new Variable("c");
final Expression pythagorean = SQRT.apply(ADD.apply(SQUARE.apply(a), SQUARE.apply(b)));
final int end = start + 2 * count;
for (int n = (start & 1) == 0 ? start + 1 : start; n < end; n += 2) {
int n2 = n * n;
int half_n2 = n2 / 2;
assign(client, a, n);
assign(client, b, half_n2);
assign(client, c, pythagorean, (double) half_n2 + 1);
assignNull(client, a);
assignNull(client, b);
assignNull(client, c);
}
}
Aggregations