Search in sources :

Example 1 with RuleVariable

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

the class RuleVariableValueMapBuilderShould method contain_values_from_context_enrollment_in_attribute_variable.

@Test
public void contain_values_from_context_enrollment_in_attribute_variable() throws ParseException {
    RuleVariable ruleVariableOne = RuleVariableAttribute.create("test_variable_one", "test_attribute_one", RuleValueType.TEXT);
    RuleVariable ruleVariableTwo = RuleVariableAttribute.create("test_variable_two", "test_attribute_two", RuleValueType.TEXT);
    Date eventDate = dateFormat.parse("2015-01-01");
    Date enrollmentDate = dateFormat.parse("2014-03-01");
    // values from ruleEnrollment should end up in ruleVariables
    RuleEnrollment ruleEnrollment = RuleEnrollment.create("test_enrollment", enrollmentDate, enrollmentDate, RuleEnrollment.Status.ACTIVE, Arrays.asList(RuleAttributeValue.create("test_attribute_one", "test_attribute_value_one"), RuleAttributeValue.create("test_attribute_two", "test_attribute_value_two")));
    // values from context ruleEvents should be ignored
    RuleEvent contextEvent = RuleEvent.create("test_context_event_one", "test_program_stage", RuleEvent.Status.ACTIVE, eventDate, new Date(), Arrays.asList(RuleDataValue.create(eventDate, "test_program_stage", "test_dataelement_one", "test_context_value_one"), RuleDataValue.create(eventDate, "test_program_stage", "test_dataelement_two", "test_context_value_two")));
    RuleEvent currentEvent = RuleEvent.create("test_event_uid", "test_program_stage", RuleEvent.Status.ACTIVE, eventDate, eventDate, Arrays.asList(RuleDataValue.create(eventDate, "test_program_stage", "test_dataelement_one", "test_value_one"), RuleDataValue.create(eventDate, "test_program_stage", "test_dataelement_two", "test_value_two")));
    // here we will expect correct values to be returned
    Map<String, RuleVariableValue> valueMap = RuleVariableValueMapBuilder.target(currentEvent).ruleEnrollment(ruleEnrollment).ruleVariables(Arrays.asList(ruleVariableOne, ruleVariableTwo)).ruleEvents(Arrays.asList(contextEvent)).build();
    assertThat(valueMap.size()).isEqualTo(12);
    assertThatVariable(valueMap.get("current_date")).hasValue(wrap(dateFormat.format(new Date()))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(new Date()));
    assertThatVariable(valueMap.get("event_date")).hasValue(wrap(dateFormat.format(eventDate))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(eventDate));
    assertThatVariable(valueMap.get("event_count")).hasValue("2").isTypeOf(RuleValueType.NUMERIC).hasCandidates("2");
    assertThatVariable(valueMap.get("event_id")).hasValue("'test_event_uid'").isTypeOf(RuleValueType.TEXT).hasCandidates("test_event_uid");
    assertThatVariable(valueMap.get("due_date")).hasValue(wrap(dateFormat.format(eventDate))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(eventDate));
    assertThatVariable(valueMap.get("enrollment_date")).hasValue(wrap(dateFormat.format(enrollmentDate))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(enrollmentDate));
    assertThatVariable(valueMap.get("enrollment_id")).hasValue("'test_enrollment'").isTypeOf(RuleValueType.TEXT).hasCandidates("test_enrollment");
    assertThatVariable(valueMap.get("enrollment_count")).hasValue("1").isTypeOf(RuleValueType.NUMERIC).hasCandidates("1");
    assertThatVariable(valueMap.get("incident_date")).hasValue(wrap(dateFormat.format(enrollmentDate))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(enrollmentDate));
    assertThatVariable(valueMap.get("tei_count")).hasValue("1").isTypeOf(RuleValueType.NUMERIC).hasCandidates("1");
    assertThatVariable(valueMap.get("test_variable_one")).hasValue("'test_attribute_value_one'").isTypeOf(RuleValueType.TEXT).hasCandidates("test_attribute_value_one");
    assertThatVariable(valueMap.get("test_variable_two")).hasValue("'test_attribute_value_two'").isTypeOf(RuleValueType.TEXT).hasCandidates("test_attribute_value_two");
}
Also used : RuleEnrollment(org.hisp.dhis.rules.models.RuleEnrollment) RuleEvent(org.hisp.dhis.rules.models.RuleEvent) Date(java.util.Date) RuleVariable(org.hisp.dhis.rules.models.RuleVariable) Test(org.junit.Test)

