← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18911: Impl JEP math function which returns 1 if argument is a zero or positive number, 0 otherwise. Ena...

 

------------------------------------------------------------
revno: 18911
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-04-15 20:00:05 +0200
message:
  Impl JEP math function which returns 1 if argument is a zero or positive number, 0 otherwise. Enabled it for use in indicator expressions.
added:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/OneIfZeroOrPositiveFunction.java
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
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/WEB-INF/classes/log4j.properties
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/css/widgets.css
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm


--
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
=== added directory 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math'
=== added file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/OneIfZeroOrPositiveFunction.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/OneIfZeroOrPositiveFunction.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/OneIfZeroOrPositiveFunction.java	2015-04-15 18:00:05 +0000
@@ -0,0 +1,71 @@
+package org.hisp.dhis.system.math;
+
+/*
+ * Copyright (c) 2004-2015, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.Stack;
+
+import org.nfunk.jep.ParseException;
+import org.nfunk.jep.function.PostfixMathCommand;
+
+/**
+ * JEP function which returns 1 if the argument is a zero or positive number, 0
+ * otherwise.
+ * 
+ * @author Lars Helge Overland
+ */
+public class OneIfZeroOrPositiveFunction
+    extends PostfixMathCommand
+{
+    public OneIfZeroOrPositiveFunction()
+    {
+        super();
+        
+        numberOfParameters = 1;
+    }
+
+    @Override
+    @SuppressWarnings( { "rawtypes", "unchecked" } )
+    public void run( Stack inStack ) throws ParseException 
+    {
+        checkStack( inStack );
+        
+        Object param = inStack.pop();
+        
+        if ( param == null || !( param instanceof Double ) )
+        {
+            throw new ParseException( "Invalid parameter type, must be double: " + param );
+        }
+        
+        double value = ( (Double) param ).doubleValue();
+        
+        double result = value >= 0 ? 1d : 0d;
+        
+        inStack.push( new Double( result ) );
+    }
+}

=== 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	2015-04-09 17:55:53 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java	2015-04-15 18:00:05 +0000
@@ -40,6 +40,7 @@
 import org.apache.commons.validator.routines.IntegerValidator;
 import org.hisp.dhis.datavalue.DataValue;
 import org.hisp.dhis.expression.Operator;
