<commit_before>/*******************************************************************************
 * Copyright (c) 2004, 2005 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.engine.olap.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.birt.core.data.ExpressionUtil;
import org.eclipse.birt.core.data.IDimLevel;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.exception.CoreException;
import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.IBaseQueryResults;
import org.eclipse.birt.data.engine.api.IBinding;
import org.eclipse.birt.data.engine.api.IConditionalExpression;
import org.eclipse.birt.data.engine.api.IScriptExpression;
import org.eclipse.birt.data.engine.api.aggregation.AggregationManager;
import org.eclipse.birt.data.engine.api.aggregation.IAggrFunction;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.expression.ExpressionCompilerUtil;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.birt.data.engine.impl.IQueryService;
import org.eclipse.birt.data.engine.olap.api.ICubeQueryResults;
import org.eclipse.birt.data.engine.olap.data.api.DimLevel;
import org.eclipse.birt.data.engine.olap.query.view.CubeQueryDefinitionUtil;
import org.eclipse.birt.data.engine.olap.script.JSCubeBindingObject;
import org.eclipse.birt.data.engine.script.ScriptConstants;
import org.mozilla.javascript.Scriptable;

/**
 *
 */

public class OlapExpressionUtil
{

	private static Logger logger = Logger.getLogger( OlapExpressionUtil.class.getName( ) );

	/**
	 * get the attribute reference name.
	 *
	 * @param dimName
	 * @param levelName
	 * @param attrName
	 * @return
	 */
	public static String getAttrReference( String dimName, String levelName,
			String attrName )
	{
		return dimName + '/' + levelName + '/' + attrName;
	}

	public static Set<IDimLevel> getAggregateOnLevel( IBinding targetBinding,
			List<IBinding> bindings, Set<IDimLevel> mostDetailed ) throws DataException
	{
		Set<String> visited = new HashSet<String>( );
		Set<IDimLevel> valid = new HashSet<IDimLevel>( );
		try
		{
			getBindings( visited, valid, targetBinding, bindings, mostDetailed );
		}
		catch ( BirtException e )
		{
			throw DataException.wrap( e );
		}

		return valid;
	}

	public static Set<IDimLevel> getAggregateOnLevel( String bindingName,
			List<IBinding> bindings, Set<IDimLevel> mostDetailed ) throws DataException
	{
		return getAggregateOnLevel( getBinding( bindingName, bindings ), bindings, mostDetailed );
	}

	public static Set<IDimLevel> getAggregateOnLevel( IBaseExpression expr,
			List<IBinding> bindings, Set<IDimLevel> mostDetailed ) throws DataException
	{
		Set<String> visited = new HashSet<String>( );
		Set<IDimLevel> valid = new HashSet<IDimLevel>( );
		try
		{
			getBindings( visited, valid, expr, bindings, mostDetailed );
		}
		catch ( BirtException e )
		{
			throw DataException.wrap( e );
		}
		return valid;
	}

	private static void getBindings( Set<String> visited, Set<IDimLevel> aggOn,
			IBaseExpression expr, List<IBinding> bindings, Set<IDimLevel> mostDetailed )
			throws DataException, CoreException
	{
		if ( expr == null )
			return;
		List<String> currentVisitBindings = ExpressionCompilerUtil.extractColumnExpression( expr,
				ExpressionUtil.DATA_INDICATOR );
		currentVisitBindings.removeAll( visited );

		if ( expr instanceof IScriptExpression )
		{
			aggOn.addAll( ExpressionUtil.getReferencedDimLevel( ( (IScriptExpression) expr ).getText( ) ) );

			for ( String bindingName : currentVisitBindings )
			{
				getBindings( visited,
						aggOn,
						getBinding( bindingName, bindings ),
						bindings, mostDetailed);
			}
		}
		else if ( expr instanceof IConditionalExpression )
		{
			IConditionalExpression cond = (IConditionalExpression) expr;
			getBindings( visited, aggOn, cond.getExpression( ), bindings, mostDetailed );
			getBindings( visited, aggOn, cond.getOperand1( ), bindings, mostDetailed );
			getBindings( visited, aggOn, cond.getOperand2( ), bindings, mostDetailed );
		}
	}

