They were originally commented out because the expression parsing utils weren't ready (now they are).
206 lines
7.6 KiB
C#
206 lines
7.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using BCG.Utilities;
|
|
|
|
using bts;
|
|
|
|
using Bts.ColumnMapExpressions;
|
|
|
|
namespace BCG
|
|
{
|
|
/// <summary>
|
|
/// JoinNameResolver is used to bring together the 2 naming processes, ie the one done ny Join to resolve
|
|
/// clashes and the second, that the user does to rename columns
|
|
/// </summary>
|
|
public class JoinNameResolver
|
|
{
|
|
/// <summary>
|
|
/// Internal class to represent a column (ie name) and its association, left or right
|
|
/// </summary>
|
|
internal class OriginalNameInfo
|
|
{
|
|
public bool isLeft;
|
|
public string colName;
|
|
public OriginalNameInfo (string colName, bool isLeft) {this.colName = colName; this.isLeft = isLeft;}
|
|
public override int GetHashCode ()
|
|
{
|
|
string prependString = isLeft ? "left" : "right";
|
|
return String.Concat (prependString, colName).GetHashCode ();
|
|
}
|
|
public override bool Equals (object obj) {return GetHashCode () == obj.GetHashCode ();}
|
|
}
|
|
|
|
|
|
private ArrayList myJoinNames;
|
|
private JoinNameResolver.OriginalNameInfo[] myOriginalColInfo;
|
|
private JoinTableSchema myJoinSchema;
|
|
private IBlueTableSchema myLeftSchema, myRightSchema;
|
|
private string myLeftColExpression, myRightColExpression;
|
|
private string[] myUserRules, myResolvedNames;
|
|
private IDictionary myFinalMap = new Hashtable ();
|
|
|
|
|
|
public JoinNameResolver (JoinTableSchema joinSchema, IBlueTableSchema leftSchema, string leftColMapExpression,
|
|
IBlueTableSchema rightSchema, string rightColMapExpression)
|
|
{
|
|
myJoinSchema = joinSchema;
|
|
myLeftSchema = leftSchema;
|
|
myRightSchema = rightSchema;
|
|
myLeftColExpression = leftColMapExpression;
|
|
myRightColExpression = rightColMapExpression;
|
|
/*initialize the joinNames one-D structure. The id is removed as it does not
|
|
* take part in the renaming process */
|
|
myJoinNames = new ArrayList (joinSchema.columnNames);
|
|
/*initialize the originalInfo one-D structure. Its an array of OriginalNameInfo, ie name and a bool indicating that
|
|
* it belongs to the left or right table */
|
|
myOriginalColInfo = new OriginalNameInfo [joinSchema.columnNames.Length - 1];
|
|
myUserRules = new String[myOriginalColInfo.Length];
|
|
myFormOriginalNameInfo (true);
|
|
myFormOriginalNameInfo (false);
|
|
myFormUserRules ();
|
|
myFormResolvedNames ();
|
|
myFormFinalMap ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Forms the array representing the name as they appear in the JoinOperand.
|
|
/// This is done so that its alligned with the myJoinNames
|
|
/// </summary>
|
|
/// <param name="isLeft">indicating whether its the left operand or the right</param>
|
|
private void myFormOriginalNameInfo (bool isLeft)
|
|
{
|
|
IBlueTableSchema operandSchema = isLeft ? myLeftSchema : myRightSchema;
|
|
foreach (string colName in operandSchema.columnNames)
|
|
{
|
|
string joinName = myJoinSchema.getRenamedName (colName, isLeft);
|
|
//joinName is bound to be unique
|
|
int index = myJoinNames.IndexOf (joinName);
|
|
Log.assert(index >= 0 && index < myOriginalColInfo.Length);
|
|
myOriginalColInfo[index] = new OriginalNameInfo(colName, isLeft);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates the user mapping from the grammer and then actually call the methods that form the array of
|
|
/// user defined names
|
|
/// </summary>
|
|
private void myFormUserRules ()
|
|
{
|
|
if (DataStreamLoadFileConfig.instance.useStaticParser)
|
|
{
|
|
myFormUserRules(true, ColumnMapExpression.Parse(myLeftColExpression));
|
|
myFormUserRules(false, ColumnMapExpression.Parse(myRightColExpression));
|
|
}
|
|
else
|
|
{
|
|
// TODO (maybe): Actually use non-static parser (unclear what advantages it will have)
|
|
myFormUserRules(true, ColumnMapExpression.Parse(myLeftColExpression));
|
|
myFormUserRules(false, ColumnMapExpression.Parse(myRightColExpression));
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Forms the array of user defined names
|
|
/// </summary>
|
|
/// <param name="isLeft"></param>
|
|
/// <param name="cme"></param>
|
|
private void myFormUserRules (bool isLeft, ColumnMapExpression cme)
|
|
{
|
|
IDictionary userMap = cme.Columns;
|
|
if (cme.IsInclusive)
|
|
for(int i = 0; i < myOriginalColInfo.Length ; i++)
|
|
{
|
|
OriginalNameInfo oni = myOriginalColInfo[i];
|
|
if (oni.isLeft == isLeft)
|
|
{
|
|
if (userMap[oni.colName] != null)
|
|
{
|
|
if ((string)userMap[oni.colName] != oni.colName)
|
|
myUserRules[i] = (string)userMap[oni.colName];
|
|
else
|
|
myUserRules[i] = oni.colName;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < myOriginalColInfo.Length; i ++)
|
|
{
|
|
OriginalNameInfo oni = myOriginalColInfo[i];
|
|
if (oni.isLeft == isLeft)
|
|
{
|
|
if (userMap[oni.colName] == null)
|
|
myUserRules[i] = oni.colName;
|
|
else if ((string)userMap[oni.colName] != oni.colName)
|
|
myUserRules[i] = (string)userMap[oni.colName];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void myFormResolvedNames()
|
|
{
|
|
string prependString;
|
|
myResolvedNames = new string[myUserRules.Length];
|
|
Array.Copy (myUserRules, myResolvedNames, myUserRules.Length);
|
|
string[] temp = myUserRules;
|
|
myUserRules = new String[temp.Length + 1];
|
|
Array.Copy (temp, 0, myUserRules, 0, temp.Length);
|
|
myUserRules[myUserRules.Length - 1] = "id";
|
|
|
|
for (int i = 0; i < myResolvedNames.Length; i ++)
|
|
{
|
|
ArrayList occurances = myFindAllInCollection (myUserRules, myResolvedNames[i]);
|
|
while (occurances.Count > 1)
|
|
{
|
|
prependString = i < myLeftSchema.columnNames.Length ? "L." : "R.";
|
|
myResolvedNames[i] = prependString + myResolvedNames[i];
|
|
occurances = myFindAllInCollection (myUserRules, myResolvedNames[i]);
|
|
occurances.AddRange (myFindAllInCollection (myResolvedNames, myResolvedNames[i]));
|
|
}
|
|
}
|
|
}
|
|
|
|
private void myFormFinalMap ()
|
|
{
|
|
foreach (string resolvedName in myResolvedNames)
|
|
if (resolvedName != null)
|
|
myFinalMap [resolvedName] = myJoinNames[Array.IndexOf (myResolvedNames, resolvedName)];
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Utility method to invert a hashtable
|
|
/// </summary>
|
|
/// <param name="arg"></param>
|
|
/// <returns></returns>
|
|
|
|
private IDictionary reciprocateDictionary (IDictionary arg)
|
|
{
|
|
IDictionary retVal = new Hashtable ();
|
|
foreach (DictionaryEntry de in arg)
|
|
retVal[de.Value] = de.Key;
|
|
return retVal;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Utility method to find all occurances of an obkect in an IList
|
|
/// </summary>
|
|
/// <param name="list"></param>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
private ArrayList myFindAllInCollection (IList list, object obj)
|
|
{
|
|
ArrayList retVal = new ArrayList ();
|
|
for (int i = 0; i < list.Count; i++)
|
|
if (list[i] != null && list[i].Equals (obj))
|
|
retVal.Add (i);
|
|
return retVal;
|
|
}
|
|
|
|
public IDictionary getMap () {return myFinalMap;}
|
|
|
|
}
|
|
}
|