Search in sources :

Example 6 with RuleVariable

use of org.hisp.dhis.rules.models.RuleVariable in project dhis2-android-sdk by dhis2.

the class RuleEngineFunctionShould method return_correct_diff_on_evaluate_days_between.

@Test
public void return_correct_diff_on_evaluate_days_between() throws Exception {
    RuleAction ruleAction = RuleActionDisplayKeyValuePair.createForFeedback("test_action_content", "d2:daysBetween(#{test_var_one}, #{test_var_two})");
    RuleVariable ruleVariableOne = RuleVariableCurrentEvent.create("test_var_one", "test_data_element_one", RuleValueType.TEXT);
    RuleVariable ruleVariableTwo = RuleVariableCurrentEvent.create("test_var_two", "test_data_element_two", RuleValueType.TEXT);
    Rule rule = Rule.create(null, null, "true", Arrays.asList(ruleAction));
    RuleEngine ruleEngine = RuleEngineContext.builder(new DuktapeEvaluator(duktape)).rules(Arrays.asList(rule)).ruleVariables(Arrays.asList(ruleVariableOne, ruleVariableTwo)).build().toEngineBuilder().build();
    RuleEvent ruleEvent = RuleEvent.create("test_event", "test_program_stage", RuleEvent.Status.ACTIVE, new Date(), new Date(), Arrays.asList(RuleDataValue.create(new Date(), "test_program_stage", "test_data_element_one", "2017-01-01"), RuleDataValue.create(new Date(), "test_program_stage", "test_data_element_two", "2017-02-01")));
    List<RuleEffect> ruleEffects = ruleEngine.evaluate(ruleEvent).call();
    assertThat(ruleEffects.size()).isEqualTo(1);
    assertThat(ruleEffects.get(0).data()).isEqualTo("31.0");
    assertThat(ruleEffects.get(0).ruleAction()).isEqualTo(ruleAction);
}
Also used : RuleEngine(org.hisp.dhis.rules.RuleEngine) RuleEffect(org.hisp.dhis.rules.models.RuleEffect) RuleAction(org.hisp.dhis.rules.models.RuleAction) Rule(org.hisp.dhis.rules.models.Rule) RuleEvent(org.hisp.dhis.rules.models.RuleEvent) Date(java.util.Date) RuleVariable(org.hisp.dhis.rules.models.RuleVariable) Test(org.junit.Test)

Example 7 with RuleVariable

use of org.hisp.dhis.rules.models.RuleVariable in project dhis2-android-sdk by dhis2.

the class RuleEngineValueTypesShould method fallback_to_default_numeric_value_on_numeric_variable_without_value.

@Test
public void fallback_to_default_numeric_value_on_numeric_variable_without_value() throws Exception {
    RuleAction ruleAction = RuleActionDisplayKeyValuePair.createForFeedback("test_action_content", "#{test_variable}");
    Rule rule = Rule.create(null, null, "true", Arrays.asList(ruleAction));
    RuleVariable ruleVariable = RuleVariableCurrentEvent.create("test_variable", "test_data_element", RuleValueType.NUMERIC);
    RuleEngine ruleEngine = RuleEngineContext.builder(new DuktapeEvaluator(duktape)).rules(Arrays.asList(rule)).ruleVariables(Arrays.asList(ruleVariable)).build().toEngineBuilder().build();
    RuleEvent ruleEvent = RuleEvent.create("test_event", "test_program_stage", RuleEvent.Status.ACTIVE, new Date(), new Date(), new ArrayList<RuleDataValue>());
    List<RuleEffect> ruleEffects = ruleEngine.evaluate(ruleEvent).call();
    assertThat(ruleEffects.size()).isEqualTo(1);
    assertThat(ruleEffects.get(0).data()).isEqualTo("0.0");
    assertThat(ruleEffects.get(0).ruleAction()).isEqualTo(ruleAction);
}
Also used : RuleEngine(org.hisp.dhis.rules.RuleEngine) RuleEffect(org.hisp.dhis.rules.models.RuleEffect) RuleDataValue(org.hisp.dhis.rules.models.RuleDataValue) RuleAction(org.hisp.dhis.rules.models.RuleAction) Rule(org.hisp.dhis.rules.models.Rule) RuleEvent(org.hisp.dhis.rules.models.RuleEvent) Date(java.util.Date) RuleVariable(org.hisp.dhis.rules.models.RuleVariable) Test(org.junit.Test)

