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

compelet the compile api #504

Merged
merged 3 commits into from
Nov 26, 2022
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
26 changes: 26 additions & 0 deletions src/main/java/com/googlecode/aviator/AviatorEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,19 @@ public static Expression getCachedExpression(final String expression) {
return getInstance().getCachedExpression(expression);
}

/**
* Compile a text expression to Expression object and cache it with the specified cache key
*
* @param cacheKey cache key
* @param expression text expression
* @param cached Whether to cache the compiled result,make true to cache it.
* @return
*/
public static Expression compile(final String cacheKey, final String expression,
final boolean cached) {
return getInstance().compile(cacheKey, expression, cached);
}


/**
* Compile a text expression to Expression object
Expand Down Expand Up @@ -519,6 +532,19 @@ public static Object exec(final String expression, final Object... values) {
return getInstance().exec(expression, values);
}

/**
* Execute a text expression with environment
*
* @param cacheKey unique key for caching
* @param expression text expression
* @param env Binding variable environment
* @param cached Whether to cache the compiled result,make true to cache it.
*/
public static Object execute(final String cacheKey, final String expression,
final Map<String, Object> env, final boolean cached) {
return getInstance().execute(cacheKey, expression, env, cached);
}


/**
* Execute a text expression with environment
Expand Down
39 changes: 25 additions & 14 deletions src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ public void setEnvProcessor(final EnvProcessor envProcessor) {
/**
* Compile a script file into expression.
*
* @param file the script file path
* @param cached whether to cached the compiled result with key is script file's absolute path.
* @param path the script file path
* @param cached whether to cache the compiled result with key is script file's absolute path.
* @since 5.0.0
* @return the compiled expression instance.
*/
Expand Down Expand Up @@ -356,10 +356,9 @@ private File tryFindFileFromClassLoader(final String path, final ClassLoader con
}

/**
* Loads a script from path and return it's exports.
* Loads a script from path and return its exports.
*
* @param path
* @param args arguments to execute the script.
* @param path the script file path
* @throws IOException
* @return the exports map.
* @since 5.0.0
Expand Down Expand Up @@ -389,10 +388,9 @@ private Map<String, Object> executeModule(final Expression exp, final String abP


/**
* Loads a script from path and return it's exports with module caching.
* Loads a script from path and return its exports with module caching.
*
* @param path
* @param args arguments to execute the script.
* @param path the script file path
* @throws IOException
* @return the exports map
* @since 5.0.0
Expand Down Expand Up @@ -902,7 +900,7 @@ private void loadModule() {
}

/**
* Set a alias name for function specified by name
* Set alias name for function specified by name
*
* @param name the origin function name
* @param aliasName the alias function name
Expand Down Expand Up @@ -1304,7 +1302,7 @@ public AviatorFunction getFunction(final String name) {
* Retrieve an aviator function by name,throw exception if not found or null.It's not thread-safe.
*
* @param name
* @param symbolTablee
* @param symbolTable
* @return
*/
public AviatorFunction getFunction(final String name, final SymbolTable symbolTable) {
Expand Down Expand Up @@ -1655,17 +1653,17 @@ public Object exec(final String expression, final Object... values) {
}
}


/**
* Execute a text expression with environment
*
* @param cacheKey unique key for caching
* @param expression text expression
* @param env Binding variable environment
* @param cached Whether to cache the compiled result,make true to cache it.
*/
public Object execute(final String expression, final Map<String, Object> env,
final boolean cached) {
Expression compiledExpression = compile(expression, cached);
public Object execute(final String cacheKey, final String expression,
final Map<String, Object> env, final boolean cached) {
Expression compiledExpression = compile(cacheKey, expression, cached);
if (compiledExpression != null) {
return compiledExpression.execute(env);
} else {
Expand All @@ -1674,6 +1672,19 @@ public Object execute(final String expression, final Map<String, Object> env,
}


/**
* Execute a text expression with environment
*
* @param expression text expression
* @param env Binding variable environment
* @param cached Whether to cache the compiled result,make true to cache it.
*/
public Object execute(final String expression, final Map<String, Object> env,
final boolean cached) {
return execute(expression, expression, env, cached);
}


/**
* Execute a text expression without caching
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,17 @@ public void evaluatorWithCache() {
assertEquals("hello world", this.instance.execute("a+b", env, true));
}

@Test
public void evaluatorWithSpecifiedCacheKey() {
Map<String, Object> env = new HashMap<>();
env.put("a", "hello");
env.put("b", " world");
assertEquals("hello world", this.instance.execute("key", "a + b", env, true));
final Expression expression = this.instance.getCachedExpressionByKey("key");
assertNotNull(expression);
assertEquals("hello world", expression.execute(env));
}


@Test(expected = UnsupportedFeatureException.class)
public void testDisableAssignment() {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/googlecode/aviator/AviatorEvaluatorUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.math.MathContext;
import java.util.HashMap;
import java.util.Map;
import com.googlecode.aviator.utils.Utils;
import org.junit.Test;
import com.googlecode.aviator.exception.CompileExpressionErrorException;

Expand Down Expand Up @@ -126,6 +127,18 @@ public void testExecIllegalArguments() {
AviatorEvaluator.exec("a-b+c", 1, 2);
}

@Test
public void testCompileCacheWithSpecifiedCacheKey() {
String expression = "1+3";
Expression exp1 = AviatorEvaluator.compile(Utils.md5sum(expression), expression, true);
Expression exp2 = AviatorEvaluator.compile(Utils.md5sum(expression), expression, true);
assertNotNull(exp1);
assertNotNull(exp2);
assertSame(exp1, exp2);

assertEquals(4, exp1.execute(null));
assertEquals(4, exp2.execute(null));
}

@Test
public void testCompileCache() {
Expand Down Expand Up @@ -177,6 +190,17 @@ public void evaluatorWithCache() {
assertEquals("hello world", AviatorEvaluator.execute("a+b", env, true));
}

@Test
public void evaluatorWithSpecifiedCacheKey() {
Map<String, Object> env = new HashMap<>();
env.put("a", "hello");
env.put("b", " world");
assertEquals("hello world", AviatorEvaluator.execute("key", "a + b", env, true));
final Expression expression = AviatorEvaluator.getInstance().getCachedExpressionByKey("key");
assertNotNull(expression);
assertEquals("hello world", expression.execute(env));
}


@Test(expected = CompileExpressionErrorException.class)
public void compileBlankExpression1() {
Expand Down