← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13922: Expression service, using map instead of integer for count signature

 

------------------------------------------------------------
revno: 13922
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-02-04 10:38:48 +0200
message:
  Expression service, using map instead of integer for count signature
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java	2014-02-04 08:24:06 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java	2014-02-04 08:38:48 +0000
@@ -123,7 +123,7 @@
     Collection<Expression> getAllExpressions();
     
     Double getIndicatorValue( Indicator indicator, Period period, Map<DataElementOperand, Double> valueMap, 
-        Map<String, Double> constantMap, Integer orgUnitCount, Integer days );
+        Map<String, Double> constantMap, Map<String, Integer> orgUnitCountMap, Integer days );
     
     /**
      * Generates the calculated value for the given expression base on the values
@@ -134,12 +134,13 @@
      *        use in the calculation.
      * @param constantMap the mapping between the constant uid and value to use
      *        in the calculation.
-     * @param orgUnitCount the number of org units to use in the calculation.
+     * @param orgUnitCountMap the mapping between organisation unit group uid and
+     *        count of org units to use in the calculation.
      * @param days the number of days to use in the calculation.
      * @return the calculated value as a double.
      */
     Double getExpressionValue( Expression expression, Map<DataElementOperand, Double> valueMap, 
-        Map<String, Double> constantMap, Integer orgUnitCount, Integer days );
+        Map<String, Double> constantMap, Map<String, Integer> orgUnitCountMap, Integer days );
     
     /**
      * Generates the calculated value for the given expression base on the values
@@ -150,7 +151,8 @@
      *        use in the calculation.
      * @param constantMap the mapping between the constant uid and value to use
      *        in the calculation.
-     * @param orgUnitCount the number of org units to use in the calculation.
+     * @param orgUnitCountMap the mapping between organisation unit group uid and
+     *        count of org units to use in the calculation.
      * @param days the number of days to use in the calculation.
      * @param set of data element operands that have values but they are incomplete
      *        (for example due to aggregation from organisationUnit children where
@@ -158,7 +160,7 @@
      * @return the calculated value as a double.
      */
     Double getExpressionValue( Expression expression, Map<DataElementOperand, Double> valueMap, 
-        Map<String, Double> constantMap, Integer orgUnitCount, Integer days, Set<DataElementOperand> incompleteValues );
+        Map<String, Double> constantMap, Map<String, Integer> orgUnitCountMap, Integer days, Set<DataElementOperand> incompleteValues );
     
     /**
      * Returns the uids of the data element totals in the given expression.
@@ -319,11 +321,15 @@
      * period, and source.
      * 
      * @param formula formula to parse.
-     * @param valueMap map containing data element identifiers and aggregated value.
-     * @param constantMap map between constants and values.  
-     * @param days The number to be substituted with the days expression in the formula.
+     * @param valueMap the mapping between data element operands and values to
+     *        use in the calculation.
+     * @param constantMap the mapping between the constant uid and value to use
+     *        in the calculation.
+     * @param orgUnitCountMap the mapping between organisation unit group uid and
+     *        count of org units to use in the calculation.
+     * @param days the number of days to use in the calculation.
      */
