Skip to content

Commit

Permalink
Support new aggregation function "Range" in model, data engine, chart
Browse files Browse the repository at this point in the history
engine and extension. New function will also appear in UI aggregation
function drop down list automatically.
  • Loading branch information
Yulin Wang committed Jul 11, 2016
1 parent 7b71f1b commit 078d643
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Mirr.displayName=Mirr
NPV.displayName=NPV
Percentile.displayName=Percentile
Quartile.displayName=Quartile
Range.displayName=Range
#Running aggregates.
MovingAverage.displayName=Moving Average
RunningSum.displayName=Running Sum
Expand Down
6 changes: 5 additions & 1 deletion chart/org.eclipse.birt.chart.engine.extension/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@
<aggregateFunction
displayName="%Quartile.displayName"
function="org.eclipse.birt.chart.extension.aggregate.Quartile"
name="Quartile"/>
name="Quartile"/>
<aggregateFunction
displayName="%Range.displayName"
function="org.eclipse.birt.chart.extension.aggregate.Range"
name="Range"/>
<!-- The Running Aggregates -->
<aggregateFunction
displayName="%MovingAverage.displayName"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*******************************************************************************
* Copyright (c) 2004, 2016 Actuate Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Actuate Corporation - initial API and implementation
*******************************************************************************/

package org.eclipse.birt.chart.extension.aggregate;

import java.math.BigDecimal;

import org.eclipse.birt.chart.aggregate.AggregateFunctionAdapter;
import org.eclipse.birt.core.data.DataType;

/**
*
*/

