use of org.hisp.dhis.rules.models.RuleDataValue in project dhis2-android-sdk by dhis2.
the class RuleEngineValueTypesShould method fallback_to_default_numeric_value_on_numeric_variable_without_value.
@Test
public void fallback_to_default_numeric_value_on_numeric_variable_without_value() 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.NUMERIC);
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("0.0");
assertThat(ruleEffects.get(0).ruleAction()).isEqualTo(ruleAction);
}
use of org.hisp.dhis.rules.models.RuleDataValue in project dhis2-android-sdk by dhis2.
the class RuleEngineValueTypesShould method fallback_to_default_text_value_on_variable_without_value.
@Test
public void fallback_to_default_text_value_on_variable_without_value() 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.TEXT);
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("");
assertThat(ruleEffects.get(0).ruleAction()).isEqualTo(ruleAction);
}
use of org.hisp.dhis.rules.models.RuleDataValue in project dhis2-core by dhis2.
the class ProgramRuleEntityMapperServiceTest method testMappedRuleEventsWithFilter.
@Test
void testMappedRuleEventsWithFilter() {
when(dataElementService.getDataElement(anyString())).thenReturn(dataElement);
List<RuleEvent> ruleEvents = subject.toMappedRuleEvents(Sets.newHashSet(programStageInstanceA, programStageInstanceB), programStageInstanceA);
assertEquals(ruleEvents.size(), 1);
RuleEvent ruleEvent = ruleEvents.get(0);
assertEquals(ruleEvent.event(), programStageInstanceB.getUid());
assertEquals(ruleEvent.programStage(), programStageInstanceB.getProgramStage().getUid());
assertEquals(ruleEvent.organisationUnit(), programStageInstanceB.getOrganisationUnit().getUid());
assertEquals(ruleEvent.organisationUnitCode(), programStageInstanceB.getOrganisationUnit().getCode());
assertEquals(ruleEvent.programStageName(), programStageInstanceB.getProgramStage().getName());
assertEquals(ruleEvent.dataValues().size(), 1);
RuleDataValue ruleDataValue = ruleEvent.dataValues().get(0);
assertEquals(ruleDataValue.dataElement(), dataElement.getUid());
assertEquals(ruleDataValue.eventDate(), ruleEvent.eventDate());
assertEquals(ruleDataValue.programStage(), programStageInstanceB.getProgramStage().getUid());
assertEquals(ruleDataValue.value(), SAMPLE_VALUE_B);
}
use of org.hisp.dhis.rules.models.RuleDataValue in project dhis2-android-sdk by dhis2.
the class RuleEngineShould method throw_illegal_state_exception_when_evaluate_if_event_is_already_in_context.
@Test
public void throw_illegal_state_exception_when_evaluate_if_event_is_already_in_context() {
RuleEvent ruleEvent = RuleEvent.create("test_event", "test_programstage", RuleEvent.Status.ACTIVE, new Date(), new Date(), new ArrayList<RuleDataValue>());
List<RuleEvent> ruleEvents = new ArrayList<>();
ruleEvents.add(ruleEvent);
RuleEngine ruleEngine = ruleEngineContext.toEngineBuilder().events(ruleEvents).build();
try {
ruleEngine.evaluate(ruleEvent);
fail("IllegalStateException was expected, but nothing was thrown.");
} catch (IllegalStateException illegalStateException) {
// noop
}
}
use of org.hisp.dhis.rules.models.RuleDataValue in project dhis2-android-sdk by dhis2.
the class RuleVariableValueMapBuilderShould method propagate_to_map_correctly_the_given_rule_enrollment_values.
@Test
public void propagate_to_map_correctly_the_given_rule_enrollment_values() throws ParseException {
RuleVariable ruleVariableOne = RuleVariableAttribute.create("test_variable_one", "test_attribute_one", RuleValueType.NUMERIC);
RuleVariable ruleVariableTwo = RuleVariableAttribute.create("test_variable_two", "test_attribute_two", RuleValueType.TEXT);
RuleVariable ruleVariableThree = RuleVariableCurrentEvent.create("test_variable_three", "test_dataelement_one", RuleValueType.BOOLEAN);
String currentDate = dateFormat.format(new Date());
Date enrollmentDate = dateFormat.parse("2017-02-02");
Date incidentDate = dateFormat.parse("2017-04-02");
RuleEnrollment ruleEnrollment = RuleEnrollment.create("test_enrollment", incidentDate, enrollmentDate, RuleEnrollment.Status.ACTIVE, Arrays.asList(RuleAttributeValue.create("test_attribute_one", "test_attribute_value_one"), RuleAttributeValue.create("test_attribute_two", "test_attribute_value_two"), RuleAttributeValue.create("test_attribute_three", "test_attribute_value_three")));
RuleEvent ruleEventOne = RuleEvent.create("test_event_one", "test_program_stage", RuleEvent.Status.ACTIVE, new Date(), new Date(), new ArrayList<RuleDataValue>());
RuleEvent ruleEventTwo = RuleEvent.create("test_event_two", "test_program_stage", RuleEvent.Status.ACTIVE, new Date(), new Date(), new ArrayList<RuleDataValue>());
Map<String, RuleVariableValue> valueMap = RuleVariableValueMapBuilder.target(ruleEnrollment).ruleVariables(Arrays.asList(ruleVariableOne, ruleVariableTwo, ruleVariableThree)).ruleEvents(Arrays.asList(ruleEventOne, ruleEventTwo)).build();
assertThat(valueMap.size()).isEqualTo(9);
assertThatVariable(valueMap.get("current_date")).hasValue(wrap(currentDate)).isTypeOf(RuleValueType.TEXT).hasCandidates(currentDate);
assertThatVariable(valueMap.get("event_count")).hasValue("2").isTypeOf(RuleValueType.NUMERIC).hasCandidates("2");
assertThatVariable(valueMap.get("enrollment_date")).hasValue(wrap(dateFormat.format(enrollmentDate))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(enrollmentDate));
assertThatVariable(valueMap.get("enrollment_id")).hasValue("'test_enrollment'").isTypeOf(RuleValueType.TEXT).hasCandidates("test_enrollment");
assertThatVariable(valueMap.get("enrollment_count")).hasValue("1").isTypeOf(RuleValueType.NUMERIC).hasCandidates("1");
assertThatVariable(valueMap.get("incident_date")).hasValue(wrap(dateFormat.format(incidentDate))).isTypeOf(RuleValueType.TEXT).hasCandidates(dateFormat.format(incidentDate));
assertThatVariable(valueMap.get("tei_count")).hasValue("1").isTypeOf(RuleValueType.NUMERIC).hasCandidates("1");
assertThatVariable(valueMap.get("test_variable_one")).hasValue("test_attribute_value_one").isTypeOf(RuleValueType.NUMERIC).hasCandidates("test_attribute_value_one");
assertThatVariable(valueMap.get("test_variable_two")).isTypeOf(RuleValueType.TEXT).hasValue("'test_attribute_value_two'").hasCandidates("test_attribute_value_two");
}
Aggregations