From 28598ddfea6affad53995cd1c3f8add7c00ee772 Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Mon, 20 Mar 2023 15:32:46 +0100 Subject: [PATCH] expression parser --- .../wf/workflows/graph/GraphLoader.java | 14 ++++- .../wf/workflows/graph/GraphLoaderTest.java | 54 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 libs/dnet-wf-service/src/test/java/eu/dnetlib/manager/wf/workflows/graph/GraphLoaderTest.java diff --git a/libs/dnet-wf-service/src/main/java/eu/dnetlib/manager/wf/workflows/graph/GraphLoader.java b/libs/dnet-wf-service/src/main/java/eu/dnetlib/manager/wf/workflows/graph/GraphLoader.java index 0b4c23e9..ff9cfa29 100644 --- a/libs/dnet-wf-service/src/main/java/eu/dnetlib/manager/wf/workflows/graph/GraphLoader.java +++ b/libs/dnet-wf-service/src/main/java/eu/dnetlib/manager/wf/workflows/graph/GraphLoader.java @@ -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 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 { diff --git a/libs/dnet-wf-service/src/test/java/eu/dnetlib/manager/wf/workflows/graph/GraphLoaderTest.java b/libs/dnet-wf-service/src/test/java/eu/dnetlib/manager/wf/workflows/graph/GraphLoaderTest.java new file mode 100644 index 00000000..f76b610b --- /dev/null +++ b/libs/dnet-wf-service/src/test/java/eu/dnetlib/manager/wf/workflows/graph/GraphLoaderTest.java @@ -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 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); + }; + } + +}