Program: 65888
Project: 65888
Buggy source:
private void createBindingsForCalculatedMeasures(ICubeQueryDefinition cqd) throws DataException {
  List<String> levelNames=new ArrayList();
  List aggrOns=org.eclipse.birt.data.engine.olap.query.view.CubeQueryDefinitionUtil.populateMeasureAggrOns(cqd);
  for (int i=0; i < aggrOns.size(); i++) {
    DimLevel level=(DimLevel)aggrOns.get(i);
    levelNames.add(ExpressionUtil.createJSDimensionExpression(level.getDimensionName(),level.getLevelName()));
  }
  List measures=cqd.getMeasures();
  Map measureMap=new HashMap();
  for (int i=0; i < measures.size(); i++) {
    measureMap.put(((MeasureDefinition)measures.get(i)).getName(),measures.get(i));
  }
  List calculatedMeasures=cqd.getDerivedMeasures();
  Map derivedMeasureMap=new HashMap();
  for (int i=0; i < calculatedMeasures.size(); i++) {
    derivedMeasureMap.put(((DerivedMeasureDefinition)calculatedMeasures.get(i)).getName(),calculatedMeasures.get(i));
  }
  Map createdBindings=new HashMap();
  List bindingsInCubeQuery=cqd.getBindings();
  for (int i=0; i < calculatedMeasures.size(); i++) {
    DerivedMeasureDefinition measureDefinition=(DerivedMeasureDefinition)calculatedMeasures.get(i);
    List referencedMeasures=ExpressionCompilerUtil.extractColumnExpression(measureDefinition.getExpression(),ExpressionUtil.MEASURE_INDICATOR);
    for (int j=0; j < referencedMeasures.size(); j++) {
      String measureName=referencedMeasures.get(j).toString();
      if (measureMap.containsKey(measureName)) {
        if (!createdBindings.containsKey(measureName)) {
          MeasureDefinition md=(MeasureDefinition)measureMap.get(measureName);
          IBinding newBinding=new Binding("String_Node_Str" + measureName);
          newBinding.setDataType(md.getDataType());
          newBinding.setExpression(new ScriptExpression(ExpressionUtil.createJSMeasureExpression(measureName)));
          newBinding.setAggrFunction(getRollUpAggregationFunctionName(md.getAggrFunction()));
          for (int a=0; a < levelNames.size(); a++)           newBinding.addAggregateOn(levelNames.get(a));
          IBinding b=getSameBindingInQuery(newBinding,bindingsInCubeQuery);
          if (b != null) {
            createdBindings.put(measureName,b);
          }
 else {
            createdBindings.put(measureName,newBinding);
            realBindings.add(newBinding);
          }
        }
      }
    }
  }
  for (int i=0; i < bindingsInCubeQuery.size(); i++) {
    IBinding binding=(IBinding)bindingsInCubeQuery.get(i);
    ScriptExpression expression=(ScriptExpression)binding.getExpression();
    List measureName=ExpressionCompilerUtil.extractColumnExpression(expression,ExpressionUtil.MEASURE_INDICATOR);
    if (measureName != null && measureName.size() > 0 && derivedMeasureMap.containsKey(measureName.get(0).toString())) {
      expression.setText("String_Node_Str" + ((ScriptExpression)((DerivedMeasureDefinition)derivedMeasureMap.get(measureName.get(0).toString())).getExpression()).getText() + "String_Node_Str");
      expression.setText(getReplacedExpressionText(expression.getText(),measureMap,derivedMeasureMap,createdBindings,binding,bindingsInCubeQuery));
      expression.setText(expression.getText().substring(1,expression.getText().length() - 1));
      binding.getAggregatOns().clear();
      binding.setAggrFunction(null);
    }
  }
}

Fixed version (ground truth):
private void createBindingsForCalculatedMeasures(List bindingsInCubeQuery,List measures,List calculatedMeasures,List aggrOns) throws DataException {
  List<String> levelNames=new ArrayList();
  for (int i=0; i < aggrOns.size(); i++) {
    DimLevel level=(DimLevel)aggrOns.get(i);
    levelNames.add(ExpressionUtil.createJSDimensionExpression(level.getDimensionName(),level.getLevelName()));
  }
  Map measureMap=new HashMap();
  for (int i=0; i < measures.size(); i++) {
    measureMap.put(((MeasureDefinition)measures.get(i)).getName(),measures.get(i));
  }
  Map derivedMeasureMap=new HashMap();
  for (int i=0; i < calculatedMeasures.size(); i++) {
    derivedMeasureMap.put(((DerivedMeasureDefinition)calculatedMeasures.get(i)).getName(),calculatedMeasures.get(i));
  }
  Map createdBindings=new HashMap();
  for (int i=0; i < calculatedMeasures.size(); i++) {
    DerivedMeasureDefinition measureDefinition=(DerivedMeasureDefinition)calculatedMeasures.get(i);
    List referencedMeasures=ExpressionCompilerUtil.extractColumnExpression(measureDefinition.getExpression(),ExpressionUtil.MEASURE_INDICATOR);
    for (int j=0; j < referencedMeasures.size(); j++) {
      String measureName=referencedMeasures.get(j).toString();
      if (measureMap.containsKey(measureName)) {
        if (!createdBindings.containsKey(measureName)) {
          MeasureDefinition md=(MeasureDefinition)measureMap.get(measureName);
          IBinding newBinding=new Binding("String_Node_Str" + measureName);
          newBinding.setDataType(md.getDataType());
          newBinding.setExpression(new ScriptExpression(ExpressionUtil.createJSMeasureExpression(measureName)));
          newBinding.setAggrFunction(getRollUpAggregationFunctionName(md.getAggrFunction()));
          for (int a=0; a < levelNames.size(); a++)           newBinding.addAggregateOn(levelNames.get(a));
          IBinding b=getSameBindingInQuery(newBinding,bindingsInCubeQuery);
          if (b != null) {
            createdBindings.put(measureName,b);
          }
 else {
            createdBindings.put(measureName,newBinding);
            realBindings.add(newBinding);
          }
        }
      }
    }
  }
  for (int i=0; i < bindingsInCubeQuery.size(); i++) {
    IBinding binding=(IBinding)bindingsInCubeQuery.get(i);
    ScriptExpression expression=cloneExpression((ScriptExpression)binding.getExpression());
    List measureName=ExpressionCompilerUtil.extractColumnExpression(expression,ExpressionUtil.MEASURE_INDICATOR);
    if (measureName != null && measureName.size() > 0 && derivedMeasureMap.containsKey(measureName.get(0).toString())) {
      expression.setText("String_Node_Str" + ((ScriptExpression)((DerivedMeasureDefinition)derivedMeasureMap.get(measureName.get(0).toString())).getExpression()).getText() + "String_Node_Str");
      expression.setText(getReplacedExpressionText(expression.getText(),measureMap,derivedMeasureMap,createdBindings,binding,bindingsInCubeQuery));
      expression.setText(expression.getText().substring(1,expression.getText().length() - 1));
      binding.getAggregatOns().clear();
      binding.setAggrFunction(null);
      binding.setExpression(expression);
    }
  }
}