use of org.hisp.dhis.rules.models.RuleAction 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);
}
use of org.hisp.dhis.rules.models.RuleAction 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);
}
use of org.hisp.dhis.rules.models.RuleAction 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);
}
use of org.hisp.dhis.rules.models.RuleAction 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);
}
use of org.hisp.dhis.rules.models.RuleAction 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);
}
Aggregations