	private static void getBindings( Set<String> visited, Set<IDimLevel> aggOn,
			IBinding binding, List<IBinding> bindings, Set<IDimLevel> mostDetailed ) throws DataException,
			CoreException
	{
		if ( binding == null || visited.contains( binding.getBindingName( ) ) )
		{
			return;
		}
		
		//if this binding is aggregation, return its aggregate on levels.
		if( !binding.getAggregatOns( ).isEmpty( ) )
		{
			for ( String expr : ( (List<String>) binding.getAggregatOns( ) ) )
			{
				aggOn.addAll( ExpressionUtil.getReferencedDimLevel( expr ) );
			}
			return;<commit_msg>Fix sorting on derived measure binding not work. [60262]<commit_after>/*******************************************************************************
 * Copyright (c) 2004, 2005 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.engine.olap.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.birt.core.data.ExpressionUtil;
import org.eclipse.birt.core.data.IDimLevel;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.exception.CoreException;
import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.IBaseQueryResults;
import org.eclipse.birt.data.engine.api.IBinding;
import org.eclipse.birt.data.engine.api.IConditionalExpression;
import org.eclipse.birt.data.engine.api.IScriptExpression;
import org.eclipse.birt.data.engine.api.aggregation.AggregationManager;
import org.eclipse.birt.data.engine.api.aggregation.IAggrFunction;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.expression.ExpressionCompilerUtil;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.birt.data.engine.impl.IQueryService;
import org.eclipse.birt.data.engine.olap.api.ICubeQueryResults;
import org.eclipse.birt.data.engine.olap.data.api.DimLevel;
import org.eclipse.birt.data.engine.olap.query.view.CubeQueryDefinitionUtil;
import org.eclipse.birt.data.engine.olap.script.JSCubeBindingObject;
import org.eclipse.birt.data.engine.script.ScriptConstants;
import org.mozilla.javascript.Scriptable;

/**
 *
 */

public class OlapExpressionUtil
{

	private static Logger logger = Logger.getLogger( OlapExpressionUtil.class.getName( ) );

	/**
	 * get the attribute reference name.
	 *
	 * @param dimName
	 * @param levelName
	 * @param attrName
	 * @return
	 */
	public static String getAttrReference( String dimName, String levelName,
			String attrName )
	{
		return dimName + '/' + levelName + '/' + attrName;
	}

	public static Set<IDimLevel> getAggregateOnLevel( IBinding targetBinding,
			List<IBinding> bindings, Set<IDimLevel> mostDetailed ) throws DataException
	{
		Set<String> visited = new HashSet<String>( );
		Set<IDimLevel> valid = new HashSet<IDimLevel>( );
		try
		{
			getBindings( visited, valid, targetBinding, bindings, mostDetailed );
		}
		catch ( BirtException e )
		{
			throw DataException.wrap( e );
		}

		return valid;
	}

	public static Set<IDimLevel> getAggregateOnLevel( String bindingName,
			List<IBinding> bindings, Set<IDimLevel> mostDetailed ) throws DataException
	{
		return getAggregateOnLevel( getBinding( bindingName, bindings ), bindings, mostDetailed );
	}

	public static Set<IDimLevel> getAggregateOnLevel( IBaseExpression expr,
			List<IBinding> bindings, Set<IDimLevel> mostDetailed ) throws DataException
	{
		Set<String> visited = new HashSet<String>( );
		Set<IDimLevel> valid = new HashSet<IDimLevel>( );
		try
		{
			getBindings( visited, valid, expr, bindings, mostDetailed );
		}
		catch ( BirtException e )
		{
			throw DataException.wrap( e );
		}
		return valid;
	}

	private static void getBindings( Set<String> visited, Set<IDimLevel> aggOn,
			IBaseExpression expr, List<IBinding> bindings, Set<IDimLevel> mostDetailed )
			throws DataException, CoreException
	{
		if ( expr == null )
			return;
		List<String> currentVisitBindings = ExpressionCompilerUtil.extractColumnExpression( expr,
				ExpressionUtil.DATA_INDICATOR );
		currentVisitBindings.removeAll( visited );

		if ( expr instanceof IScriptExpression )
		{
			aggOn.addAll( ExpressionUtil.getReferencedDimLevel( ( (IScriptExpression) expr ).getText( ) ) );

			for ( String bindingName : currentVisitBindings )
			{
				getBindings( visited,
						aggOn,
						getBinding( bindingName, bindings ),
						bindings, mostDetailed);
			}
		}
		else if ( expr instanceof IConditionalExpression )
		{
			IConditionalExpression cond = (IConditionalExpression) expr;
			getBindings( visited, aggOn, cond.getExpression( ), bindings, mostDetailed );
			getBindings( visited, aggOn, cond.getOperand1( ), bindings, mostDetailed );
			getBindings( visited, aggOn, cond.getOperand2( ), bindings, mostDetailed );
		}
	}

	private static void getBindings( Set<String> visited, Set<IDimLevel> aggOn,
			IBinding binding, List<IBinding> bindings, Set<IDimLevel> mostDetailed ) throws DataException,
			CoreException
	{
		if ( binding == null || visited.contains( binding.getBindingName( ) ) )
		{
			return;
		}
		
		//if this binding is aggregation, return its aggregate on levels.
		if( !binding.getAggregatOns( ).isEmpty( ) )
		{
			for ( String expr : ( (List<String>) binding.getAggregatOns( ) ) )
			{
				aggOn.addAll( ExpressionUtil.getReferencedDimLevel( expr ) );
			}
			
			if ( !CubeQueryDefinitionUtil.isRunnnigAggr( binding.getAggrFunction( ) ) )
				return;
{'fraction_non_alphanumeric': 0.132768621673879, 'fraction_numerical': 0.00340668446736028, 'mean_word_length': 4.786893979754928, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7396542', 'n_tokens_mistral': 3615, 'n_tokens_neox': 3276, 'n_words': 716}