Skip to content

Commit

Permalink
Code optimizations to operation printer (-150 LOC, 1KB assembly size)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Nov 30, 2024
1 parent bbab8ba commit 8f36223
Showing 1 changed file with 7 additions and 156 deletions.
163 changes: 7 additions & 156 deletions RDFSharp/Query/Mirella/RDFOperationPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,14 @@ internal static string PrintInsertWhereOperation(RDFInsertWhereOperation insertW

if (insertWhereOperation != null)
{
#region PREFIXES
List<RDFNamespace> prefixes = insertWhereOperation.GetPrefixes();
sb.Append(PrintPrefixes(prefixes));
#endregion

#region TEMPLATES

sb.AppendLine("INSERT {");
insertWhereOperation.InsertTemplates.ForEach(tp => sb.Append(PrintPattern(prefixes, tp)));
sb.AppendLine("}");
#endregion

#region BODY
sb.AppendLine("WHERE {");
sb.Append(PrintBodyMembers(prefixes, insertWhereOperation));
sb.Append("}");
#endregion

RDFQueryPrinter.PrintWhereClause(insertWhereOperation, sb, prefixes, string.Empty, 0, false);
}

return sb.ToString();
Expand All @@ -91,16 +83,12 @@ internal static string PrintDeleteDataOperation(RDFDeleteDataOperation deleteDat

if (deleteDataOperation != null)
{
#region PREFIXES
List<RDFNamespace> prefixes = deleteDataOperation.GetPrefixes();
sb.Append(PrintPrefixes(prefixes));
#endregion

#region TEMPLATES

sb.AppendLine("DELETE DATA {");
deleteDataOperation.DeleteTemplates.ForEach(tp => sb.Append(PrintPattern(prefixes, tp)));
sb.Append("}");
#endregion
}

return sb.ToString();
Expand All @@ -115,22 +103,14 @@ internal static string PrintDeleteWhereOperation(RDFDeleteWhereOperation deleteW

if (deleteWhereOperation != null)
{
#region PREFIXES
List<RDFNamespace> prefixes = deleteWhereOperation.GetPrefixes();
sb.Append(PrintPrefixes(prefixes));
#endregion

#region TEMPLATES
sb.AppendLine("DELETE {");
deleteWhereOperation.DeleteTemplates.ForEach(tp => sb.Append(PrintPattern(prefixes, tp)));
sb.AppendLine("}");
#endregion

#region BODY
sb.AppendLine("WHERE {");
sb.Append(PrintBodyMembers(prefixes, deleteWhereOperation));
sb.Append("}");
#endregion
RDFQueryPrinter.PrintWhereClause(deleteWhereOperation, sb, prefixes, string.Empty, 0, false);
}

return sb.ToString();
Expand All @@ -145,25 +125,18 @@ internal static string PrintDeleteInsertWhereOperation(RDFDeleteInsertWhereOpera

if (deleteInsertWhereOperation != null)
{
#region PREFIXES
List<RDFNamespace> prefixes = deleteInsertWhereOperation.GetPrefixes();
sb.Append(PrintPrefixes(prefixes));
#endregion

#region TEMPLATES
sb.AppendLine("DELETE {");
deleteInsertWhereOperation.DeleteTemplates.ForEach(tp => sb.Append(PrintPattern(prefixes, tp)));
sb.AppendLine("}");

sb.AppendLine("INSERT {");
deleteInsertWhereOperation.InsertTemplates.ForEach(tp => sb.Append(PrintPattern(prefixes, tp)));
sb.AppendLine("}");
#endregion

#region BODY
sb.AppendLine("WHERE {");
sb.Append(PrintBodyMembers(prefixes, deleteInsertWhereOperation));
sb.Append("}");
#endregion
RDFQueryPrinter.PrintWhereClause(deleteInsertWhereOperation, sb, prefixes, string.Empty, 0, false);
}

return sb.ToString();
Expand Down Expand Up @@ -253,128 +226,6 @@ private static string PrintPattern(List<RDFNamespace> operationPrefixes, RDFPatt

return string.Concat(" ", tpString, " .", Environment.NewLine);
}

private static string PrintBodyMembers(List<RDFNamespace> operationPrefixes, RDFOperation operation)
{
StringBuilder sb = new StringBuilder();

#region MEMBERS
bool printingUnion = false;
List<RDFQueryMember> evaluableQueryMembers = operation.GetEvaluableQueryMembers().ToList();
RDFQueryMember lastQueryMbr = evaluableQueryMembers.LastOrDefault();
foreach (RDFQueryMember queryMember in evaluableQueryMembers)
{
#region PATTERNGROUPS
if (queryMember is RDFPatternGroup pGroup)
{
//Current pattern group is set as UNION with the next one
if (pGroup.JoinAsUnion)
{
//Current pattern group IS NOT the last of the query
//(so UNION keyword must be appended at last)
if (!queryMember.Equals(lastQueryMbr))
{
//Begin a new Union block
if (!printingUnion)
{
printingUnion = true;
sb.AppendLine(" {");
}
sb.Append(RDFQueryPrinter.PrintPatternGroup(pGroup, 2, true, operationPrefixes));
sb.AppendLine(" UNION");
}

//Current pattern group IS the last of the query
//(so UNION keyword must not be appended at last)
else
{
//End the Union block
if (printingUnion)
{
printingUnion = false;
sb.Append(RDFQueryPrinter.PrintPatternGroup(pGroup, 2, true, operationPrefixes));
sb.AppendLine(" }");
}
else
sb.Append(RDFQueryPrinter.PrintPatternGroup(pGroup, 0, false, operationPrefixes));
}
}

//Current pattern group is set as INTERSECT with the next one
else
{
//End the Union block
if (printingUnion)
{
printingUnion = false;
sb.Append(RDFQueryPrinter.PrintPatternGroup(pGroup, 2, true, operationPrefixes));
sb.AppendLine(" }");
}
else
sb.Append(RDFQueryPrinter.PrintPatternGroup(pGroup, 0, false, operationPrefixes));
}
}
#endregion

#region SUBQUERY
else if (queryMember is RDFSelectQuery sqQueryMember)
{
//Merge main query prefixes
operation.GetPrefixes().ForEach(pf1 => sqQueryMember.AddPrefix(pf1));

//Current subquery is set as UNION with the next one
if (sqQueryMember.JoinAsUnion)
{
//Current subquery IS NOT the last of the query
//(so UNION keyword must be appended at last)
if (!sqQueryMember.Equals(lastQueryMbr))
{
//Begin a new Union block
if (!printingUnion)
{
printingUnion = true;
sb.AppendLine(" {");
}
sb.Append(RDFQueryPrinter.PrintSelectQuery(sqQueryMember, 1, true));
sb.AppendLine(" UNION");
}

//Current query IS the last of the query
//(so UNION keyword must not be appended at last)
else
{
//End the Union block
if (printingUnion)
{
printingUnion = false;
sb.Append(RDFQueryPrinter.PrintSelectQuery(sqQueryMember, 1, true));
sb.AppendLine(" }");
}
else
sb.Append(RDFQueryPrinter.PrintSelectQuery(sqQueryMember, 1, false));
}
}

//Current query is set as INTERSECT with the next one
else
{
//End the Union block
if (printingUnion)
{
printingUnion = false;
sb.Append(RDFQueryPrinter.PrintSelectQuery(sqQueryMember, 1, true));
sb.AppendLine(" }");
}
else
sb.Append(RDFQueryPrinter.PrintSelectQuery(sqQueryMember, 1, false));
}
}
#endregion
}
#endregion

return sb.ToString();
}
#endregion
}
}

0 comments on commit 8f36223

Please sign in to comment.