Search in sources :

Example 11 with RuleDataValue

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

the class RuleEngineFunctionShould method return_false_if_no_evaluate_value_specified_has_value.

@Test
public void return_false_if_no_evaluate_value_specified_has_value() throws Exception {
    RuleAction ruleAction = RuleActionDisplayKeyValuePair.createForFeedback("test_action_content", "d2:hasValue('test_variable')");
    RuleVariable ruleVariable = RuleVariableCurrentEvent.create("test_variable", "test_data_element", 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(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("false");
    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 12 with RuleDataValue

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

the class RuleEngineValueTypesShould method fallback_to_default_boolean_value_when_boolean_variable_without_valueM.

@Test
public void fallback_to_default_boolean_value_when_boolean_variable_without_valueM() 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.BOOLEAN);
    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("false");
    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 13 with RuleDataValue

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

the class RuleVariableValueMapBuilder method createPreviousEventVariableValue.

private void createPreviousEventVariableValue(@Nonnull Map<String, RuleVariableValue> valueMap, @Nonnull RuleVariablePreviousEvent variable) {
    if (ruleEvent == null) {
        return;
    }
    RuleVariableValue variableValue = null;
    List<RuleDataValue> ruleDataValues = allEventsValues.get(variable.dataElement());
    if (ruleDataValues != null && !ruleDataValues.isEmpty()) {
        for (RuleDataValue ruleDataValue : ruleDataValues) {
            // which is assumed to be best candidate.
            if (ruleEvent.eventDate().compareTo(ruleDataValue.eventDate()) > 0) {
                variableValue = create(ruleDataValue.value(), variable.dataElementType(), Utils.values(ruleDataValues));
                break;
            }
        }
    }
    if (variableValue == null) {
        variableValue = create(variable.dataElementType());
    }
    valueMap.put(variable.name(), variableValue);
}
Also used : RuleDataValue(org.hisp.dhis.rules.models.RuleDataValue)

Example 14 with RuleDataValue

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

the class ProgramRuleEntityMapperServiceTest method testMappedRuleEvent.

@Test
void testMappedRuleEvent() {
    when(dataElementService.getDataElement(anyString())).thenReturn(dataElement);
    RuleEvent ruleEvent = subject.toMappedRuleEvent(programStageInstanceA);
    assertEquals(ruleEvent.event(), programStageInstanceA.getUid());
    assertEquals(ruleEvent.programStage(), programStageInstanceA.getProgramStage().getUid());
    assertEquals(ruleEvent.organisationUnit(), programStageInstanceA.getOrganisationUnit().getUid());
    assertEquals(ruleEvent.organisationUnitCode(), programStageInstanceA.getOrganisationUnit().getCode());
    assertEquals(ruleEvent.programStageName(), programStageInstanceA.getProgramStage().getName());
    assertEquals(ruleEvent.dataValues().size(), 1);
    RuleDataValue ruleDataValue = ruleEvent.dataValues().get(0);
    assertEquals(ruleDataValue.dataElement(), dataElement.getUid());
    assertEquals(ruleDataValue.eventDate(), ruleEvent.eventDate());
    assertEquals(ruleDataValue.programStage(), programStageInstanceA.getProgramStage().getUid());
    assertEquals(ruleDataValue.value(), SAMPLE_VALUE_A);
}
Also used : RuleDataValue(org.hisp.dhis.rules.models.RuleDataValue) RuleEvent(org.hisp.dhis.rules.models.RuleEvent) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Aggregations

RuleDataValue (org.hisp.dhis.rules.models.RuleDataValue)14 RuleEvent (org.hisp.dhis.rules.models.RuleEvent)10 Date (java.util.Date)7 Test (org.junit.Test)7 RuleVariable (org.hisp.dhis.rules.models.RuleVariable)6 RuleEngine (org.hisp.dhis.rules.RuleEngine)4 Rule (org.hisp.dhis.rules.models.Rule)4 RuleAction (org.hisp.dhis.rules.models.RuleAction)4 RuleEffect (org.hisp.dhis.rules.models.RuleEffect)4 ArrayList (java.util.ArrayList)3 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)2 Test (org.junit.jupiter.api.Test)2 RuleEnrollment (org.hisp.dhis.rules.models.RuleEnrollment)1