Example 8 with RuleVariable

use of org.hisp.dhis.rules.models.RuleVariable in project dhis2-android-sdk by dhis2.

the class RuleEngineValueTypesShould method fallback_to_default_text_value_on_variable_without_value.

@Test
public void fallback_to_default_text_value_on_variable_without_value() throws Exception {
    RuleAction ruleAction = RuleActionDisplayKeyValuePair.createForFeedback("test_action_content", "#{test_variable}");
    Rule rule = Rule.create(null, null, "true", Arrays.asList(ruleAction));
    RuleVariable ruleVariable = RuleVariableCurrentEvent.create("test_variable", "test_data_element", RuleValueType.TEXT);
    RuleEngine ruleEngine = RuleEngineContext.builder(new DuktapeEvaluator(duktape)).rules(Arrays.asList(rule)).ruleVariables(Arrays.asList(ruleVariable)).build().toEngineBuilder().build();
    RuleEvent ruleEvent = RuleEvent.create("test_event", "test_program_stage", RuleEvent.Status.ACTIVE, new Date(), new Date(), new ArrayList<RuleDataValue>());
    List<RuleEffect> ruleEffects = ruleEngine.evaluate(ruleEvent).call();
    assertThat(ruleEffects.size()).isEqualTo(1);
    assertThat(ruleEffects.get(0).data()).isEqualTo("");
    assertThat(ruleEffects.get(0).ruleAction()).isEqualTo(ruleAction);
}
Also used : RuleEngine(org.hisp.dhis.rules.RuleEngine) RuleEffect(org.hisp.dhis.rules.models.RuleEffect) RuleDataValue(org.hisp.dhis.rules.models.RuleDataValue) RuleAction(org.hisp.dhis.rules.models.RuleAction) Rule(org.hisp.dhis.rules.models.Rule) RuleEvent(org.hisp.dhis.rules.models.RuleEvent) Date(java.util.Date) RuleVariable(org.hisp.dhis.rules.models.RuleVariable) Test(org.junit.Test)

Example 9 with RuleVariable

use of org.hisp.dhis.rules.models.RuleVariable in project dhis2-core by dhis2.

the class ProgramRuleEntityMapperServiceTest method testMappedRuleVariableValues.

@Test
void testMappedRuleVariableValues() {
    when(programRuleVariableService.getAllProgramRuleVariable()).thenReturn(programRuleVariables);
    RuleVariableAttribute ruleVariableAttribute;
    RuleVariableCalculatedValue ruleVariableCalculatedValue;
    List<RuleVariable> ruleVariables = subject.toMappedProgramRuleVariables();
    assertEquals(ruleVariables.size(), 3);
    for (RuleVariable variable : ruleVariables) {
        if (variable instanceof RuleVariableAttribute) {
            ruleVariableAttribute = (RuleVariableAttribute) variable;
            assertEquals(ruleVariableAttribute.trackedEntityAttribute(), programRuleVariableB.getAttribute().getUid());
            assertEquals(ruleVariableAttribute.name(), programRuleVariableB.getName());
        }
        if (variable instanceof RuleVariableCalculatedValue) {
            ruleVariableCalculatedValue = (RuleVariableCalculatedValue) variable;
            assertEquals(ruleVariableCalculatedValue.name(), programRuleVariableA.getName());
        }
    }
}
Also used : RuleVariableAttribute(org.hisp.dhis.rules.models.RuleVariableAttribute) RuleVariableCalculatedValue(org.hisp.dhis.rules.models.RuleVariableCalculatedValue) RuleVariable(org.hisp.dhis.rules.models.RuleVariable) ProgramRuleVariable(org.hisp.dhis.programrule.ProgramRuleVariable) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 10 with RuleVariable

