Search in sources :

Example 11 with Expression

use of org.hisp.dhis.expression.Expression in project dhis2-core by dhis2.

the class ValidationRuleStoreTest method testGetValidationRuleCount.

@Test
public void testGetValidationRuleCount() {
    Set<DataElement> dataElementsA = new HashSet<>();
    dataElementsA.add(dataElementA);
    dataElementsA.add(dataElementB);
    Set<DataElement> dataElementsB = new HashSet<>();
    dataElementsB.add(dataElementC);
    dataElementsB.add(dataElementD);
    Set<DataElement> dataElementsD = new HashSet<>();
    dataElementsD.addAll(dataElementsA);
    dataElementsD.addAll(dataElementsB);
    Expression expression1 = new Expression("Expression1", "Expression1");
    Expression expression2 = new Expression("Expression2", "Expression2");
    Expression expression3 = new Expression("Expression3", "Expression3");
    expressionService.addExpression(expression1);
    expressionService.addExpression(expression2);
    expressionService.addExpression(expression3);
    ValidationRule ruleA = createValidationRule('A', equal_to, expression1, expression3, periodType);
    ValidationRule ruleB = createValidationRule('B', equal_to, expression2, expression3, periodType);
    ValidationRule ruleC = createValidationRule('C', equal_to, expression3, expression3, periodType);
    validationRuleStore.save(ruleA);
    validationRuleStore.save(ruleB);
    validationRuleStore.save(ruleC);
    assertNotNull(validationRuleStore.getCount());
    assertEquals(3, validationRuleStore.getCount());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Expression(org.hisp.dhis.expression.Expression) HashSet(java.util.HashSet) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 12 with Expression

use of org.hisp.dhis.expression.Expression in project dhis2-core by dhis2.

the class PredictorServiceTest method setUpTest.

// -------------------------------------------------------------------------
// Fixture
// -------------------------------------------------------------------------
@Override
public void setUpTest() throws Exception {
    orgUnitLevel1 = new OrganisationUnitLevel(1, "Level1");
    organisationUnitService.addOrganisationUnitLevel(orgUnitLevel1);
    dataElementA = createDataElement('A');
    dataElementB = createDataElement('B');
    dataElementC = createDataElement('C');
    dataElementD = createDataElement('D');
    dataElementX = createDataElement('X', ValueType.NUMBER, AggregationType.NONE);
    dataElementService.addDataElement(dataElementA);
    dataElementService.addDataElement(dataElementB);
    dataElementService.addDataElement(dataElementC);
    dataElementService.addDataElement(dataElementD);
    dataElementService.addDataElement(dataElementX);
    dataElements = new HashSet<>();
    dataElements.add(dataElementA);
    dataElements.add(dataElementB);
    dataElements.add(dataElementC);
    dataElements.add(dataElementD);
    sourceA = createOrganisationUnit('A');
    sourceB = createOrganisationUnit('B');
    sourceC = createOrganisationUnit('C', sourceB);
    sourceD = createOrganisationUnit('D', sourceB);
    sourceE = createOrganisationUnit('E', sourceD);
    sourceF = createOrganisationUnit('F', sourceD);
    sourceG = createOrganisationUnit('G');
    organisationUnitService.addOrganisationUnit(sourceA);
    organisationUnitService.addOrganisationUnit(sourceB);
    organisationUnitService.addOrganisationUnit(sourceC);
    organisationUnitService.addOrganisationUnit(sourceD);
    organisationUnitService.addOrganisationUnit(sourceE);
    organisationUnitService.addOrganisationUnit(sourceF);
    organisationUnitService.addOrganisationUnit(sourceG);
    periodTypeMonthly = PeriodType.getPeriodTypeByName("Monthly");
    dataSetMonthly = createDataSet('M', periodTypeMonthly);
    dataSetMonthly.addDataSetElement(dataElementA);
    dataSetMonthly.addDataSetElement(dataElementB);
    dataSetMonthly.addDataSetElement(dataElementC);
    dataSetMonthly.addDataSetElement(dataElementD);
    dataSetMonthly.addDataSetElement(dataElementX);
    dataSetMonthly.addOrganisationUnit(sourceA);
    dataSetMonthly.addOrganisationUnit(sourceB);
    dataSetMonthly.addOrganisationUnit(sourceC);
    dataSetMonthly.addOrganisationUnit(sourceD);
    dataSetMonthly.addOrganisationUnit(sourceE);
    dataSetMonthly.addOrganisationUnit(sourceG);
    dataSetService.addDataSet(dataSetMonthly);
    DataElementCategoryOptionCombo categoryOptionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
    defaultCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
    altCategoryOption = new DataElementCategoryOption("AltCategoryOption");
    categoryService.addDataElementCategoryOption(altCategoryOption);
    altDataElementCategory = createDataElementCategory('A', altCategoryOption);
    categoryService.addDataElementCategory(altDataElementCategory);
    altDataElementCategoryCombo = createCategoryCombo('Y', altDataElementCategory);
    categoryService.addDataElementCategoryCombo(altDataElementCategoryCombo);
    altCombo = createCategoryOptionCombo('Z', altDataElementCategoryCombo, altCategoryOption);
    optionCombos = new HashSet<>();
    optionCombos.add(categoryOptionCombo);
    optionCombos.add(altCombo);
    categoryService.addDataElementCategoryOptionCombo(altCombo);
    expressionA = new Expression("AVG(#{" + dataElementA.getUid() + "})+1.5*STDDEV(#{" + dataElementA.getUid() + "})", "descriptionA");
    expressionB = new Expression("AVG(#{" + dataElementB.getUid() + "." + defaultCombo.getUid() + "})", "descriptionB");
    expressionC = new Expression("1234", "descriptionC");
    expressionD = new Expression(SYMBOL_DAYS, "descriptionD");
    expressionService.addExpression(expressionA);
    expressionService.addExpression(expressionB);
    expressionService.addExpression(expressionC);
    expressionService.addExpression(expressionD);
    Set<OrganisationUnit> units = newHashSet(sourceA, sourceB, sourceG);
    CurrentUserService mockCurrentUserService = new MockCurrentUserService(true, units, units);
    setDependency(predictorService, "currentUserService", mockCurrentUserService, CurrentUserService.class);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) Expression(org.hisp.dhis.expression.Expression) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 13 with Expression

use of org.hisp.dhis.expression.Expression in project dhis2-core by dhis2.

the class PredictorStoreTest method testGetPredictorCount.

@Test
public void testGetPredictorCount() {
    Set<DataElement> dataElementsA = new HashSet<>();
    dataElementsA.add(dataElementA);
    dataElementsA.add(dataElementB);
    Set<DataElement> dataElementsB = new HashSet<>();
    dataElementsB.add(dataElementC);
    dataElementsB.add(dataElementD);
    Set<DataElement> dataElementsD = new HashSet<>();
    dataElementsD.addAll(dataElementsA);
    dataElementsD.addAll(dataElementsB);
    Expression expression1 = new Expression("Expression1", "Expression1");
    Expression expression2 = new Expression("Expression2", "Expression2");
    Expression expression3 = new Expression("Expression3", "Expression3");
    expressionService.addExpression(expression1);
    expressionService.addExpression(expression2);
    expressionService.addExpression(expression3);
    Predictor predictorA = createPredictor(dataElementX, defaultCombo, "A", expressionA, expressionB, periodType, orgUnitLevel1, 6, 1, 0);
    Predictor predictorB = createPredictor(dataElementX, defaultCombo, "B", expressionA, expressionB, periodType, orgUnitLevel1, 6, 1, 0);
    Predictor predictorC = createPredictor(dataElementX, defaultCombo, "C", expressionA, expressionB, periodType, orgUnitLevel1, 6, 1, 0);
    predictorStore.save(predictorA);
    predictorStore.save(predictorB);
    predictorStore.save(predictorC);
    assertNotNull(predictorStore.getCount());
    assertEquals(3, predictorStore.getCount());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Expression(org.hisp.dhis.expression.Expression) HashSet(java.util.HashSet) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 14 with Expression

use of org.hisp.dhis.expression.Expression in project dhis2-core by dhis2.

the class ValidationServiceTest method setUpTest.

// -------------------------------------------------------------------------
// Fixture
// -------------------------------------------------------------------------
@Override
public void setUpTest() throws Exception {
    CurrentUserService currentUserService = new MockCurrentUserService(allSources, null);
    setDependency(validationService, "currentUserService", currentUserService, CurrentUserService.class);
    periodTypeWeekly = new WeeklyPeriodType();
    periodTypeMonthly = new MonthlyPeriodType();
    periodTypeYearly = new YearlyPeriodType();
    dataElementA = createDataElement('A');
    dataElementB = createDataElement('B');
    dataElementC = createDataElement('C');
    dataElementD = createDataElement('D');
    dataElementE = createDataElement('E');
    dataElementService.addDataElement(dataElementA);
    dataElementService.addDataElement(dataElementB);
    dataElementService.addDataElement(dataElementC);
    dataElementService.addDataElement(dataElementD);
    dataElementService.addDataElement(dataElementE);
    optionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
    String suffix = SEPARATOR + optionCombo.getUid();
    optionCombos = new HashSet<>();
    optionCombos.add(optionCombo);
    expressionA = new Expression("#{" + dataElementA.getUid() + suffix + "} + #{" + dataElementB.getUid() + suffix + "}", "expressionA");
    expressionB = new Expression("#{" + dataElementC.getUid() + suffix + "} - #{" + dataElementD.getUid() + suffix + "}", "expressionB");
    expressionC = new Expression("#{" + dataElementB.getUid() + suffix + "} * 2", "expressionC");
    expressionD = new Expression("#{" + dataElementB.getUid() + suffix + "}", "expressionD");
    expressionE = new Expression("AVG(#{" + dataElementB.getUid() + suffix + "} * 1.5)", "expressionE");
    expressionF = new Expression("#{" + dataElementB.getUid() + suffix + "} / #{" + dataElementE.getUid() + suffix + "}", "expressionF");
    expressionG = new Expression("AVG(#{" + dataElementB.getUid() + suffix + "} * 1.5 / #{" + dataElementE.getUid() + suffix + "})", "expressionG");
    expressionH = new Expression("AVG(#{" + dataElementB.getUid() + suffix + "}) + 1.5*STDDEV(#{" + dataElementB.getUid() + suffix + "})", "expressionH");
    expressionI = new Expression("#{" + dataElementA.getUid() + suffix + "}", "expressionI");
    expressionJ = new Expression("#{" + dataElementB.getUid() + suffix + "}", "expressionJ");
    expressionK = new Expression("#{" + dataElementC.getUid() + "}", "expressionK", NEVER_SKIP);
    expressionL = new Expression("#{" + dataElementD.getUid() + "}", "expressionL", NEVER_SKIP);
    expressionP = new Expression(SYMBOL_DAYS, "expressionP", NEVER_SKIP);
    expressionQ = new Expression("#{" + dataElementE.getUid() + "}", "expressionQ", NEVER_SKIP);
    expressionService.addExpression(expressionA);
    expressionService.addExpression(expressionB);
    expressionService.addExpression(expressionC);
    expressionService.addExpression(expressionD);
    expressionService.addExpression(expressionE);
    expressionService.addExpression(expressionF);
    expressionService.addExpression(expressionG);
    expressionService.addExpression(expressionH);
    expressionService.addExpression(expressionI);
    expressionService.addExpression(expressionJ);
    expressionService.addExpression(expressionK);
    expressionService.addExpression(expressionL);
    expressionService.addExpression(expressionP);
    expressionService.addExpression(expressionQ);
    periodA = createPeriod(periodTypeMonthly, getDate(2000, 3, 1), getDate(2000, 3, 31));
    periodB = createPeriod(periodTypeMonthly, getDate(2000, 4, 1), getDate(2000, 4, 30));
    periodY = createPeriod(periodTypeYearly, getDate(2000, 1, 1), getDate(2000, 12, 31));
    dayInPeriodA = periodService.getDayInPeriod(periodA, new Date());
    dayInPeriodB = periodService.getDayInPeriod(periodB, new Date());
    dayInPeriodY = periodService.getDayInPeriod(periodY, new Date());
    dataSetWeekly = createDataSet('W', periodTypeWeekly);
    dataSetMonthly = createDataSet('M', periodTypeMonthly);
    dataSetYearly = createDataSet('Y', periodTypeYearly);
    sourceA = createOrganisationUnit('A');
    sourceB = createOrganisationUnit('B');
    sourceC = createOrganisationUnit('C', sourceB);
    sourceD = createOrganisationUnit('D', sourceB);
    sourceE = createOrganisationUnit('E', sourceD);
    sourceF = createOrganisationUnit('F', sourceD);
    sourceG = createOrganisationUnit('G');
    sourcesA.add(sourceA);
    sourcesA.add(sourceB);
    allSources.add(sourceA);
    allSources.add(sourceB);
    allSources.add(sourceC);
    allSources.add(sourceD);
    allSources.add(sourceE);
    allSources.add(sourceF);
    allSources.add(sourceG);
    dataSetMonthly.addOrganisationUnit(sourceA);
    dataSetMonthly.addOrganisationUnit(sourceB);
    dataSetMonthly.addOrganisationUnit(sourceC);
    dataSetMonthly.addOrganisationUnit(sourceD);
    dataSetMonthly.addOrganisationUnit(sourceE);
    dataSetMonthly.addOrganisationUnit(sourceF);
    dataSetWeekly.addOrganisationUnit(sourceB);
    dataSetWeekly.addOrganisationUnit(sourceC);
    dataSetWeekly.addOrganisationUnit(sourceD);
    dataSetWeekly.addOrganisationUnit(sourceE);
    dataSetWeekly.addOrganisationUnit(sourceF);
    dataSetWeekly.addOrganisationUnit(sourceG);
    dataSetYearly.addOrganisationUnit(sourceB);
    dataSetYearly.addOrganisationUnit(sourceC);
    dataSetYearly.addOrganisationUnit(sourceD);
    dataSetYearly.addOrganisationUnit(sourceE);
    dataSetYearly.addOrganisationUnit(sourceF);
    organisationUnitService.addOrganisationUnit(sourceA);
    organisationUnitService.addOrganisationUnit(sourceB);
    organisationUnitService.addOrganisationUnit(sourceC);
    organisationUnitService.addOrganisationUnit(sourceD);
    organisationUnitService.addOrganisationUnit(sourceE);
    organisationUnitService.addOrganisationUnit(sourceF);
    organisationUnitService.addOrganisationUnit(sourceG);
    dataSetMonthly.addDataSetElement(dataElementA);
    dataSetMonthly.addDataSetElement(dataElementB);
    dataSetMonthly.addDataSetElement(dataElementC);
    dataSetMonthly.addDataSetElement(dataElementD);
    dataSetWeekly.addDataSetElement(dataElementE);
    dataSetYearly.addDataSetElement(dataElementE);
    dataSetService.addDataSet(dataSetWeekly);
    dataSetService.addDataSet(dataSetMonthly);
    dataSetService.addDataSet(dataSetYearly);
    dataElementService.updateDataElement(dataElementA);
    dataElementService.updateDataElement(dataElementB);
    dataElementService.updateDataElement(dataElementC);
    dataElementService.updateDataElement(dataElementD);
    dataElementService.updateDataElement(dataElementE);
    // deA + deB = deC - deD
    validationRuleA = createValidationRule("A", equal_to, expressionA, expressionB, periodTypeMonthly);
    // deC - deD > deB * 2
    validationRuleB = createValidationRule("B", greater_than, expressionB, expressionC, periodTypeMonthly);
    // deC - deD <= deA + deB
    validationRuleC = createValidationRule("C", less_than_or_equal_to, expressionB, expressionA, periodTypeMonthly);
    // deA + deB < deB * 2
    validationRuleD = createValidationRule("D", less_than, expressionA, expressionC, periodTypeMonthly);
    // deA [Compulsory pair] deB
    validationRuleE = createValidationRule("E", compulsory_pair, expressionI, expressionJ, periodTypeMonthly);
    // deA [Exclusive pair] deB
    validationRuleF = createValidationRule("F", exclusive_pair, expressionI, expressionJ, periodTypeMonthly);
    // deC = deD
    validationRuleG = createValidationRule("G", equal_to, expressionK, expressionL, periodTypeMonthly);
    // deA = [days]
    validationRuleP = createValidationRule("P", equal_to, expressionI, expressionP, periodTypeMonthly);
    // deE = [days]
    validationRuleQ = createValidationRule("Q", equal_to, expressionQ, expressionP, periodTypeYearly);
    // deA + deB = deB * 2
    validationRuleX = createValidationRule("X", equal_to, expressionA, expressionC, periodTypeMonthly);
    group = createValidationRuleGroup('A');
    defaultCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
}
Also used : MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) Expression(org.hisp.dhis.expression.Expression) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService)

