1
package org.hisp.dhis.datamart.util;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import java.util.HashSet;
33
import java.util.regex.Matcher;
34
import java.util.regex.Pattern;
36
import org.hisp.dhis.dataelement.Operand;
38
import static org.hisp.dhis.expression.Expression.SEPARATOR;
41
* @author Lars Helge Overland
42
* @version $Id: ParserUtil.java 5510 2008-07-30 16:30:27Z larshelg $
44
public class ParserUtil
46
private static final String NULL_REPLACEMENT = "0";
49
* Returns the data element identifiers in the given expression. Returns null
50
* if the formula is null, returns an empty set if no data elements exist in
51
* the formula, otherwise a set of the data elements in the formula.
53
* @param formula The formula to be parsed.
55
public static Set<Integer> getDataElementIdsInExpression( String expression )
57
Set<Integer> dataElementIdsInExpression = null;
59
if ( expression != null )
61
dataElementIdsInExpression = new HashSet<Integer>();
63
final Matcher matcher = getMatcher( "(\\[\\d+\\" + SEPARATOR + "\\d+\\])", expression );
65
while ( matcher.find() )
67
String replaceString = matcher.group();
69
replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
71
replaceString = replaceString.substring( 0, replaceString.indexOf( SEPARATOR ) );
73
final int dataElementId = Integer.parseInt( replaceString );
75
dataElementIdsInExpression.add( dataElementId );
79
return dataElementIdsInExpression;
83
* Generates an expression where the Operand identifiers, consisting of
84
* data element id and category option combo id, are replaced
85
* by the aggregated value for the relevant combination of data element,
88
* @param formula The formula to parse.
89
* @param valueMap The map containing data element identifiers and aggregated value.
91
public static String generateExpression( final String formula, final Map<Operand, Double> valueMap )
95
final Pattern pattern = Pattern.compile( "(\\[\\d+\\.\\d+\\])" );
97
final Matcher matcher = pattern.matcher( formula );
99
final StringBuffer buffer = new StringBuffer();
101
Double aggregatedValue = null;
103
while ( matcher.find() )
105
String replaceString = matcher.group();
107
replaceString = replaceString.replaceAll( "[\\[\\]]", "" );
109
int dataElementId = Integer.parseInt( replaceString.substring( 0, replaceString.indexOf( SEPARATOR ) ) );
110
int categoryOptionComboId = Integer.parseInt( replaceString.substring( replaceString.indexOf( SEPARATOR ) + 1 ) );
112
final Operand operand = new Operand( dataElementId, categoryOptionComboId );
114
aggregatedValue = valueMap.get( operand );
116
replaceString = ( aggregatedValue == null ) ? NULL_REPLACEMENT : String.valueOf( aggregatedValue );
118
matcher.appendReplacement( buffer, replaceString );
121
matcher.appendTail( buffer );
123
return buffer.toString();
125
catch ( NumberFormatException ex )
127
throw new RuntimeException( "Illegal data element id", ex );
132
* Returns a matcher object compiled with the given regex and matched with the given expression.
134
* @param regex The regular expression.
135
* @param formula The formula.
137
public static Matcher getMatcher( final String regex, final String formula )
139
final Pattern pattern = Pattern.compile( regex );
141
return pattern.matcher( formula );