Example 2 with RuleVariable

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

the class RuleVariableValueMapBuilderShould method thrown_illegal_state_exception_on_duplicate_events.

@Test
public void thrown_illegal_state_exception_on_duplicate_events() {
    RuleEvent ruleEvent = RuleEvent.create("test_event_two", "test_program_stage", RuleEvent.Status.ACTIVE, new Date(), new Date(), new ArrayList<RuleDataValue>());
    try {
        RuleVariableValueMapBuilder.target(ruleEvent).ruleVariables(new ArrayList<RuleVariable>()).ruleEvents(Arrays.asList(ruleEvent)).build();
        fail("IllegalStateException was expected, but nothing was thrown.");
    } catch (IllegalStateException illegalStateException) {
    // noop
    }
}
Also used : RuleDataValue(org.hisp.dhis.rules.models.RuleDataValue) RuleEvent(org.hisp.dhis.rules.models.RuleEvent) Date(java.util.Date) RuleVariable(org.hisp.dhis.rules.models.RuleVariable) Test(org.junit.Test)

Example 3 with RuleVariable

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

the class RuleVariableValueMapBuilderShould method return_values_from_newest_event_added_in_first_event.

@Test
public void return_values_from_newest_event_added_in_first_event() throws ParseException {
    RuleVariable ruleVariable = RuleVariableNewestStageEvent.create("test_variable", "test_dataelement", "test_program_stage_one", RuleValueType.TEXT);
    Date dateEventOne = dateFormat.parse("2014-02-03");
    Date dateEventTwo = dateFormat.parse("2014-03-03");
    Date dateEventThree = dateFormat.parse("2015-02-03");
    Date dateEventCurrent = dateFormat.parse("2011-02-03");
    Date dateEventDueCurrent = dateFormat.parse("2011-02-03");
    RuleEvent eventOne = RuleEvent.create("test_event_uid_one", "test_program_stage_one", RuleEvent.Status.ACTIVE, dateEventOne, dateEventOne, Arrays.asList(RuleDataValue.create(dateEventOne, "test_program_stage_one", "test_dataelement", "test_value_one")));
    RuleEvent eventTwo = RuleEvent.create("test_event_uid_two", "test_program_stage_two", RuleEvent.Status.ACTIVE, dateEventTwo, dateEventTwo, Arrays.asList(RuleDataValue.create(dateEventTwo, "test_program_stage_two", "test_dataelement", "test_value_two")));
    RuleEvent eventThree = RuleEvent.create("test_event_uid_three", "test_program_stage_two", RuleEvent.Status.ACTIVE, dateEventThree, dateEventThree, Arrays.asList(RuleDataValue.create(dateEventThree, "test_program_stage_two", "test_dataelement", "test_value_three")));
    RuleEvent eventCurrent = RuleEvent.create("test_event_uid_current", "test_program_stage_one", RuleEvent.Status.ACTIVE, dateEventCurrent, dateEventDueCurrent, Arrays.asList(RuleDataValue.create(dateEventCurrent, "test_program_stage_one", "test_dataelement", "test_value_current")));
    Map<String, RuleVariableValue> valueMap = RuleVariableValueMapBuilder.target(eventCurrent).ruleVariables(Arrays.asList(ruleVariable)).ruleEvents(Arrays.asList(eventOne, eventTwo, eventThree)).build();
    assertThat(valueMap.size()).isEqualTo(6);
    assertThatVariable(valueMap.get("current_date")).hasValue(wrap(dateFormat.format(new Date()))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(new Date()));
    assertThatVariable(valueMap.get("event_date")).hasValue(wrap(dateFormat.format(dateEventCurrent))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(dateEventCurrent));
    assertThatVariable(valueMap.get("event_count")).hasValue("4").isTypeOf(RuleValueType.NUMERIC).hasCandidates("4");
    assertThatVariable(valueMap.get("event_id")).hasValue("'test_event_uid_current'").isTypeOf(RuleValueType.TEXT).hasCandidates("test_event_uid_current");
    assertThatVariable(valueMap.get("due_date")).hasValue(wrap(dateFormat.format(dateEventDueCurrent))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(dateEventDueCurrent));
    assertThatVariable(valueMap.get("test_variable")).hasValue("'test_value_one'").isTypeOf(RuleValueType.TEXT).hasCandidates("test_value_one", "test_value_current");
}
Also used : RuleEvent(org.hisp.dhis.rules.models.RuleEvent) Date(java.util.Date) RuleVariable(org.hisp.dhis.rules.models.RuleVariable) Test(org.junit.Test)

Example 4 with RuleVariable

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

the class RuleVariableValueMapBuilderShould method return_values_from_newest_event_added_in_target.

@Test
public void return_values_from_newest_event_added_in_target() throws ParseException {
    RuleVariable ruleVariableOne = RuleVariableNewestEvent.create("test_variable_one", "test_dataelement_one", RuleValueType.TEXT);
    RuleVariable ruleVariableTwo = RuleVariableNewestEvent.create("test_variable_two", "test_dataelement_two", RuleValueType.TEXT);
    Date dateEventOne = dateFormat.parse("2013-01-01");
    Date dateEventTwo = dateFormat.parse("2014-01-01");
    Date dateEventCurrent = dateFormat.parse("2015-01-01");
    Date dateEventDueCurrent = dateFormat.parse("2016-01-01");
    RuleEvent firstRuleEvent = RuleEvent.create("test_event_uid_one", "test_program_stage", RuleEvent.Status.ACTIVE, dateEventOne, dateEventOne, Arrays.asList(RuleDataValue.create(dateEventOne, "test_program_stage", "test_dataelement_one", "test_value_dataelement_one_first"), RuleDataValue.create(dateEventOne, "test_program_stage", "test_dataelement_two", "test_value_dataelement_two_first")));
    RuleEvent secondRuleEvent = RuleEvent.create("test_event_uid_two", "test_program_stage", RuleEvent.Status.ACTIVE, dateEventTwo, dateEventTwo, Arrays.asList(RuleDataValue.create(dateEventTwo, "test_program_stage", "test_dataelement_one", "test_value_dataelement_one_second"), RuleDataValue.create(dateEventTwo, "test_program_stage", "test_dataelement_two", "test_value_dataelement_two_second")));
    RuleEvent currentEvent = RuleEvent.create("test_event_uid_current", "test_program_stage", RuleEvent.Status.ACTIVE, dateEventCurrent, dateEventDueCurrent, Arrays.asList(RuleDataValue.create(dateEventCurrent, "test_program_stage", "test_dataelement_one", "test_value_dataelement_one_current"), RuleDataValue.create(dateEventCurrent, "test_program_stage", "test_dataelement_two", "test_value_dataelement_two_current")));
    Map<String, RuleVariableValue> valueMap = RuleVariableValueMapBuilder.target(currentEvent).ruleVariables(Arrays.asList(ruleVariableOne, ruleVariableTwo)).ruleEvents(Arrays.asList(firstRuleEvent, secondRuleEvent)).build();
    assertThat(valueMap.size()).isEqualTo(7);
    assertThatVariable(valueMap.get("current_date")).hasValue(wrap(dateFormat.format(new Date()))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(new Date()));
    assertThatVariable(valueMap.get("event_date")).hasValue(wrap(dateFormat.format(dateEventCurrent))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(dateEventCurrent));
    assertThatVariable(valueMap.get("event_count")).hasValue("3").isTypeOf(RuleValueType.NUMERIC).hasCandidates("3");
    assertThatVariable(valueMap.get("event_id")).hasValue("'test_event_uid_current'").isTypeOf(RuleValueType.TEXT).hasCandidates("test_event_uid_current");
    assertThatVariable(valueMap.get("due_date")).hasValue(wrap(dateFormat.format(dateEventDueCurrent))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(dateEventDueCurrent));
    assertThatVariable(valueMap.get("test_variable_one")).hasValue("'test_value_dataelement_one_current'").isTypeOf(RuleValueType.TEXT).hasCandidates("test_value_dataelement_one_current", "test_value_dataelement_one_second", "test_value_dataelement_one_first");
    assertThatVariable(valueMap.get("test_variable_two")).hasValue("'test_value_dataelement_two_current'").isTypeOf(RuleValueType.TEXT).hasCandidates("test_value_dataelement_two_current", "test_value_dataelement_two_second", "test_value_dataelement_two_first");
}
Also used : RuleEvent(org.hisp.dhis.rules.models.RuleEvent) Date(java.util.Date) RuleVariable(org.hisp.dhis.rules.models.RuleVariable) Test(org.junit.Test)