Example 15 with Expression

use of org.hisp.dhis.expression.Expression in project dhis2-core by dhis2.

the class ValidationRuleDeletionHandler method deleteExpression.

@Override
public void deleteExpression(Expression expression) {
    Iterator<ValidationRule> iterator = validationRuleService.getAllValidationRules().iterator();
    while (iterator.hasNext()) {
        ValidationRule rule = iterator.next();
        Expression leftSide = rule.getLeftSide();
        Expression rightSide = rule.getRightSide();
        if ((leftSide != null && leftSide.equals(expression)) || (rightSide != null && rightSide.equals(expression))) {
            iterator.remove();
            validationRuleService.deleteValidationRule(rule);
        }
    }
}
Also used : Expression(org.hisp.dhis.expression.Expression)

Aggregations

Expression (org.hisp.dhis.expression.Expression)18 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)5 DataElement (org.hisp.dhis.dataelement.DataElement)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 DhisSpringTest (org.hisp.dhis.DhisSpringTest)2 DhisTest (org.hisp.dhis.DhisTest)2 IntegrationTest (org.hisp.dhis.IntegrationTest)2 MockCurrentUserService (org.hisp.dhis.mock.MockCurrentUserService)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 OrganisationUnitLevel (org.hisp.dhis.organisationunit.OrganisationUnitLevel)2 PeriodType (org.hisp.dhis.period.PeriodType)2 Predictor (org.hisp.dhis.predictor.Predictor)2 CurrentUserService (org.hisp.dhis.user.CurrentUserService)2 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 ArrayList (java.util.ArrayList)1 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)1 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)1 DataElementCategoryOption (org.hisp.dhis.dataelement.DataElementCategoryOption)1 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)1