Search in sources :

Example 6 with RuleEvent

use of org.hisp.dhis.rules.models.RuleEvent 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 7 with RuleEvent

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

the class RuleVariableValueMapBuilder method buildAllEventValues.

private void buildAllEventValues() {
    List<RuleEvent> events = new ArrayList<>(ruleEvents);
    if (ruleEvent != null) {
        // target event should be among the list of all
        // events in order to achieve correct behavior
        events.add(ruleEvent);
    }
    // sort list of events by eventDate:
    Collections.sort(events, RuleEvent.EVENT_DATE_COMPARATOR);
    // aggregating values by data element uid
    for (int i = 0; i < events.size(); i++) {
        RuleEvent ruleEvent = events.get(i);
        for (int j = 0; j < ruleEvent.dataValues().size(); j++) {
            RuleDataValue ruleDataValue = ruleEvent.dataValues().get(j);
            // push new list if it is not there for the given data element
            if (!allEventsValues.containsKey(ruleDataValue.dataElement())) {
                allEventsValues.put(ruleDataValue.dataElement(), // NOPMD
                new ArrayList<RuleDataValue>(events.size()));
            }
            // append data value to the list
            allEventsValues.get(ruleDataValue.dataElement()).add(ruleDataValue);
        }
    }
}
Also used : RuleDataValue(org.hisp.dhis.rules.models.RuleDataValue) ArrayList(java.util.ArrayList) RuleEvent(org.hisp.dhis.rules.models.RuleEvent)

Example 8 with RuleEvent

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

the class RuleEngineEffectTypesShould method return_warning_effect_after_create_simple_condition.

@Test
public void return_warning_effect_after_create_simple_condition() throws Exception {
    RuleAction ruleAction = RuleActionShowWarning.create("test_warning_message", null, "target_field");
    Rule rule = Rule.create(null, null, "true", Arrays.asList(ruleAction));
    RuleEngine ruleEngine = RuleEngineContext.builder(new DuktapeEvaluator(duktape)).rules(Arrays.asList(rule)).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", "test_value")));
    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) 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) Test(org.junit.Test)

Example 9 with RuleEvent

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

the class RuleEngineEffectTypesShould method return_on_completion_effect_after_create_simple_condition.

@Test
public void return_on_completion_effect_after_create_simple_condition() throws Exception {
    RuleAction ruleAction = RuleActionWarningOnCompletion.create("test_warning_message", "2 + 2", "target_field");
    Rule rule = Rule.create(null, null, "true", Arrays.asList(ruleAction));
    RuleEngine ruleEngine = RuleEngineContext.builder(new DuktapeEvaluator(duktape)).rules(Arrays.asList(rule)).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", "test_value")));
    List<RuleEffect> ruleEffects = ruleEngine.evaluate(ruleEvent).call();
    assertThat(ruleEffects.size()).isEqualTo(1);
    assertThat(ruleEffects.get(0).data()).isEqualTo("4.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) Test(org.junit.Test)

Example 10 with RuleEvent

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

the class RuleEngineEffectTypesShould method return_hide_section_effect_after_create_simple_condition.

@Test
public void return_hide_section_effect_after_create_simple_condition() throws Exception {
    RuleAction ruleAction = RuleActionHideSection.create("test_section");
    Rule rule = Rule.create(null, null, "true", Arrays.asList(ruleAction));
    RuleEngine ruleEngine = RuleEngineContext.builder(new DuktapeEvaluator(duktape)).rules(Arrays.asList(rule)).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", "test_value")));
    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) 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) Test(org.junit.Test)

Aggregations

RuleEvent (org.hisp.dhis.rules.models.RuleEvent)34 Test (org.junit.Test)31 Date (java.util.Date)28 RuleEngine (org.hisp.dhis.rules.RuleEngine)18 Rule (org.hisp.dhis.rules.models.Rule)18 RuleAction (org.hisp.dhis.rules.models.RuleAction)18 RuleEffect (org.hisp.dhis.rules.models.RuleEffect)18 RuleVariable (org.hisp.dhis.rules.models.RuleVariable)17 RuleDataValue (org.hisp.dhis.rules.models.RuleDataValue)10 ArrayList (java.util.ArrayList)3 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)2 RuleEnrollment (org.hisp.dhis.rules.models.RuleEnrollment)2 Test (org.junit.jupiter.api.Test)2