expression parser
This commit is contained in:
parent
ab3c0851ed
commit
28598ddfea
|
@ -6,7 +6,11 @@ import java.util.Set;
|
|||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.expression.MapAccessor;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
|
@ -64,8 +68,14 @@ public class GraphLoader {
|
|||
}
|
||||
|
||||
private Function<Env, Boolean> generateFunction(final String condition) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
return env -> {
|
||||
final ExpressionParser parser = new SpelExpressionParser();
|
||||
|
||||
final StandardEvaluationContext context = new StandardEvaluationContext(env.getAttributes());
|
||||
context.addPropertyAccessor(new MapAccessor());
|
||||
|
||||
return parser.parseExpression(condition).getValue(context, Boolean.class);
|
||||
};
|
||||
}
|
||||
|
||||
private void checkValidity(final Graph graph) throws WorkflowManagerException {
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package eu.dnetlib.manager.wf.workflows.graph;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.expression.MapAccessor;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
|
||||
import eu.dnetlib.manager.wf.workflows.procs.Env;
|
||||
|
||||
public class GraphLoaderTest {
|
||||
|
||||
private Env env;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
env = new Env();
|
||||
env.setAttribute("author", "Michele Artini");
|
||||
env.setAttribute("age", 47);
|
||||
}
|
||||
|
||||
@Test
|
||||
final void testExpressions() {
|
||||
assertTrue(evalFunction("age == 47").apply(env));
|
||||
assertTrue(evalFunction("age > 40").apply(env));
|
||||
assertTrue(evalFunction("author == 'Michele Artini'").apply(env));
|
||||
assertTrue(evalFunction("age == 47 && author == 'Michele Artini'").apply(env));
|
||||
assertTrue(evalFunction("age == 47 || author == 'Michele Artini'").apply(env));
|
||||
assertTrue(evalFunction("age == 47 || author == 'Claudio Atzori'").apply(env));
|
||||
assertTrue(evalFunction("age == 22 || author == 'Michele Artini'").apply(env));
|
||||
assertFalse(evalFunction("age != 47").apply(env));
|
||||
assertFalse(evalFunction("age < 40").apply(env));
|
||||
assertFalse(evalFunction("author != 'Michele Artini'").apply(env));
|
||||
assertFalse(evalFunction("age == 47 && author == 'Claudio Atzori'").apply(env));
|
||||
}
|
||||
|
||||
private Function<Env, Boolean> evalFunction(final String f) {
|
||||
return env -> {
|
||||
final ExpressionParser parser = new SpelExpressionParser();
|
||||
|
||||
final StandardEvaluationContext context = new StandardEvaluationContext(env.getAttributes());
|
||||
context.addPropertyAccessor(new MapAccessor());
|
||||
|
||||
return parser.parseExpression(f).getValue(context, Boolean.class);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue