Search in sources :

Example 16 with RuleVariable

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

the class RuleVariableValueMapBuilderShould method contain_value_of_newest_rule_event.

@Test
public void contain_value_of_newest_rule_event() 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 oldestEventDate = dateFormat.parse("2013-01-01");
    Date newestEventDate = dateFormat.parse("2017-01-01");
    Date currentEventDate = dateFormat.parse("2015-01-01");
    Date currentEventDueDate = dateFormat.parse("2016-01-01");
    RuleEvent oldestRuleEvent = RuleEvent.create("test_event_uid_oldest", "test_program_stage", RuleEvent.Status.ACTIVE, oldestEventDate, oldestEventDate, Arrays.asList(RuleDataValue.create(oldestEventDate, "test_program_stage", "test_dataelement_one", "test_value_one_oldest"), RuleDataValue.create(oldestEventDate, "test_program_stage", "test_dataelement_two", "test_value_two_oldest")));
    RuleEvent newestRuleEvent = RuleEvent.create("test_event_uid_newest", "test_program_stage", RuleEvent.Status.ACTIVE, newestEventDate, newestEventDate, Arrays.asList(RuleDataValue.create(newestEventDate, "test_program_stage", "test_dataelement_one", "test_value_one_newest"), RuleDataValue.create(newestEventDate, "test_program_stage", "test_dataelement_two", "test_value_two_newest")));
    RuleEvent currentEvent = RuleEvent.create("test_event_uid_current", "test_program_stage", RuleEvent.Status.ACTIVE, currentEventDate, currentEventDueDate, Arrays.asList(RuleDataValue.create(currentEventDate, "test_program_stage", "test_dataelement_one", "test_value_one_current"), RuleDataValue.create(currentEventDate, "test_program_stage", "test_dataelement_two", "test_value_two_current")));
    Map<String, RuleVariableValue> valueMap = RuleVariableValueMapBuilder.target(currentEvent).ruleVariables(Arrays.asList(ruleVariableOne, ruleVariableTwo)).ruleEvents(Arrays.asList(oldestRuleEvent, newestRuleEvent)).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(currentEventDate))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(currentEventDate));
    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(currentEventDueDate))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(currentEventDueDate));
    assertThatVariable(valueMap.get("test_variable_one")).hasValue("'test_value_one_newest'").isTypeOf(RuleValueType.TEXT).hasCandidates("test_value_one_newest", "test_value_one_current", "test_value_one_oldest");
    assertThatVariable(valueMap.get("test_variable_two")).hasValue("'test_value_two_newest'").isTypeOf(RuleValueType.TEXT).hasCandidates("test_value_two_newest", "test_value_two_current", "test_value_two_oldest");
}
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 17 with RuleVariable

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

the class RuleEngineFunctionShould method return_expected_values_on_nested_functions_calls.

@Test
public void return_expected_values_on_nested_functions_calls() throws Exception {
    RuleAction ruleAction = RuleActionDisplayKeyValuePair.createForFeedback("test_action_content", "d2:floor(#{test_var_one} + d2:ceil(#{test_var_three})) " + "/ 5 * d2:ceil(#{test_var_two})");
    RuleVariable ruleVariableOne = RuleVariableCurrentEvent.create("test_var_one", "test_data_element_one", RuleValueType.NUMERIC);
    RuleVariable ruleVariableTwo = RuleVariableCurrentEvent.create("test_var_two", "test_data_element_two", RuleValueType.NUMERIC);
    RuleVariable ruleVariableThree = RuleVariableCurrentEvent.create("test_var_three", "test_data_element_three", RuleValueType.NUMERIC);
    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, ruleVariableThree)).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", "19.9"), RuleDataValue.create(new Date(), "test_program_stage", "test_data_element_two", "0.9"), RuleDataValue.create(new Date(), "test_program_stage", "test_data_element_three", "10.6")));
    List<RuleEffect> ruleEffects = ruleEngine.evaluate(ruleEvent).call();
    assertThat(ruleEffects.size()).isEqualTo(1);
    assertThat(ruleEffects.get(0).data()).isEqualTo("6.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 18 with RuleVariable

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

the class RuleEngineFunctionShould method return_true_if_evaluate_value_specified_has_value.

@Test
public void return_true_if_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(), 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("true");
    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 19 with RuleVariable

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

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

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