use of org.hisp.dhis.rules.models.RuleVariable in project dhis2-android-sdk by dhis2.

the class RuleEngineContextShould method thrown_unsupported_operation_exception_when_edit_immutable_copy_of_rules_in_builder.

@Test
public void thrown_unsupported_operation_exception_when_edit_immutable_copy_of_rules_in_builder() {
    RuleVariable ruleVariableOne = mock(RuleVariable.class);
    RuleVariable ruleVariableTwo = mock(RuleVariable.class);
    Rule ruleOne = mock(Rule.class);
    Rule ruleTwo = mock(Rule.class);
    List<RuleVariable> ruleVariables = new ArrayList<>();
    List<Rule> rules = new ArrayList<>();
    ruleVariables.add(ruleVariableOne);
    rules.add(ruleOne);
    RuleEngineContext ruleEngineContext = RuleEngineContext.builder(ruleExpressionEvaluator).ruleVariables(ruleVariables).rules(rules).build();
    ruleVariables.add(ruleVariableTwo);
    rules.add(ruleTwo);
    assertThat(ruleEngineContext.expressionEvaluator()).isEqualTo(ruleExpressionEvaluator);
    assertThat(ruleEngineContext.ruleVariables().size()).isEqualTo(1);
    assertThat(ruleEngineContext.ruleVariables().get(0)).isEqualTo(ruleVariableOne);
    assertThat(ruleEngineContext.rules().size()).isEqualTo(1);
    assertThat(ruleEngineContext.rules().get(0)).isEqualTo(ruleOne);
    try {
        ruleEngineContext.ruleVariables().clear();
        fail("UnsupportedOperationException was expected, but nothing was thrown.");
    } catch (UnsupportedOperationException unsupportedOperationException) {
    // noop
    }
    try {
        ruleEngineContext.rules().clear();
        fail("UnsupportedOperationException was expected, but nothing was thrown.");
    } catch (UnsupportedOperationException unsupportedOperationException) {
    // noop
    }
}
Also used : ArrayList(java.util.ArrayList) Rule(org.hisp.dhis.rules.models.Rule) RuleVariable(org.hisp.dhis.rules.models.RuleVariable) Test(org.junit.Test)

Aggregations

RuleVariable (org.hisp.dhis.rules.models.RuleVariable)20 Test (org.junit.Test)18 RuleEvent (org.hisp.dhis.rules.models.RuleEvent)17 Date (java.util.Date)16 Rule (org.hisp.dhis.rules.models.Rule)8 RuleEngine (org.hisp.dhis.rules.RuleEngine)7 RuleAction (org.hisp.dhis.rules.models.RuleAction)7 RuleEffect (org.hisp.dhis.rules.models.RuleEffect)7 RuleDataValue (org.hisp.dhis.rules.models.RuleDataValue)6 RuleEnrollment (org.hisp.dhis.rules.models.RuleEnrollment)2 RuleVariableAttribute (org.hisp.dhis.rules.models.RuleVariableAttribute)2 ArrayList (java.util.ArrayList)1 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)1 ProgramRuleVariable (org.hisp.dhis.programrule.ProgramRuleVariable)1 RuleVariableCalculatedValue (org.hisp.dhis.rules.models.RuleVariableCalculatedValue)1 RuleVariableCurrentEvent (org.hisp.dhis.rules.models.RuleVariableCurrentEvent)1 RuleVariableNewestEvent (org.hisp.dhis.rules.models.RuleVariableNewestEvent)1 RuleVariableNewestStageEvent (org.hisp.dhis.rules.models.RuleVariableNewestStageEvent)1 RuleVariablePreviousEvent (org.hisp.dhis.rules.models.RuleVariablePreviousEvent)1 Test (org.junit.jupiter.api.Test)1