public class Range extends AggregateFunctionAdapter
{

private Object max;
private Object min;

@SuppressWarnings({
"unchecked", "rawtypes"
})
public void accumulate( Object oValue ) throws IllegalArgumentException
{
if ( max == null )
{
max = oValue;
min = oValue;
}
else if ( oValue instanceof Comparable )
{
max = ( (Comparable) oValue ).compareTo( max ) >= 0 ? oValue : max;
min = ( (Comparable) oValue ).compareTo( min ) <= 0 ? oValue : min;
}

}

public Object getAggregatedValue( )
{
switch ( getDataType( ) )
{
case NUMBER :
return new Double( ( (double) max ) - (double) min );

case BIGDECIMAL :
return ( (BigDecimal) max ).subtract( (BigDecimal) min );

default :
return null; // THIS CONDITION SHOULD NEVER ARISE
}
}

public void initialize( )
{
max = null;
min = null;
}

@Override
public int getBIRTDataType( )
{
return DataType.DOUBLE_TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public static final class DefaultAggregations
public static final String PERCENT_RANK = "PercentRank"; //$NON-NLS-1$
public static final String PERCENT_SUM = "PercentSum"; //$NON-NLS-1$
public static final String RUNNING_COUNT = "RunningCount"; //$NON-NLS-1$
public static final String RANGE = "Range"; //$NON-NLS-1$
}

/**
Expand Down Expand Up @@ -281,6 +282,11 @@ public static final class DefaultAggregations
{
DefaultAggregations.MAX,
"Max", "org.eclipse.birt.chart.extension.aggregate.Max" //$NON-NLS-1$ //$NON-NLS-2$
},
{
DefaultAggregations.RANGE,
"Range", //$NON-NLS-1$
"org.eclipse.birt.chart.extension.aggregate.Range" //$NON-NLS-1$
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,10 @@ else if ( PluginSettings.DefaultAggregations.RUNNING_COUNT.equals( agg ) )
{
return IBuildInAggregation.TOTAL_RUNNINGCOUNT_FUNC;
}

else if ( PluginSettings.DefaultAggregations.RANGE.equals( agg ) )
{
return IBuildInAggregation.TOTAL_RANGE_FUNC;
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ public interface IBuildInAggregation
public static final String TOTAL_PERCENTSUM_FUNC = "PERCENTSUM";//$NON-NLS-1$
public static final String TOTAL_RUNNINGCOUNT_FUNC = "RUNNINGCOUNT";//$NON-NLS-1$
public static final String TOTAL_CONCATENATE_FUNC = "CONCATENATE";//$NON-NLS-1$
public static final String TOTAL_RANGE_FUNC = "RANGE";//$NON-NLS-1$

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ TotalConcatenate.paramDescription.expression=the data field to be aggregated on
TotalConcatenate.paramDescription.separator=the separator of the concatenated string
TotalConcatenate.paramDescription.maxLength=max character number of the concatenated value
TotalConcatenate.paramDescription.showAllValues=whether should show all values
TotalRange.description=function Total.RANGE()
TotalRange.displayName=RANGE

#aggregation error messages
aggregation.BadOperandType=Unexpected operand data type: {0}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
public class BuildInAggregationFactory implements IAggregationFactory
{

private Map aggrMap = new HashMap( );
private List aggregations = new ArrayList( );
private Map<String, IAggrFunction> aggrMap = new HashMap<String, IAggrFunction>( );
private List<IAggrFunction> aggregations = new ArrayList<IAggrFunction>( );

/**
*
Expand Down Expand Up @@ -140,6 +140,10 @@ private void populateAggregations( )
final TotalConcatenate totalConcatenate = new TotalConcatenate( );
aggrMap.put( IBuildInAggregation.TOTAL_CONCATENATE_FUNC, totalConcatenate );
aggregations.add( totalConcatenate );

final TotalRange totalRange = new TotalRange( );
aggrMap.put( IBuildInAggregation.TOTAL_RANGE_FUNC, totalRange );
aggregations.add( totalRange );
}

/**
Expand All @@ -150,16 +154,12 @@ public BuildInAggregationFactory( )
populateAggregations( );
}





/*
* (non-Javadoc)
*
* @see org.eclipse.birt.data.engine.api.aggregation.IAggregationFactory#getAggregations()
*/
public List getAggregations( )
public List<IAggrFunction> getAggregations( )
{
return aggregations;
}
Expand All @@ -171,6 +171,6 @@ public List getAggregations( )
*/
public IAggrFunction getAggregation( String name )
{
return (IAggrFunction) aggrMap.get( name.toUpperCase( ) );
return aggrMap.get( name.toUpperCase( ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/**
*************************************************************************
* Copyright (c) 2004, 2016 Actuate Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Actuate Corporation - initial API and implementation
*
*************************************************************************
*/

package org.eclipse.birt.data.aggregation.impl;

import org.eclipse.birt.core.data.DataType;
import org.eclipse.birt.data.aggregation.api.IBuildInAggregation;
import org.eclipse.birt.data.aggregation.calculator.CalculatorFactory;
import org.eclipse.birt.data.aggregation.calculator.ICalculator;
import org.eclipse.birt.data.aggregation.i18n.Messages;
import org.eclipse.birt.data.engine.api.aggregation.Accumulator;
import org.eclipse.birt.data.engine.api.aggregation.IParameterDefn;
import org.eclipse.birt.data.engine.core.DataException;

/**
*
* Implements the built-in Total.Range aggregation
*/
public class TotalRange extends AggrFunction
{

public String getName( )
{
return IBuildInAggregation.TOTAL_RANGE_FUNC;
}

public int getType( )
{
return SUMMARY_AGGR;
}

public int getDataType( )
{
return DataType.DOUBLE_TYPE;
}

public IParameterDefn[] getParameterDefn( )
{
return new IParameterDefn[]{
new ParameterDefn( Constants.EXPRESSION_NAME,
Constants.EXPRESSION_DISPLAY_NAME,
false,
true,
SupportedDataTypes.CALCULATABLE,
"" )//$NON-NLS-1$
};
}

public Accumulator newAccumulator( )
{
return new MyAccumulator(
CalculatorFactory.getCalculator( getDataType( ) ) );
}

private static class MyAccumulator extends SummaryAccumulator
{

private Object max = null;
private Object min = null;

private boolean isRowAvailable = false;

MyAccumulator( ICalculator calc )
{
super( calc );
}

public void start( )
{
super.start( );
max = null;
min = null;
isRowAvailable = false;
}

public void onRow( Object[] args )
{
assert ( args.length > 0 );
if ( args[0] != null )
{
if ( !isRowAvailable )
{
isRowAvailable = true;
max = args[0];
min = max;
return;
}
if ( isGreaterThan( args[0], max ) )
{
max = args[0];
}
else if ( isLessThan( args[0], min ) )
{
min = args[0];
}
}
}

public Object getSummaryValue( ) throws DataException
{
return calculator.subtract( calculator.getTypedObject( max ),
calculator.getTypedObject( min ) );
}

@SuppressWarnings({
"unchecked", "rawtypes"
})
private boolean isGreaterThan( Object origin, Object target )
{
if ( ( origin instanceof Comparable )
&& ( target instanceof Comparable ) )
{
return ( (Comparable) origin ).compareTo( target ) > 0;
}
else
{
throw new RuntimeException( Messages.getString(
"TotalMax.exception.cannot_get_max_value" ) ); //$NON-NLS-1$
}
}

@SuppressWarnings({
"unchecked", "rawtypes"
})
private boolean isLessThan( Object origin, Object target )
{
if ( ( origin instanceof Comparable )
&& ( target instanceof Comparable ) )
{
return ( (Comparable) origin ).compareTo( target ) < 0;
}
else
{
throw new RuntimeException( Messages.getString(
"TotalMin.exception.cannot_get_min_value" ) ); //$NON-NLS-1$
}
}
}

public String getDescription( )
{
return Messages.getString( "TotalRange.description" ); //$NON-NLS-1$
}

public String getDisplayName( )
{
return Messages.getString( "TotalRange.displayName" ); //$NON-NLS-1$
}
}
Loading

0 comments on commit 078d643

Please sign in to comment.