Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds more tests and documentation for copy api #434

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ EvaluationValue result = expression
System.out.println(result.getNumberValue()); // prints 6.00
```

### Expression can be copied and evaluated with a different set of values:

Using a copy of the expression allows a thread-safe evaluation of that copy, without parsing the expression again.
The copy uses the same expression string, configuration and syntax tree.
The existing expression will be parsed to populate the syntax tree.

Make sure each thread has its own copy of the original expression.
```java
Expression expression = new Expression("a + b").with("a", 1).and("b", 2);
Expression copiedExpression = expression.copy().with("a", 3).and("b", 4);

EvaluationValue result = expression.evaluate();
EvaluationValue copiedResult = copiedExpression.evaluate();

System.out.println(result.getNumberValue()); // prints 3
System.out.println(copiedResult.getNumberValue()); // prints 7
```

### Values can be passed in a map

Instead of specifying the variable values one by one, they can be set by defining a map with names and values and then
Expand Down
5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ System.out.println(result.getNumberValue()); // prints 6.00

### Expression can be copied and evaluated with a different set of values:

Using a copy of the expression allows a thread-safe evaluation of that copy, without parsing the expression again.
The copy uses the same expression string, configuration and syntax tree.
The existing expression will be parsed to populate the syntax tree.

Make sure each thread has its own copy of the original expression.
```java
Expression expression = new Expression("a + b").with("a", 1).and("b", 2);
Expression copiedExpression = expression.copy().with("a", 3).and("b", 4);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/ezylang/evalex/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public Expression(String expressionString, ExpressionConfiguration configuration
}

/**
* Creates a copy with the same expression string, configuration, dataAccessor and syntax tree
* from an existing expression. The existing expression will be parsed to populate the syntax tree
* Creates a copy with the same expression string, configuration and syntax tree from an existing
* expression. The existing expression will be parsed to populate the syntax tree.
*
* @param expression An existing expression.
* @throws ParseException If there were problems while parsing the existing expression.
Expand All @@ -77,6 +77,7 @@ public Expression(Expression expression) throws ParseException {
this(expression.getExpressionString(), expression.getConfiguration());
this.abstractSyntaxTree = expression.getAbstractSyntaxTree();
}

/**
* Evaluates the expression by parsing it (if not done before) and the evaluating it.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2012-2024 Udo Klimaschewski

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.ezylang.evalex;

import static org.assertj.core.api.Assertions.assertThat;

import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.parser.ParseException;
import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;

class ExpressionEvaluationMultiThreadedTest {
@Test
void testThreadLocal() throws InterruptedException {

AtomicInteger errorCount = new AtomicInteger();

Expression expression = new Expression("a+b");

SecureRandom random = new SecureRandom();

// start 100 threads
ExecutorService es = Executors.newCachedThreadPool();
for (int t = 0; t < 100; t++) {
es.execute(
() -> {
try {
for (int i = 0; i < 100; i++) {

BigDecimal a = new BigDecimal(random.nextInt());
BigDecimal b = new BigDecimal(random.nextInt());
EvaluationValue result = expression.copy().with("a", a).and("b", b).evaluate();

BigDecimal sum = a.add(b);

if (sum.compareTo(result.getNumberValue()) != 0) {
errorCount.getAndIncrement();
System.err.printf(
"Error adding decimals: %s + %s should be %s but is %s%n",
a.toPlainString(),
b.toPlainString(),
sum.toPlainString(),
result.getNumberValue().toPlainString());
}
}
} catch (EvaluationException | ParseException e) {
e.printStackTrace();
errorCount.getAndIncrement();
}
});
}
es.shutdown();

// normal termination, no timeout
assertThat(es.awaitTermination(60, TimeUnit.SECONDS)).isTrue();

assertThat(errorCount).hasValue(0);
}
}
14 changes: 14 additions & 0 deletions src/test/java/com/ezylang/evalex/ExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,18 @@ void testCopy() throws ParseException, EvaluationException {
assertThat(result.getStringValue()).isEqualTo("3");
assertThat(copiedResult.getStringValue()).isEqualTo("7");
}

@Test
void testCopyCreatesNewDataAccessor() throws ParseException {
Expression expression = new Expression(("a"));
Expression expressionCopy = expression.copy();

expression.getDataAccessor().setData("a", EvaluationValue.stringValue("1"));
expressionCopy.getDataAccessor().setData("a", EvaluationValue.stringValue("2"));

assertThat(expression.getDataAccessor().getData("a"))
.isEqualTo(EvaluationValue.stringValue("1"));
assertThat(expressionCopy.getDataAccessor().getData("a"))
.isEqualTo(EvaluationValue.stringValue("2"));
}
}
Loading