Search in sources :

Example 1 with RuleEvent

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

the class RuleEngineShould method propagate_immutable_events_list.

@Test
public void propagate_immutable_events_list() {
    RuleEvent ruleEventOne = mock(RuleEvent.class);
    RuleEvent ruleEventTwo = mock(RuleEvent.class);
    List<RuleEvent> ruleEvents = new ArrayList<>();
    ruleEvents.add(ruleEventOne);
    RuleEngine ruleEngine = ruleEngineContext.toEngineBuilder().events(ruleEvents).build();
    ruleEvents.add(ruleEventTwo);
    assertThat(ruleEngine.events().size()).isEqualTo(1);
    assertThat(ruleEngine.events().get(0)).isEqualTo(ruleEventOne);
    try {
        ruleEngine.events().clear();
        fail("UnsupportedOperationException was expected, but nothing was thrown.");
    } catch (UnsupportedOperationException unsupportedOperationException) {
    // noop
    }
}
Also used : ArrayList(java.util.ArrayList) RuleEvent(org.hisp.dhis.rules.models.RuleEvent) Test(org.junit.Test)

Example 2 with RuleEvent

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

the class RuleEngineShould method throw_illegal_argument_exception_when_evaluate_with_null_event.

@Test
public void throw_illegal_argument_exception_when_evaluate_with_null_event() {
    try {
        RuleEvent ruleEvent = null;
        ruleEngineContext.toEngineBuilder().build().evaluate(ruleEvent);
        fail("IllegalArgumentException was expected, but nothing was thrown.");
    } catch (IllegalArgumentException illegalArgumentException) {
    // noop
    }
}
Also used : RuleEvent(org.hisp.dhis.rules.models.RuleEvent) Test(org.junit.Test)

Example 3 with RuleEvent

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

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

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