Skip to content

Commit

Permalink
chore: use KMP expression parser snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
jbee committed Nov 30, 2023
1 parent 8ac8b96 commit eba7478
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 65 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.3.0</version>
<version>3.4.1</version>
<executions>
<execution>
<id>enforce-no-releases</id>
Expand Down Expand Up @@ -171,7 +171,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.3.0</version>
<version>3.4.1</version>
<executions>
<execution>
<id>enforce-no-snapshots</id>
Expand Down Expand Up @@ -242,8 +242,8 @@
</dependency>
<dependency>
<groupId>org.hisp.dhis.lib.expression</groupId>
<artifactId>expression-parser</artifactId>
<version>1.0.3</version>
<artifactId>expression-parser-jvm</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
</dependencies>

Expand Down
15 changes: 7 additions & 8 deletions src/main/java/org/hisp/dhis/rules/RuleConditionEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -86,7 +88,7 @@ public List<RuleEvaluationResult> 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 );
}
}

Expand Down Expand Up @@ -179,14 +181,11 @@ private String process(String condition, Map<String, RuleVariableValue> 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<String, VariableValue> 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();
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/hisp/dhis/rules/RuleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,18 @@ private RuleValidationResult getExpressionDescription(String expression, Express
for (Map.Entry<String, DataItem> 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<String, String> displayNames = new HashMap<>();
for (Map.Entry<String, DataItem> 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();
}
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/org/hisp/dhis/rules/RuleVariableValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public record RuleVariableValue(
@Nonnull RuleValueType type,
@Nonnull List<String> candidates,
@CheckForNull String eventDate
) implements VariableValue {
) {
private static final String DATE_PATTERN = "yyyy-MM-dd";

@Nonnull
Expand Down Expand Up @@ -47,19 +47,16 @@ 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;
case BOOLEAN -> ValueType.BOOLEAN;
default -> ValueType.STRING;
};
}

@Override
public final Object valueOrDefault() {
String value = value();
return value != null ? value : type().defaultValue();
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, DataItem> itemStore )
Expand Down
33 changes: 0 additions & 33 deletions src/test/java/org/hisp/dhis/rules/gs1/GS1ElementsTest.java

This file was deleted.

0 comments on commit eba7478

Please sign in to comment.