Search in sources :

Example 21 with RuleEffect

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

the class ProgramRuleEngineServiceTest method testWithImplementableActionExist_programInstance.

@Test
void testWithImplementableActionExist_programInstance() {
    doAnswer(invocationOnMock -> {
        ruleEffects.add((RuleEffect) invocationOnMock.getArguments()[0]);
        return ruleEffects;
    }).when(ruleActionSendMessage).implement(any(), any(ProgramInstance.class));
    List<RuleEffect> effects = new ArrayList<>();
    effects.add(RuleEffect.create("", RuleActionSendMessage.create(NOTIFICATION_UID, DATA)));
    when(programInstanceService.getProgramInstance(anyLong())).thenReturn(programInstance);
    when(programRuleEngine.evaluate(any(), any())).thenReturn(effects);
    setProgramRuleActionType_SendMessage();
    ArgumentCaptor<ProgramInstance> argumentCaptor = ArgumentCaptor.forClass(ProgramInstance.class);
    List<RuleEffect> ruleEffects = service.evaluateEnrollmentAndRunEffects(programInstance.getId());
    assertEquals(1, ruleEffects.size());
    RuleAction action = ruleEffects.get(0).ruleAction();
    if (action instanceof RuleActionSendMessage) {
        RuleActionSendMessage ruleActionSendMessage = (RuleActionSendMessage) action;
        assertEquals(NOTIFICATION_UID, ruleActionSendMessage.notification());
    }
    verify(programRuleEngine, times(1)).evaluate(argumentCaptor.capture(), any());
    assertEquals(programInstance, argumentCaptor.getValue());
    verify(ruleActionSendMessage).accept(action);
    verify(ruleActionSendMessage).implement(any(RuleEffect.class), argumentCaptor.capture());
    assertEquals(1, this.ruleEffects.size());
    assertTrue(this.ruleEffects.get(0).ruleAction() instanceof RuleActionSendMessage);
}
Also used : RuleEffect(org.hisp.dhis.rules.models.RuleEffect) RuleActionSendMessage(org.hisp.dhis.rules.models.RuleActionSendMessage) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramRuleAction(org.hisp.dhis.programrule.ProgramRuleAction) RuleAction(org.hisp.dhis.rules.models.RuleAction) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 22 with RuleEffect

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

the class TrackerRuleEngineThread method call.

@Override
public void call() {
    if (sideEffectDataBundle == null) {
        return;
    }
    Map<String, List<RuleEffect>> enrollmentRuleEffects = trackerSideEffectConverterService.toRuleEffects(sideEffectDataBundle.getEnrollmentRuleEffects());
    Map<String, List<RuleEffect>> eventRuleEffects = trackerSideEffectConverterService.toRuleEffects(sideEffectDataBundle.getEventRuleEffects());
    for (RuleActionImplementer ruleActionImplementer : ruleActionImplementers) {
        for (Map.Entry<String, List<RuleEffect>> entry : enrollmentRuleEffects.entrySet()) {
            ProgramInstance pi = sideEffectDataBundle.getProgramInstance();
            pi.setProgram(sideEffectDataBundle.getProgram());
            entry.getValue().stream().filter(effect -> ruleActionImplementer.accept(effect.ruleAction())).forEach(effect -> ruleActionImplementer.implement(effect, pi));
        }
        for (Map.Entry<String, List<RuleEffect>> entry : eventRuleEffects.entrySet()) {
            ProgramStageInstance psi = sideEffectDataBundle.getProgramStageInstance();
            psi.getProgramStage().setProgram(sideEffectDataBundle.getProgram());
            entry.getValue().stream().filter(effect -> ruleActionImplementer.accept(effect.ruleAction())).forEach(effect -> ruleActionImplementer.implement(effect, psi));
        }
    }
    notifier.notify(sideEffectDataBundle.getJobConfiguration(), "Tracker Rule-engine side effects completed");
}
Also used : RuleActionImplementer(org.hisp.dhis.programrule.engine.RuleActionImplementer) RuleEffect(org.hisp.dhis.rules.models.RuleEffect) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) TrackerSideEffectConverterService(org.hisp.dhis.tracker.converter.TrackerSideEffectConverterService) Scope(org.springframework.context.annotation.Scope) Notifier(org.hisp.dhis.system.notification.Notifier) List(java.util.List) Component(org.springframework.stereotype.Component) Lists(com.google.common.collect.Lists) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Qualifier(org.springframework.beans.factory.annotation.Qualifier) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) SecurityContextRunnable(org.hisp.dhis.security.SecurityContextRunnable) RuleActionImplementer(org.hisp.dhis.programrule.engine.RuleActionImplementer) ProgramInstance(org.hisp.dhis.program.ProgramInstance) List(java.util.List) Map(java.util.Map) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

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