Search in sources :

Example 11 with RuleEffect

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

the class RuleEngineEffectTypesShould method return_display_key_value_pair_effect_after_create_simple_condition.

@Test
public void return_display_key_value_pair_effect_after_create_simple_condition() throws Exception {
    RuleAction ruleAction = RuleActionDisplayKeyValuePair.createForFeedback("test_action_content", "2 + 2");
    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 12 with RuleEffect

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

the class RuleEngineEffectTypesShould method return_display_text_effect_after_create_simple_condition.

@Test
public void return_display_text_effect_after_create_simple_condition() throws Exception {
    RuleAction ruleAction = RuleActionDisplayText.createForFeedback("test_action_content", "2 + 2");
    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 13 with RuleEffect

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

the class RuleEngineEffectTypesShould method return_set_mandatory_effect_after_create_simple_condition.

@Test
public void return_set_mandatory_effect_after_create_simple_condition() throws Exception {
    RuleAction ruleAction = RuleActionSetMandatoryField.create("test_data_element");
    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 14 with RuleEffect

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

the class RuleEngineEffectTypesShould method return_assigned_effect_when_create_simple_condition.

@Test
public void return_assigned_effect_when_create_simple_condition() throws Exception {
    RuleAction ruleAction = RuleActionAssign.create("test_action_content", "\'test_string\'", "test_data_element");
    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("test_string");
    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 15 with RuleEffect

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

Aggregations

RuleEffect (org.hisp.dhis.rules.models.RuleEffect)22 RuleAction (org.hisp.dhis.rules.models.RuleAction)19 Date (java.util.Date)18 RuleEngine (org.hisp.dhis.rules.RuleEngine)18 Rule (org.hisp.dhis.rules.models.Rule)18 RuleEvent (org.hisp.dhis.rules.models.RuleEvent)18 Test (org.junit.Test)18 RuleVariable (org.hisp.dhis.rules.models.RuleVariable)7 ProgramInstance (org.hisp.dhis.program.ProgramInstance)4 RuleDataValue (org.hisp.dhis.rules.models.RuleDataValue)4 Lists (com.google.common.collect.Lists)3 List (java.util.List)3 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)3 Qualifier (org.springframework.beans.factory.annotation.Qualifier)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Slf4j (lombok.extern.slf4j.Slf4j)2 SYSTEM_PROGRAM_RULE_SERVER_EXECUTION (org.hisp.dhis.external.conf.ConfigurationKey.SYSTEM_PROGRAM_RULE_SERVER_EXECUTION)2 DhisConfigurationProvider (org.hisp.dhis.external.conf.DhisConfigurationProvider)2 Program (org.hisp.dhis.program.Program)2 ProgramInstanceService (org.hisp.dhis.program.ProgramInstanceService)2