← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11621: Enabling mathematical functions in expression indicators. This will allow for more advanced indic...

 

------------------------------------------------------------
revno: 11621
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-08-09 10:09:35 +0200
message:
  Enabling mathematical functions in expression indicators. This will allow for more advanced indicator expressions.
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.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-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java	2013-07-17 19:40:31 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java	2013-08-09 08:09:35 +0000
@@ -69,7 +69,8 @@
         final String expression = leftSide + operator.getMathematicalOperator() + rightSide;
         
         final JEP parser = new JEP();
-        
+
+        parser.addStandardFunctions();
         parser.parseExpression( expression );
         
         return ( parser.getValue() == 1.0 );
@@ -84,7 +85,8 @@
     public static double calculateExpression( String expression )   
     {
         final JEP parser = new JEP();
-        
+
+        parser.addStandardFunctions();
         parser.parseExpression( expression );
         
         double result = parser.getValue();
@@ -101,7 +103,8 @@
     public static boolean expressionHasErrors( String expression )
     {
         final JEP parser = new JEP();
-        
+
+        parser.addStandardFunctions();
         parser.parseExpression( expression );
         
         return parser.hasError();
@@ -117,7 +120,8 @@
     public static String getExpressionErrorInfo( String expression )
     {
         final JEP parser = new JEP();
-        
+
+        parser.addStandardFunctions();
         parser.parseExpression( expression );
         
         return parser.getErrorInfo();

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java	2013-07-17 19:40:31 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java	2013-08-09 08:09:35 +0000
@@ -253,4 +253,14 @@
         assertEquals( -0.43, MathUtils.getRounded( -0.43123 ), 0.01 );
         assertEquals( -10, MathUtils.getRounded( -10.00 ), 0.01 );        
     }
+    
+    @Test
+    public void testCalculateExpression()
+    {
+        assertEquals( 3d, MathUtils.calculateExpression( "1+2" ), 0.01 );
+        assertEquals( 3d, MathUtils.calculateExpression( "abs(3)" ), 0.01 );
+        assertEquals( 3d, MathUtils.calculateExpression( "abs(-3)" ), 0.01 );
+        assertEquals( 3d, MathUtils.calculateExpression( "abs(3-6)" ), 0.01 );
+        assertEquals( 5d, MathUtils.calculateExpression( "sqrt(25)" ), 0.01 );
+    }
 }