Search in sources :

Example 11 with RuleEvent

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

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

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

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

the class ProgramRuleEntityMapperServiceTest method testMappedRuleEventsWithFilter.

@Test
void testMappedRuleEventsWithFilter() {
    when(dataElementService.getDataElement(anyString())).thenReturn(dataElement);
    List<RuleEvent> ruleEvents = subject.toMappedRuleEvents(Sets.newHashSet(programStageInstanceA, programStageInstanceB), programStageInstanceA);
    assertEquals(ruleEvents.size(), 1);
    RuleEvent ruleEvent = ruleEvents.get(0);
    assertEquals(ruleEvent.event(), programStageInstanceB.getUid());
    assertEquals(ruleEvent.programStage(), programStageInstanceB.getProgramStage().getUid());
    assertEquals(ruleEvent.organisationUnit(), programStageInstanceB.getOrganisationUnit().getUid());
    assertEquals(ruleEvent.organisationUnitCode(), programStageInstanceB.getOrganisationUnit().getCode());
    assertEquals(ruleEvent.programStageName(), programStageInstanceB.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(), programStageInstanceB.getProgramStage().getUid());
    assertEquals(ruleDataValue.value(), SAMPLE_VALUE_B);
}
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)

Example 15 with RuleEvent

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

the class RuleEngineShould method throw_illegal_state_exception_when_evaluate_if_event_is_already_in_context.

@Test
public void throw_illegal_state_exception_when_evaluate_if_event_is_already_in_context() {
    RuleEvent ruleEvent = RuleEvent.create("test_event", "test_programstage", RuleEvent.Status.ACTIVE, new Date(), new Date(), new ArrayList<RuleDataValue>());
    List<RuleEvent> ruleEvents = new ArrayList<>();
    ruleEvents.add(ruleEvent);
    RuleEngine ruleEngine = ruleEngineContext.toEngineBuilder().events(ruleEvents).build();
    try {
        ruleEngine.evaluate(ruleEvent);
        fail("IllegalStateException was expected, but nothing was thrown.");
    } catch (IllegalStateException illegalStateException) {
    // noop
    }
}
Also used : RuleDataValue(org.hisp.dhis.rules.models.RuleDataValue) ArrayList(java.util.ArrayList) 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