/// removing the non used predicates domains and the literals type of owners
/// </summary>
/// <param name="tokens">list </param>of tokens
/// <returns>cleaned list of tokens </returns>
private List <QueryBucket> cleanBucket(List <QueryBucket> queryBuckets)
{
#region removing Buckets which still have question left >1
foreach (QueryBucket querybucket in queryBuckets.ToList())
{
if (querybucket.questionLeft.Length > 0)
{
queryBuckets.Remove(querybucket);
}
}
#endregion
#region remove Predicates domains and type of owners
foreach (QueryBucket bucket in queryBuckets.ToList())
{
//adding predicates and literals to a list
List <LexiconPredicate> predicateList = new List <LexiconPredicate>();
List <LexiconLiteral> literalList = new List <LexiconLiteral>();
foreach (LexiconToken token in bucket.tokens)
{
if (token is LexiconPredicate)
{
predicateList.Add(token as LexiconPredicate);
}
if (token is LexiconLiteral)
{
literalList.Add(token as LexiconLiteral);
}
}
if (predicateList.Count > 0)
{
//removing domains and ranges that are not used
foreach (LexiconToken token in bucket.tokens.ToList())
{
if (token is LexiconPredicate)
{
//casting the lexicontoken to lexicon predicate
LexiconPredicate oldPredicate = token as LexiconPredicate;
//cloning the token to be modified
LexiconPredicate predicateToReplace = (LexiconPredicate)token.getClone(token);
foreach (string oldPredDomain in oldPredicate.domains.ToList())
{
bool exist = false;
foreach (LexiconLiteral tmpliteral in literalList)
{
if (tmpliteral.typeOfOwner.Contains(oldPredDomain))
{
exist = true;
}
}
//if this domains doesn't contained in any of literals type of owners remove it as it wont match|join with anything
if (!exist)
{
//old bucket = new bucket and then modify in the new in order then to be able to remove the old
predicateToReplace = oldPredicate.getClone(oldPredicate) as LexiconPredicate;
//removing domain not used
predicateToReplace.domains.Remove(oldPredDomain);
//remove the old bucket and replace it with new modified one // needed because of reference issues
bucket.tokens.Remove(oldPredicate);
bucket.tokens.Add(predicateToReplace);
oldPredicate = predicateToReplace;
//remove the predicate if it doesnt have any domains left
if (oldPredicate.domains.Count == 0)
{
bucket.tokens.Remove(oldPredicate);
predicateList.Remove(oldPredicate as LexiconPredicate);
//remove the bucket if it's free from predicates
if (bucket.tokens.Count == 0)
{
queryBuckets.Remove(bucket);
}
}
}
}
}
if (token is LexiconLiteral)
{
LexiconLiteral oldLiteral = token as LexiconLiteral;
LexiconLiteral newLiteral = token.getClone(token) as LexiconLiteral;
foreach (string typeofowner in oldLiteral.typeOfOwner.ToList())
{
bool exist = false;
foreach (LexiconPredicate tmmpredicate in predicateList)
{
if (tmmpredicate.domains.Contains(typeofowner))
{
exist = true;
}
}
if (!exist)
{
//taking a copy from the old literal in order to remove it from the bucket when replacing it with the newliteral
newLiteral = oldLiteral.getClone(oldLiteral) as LexiconLiteral;
// removing typeofowner not used
newLiteral.typeOfOwner.Remove(typeofowner);
// updating the bucket tokens by replacing the old literal with the new one
bucket.tokens.Remove(oldLiteral);
bucket.tokens.Add(newLiteral);
oldLiteral = newLiteral;
if (oldLiteral.typeOfOwner.Count == 0)
{
bucket.tokens.Remove(oldLiteral);
literalList.Remove(oldLiteral as LexiconLiteral);
//remove the bucket if it's free from Tokens
if (bucket.tokens.Count == 0)
{
queryBuckets.Remove(bucket);
}
}
}
}
}
}
}
else
{
bucket.literalOnly = true;
}
}
#endregion
#region remove the multiple domains and multiple ranges
foreach (QueryBucket bucket in queryBuckets)
{
foreach (LexiconToken predicateToken in bucket.tokens)
{
if (predicateToken is LexiconPredicate)
{
foreach (LexiconToken literalToken in bucket.tokens)
{
if (literalToken is LexiconLiteral && Enumerable.SequenceEqual((predicateToken as LexiconPredicate).domains, (literalToken as LexiconLiteral).typeOfOwner))
{
(predicateToken as LexiconPredicate).domains.RemoveRange(1, (predicateToken as LexiconPredicate).domains.Count - 1);
(literalToken as LexiconLiteral).typeOfOwner.RemoveRange(1, (literalToken as LexiconLiteral).typeOfOwner.Count - 1);
}
}
}
}
}
#endregion
return(queryBuckets);
}