-    String generateExpression( String expression, Map<DataElementOperand, Double> valueMap, Map<String, Double> constantMap, Integer orgUnitCount, Integer days, boolean nullIfNoValues );
+    String generateExpression( String expression, Map<DataElementOperand, Double> valueMap, Map<String, Double> constantMap, Map<String, Integer> orgUnitCountMap, Integer days, boolean nullIfNoValues );
     
     /**
      * Returns all Operands included in the formulas for the given collection of

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java	2014-02-04 08:24:06 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java	2014-02-04 08:38:48 +0000
@@ -156,14 +156,14 @@
     // -------------------------------------------------------------------------
     
     public Double getIndicatorValue( Indicator indicator, Period period, Map<DataElementOperand, Double> valueMap, 
-        Map<String, Double> constantMap, Integer orgUnitCount, Integer days )
+        Map<String, Double> constantMap, Map<String, Integer> orgUnitCountMap, Integer days )
     {
         if ( indicator == null || indicator.getExplodedNumeratorFallback() == null || indicator.getExplodedDenominatorFallback() == null )
         {
             return null;
         }
         
-        final String denominatorExpression = generateExpression( indicator.getExplodedDenominatorFallback(), valueMap, constantMap, orgUnitCount, days, false );
+        final String denominatorExpression = generateExpression( indicator.getExplodedDenominatorFallback(), valueMap, constantMap, orgUnitCountMap, days, false );
         
         if ( denominatorExpression == null )
         {
@@ -174,7 +174,7 @@
         
         if ( !isEqual( denominatorValue, 0d ) )
         {
-            final String numeratorExpression = generateExpression( indicator.getExplodedNumeratorFallback(), valueMap, constantMap, orgUnitCount, days, false );
+            final String numeratorExpression = generateExpression( indicator.getExplodedNumeratorFallback(), valueMap, constantMap, orgUnitCountMap, days, false );
             
             if ( numeratorExpression == null )
             {
@@ -194,18 +194,18 @@
     }
 
     public Double getExpressionValue( Expression expression, Map<DataElementOperand, Double> valueMap,
-        Map<String, Double> constantMap, Integer orgUnitCount, Integer days )
+        Map<String, Double> constantMap, Map<String, Integer> orgUnitCountMap, Integer days )
     {
         final String expressionString = generateExpression( expression.getExpression(), valueMap, constantMap, 
-            orgUnitCount, days, expression.isNullIfBlank() );
+            orgUnitCountMap, days, expression.isNullIfBlank() );
 
         return expressionString != null ? calculateExpression( expressionString ) : null;
     }
 
     public Double getExpressionValue( Expression expression, Map<DataElementOperand, Double> valueMap,
-        Map<String, Double> constantMap, Integer orgUnitCount, Integer days, Set<DataElementOperand> incompleteValues )
+        Map<String, Double> constantMap, Map<String, Integer> orgUnitCountMap, Integer days, Set<DataElementOperand> incompleteValues )
     {
-        final String expressionString = generateExpression( expression.getExpression(), valueMap, constantMap, orgUnitCount, days,
+        final String expressionString = generateExpression( expression.getExpression(), valueMap, constantMap, orgUnitCountMap, days,
             expression.isNullIfBlank(), incompleteValues );
 
         return expressionString != null ? calculateExpression( expressionString ) : null;
@@ -771,13 +771,13 @@
 
     @Transactional
     public String generateExpression( String expression, Map<DataElementOperand, Double> valueMap, 
-        Map<String, Double> constantMap, Integer orgUnitCount, Integer days, boolean nullIfNoValues )
+        Map<String, Double> constantMap, Map<String, Integer> orgUnitCountMap, Integer days, boolean nullIfNoValues )
     {
-    	return generateExpression( expression, valueMap, constantMap, orgUnitCount, days, nullIfNoValues, null );
+    	return generateExpression( expression, valueMap, constantMap, orgUnitCountMap, days, nullIfNoValues, null );
     }
 
     private String generateExpression( String expression, Map<DataElementOperand, Double> valueMap, 
-        Map<String, Double> constantMap, Integer orgUnitCount, Integer days, boolean nullIfNoValues, Set<DataElementOperand> incompleteValues )
+        Map<String, Double> constantMap, Map<String, Integer> orgUnitCountMap, Integer days, boolean nullIfNoValues, Set<DataElementOperand> incompleteValues )
     {
         if ( expression == null || expression.isEmpty() )
         {
@@ -818,7 +818,7 @@
         
         while ( matcher.find() )
         {
-            final Double constant = constantMap.get( matcher.group( 1 ) );
+            final Double constant = constantMap != null ? constantMap.get( matcher.group( 1 ) ) : null;
             
             String replacement = constant != null ? String.valueOf( constant ) : NULL_REPLACEMENT;
             
@@ -836,7 +836,9 @@
         
         while ( matcher.find() )
         {
-            String replacement = orgUnitCount != null ? String.valueOf( orgUnitCount ) : NULL_REPLACEMENT;
+            final Integer count = orgUnitCountMap != null ? orgUnitCountMap.get( matcher.group( 1 ) ) : null;
+            
+            String replacement = count != null ? String.valueOf( count ) : NULL_REPLACEMENT;
             
             matcher.appendReplacement( sb, replacement );
         }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java	2014-01-31 18:08:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java	2014-02-04 08:38:48 +0000
@@ -354,11 +354,14 @@
         
         Map<String, Double> constantMap = new HashMap<String, Double>();
         constantMap.put( constantA.getUid(), 2.0 );
+        
+        Map<String, Integer> orgUnitCountMap = new HashMap<String, Integer>();
+        orgUnitCountMap.put( groupA.getUid(), groupA.getMembers().size() );
 
         assertEquals( "12.0+34.0", expressionService.generateExpression( expressionA, valueMap, constantMap, null, null, false ) );
         assertEquals( "12.0+5", expressionService.generateExpression( expressionD, valueMap, constantMap, null, 5, false ) );
         assertEquals( "12.0*2.0", expressionService.generateExpression( expressionE, valueMap, constantMap, null, null, false ) );
-        assertEquals( "12.0*3", expressionService.generateExpression( expressionH, valueMap, constantMap, 3, null, false ) );
+        assertEquals( "12.0*3", expressionService.generateExpression( expressionH, valueMap, constantMap, orgUnitCountMap, null, false ) );
     }
 
     @Test
@@ -388,10 +391,13 @@
         Map<String, Double> constantMap = new HashMap<String, Double>();
         constantMap.put( constantA.getUid(), 2.0 );
         
+        Map<String, Integer> orgUnitCountMap = new HashMap<String, Integer>();
+        orgUnitCountMap.put( groupA.getUid(), groupA.getMembers().size() );
+        
         assertEquals( 46d, expressionService.getExpressionValue( expA, valueMap, constantMap, null, null ), DELTA );
         assertEquals( 17d, expressionService.getExpressionValue( expD, valueMap, constantMap, null, 5 ), DELTA );
         assertEquals( 24d, expressionService.getExpressionValue( expE, valueMap, constantMap, null, null ), DELTA );
-        assertEquals( 36d, expressionService.getExpressionValue( expH, valueMap, constantMap, 3, null ), DELTA );
+        assertEquals( 36d, expressionService.getExpressionValue( expH, valueMap, constantMap, orgUnitCountMap, null ), DELTA );
     }
     
     @Test