<commit_before>
package edu.wustl.common.query.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import edu.common.dynamicextensions.domaininterface.AssociationInterface;
import edu.common.dynamicextensions.domaininterface.AttributeInterface;
import edu.common.dynamicextensions.domaininterface.AttributeTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.BooleanTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.DateTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.DoubleTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.EntityInterface;
import edu.common.dynamicextensions.domaininterface.IntegerTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.LongTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.StringTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.databaseproperties.ConstraintKeyPropertiesInterface;
import edu.common.dynamicextensions.domaininterface.databaseproperties.ConstraintPropertiesInterface;
import edu.common.dynamicextensions.entitymanager.DataTypeFactory;
import edu.common.dynamicextensions.entitymanager.EntityManagerConstantsInterface;
import edu.common.dynamicextensions.exception.DataTypeFactoryInitializationException;
import edu.common.dynamicextensions.exception.DynamicExtensionsSystemException;
import edu.wustl.common.query.exeptions.SQLXMLException;
import edu.wustl.common.query.impl.predicate.PredicateGenerator;
import edu.wustl.common.query.queryobject.impl.OutputTreeDataNode;
import edu.wustl.common.query.queryobject.impl.metadata.QueryOutputTreeAttributeMetadata;
import edu.wustl.common.query.queryobject.util.QueryObjectProcessor;
import edu.wustl.common.querysuite.exceptions.MultipleRootsException;
import edu.wustl.common.querysuite.exceptions.SqlException;
import edu.wustl.common.querysuite.metadata.associations.IAssociation;
import edu.wustl.common.querysuite.metadata.associations.IIntraModelAssociation;
import edu.wustl.common.querysuite.queryobject.ICondition;
import edu.wustl.common.querysuite.queryobject.IExpression;
import edu.wustl.common.querysuite.queryobject.IOutputAttribute;
import edu.wustl.common.querysuite.queryobject.IQuery;
import edu.wustl.common.querysuite.queryobject.RelationalOperator;
import edu.wustl.common.querysuite.queryobject.impl.JoinGraph;
import edu.wustl.common.querysuite.queryobject.impl.ParameterizedQuery;
import edu.wustl.common.util.logger.Logger;
import edu.wustl.metadata.util.DyExtnObjectCloner;
import edu.wustl.query.util.global.Constants;
import edu.wustl.query.util.global.Utility;
import edu.wustl.query.util.querysuite.QueryCSMUtil;
import edu.wustl.query.xquerydatatypes.XQueryAttributeType;
import edu.wustl.query.xquerydatatypes.XQueryDataTypeInitializationException;

/**
 * 
 * @author juberahamad_patel
 * 
 * abstract class from which the xquery generator classes extend
 *
 */
public abstract class AbstractXQueryGenerator extends QueryGenerator
{

	/**
	 * the suffix used to generate sql column names on the fly, like column0, column1 etc.
	 */
	protected int suffix = 0;
	
	/**
	 * the set of expressions whose entites have a separate XML file, where they are the root element
	 */
	protected Set<IExpression> mainExpressions;

	/**
	 * map of expressions for entities and the xpath used to reach them
	 * xpath could be a variable name or a path
	 */
	protected Map<IExpression, String> entityPaths;

	/**
	 * map of expressions which have one to many relationships with their parents 
	 * expressions and their respective target roles
	 */
	protected Map<IExpression, String> targetRoles;

	/**
	 * the map of exprssions for which a variable is created in the for clause and the variables 
	 */
	protected Map<IExpression, String> forVariables;

	/**
	 * the selected attributes (ie the ones going in SELECT part) and their aliases
	 */
	protected Map<IOutputAttribute, String> attributeAliases;

	//private static org.apache.log4j.Logger logger =Logger.getLogger(XQueryGenerator.class);

