dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #37117
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18984: Added class UnaryDoubleFunction useful for single, double argument functions. Impl function ZeroI...
------------------------------------------------------------
revno: 18984
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-04-22 17:07:43 +0200
message:
Added class UnaryDoubleFunction useful for single, double argument functions. Impl function ZeroIfNegativeFunction.
added:
dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/UnaryDoubleFunction.java
dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/ZeroIfNegativeFunction.java
modified:
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/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-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
=== modified 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 2015-04-15 18:00:05 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/OneIfZeroOrPositiveFunction.java 2015-04-22 15:07:43 +0000
@@ -28,11 +28,6 @@
* 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.
@@ -40,32 +35,16 @@
* @author Lars Helge Overland
*/
public class OneIfZeroOrPositiveFunction
- extends PostfixMathCommand
+ extends UnaryDoubleFunction
{
public OneIfZeroOrPositiveFunction()
{
super();
-
- numberOfParameters = 1;
}
@Override
- @SuppressWarnings( { "rawtypes", "unchecked" } )
- public void run( Stack inStack ) throws ParseException
+ public Double eval( double arg )
{
- 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 ) );
+ return ( arg >= 0 ) ? 1d : 0d;
}
}
=== added file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/UnaryDoubleFunction.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/UnaryDoubleFunction.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/UnaryDoubleFunction.java 2015-04-22 15:07:43 +0000
@@ -0,0 +1,72 @@
+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;
+
+/**
+ * Abstract JEP function for a single, numerical argument.
+ *
+ * @author Lars Helge Overland
+ */
+public abstract class UnaryDoubleFunction
+ extends PostfixMathCommand
+{
+ public UnaryDoubleFunction()
+ {
+ 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 arg = ( (Double) param ).doubleValue();
+
+ Double result = eval( arg );
+
+ inStack.push( result );
+ }
+
+ public abstract Double eval( double arg );
+}
=== added file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/ZeroIfNegativeFunction.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/ZeroIfNegativeFunction.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/math/ZeroIfNegativeFunction.java 2015-04-22 15:07:43 +0000
@@ -0,0 +1,50 @@
+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.
+ */
+
+/**
+ * JEP function which returns the value if the argument is a zero or positive
+ * number, 0 otherwise.
+ *
+ * @author Lars Helge Overland
+ */
+public class ZeroIfNegativeFunction
+ extends UnaryDoubleFunction
+{
+ public ZeroIfNegativeFunction()
+ {
+ super();
+ }
+
+ @Override
+ public Double eval( double arg )
+ {
+ return Math.max( 0d, arg );
+ }
+}
=== 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-15 18:00:05 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java 2015-04-22 15:07:43 +0000
@@ -41,6 +41,7 @@
import org.hisp.dhis.datavalue.DataValue;
import org.hisp.dhis.expression.Operator;
import org.hisp.dhis.system.math.OneIfZeroOrPositiveFunction;
+import org.hisp.dhis.system.math.ZeroIfNegativeFunction;
import org.nfunk.jep.JEP;
/**
@@ -51,6 +52,7 @@
public static final Double ZERO = new Double( 0 );
public static final String ONEIFZEROORPOSITIVE_FUNCTION_NAME = "oizp";
+ public static final String ZEROIFNEGATIVE_FUNCTION_NAME = "zing";
private static DoubleValidator DOUBLE_VALIDATOR = new DoubleValidator();
private static IntegerValidator INT_VALIDATOR = new IntegerValidator();
@@ -150,6 +152,7 @@
parser.addStandardFunctions();
parser.addStandardConstants();
parser.addFunction( ONEIFZEROORPOSITIVE_FUNCTION_NAME, new OneIfZeroOrPositiveFunction() );
+ parser.addFunction( ZEROIFNEGATIVE_FUNCTION_NAME, new ZeroIfNegativeFunction() );
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-15 18:00:05 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java 2015-04-22 15:07:43 +0000
@@ -334,4 +334,15 @@
assertEquals( 4d, MathUtils.calculateExpression( "oizp(0) + 3" ), DELTA );
assertEquals( 5d, MathUtils.calculateExpression( "oizp(-3) + 5" ), DELTA );
}
+
+ @Test
+ public void testCalculateExpressionZeroIfNegative()
+ {
+ assertEquals( 314d, MathUtils.calculateExpression( "zing(314)" ), DELTA );
+ assertEquals( 0d, MathUtils.calculateExpression( "zing(0)" ), DELTA );
+ assertEquals( 0d, MathUtils.calculateExpression( "zing(-3)" ), DELTA );
+ assertEquals( -3d, MathUtils.calculateExpression( "zing(0) - 3" ), DELTA );
+ assertEquals( 5d, MathUtils.calculateExpression( "zing(-3) + 5" ), DELTA );
+ assertEquals( -2d, MathUtils.calculateExpression( "zing(-3) - 2" ), DELTA );
+ }
}
=== 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-04-15 18:00:05 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm 2015-04-22 15:07:43 +0000
@@ -100,7 +100,7 @@
<p></p>
<table>
<tr>
- <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>
+ <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) zing(x)</span></th>
</tr>
<tr>
<td>