Example 5 with RuleVariable

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

the class RuleVariableValueMapBuilder method buildRuleVariableValues.

private void buildRuleVariableValues(@Nonnull Map<String, RuleVariableValue> valueMap) {
    for (RuleVariable ruleVariable : ruleVariables) {
        if (ruleVariable instanceof RuleVariableAttribute) {
            RuleVariableAttribute ruleVariableAttribute = (RuleVariableAttribute) ruleVariable;
            createAttributeVariableValue(valueMap, ruleVariableAttribute);
        } else if (ruleVariable instanceof RuleVariableCurrentEvent) {
            RuleVariableCurrentEvent currentEventVariable = (RuleVariableCurrentEvent) ruleVariable;
            createCurrentEventVariableValue(valueMap, currentEventVariable);
        } else if (ruleVariable instanceof RuleVariablePreviousEvent) {
            RuleVariablePreviousEvent ruleVariablePreviousEvent = (RuleVariablePreviousEvent) ruleVariable;
            createPreviousEventVariableValue(valueMap, ruleVariablePreviousEvent);
        } else if (ruleVariable instanceof RuleVariableNewestEvent) {
            RuleVariableNewestEvent ruleVariableNewestEvent = (RuleVariableNewestEvent) ruleVariable;
            createNewestEventVariableValue(valueMap, ruleVariableNewestEvent);
        } else if (ruleVariable instanceof RuleVariableNewestStageEvent) {
            RuleVariableNewestStageEvent ruleVariableNewestEvent = (RuleVariableNewestStageEvent) ruleVariable;
            createNewestStageEventVariableValue(valueMap, ruleVariableNewestEvent);
        } else {
            throw new IllegalArgumentException("Unsupported RuleVariable type: " + ruleVariable.getClass());
        }
    }
}
Also used : RuleVariablePreviousEvent(org.hisp.dhis.rules.models.RuleVariablePreviousEvent) RuleVariableNewestStageEvent(org.hisp.dhis.rules.models.RuleVariableNewestStageEvent) RuleVariableAttribute(org.hisp.dhis.rules.models.RuleVariableAttribute) RuleVariableCurrentEvent(org.hisp.dhis.rules.models.RuleVariableCurrentEvent) RuleVariableNewestEvent(org.hisp.dhis.rules.models.RuleVariableNewestEvent) RuleVariable(org.hisp.dhis.rules.models.RuleVariable)

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