	/**
	 * Generates SQL for the given Query Object.
	 * 
	 * @param query 
	 * @return the String representing SQL for the given Query object.
	 * @throws MultipleRootsException 
	 * @throws SqlException 
	 * @throws XQueryDataTypeInitializationException 
	 * @see edu.wustl.common.querysuite.queryengine.ISqlGenerator#generateSQL(edu.wustl.common.querysuite.queryobject.IQuery)
	 */

	/*
	 * The following function takes IQuery object as input for further processing
	 * @parameters : IQuery query= The query object
	 * @parameters : char QueryType = representing the kind of query whether aggregate or normal
	 */
	public String generateQuery(IQuery query) throws MultipleRootsException, SqlException
	{
		String formedQuery = null;
		try
		{
			prepare(query);
			formedQuery = formQuery();

		}
		catch (SQLXMLException e)
		{
			throw new SqlException("problem while trying to build xquery", e);
		}
		catch (DynamicExtensionsSystemException e)
		{
			throw new SqlException("problem while trying to build xquery", e);
		}
		catch (XQueryDataTypeInitializationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		log(formedQuery);
		return formedQuery;
	}

	/**
	 * prepare the data structures and information required to generate xquery
	 * This is step I of xquery generation 
	 * @param query
	 * @throws MultipleRootsException
	 */
	private void prepare(IQuery query) throws MultipleRootsException
	{
		//populate selected attributes and their aliases
		attributeAliases = new LinkedHashMap<IOutputAttribute, String>();
		IQuery queryClone = new DyExtnObjectCloner().clone(query);
		for (IOutputAttribute selectedAttribute : ((ParameterizedQuery) queryClone)
				.getOutputAttributeList())
		{
			String attributeAlias = getAliasFor(selectedAttribute);
			attributeAliases.put(selectedAttribute, attributeAlias);
		}<commit_msg>do not log xQueries while executing Test Cases<commit_after>
package edu.wustl.common.query.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import edu.common.dynamicextensions.domaininterface.AssociationInterface;
import edu.common.dynamicextensions.domaininterface.AttributeInterface;
import edu.common.dynamicextensions.domaininterface.AttributeTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.BooleanTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.DateTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.DoubleTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.EntityInterface;
import edu.common.dynamicextensions.domaininterface.IntegerTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.LongTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.StringTypeInformationInterface;
import edu.common.dynamicextensions.domaininterface.databaseproperties.ConstraintKeyPropertiesInterface;
import edu.common.dynamicextensions.domaininterface.databaseproperties.ConstraintPropertiesInterface;
import edu.common.dynamicextensions.entitymanager.DataTypeFactory;
import edu.common.dynamicextensions.entitymanager.EntityManagerConstantsInterface;
import edu.common.dynamicextensions.exception.DataTypeFactoryInitializationException;
import edu.common.dynamicextensions.exception.DynamicExtensionsSystemException;
import edu.wustl.common.query.exeptions.SQLXMLException;
import edu.wustl.common.query.impl.predicate.PredicateGenerator;
import edu.wustl.common.query.queryobject.impl.OutputTreeDataNode;
import edu.wustl.common.query.queryobject.impl.metadata.QueryOutputTreeAttributeMetadata;
import edu.wustl.common.query.queryobject.util.QueryObjectProcessor;
import edu.wustl.common.querysuite.exceptions.MultipleRootsException;
import edu.wustl.common.querysuite.exceptions.SqlException;
import edu.wustl.common.querysuite.metadata.associations.IAssociation;
import edu.wustl.common.querysuite.metadata.associations.IIntraModelAssociation;
import edu.wustl.common.querysuite.queryobject.ICondition;
import edu.wustl.common.querysuite.queryobject.IExpression;
import edu.wustl.common.querysuite.queryobject.IOutputAttribute;
import edu.wustl.common.querysuite.queryobject.IQuery;
import edu.wustl.common.querysuite.queryobject.RelationalOperator;
import edu.wustl.common.querysuite.queryobject.impl.JoinGraph;
import edu.wustl.common.querysuite.queryobject.impl.ParameterizedQuery;
import edu.wustl.common.util.logger.Logger;
import edu.wustl.metadata.util.DyExtnObjectCloner;
import edu.wustl.query.util.global.Constants;
import edu.wustl.query.util.global.Utility;
import edu.wustl.query.util.global.Variables;
import edu.wustl.query.util.querysuite.QueryCSMUtil;
import edu.wustl.query.xquerydatatypes.XQueryAttributeType;
import edu.wustl.query.xquerydatatypes.XQueryDataTypeInitializationException;

/**
 * 
 * @author juberahamad_patel
 * 
 * abstract class from which the xquery generator classes extend
 *
 */
public abstract class AbstractXQueryGenerator extends QueryGenerator
{

	/**
	 * the suffix used to generate sql column names on the fly, like column0, column1 etc.
	 */
	protected int suffix = 0;
	
	/**
	 * the set of expressions whose entites have a separate XML file, where they are the root element
	 */
	protected Set<IExpression> mainExpressions;

	/**
	 * map of expressions for entities and the xpath used to reach them
	 * xpath could be a variable name or a path
	 */
	protected Map<IExpression, String> entityPaths;

	/**
	 * map of expressions which have one to many relationships with their parents 
	 * expressions and their respective target roles
	 */
	protected Map<IExpression, String> targetRoles;

	/**
	 * the map of exprssions for which a variable is created in the for clause and the variables 
	 */
	protected Map<IExpression, String> forVariables;

	/**
	 * the selected attributes (ie the ones going in SELECT part) and their aliases
	 */
	protected Map<IOutputAttribute, String> attributeAliases;

	//private static org.apache.log4j.Logger logger =Logger.getLogger(XQueryGenerator.class);

	/**
	 * Generates SQL for the given Query Object.
	 * 
	 * @param query 
	 * @return the String representing SQL for the given Query object.
	 * @throws MultipleRootsException 
	 * @throws SqlException 
	 * @throws XQueryDataTypeInitializationException 
	 * @see edu.wustl.common.querysuite.queryengine.ISqlGenerator#generateSQL(edu.wustl.common.querysuite.queryobject.IQuery)
	 */

	/*
	 * The following function takes IQuery object as input for further processing
	 * @parameters : IQuery query= The query object
	 * @parameters : char QueryType = representing the kind of query whether aggregate or normal
	 */
	public String generateQuery(IQuery query) throws MultipleRootsException, SqlException
	{
		String formedQuery = null;
		try
		{
			prepare(query);
			formedQuery = formQuery();

		}
		catch (SQLXMLException e)
		{
			throw new SqlException("problem while trying to build xquery", e);
		}
		catch (DynamicExtensionsSystemException e)
		{
			throw new SqlException("problem while trying to build xquery", e);
		}
		catch (XQueryDataTypeInitializationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!Variables.isExecutingTCFramework)
		{
			log(formedQuery);
		}
		return formedQuery;
	}

	/**
	 * prepare the data structures and information required to generate xquery
	 * This is step I of xquery generation 
	 * @param query
	 * @throws MultipleRootsException
	 */
	private void prepare(IQuery query) throws MultipleRootsException
	{
		//populate selected attributes and their aliases
		attributeAliases = new LinkedHashMap<IOutputAttribute, String>();
		IQuery queryClone = new DyExtnObjectCloner().clone(query);
		for (IOutputAttribute selectedAttribute : ((ParameterizedQuery) queryClone)
				.getOutputAttributeList())
		{
			String attributeAlias = getAliasFor(selectedAttribute);
			attributeAliases.put(selectedAttribute, attributeAlias);
		}
{'fraction_non_alphanumeric': 0.08126590330788803, 'fraction_numerical': 0.0006361323155216285, 'mean_word_length': 5.689893617021276, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '139280', 'n_tokens_mistral': 3791, 'n_tokens_neox': 3597, 'n_words': 822}