Search in sources :

Example 1 with Rule

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

the class RuleEngineShould method not_fail_when_the_iteration_over_rule_list_is_concurrent.

@Test
public void not_fail_when_the_iteration_over_rule_list_is_concurrent() throws InterruptedException {
    final RuleEngine ruleEngine = RuleEngineContext.builder(mock(RuleExpressionEvaluator.class)).rules(Arrays.asList(mock(Rule.class), mock(Rule.class))).build().toEngineBuilder().build();
    final CountDownLatch threadOneLatch = new CountDownLatch(1);
    final CountDownLatch threadTwoLatch = new CountDownLatch(1);
    new Thread() {

        @Override
        public void run() {
            for (Rule rule : ruleEngine.executionContext().rules()) {
                try {
                    threadTwoLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                threadOneLatch.countDown();
            }
        }
    }.start();
    new Thread() {

        @Override
        public void run() {
            for (Rule rule : ruleEngine.executionContext().rules()) {
                threadTwoLatch.countDown();
                try {
                    threadOneLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
    assertThat(threadOneLatch.await(4, TimeUnit.SECONDS)).isTrue();
    assertThat(threadTwoLatch.await(4, TimeUnit.SECONDS)).isTrue();
}
Also used : Rule(org.hisp.dhis.rules.models.Rule) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with Rule

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

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

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

Example 5 with Rule

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

Aggregations

Rule (org.hisp.dhis.rules.models.Rule)20 Test (org.junit.Test)20 Date (java.util.Date)18 RuleEngine (org.hisp.dhis.rules.RuleEngine)18 RuleAction (org.hisp.dhis.rules.models.RuleAction)18 RuleEffect (org.hisp.dhis.rules.models.RuleEffect)18 RuleEvent (org.hisp.dhis.rules.models.RuleEvent)18 RuleVariable (org.hisp.dhis.rules.models.RuleVariable)8 RuleDataValue (org.hisp.dhis.rules.models.RuleDataValue)4 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1