diff --git a/pom.xml b/pom.xml index e0cf4f87..2dee82df 100644 --- a/pom.xml +++ b/pom.xml @@ -143,7 +143,7 @@ org.apache.maven.plugins maven-enforcer-plugin - 3.3.0 + 3.4.1 enforce-no-releases @@ -171,7 +171,7 @@ org.apache.maven.plugins maven-enforcer-plugin - 3.3.0 + 3.4.1 enforce-no-snapshots @@ -242,8 +242,8 @@ org.hisp.dhis.lib.expression - expression-parser - 1.0.3 + expression-parser-jvm + 1.1.0-SNAPSHOT diff --git a/src/main/java/org/hisp/dhis/rules/RuleConditionEvaluator.java b/src/main/java/org/hisp/dhis/rules/RuleConditionEvaluator.java index 65b9dc9c..34a005b6 100644 --- a/src/main/java/org/hisp/dhis/rules/RuleConditionEvaluator.java +++ b/src/main/java/org/hisp/dhis/rules/RuleConditionEvaluator.java @@ -3,6 +3,7 @@ import org.hisp.dhis.lib.expression.Expression; import org.hisp.dhis.lib.expression.spi.ExpressionData; import org.hisp.dhis.lib.expression.spi.IllegalExpressionException; +import org.hisp.dhis.lib.expression.spi.VariableValue; import org.hisp.dhis.rules.models.Rule; import org.hisp.dhis.rules.models.RuleAction; import org.hisp.dhis.rules.models.RuleActionAssign; @@ -18,6 +19,7 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; public class RuleConditionEvaluator { @@ -86,7 +88,7 @@ public List getRuleEvaluationResults( TrackerObjectType ta ruleEffects.add( create( rule, action, valueMap, supplementaryData ) ); } } catch ( Exception e ) { - addRuleErrorResult( rule,action, e, targetType, targetUid, ruleEvaluationResults ); + addRuleErrorResult( rule, action, e, targetType, targetUid, ruleEvaluationResults ); } } @@ -179,14 +181,11 @@ private String process(String condition, Map valueMap return ""; } - Expression expression = new Expression(condition, mode); - - ExpressionData build = ExpressionData.builder() - .supplementaryValues(supplementaryData) - .programRuleVariableValues(valueMap) - .build(); + Expression expression = new Expression(condition, mode, false); + Map programRuleVariableValues = valueMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().toVariableValue())); + ExpressionData build = new ExpressionData(programRuleVariableValues, Map.of(), supplementaryData, Map.of(), Map.of()); return convertInteger( expression.evaluate(name -> { - throw new UnsupportedOperationException(name); + throw new UnsupportedOperationException("function not supported: "+name); }, build) ).toString(); } diff --git a/src/main/java/org/hisp/dhis/rules/RuleEngine.java b/src/main/java/org/hisp/dhis/rules/RuleEngine.java index 4cc97a09..c907b097 100644 --- a/src/main/java/org/hisp/dhis/rules/RuleEngine.java +++ b/src/main/java/org/hisp/dhis/rules/RuleEngine.java @@ -114,18 +114,18 @@ private RuleValidationResult getExpressionDescription(String expression, Express for (Map.Entry e : executionContext.dataItemStore().entrySet()) { validationMap.put(e.getKey(), e.getValue().valueType().toValueType()); } - new Expression(expression, mode).validate( validationMap ); + new Expression(expression, mode, false).validate( validationMap ); Map displayNames = new HashMap<>(); for (Map.Entry e : executionContext.dataItemStore().entrySet()) { displayNames.put(e.getKey(), e.getValue().displayName()); } - String description = new Expression(expression, mode).describe(displayNames); + String description = new Expression(expression, mode, false).describe(displayNames); return RuleValidationResult.builder().isValid( true ).description(description).build(); } catch (IllegalExpressionException | ParseException ex) { return RuleValidationResult.builder() .isValid(false) - .exception(new RuleEngineValidationException(ex.getMessage())) + .exception(new RuleEngineValidationException(ex)) .errorMessage(ex.getMessage()) .build(); } diff --git a/src/main/java/org/hisp/dhis/rules/RuleVariableValue.java b/src/main/java/org/hisp/dhis/rules/RuleVariableValue.java index c0ef89d0..f0148932 100644 --- a/src/main/java/org/hisp/dhis/rules/RuleVariableValue.java +++ b/src/main/java/org/hisp/dhis/rules/RuleVariableValue.java @@ -15,7 +15,7 @@ public record RuleVariableValue( @Nonnull RuleValueType type, @Nonnull List candidates, @CheckForNull String eventDate -) implements VariableValue { +) { private static final String DATE_PATTERN = "yyyy-MM-dd"; @Nonnull @@ -47,8 +47,11 @@ private static String getFormattedDate( Date date ) return format.format( date ); } - @Override - public final ValueType valueType() { + public VariableValue toVariableValue() { + return new VariableValue(valueType(), value, candidates, eventDate); + } + + private ValueType valueType() { return switch (type()) { case DATE -> ValueType.DATE; case NUMERIC -> ValueType.NUMBER; @@ -56,10 +59,4 @@ public final ValueType valueType() { default -> ValueType.STRING; }; } - - @Override - public final Object valueOrDefault() { - String value = value(); - return value != null ? value : type().defaultValue(); - } } diff --git a/src/main/java/org/hisp/dhis/rules/models/RuleEngineValidationException.java b/src/main/java/org/hisp/dhis/rules/models/RuleEngineValidationException.java index 992a98a8..5e8c3448 100644 --- a/src/main/java/org/hisp/dhis/rules/models/RuleEngineValidationException.java +++ b/src/main/java/org/hisp/dhis/rules/models/RuleEngineValidationException.java @@ -1,9 +1,7 @@ package org.hisp.dhis.rules.models; -import org.hisp.dhis.lib.expression.spi.ParseException; - -public class RuleEngineValidationException extends ParseException { - public RuleEngineValidationException(String s) { - super(s); +public class RuleEngineValidationException extends IllegalArgumentException { + public RuleEngineValidationException(IllegalArgumentException cause) { + super(cause.getMessage()); } } \ No newline at end of file diff --git a/src/test/java/org/hisp/dhis/rules/RuleEngineGetDescriptionTest.java b/src/test/java/org/hisp/dhis/rules/RuleEngineGetDescriptionTest.java index a9925196..e86a2f9b 100644 --- a/src/test/java/org/hisp/dhis/rules/RuleEngineGetDescriptionTest.java +++ b/src/test/java/org/hisp/dhis/rules/RuleEngineGetDescriptionTest.java @@ -28,10 +28,10 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import org.hisp.dhis.lib.expression.spi.ParseException; import org.hisp.dhis.rules.models.Rule; import org.hisp.dhis.rules.models.RuleAction; import org.hisp.dhis.rules.models.RuleActionText; +import org.hisp.dhis.rules.models.RuleEngineValidationException; import org.hisp.dhis.rules.models.RuleValidationResult; import org.hisp.dhis.rules.models.TriggerEnvironment; import org.junit.Before; @@ -362,12 +362,12 @@ public void testGetDescriptionForDataFieldExpression() result = ruleEngine.evaluateDataFieldExpression( "1 + 1 +" ); assertNotNull( result ); assertFalse( result.valid() ); - assertTrue(result.exception() instanceof ParseException); + assertTrue(result.exception() instanceof RuleEngineValidationException); result = ruleEngine.evaluateDataFieldExpression( "d2:hasValue(#{test_var_two}) && d2:count(#{test_var_one}) > 0 (" ); assertNotNull( result ); assertFalse( result.valid() ); - assertTrue( result.exception() instanceof ParseException); + assertTrue( result.exception() instanceof RuleEngineValidationException); } private RuleEngine.Builder getRuleEngineBuilderForDescription( Map itemStore ) diff --git a/src/test/java/org/hisp/dhis/rules/gs1/GS1ElementsTest.java b/src/test/java/org/hisp/dhis/rules/gs1/GS1ElementsTest.java deleted file mode 100644 index 9a4eadcc..00000000 --- a/src/test/java/org/hisp/dhis/rules/gs1/GS1ElementsTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.hisp.dhis.rules.gs1; - -import org.hisp.dhis.lib.expression.math.GS1Elements; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -import static org.junit.Assert.assertEquals; - -@RunWith(JUnit4.class) -public class GS1ElementsTest { - @Test - public void shouldReturnGS1Element(){ - assertEquals( GS1Elements.GTIN , GS1Elements.fromKey("gtin") ); - assertEquals( GS1Elements.GTIN , GS1Elements.fromKey("GTIN") ); - assertEquals( GS1Elements.LOT_NUMBER , GS1Elements.fromKey("lot number") ); - assertEquals( GS1Elements.LOT_NUMBER , GS1Elements.fromKey("LOT_NUMBER") ); - assertEquals( GS1Elements.LOT_NUMBER , GS1Elements.fromKey("LOT NUMBER") ); - assertEquals( GS1Elements.AREA_I2 , GS1Elements.fromKey("AREA I2") ); - assertEquals( GS1Elements.PAY_TO , GS1Elements.fromKey("PAY_TO") ); - assertEquals( GS1Elements.PAY_TO , GS1Elements.fromKey("PAY TO") ); - assertEquals( GS1Elements.PAY_TO , GS1Elements.fromKey("pay to") ); - assertEquals( GS1Elements.AREA_I2 , GS1Elements.fromKey("AREA I2") ); - assertEquals( GS1Elements.PRODUCT_URL , GS1Elements.fromKey("product url") ); - assertEquals( GS1Elements.PRODUCT_URL , GS1Elements.fromKey("product_url") ); - assertEquals( GS1Elements.PRODUCT_URL , GS1Elements.fromKey("productUrl") ); - assertEquals( GS1Elements.PRODUCT_URL , GS1Elements.fromKey("ProductUrl") ); - assertEquals( GS1Elements.PRODUCT_URL , GS1Elements.fromKey("Product_Url") ); - assertEquals( GS1Elements.PRODUCT_URL , GS1Elements.fromKey("PRODUCT_URL") ); - assertEquals( GS1Elements.PRODUCT_URL , GS1Elements.fromKey("PRODUCT URL") ); - assertEquals( GS1Elements.PRODUCT_URL , GS1Elements.fromKey("PRODUCTURL") ); - } -}