+import org.hisp.dhis.system.math.OneIfZeroOrPositiveFunction;
 import org.nfunk.jep.JEP;
 
 /**
@@ -49,6 +50,8 @@
 {
     public static final Double ZERO = new Double( 0 );
     
+    public static final String ONEIFZEROORPOSITIVE_FUNCTION_NAME = "oizp";
+    
     private static DoubleValidator DOUBLE_VALIDATOR = new DoubleValidator();
     private static IntegerValidator INT_VALIDATOR = new IntegerValidator();
     
@@ -146,6 +149,7 @@
         final JEP parser = new JEP();
         parser.addStandardFunctions();
         parser.addStandardConstants();
+        parser.addFunction( ONEIFZEROORPOSITIVE_FUNCTION_NAME, new OneIfZeroOrPositiveFunction() );
         return parser;
     }
     

=== 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	2015-04-09 17:55:53 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java	2015-04-15 18:00:05 +0000
@@ -49,6 +49,8 @@
  */
 public class MathUtilsTest
 {
+    private static final double DELTA = 0.01;
+    
     @Test
     public void testExpressionIsTrue()
     {
@@ -66,7 +68,7 @@
     {
         double[] array = { 5.0, 2.0, 6.0, 12.0 };
         
-        assertEquals( 2.0, MathUtils.getMin( array ), 0.01 );
+        assertEquals( 2.0, MathUtils.getMin( array ), DELTA );
     }
 
     @Test
@@ -74,7 +76,7 @@
     {
         double[] array = { 5.0, 2.0, 12.0, 6.0 };
         
-        assertEquals( 12.0, MathUtils.getMax( array ), 0.01 );
+        assertEquals( 12.0, MathUtils.getMax( array ), DELTA );
     }
 
     @Test
@@ -289,36 +291,47 @@
     @Test
     public void testGetAverage()
     {
-        assertEquals( 7.5, MathUtils.getAverage( Arrays.asList( 5.0, 5.0, 10.0, 10.0 ) ), 0.01 );
+        assertEquals( 7.5, MathUtils.getAverage( Arrays.asList( 5.0, 5.0, 10.0, 10.0 ) ), DELTA );
     }
     
     @Test
     public void testGetRounded()
     {
-        assertEquals( 10, MathUtils.getRounded( 10.00 ), 0.01 );
-        assertEquals( 10, MathUtils.getRounded( 10 ), 0.01 );
-        assertEquals( 0.53, MathUtils.getRounded( 0.5281 ), 0.01 );
-        assertEquals( 0.5, MathUtils.getRounded( 0.5 ), 0.01 );
-        assertEquals( 0, MathUtils.getRounded( 0 ), 0.01 );
-        assertEquals( -0.43, MathUtils.getRounded( -0.43123 ), 0.01 );
-        assertEquals( -10, MathUtils.getRounded( -10.00 ), 0.01 );        
+        assertEquals( 10, MathUtils.getRounded( 10.00 ), DELTA );
+        assertEquals( 10, MathUtils.getRounded( 10 ), DELTA );
+        assertEquals( 0.53, MathUtils.getRounded( 0.5281 ), DELTA );
+        assertEquals( 0.5, MathUtils.getRounded( 0.5 ), DELTA );
+        assertEquals( 0, MathUtils.getRounded( 0 ), DELTA );
+        assertEquals( -0.43, MathUtils.getRounded( -0.43123 ), DELTA );
+        assertEquals( -10, MathUtils.getRounded( -10.00 ), DELTA );        
     }
     
     @Test
     public void testFunctionExpression()
     {
-        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 );
-        assertEquals( 1d, MathUtils.calculateExpression( "mod(7,2)" ), 0.01 );
+        assertEquals( 3d, MathUtils.calculateExpression( "1+2" ), DELTA );
+        assertEquals( 3d, MathUtils.calculateExpression( "abs(3)" ), DELTA );
+        assertEquals( 3d, MathUtils.calculateExpression( "abs(-3)" ), DELTA );
+        assertEquals( 3d, MathUtils.calculateExpression( "abs(3-6)" ), DELTA );
+        assertEquals( 5d, MathUtils.calculateExpression( "sqrt(25)" ), DELTA );
+        assertEquals( 1d, MathUtils.calculateExpression( "mod(7,2)" ), DELTA );
     }
     
     @Test
     public void testCalculateExpression()
     {
-        assertEquals( 84d, MathUtils.calculateExpression( "70/1000*12*100" ), 0.01 );
-        assertEquals( 1158d, MathUtils.calculateExpression( "70+1000-12+100" ), 0.01 );        
+        assertEquals( 84d, MathUtils.calculateExpression( "70/1000*12*100" ), DELTA );
+        assertEquals( 1158d, MathUtils.calculateExpression( "70+1000-12+100" ), DELTA );        
+    }
+    
+    @Test
+    public void testCalculateExpressionOneIfZeroOrPositive()
+    {
+        assertEquals( 1d, MathUtils.calculateExpression( "oizp(314)" ), DELTA );
+        assertEquals( 1d, MathUtils.calculateExpression( "oizp(0)" ), DELTA );
+        assertEquals( 0d, MathUtils.calculateExpression( "oizp(-3)" ), DELTA );
+        assertEquals( 5d, MathUtils.calculateExpression( "4 + oizp(314)" ), DELTA );
+        assertEquals( 4d, MathUtils.calculateExpression( "oizp(0) + 3" ), DELTA );
+        assertEquals( 5d, MathUtils.calculateExpression( "oizp(-3) + 5" ), DELTA );
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/WEB-INF/classes/log4j.properties'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/WEB-INF/classes/log4j.properties	2015-04-15 12:00:38 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/WEB-INF/classes/log4j.properties	2015-04-15 18:00:05 +0000
@@ -11,7 +11,7 @@
 # File appender
 log4j.appender.file = org.apache.log4j.RollingFileAppender
 log4j.appender.file.File = dhis.log
-log4j.appender.file.MaxFileSize = 10MB
+log4j.appender.file.MaxFileSize = 25MB
 log4j.appender.file.MaxBackupIndex = 3
 log4j.appender.file.layout = org.apache.log4j.PatternLayout
 log4j.appender.file.layout.ConversionPattern = * %-5p %d{ISO8601} %m (%F [%t])%n

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/css/widgets.css'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/css/widgets.css	2014-11-04 02:32:10 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/css/widgets.css	2015-04-15 18:00:05 +0000
@@ -933,6 +933,7 @@
 .tipText
 {
 	color: #777;
+	font-weight: normal;
 }
 
 .fa-question-circle

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm	2015-03-29 20:22:47 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm	2015-04-15 18:00:05 +0000
@@ -100,7 +100,7 @@
 <p></p>
 <table>
 	<tr>
-        <th>$i18n.getString( "expression" )</th>
+        <th>$i18n.getString( "expression" ) <span class="tipText">Tip: use abs(x) sin(x) cos(x) tan(x) ln(x) log(x) sqrt(x) mod(x,y) oizp(x)</span></th>
       </tr>	
 	<tr>
 		<td>