Search in sources :

Example 16 with RuleEffect

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

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

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

Example 19 with RuleEffect

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

the class DefaultProgramRuleEngineService method evaluateEventAndRunEffects.

private List<RuleEffect> evaluateEventAndRunEffects(ProgramStageInstance psi) {
    if (psi == null) {
        return Lists.newArrayList();
    }
    ProgramInstance programInstance = programInstanceService.getProgramInstance(psi.getProgramInstance().getId());
    List<RuleEffect> ruleEffects = programRuleEngine.evaluate(programInstance, psi, programInstance.getProgramStageInstances());
    for (RuleEffect effect : ruleEffects) {
        ruleActionImplementers.stream().filter(i -> i.accept(effect.ruleAction())).forEach(i -> {
            log.debug(String.format("Invoking action implementer: %s", i.getClass().getSimpleName()));
            i.implement(effect, psi);
        });
    }
    return ruleEffects;
}
Also used : DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) SYSTEM_PROGRAM_RULE_SERVER_EXECUTION(org.hisp.dhis.external.conf.ConfigurationKey.SYSTEM_PROGRAM_RULE_SERVER_EXECUTION) RuleEffect(org.hisp.dhis.rules.models.RuleEffect) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Program(org.hisp.dhis.program.Program) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) Qualifier(org.springframework.beans.factory.annotation.Qualifier) RuleValidationResult(org.hisp.dhis.rules.models.RuleValidationResult) ProgramService(org.hisp.dhis.program.ProgramService) Transactional(org.springframework.transaction.annotation.Transactional) RuleEffect(org.hisp.dhis.rules.models.RuleEffect) ProgramInstance(org.hisp.dhis.program.ProgramInstance)

Example 20 with RuleEffect

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

the class DefaultProgramRuleEngineService method evaluateEnrollmentAndRunEffects.

@Override
@Transactional
public List<RuleEffect> evaluateEnrollmentAndRunEffects(long enrollment) {
    if (config.isDisabled(SYSTEM_PROGRAM_RULE_SERVER_EXECUTION)) {
        return Lists.newArrayList();
    }
    ProgramInstance programInstance = programInstanceService.getProgramInstance(enrollment);
    if (programInstance == null) {
        return Lists.newArrayList();
    }
    List<RuleEffect> ruleEffects = programRuleEngine.evaluate(programInstance, programInstance.getProgramStageInstances());
    for (RuleEffect effect : ruleEffects) {
        ruleActionImplementers.stream().filter(i -> i.accept(effect.ruleAction())).forEach(i -> {
            log.debug(String.format("Invoking action implementer: %s", i.getClass().getSimpleName()));
            i.implement(effect, programInstance);
        });
    }
    return ruleEffects;
}
Also used : DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) SYSTEM_PROGRAM_RULE_SERVER_EXECUTION(org.hisp.dhis.external.conf.ConfigurationKey.SYSTEM_PROGRAM_RULE_SERVER_EXECUTION) RuleEffect(org.hisp.dhis.rules.models.RuleEffect) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Program(org.hisp.dhis.program.Program) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) Qualifier(org.springframework.beans.factory.annotation.Qualifier) RuleValidationResult(org.hisp.dhis.rules.models.RuleValidationResult) ProgramService(org.hisp.dhis.program.ProgramService) Transactional(org.springframework.transaction.annotation.Transactional) RuleEffect(org.hisp.dhis.rules.models.RuleEffect) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Transactional(org.springframework.transaction.annotation.Transactional)

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