diff options
author | Nicholas Car <nicholas.car@surroundaustralia.com> | 2021-07-20 23:13:06 +1000 |
---|---|---|
committer | Nicholas Car <nicholas.car@surroundaustralia.com> | 2021-07-20 23:13:06 +1000 |
commit | 17969dccdc6b182ef19c1a9e0b86a6585026aeab (patch) | |
tree | e9d421e78624f0e9cd11f8217c2d92c129963012 /rdflib/plugins/sparql/algebra.py | |
parent | 7ebfd306543d8cc0a8b48700870e6fba3d7d256d (diff) | |
download | rdflib-17969dccdc6b182ef19c1a9e0b86a6585026aeab.tar.gz |
blacked everything6.0.0
Diffstat (limited to 'rdflib/plugins/sparql/algebra.py')
-rw-r--r-- | rdflib/plugins/sparql/algebra.py | 486 |
1 files changed, 349 insertions, 137 deletions
diff --git a/rdflib/plugins/sparql/algebra.py b/rdflib/plugins/sparql/algebra.py index 6107d38c..dd5216bb 100644 --- a/rdflib/plugins/sparql/algebra.py +++ b/rdflib/plugins/sparql/algebra.py @@ -808,14 +808,21 @@ def translateAlgebra(query_algebra: Query = None): :return: The query form generated from the SPARQL 1.1 algebra tree for select queries. """ + def overwrite(text): file = open("query.txt", "w+") file.write(text) file.close() - def replace(old, new, search_from_match: str = None, search_from_match_occurrence: int = None, count: int = 1): + def replace( + old, + new, + search_from_match: str = None, + search_from_match_occurrence: int = None, + count: int = 1, + ): # Read in the file - with open('query.txt', 'r') as file: + with open("query.txt", "r") as file: filedata = file.read() def find_nth(haystack, needle, n): @@ -826,7 +833,9 @@ def translateAlgebra(query_algebra: Query = None): return start if search_from_match and search_from_match_occurrence: - position = find_nth(filedata, search_from_match, search_from_match_occurrence) + position = find_nth( + filedata, search_from_match, search_from_match_occurrence + ) filedata_pre = filedata[:position] filedata_post = filedata[position:].replace(old, new, count) filedata = filedata_pre + filedata_post @@ -834,7 +843,7 @@ def translateAlgebra(query_algebra: Query = None): filedata = filedata.replace(old, new, count) # Write the file out again - with open('query.txt', 'w') as file: + with open("query.txt", "w") as file: file.write(filedata) def convert_node_arg(node_arg): @@ -848,7 +857,8 @@ def translateAlgebra(query_algebra: Query = None): return node_arg else: raise ExpressionNotCoveredException( - "The expression {0} might not be covered yet.".format(node_arg)) + "The expression {0} might not be covered yet.".format(node_arg) + ) def sparql_query_text(node): """ @@ -867,8 +877,10 @@ def translateAlgebra(query_algebra: Query = None): elif node.name == "BGP": # Identifiers or Paths # Negated path throws a type error. Probably n3() method of negated paths should be fixed - triples = "".join(triple[0].n3() + " " + triple[1].n3() + " " + triple[2].n3() + "." - for triple in node.triples) + triples = "".join( + triple[0].n3() + " " + triple[1].n3() + " " + triple[2].n3() + "." + for triple in node.triples + ) replace("{BGP}", triples) # The dummy -*-SELECT-*- is placed during a SelectQuery or Multiset pattern in order to be able # to match extended variables in a specific Select-clause (see "Extend" below) @@ -880,32 +892,45 @@ def translateAlgebra(query_algebra: Query = None): elif node.name == "Join": replace("{Join}", "{" + node.p1.name + "}{" + node.p2.name + "}") # elif node.name == "LeftJoin": - replace("{LeftJoin}", "{" + node.p1.name + "}OPTIONAL{{" + node.p2.name + "}}") + replace( + "{LeftJoin}", + "{" + node.p1.name + "}OPTIONAL{{" + node.p2.name + "}}", + ) elif node.name == "Filter": if isinstance(node.expr, CompValue): expr = node.expr.name else: - raise ExpressionNotCoveredException("This expression might not be covered yet.") + raise ExpressionNotCoveredException( + "This expression might not be covered yet." + ) if node.p: # Filter with p=AggregateJoin = Having if node.p.name == "AggregateJoin": replace("{Filter}", "{" + node.p.name + "}") replace("{Having}", "HAVING({" + expr + "})") else: - replace("{Filter}", "FILTER({" + expr + "}) {" + node.p.name + "}") + replace( + "{Filter}", "FILTER({" + expr + "}) {" + node.p.name + "}" + ) else: replace("{Filter}", "FILTER({" + expr + "})") elif node.name == "Union": - replace("{Union}", "{{" + node.p1.name + "}}UNION{{" + node.p2.name + "}}") + replace( + "{Union}", "{{" + node.p1.name + "}}UNION{{" + node.p2.name + "}}" + ) elif node.name == "Graph": expr = "GRAPH " + node.term.n3() + " {{" + node.p.name + "}}" replace("{Graph}", expr) elif node.name == "Extend": - query_string = open('query.txt', 'r').read().lower() - select_occurrences = query_string.count('-*-select-*-') - replace(node.var.n3(), "(" + convert_node_arg(node.expr) + " as " + node.var.n3() + ")", - search_from_match='-*-select-*-', search_from_match_occurrence=select_occurrences) + query_string = open("query.txt", "r").read().lower() + select_occurrences = query_string.count("-*-select-*-") + replace( + node.var.n3(), + "(" + convert_node_arg(node.expr) + " as " + node.var.n3() + ")", + search_from_match="-*-select-*-", + search_from_match_occurrence=select_occurrences, + ) replace("{Extend}", "{" + node.p.name + "}") elif node.name == "Minus": expr = "{" + node.p1.name + "}MINUS{{" + node.p2.name + "}}" @@ -917,7 +942,9 @@ def translateAlgebra(query_algebra: Query = None): if isinstance(var, Identifier): group_by_vars.append(var.n3()) else: - raise ExpressionNotCoveredException("This expression might not be covered yet.") + raise ExpressionNotCoveredException( + "This expression might not be covered yet." + ) replace("{Group}", "{" + node.p.name + "}") replace("{GroupBy}", "GROUP BY " + " ".join(group_by_vars) + " ") else: @@ -928,33 +955,66 @@ def translateAlgebra(query_algebra: Query = None): if isinstance(agg_func.res, Identifier): identifier = agg_func.res.n3() else: - raise ExpressionNotCoveredException("This expression might not be covered yet.") - agg_func_name = agg_func.name.split('_')[1] + raise ExpressionNotCoveredException( + "This expression might not be covered yet." + ) + agg_func_name = agg_func.name.split("_")[1] distinct = "" if agg_func.distinct: distinct = agg_func.distinct + " " - if agg_func_name == 'GroupConcat': - replace(identifier, "GROUP_CONCAT" + "(" + distinct - + agg_func.vars.n3() + ";SEPARATOR=" + agg_func.separator.n3() + ")") + if agg_func_name == "GroupConcat": + replace( + identifier, + "GROUP_CONCAT" + + "(" + + distinct + + agg_func.vars.n3() + + ";SEPARATOR=" + + agg_func.separator.n3() + + ")", + ) else: - replace(identifier, - agg_func_name.upper() + "(" + distinct + convert_node_arg(agg_func.vars) + ")") + replace( + identifier, + agg_func_name.upper() + + "(" + + distinct + + convert_node_arg(agg_func.vars) + + ")", + ) # For non-aggregated variables the aggregation function "sample" is automatically assigned. # However, we do not want to have "sample" wrapped around non-aggregated variables. That is # why we replace it. If "sample" is used on purpose it will not be replaced as the alias # must be different from the variable in this case. - replace("(SAMPLE({0}) as {0})".format(convert_node_arg(agg_func.vars)), - convert_node_arg(agg_func.vars)) + replace( + "(SAMPLE({0}) as {0})".format(convert_node_arg(agg_func.vars)), + convert_node_arg(agg_func.vars), + ) elif node.name == "GroupGraphPatternSub": - replace("GroupGraphPatternSub", " ".join([convert_node_arg(pattern) for pattern in node.part])) + replace( + "GroupGraphPatternSub", + " ".join([convert_node_arg(pattern) for pattern in node.part]), + ) elif node.name == "TriplesBlock": print("triplesblock") - replace("{TriplesBlock}", "".join(triple[0].n3() + " " + triple[1].n3() + " " + triple[2].n3() + "." - for triple in node.triples)) + replace( + "{TriplesBlock}", + "".join( + triple[0].n3() + + " " + + triple[1].n3() + + " " + + triple[2].n3() + + "." + for triple in node.triples + ), + ) # 18.2 Solution modifiers elif node.name == "ToList": - raise ExpressionNotCoveredException("This expression might not be covered yet.") + raise ExpressionNotCoveredException( + "This expression might not be covered yet." + ) elif node.name == "OrderBy": order_conditions = [] for c in node.expr: @@ -966,7 +1026,9 @@ def translateAlgebra(query_algebra: Query = None): cond = var order_conditions.append(cond) else: - raise ExpressionNotCoveredException("This expression might not be covered yet.") + raise ExpressionNotCoveredException( + "This expression might not be covered yet." + ) replace("{OrderBy}", "{" + node.p.name + "}") replace("{OrderConditions}", " ".join(order_conditions) + " ") elif node.name == "Project": @@ -975,12 +1037,22 @@ def translateAlgebra(query_algebra: Query = None): if isinstance(var, Identifier): project_variables.append(var.n3()) else: - raise ExpressionNotCoveredException("This expression might not be covered yet.") + raise ExpressionNotCoveredException( + "This expression might not be covered yet." + ) order_by_pattern = "" if node.p.name == "OrderBy": order_by_pattern = "ORDER BY {OrderConditions}" - replace("{Project}", " ".join(project_variables) + "{{" + node.p.name + "}}" - + "{GroupBy}" + order_by_pattern + "{Having}") + replace( + "{Project}", + " ".join(project_variables) + + "{{" + + node.p.name + + "}}" + + "{GroupBy}" + + order_by_pattern + + "{Having}", + ) elif node.name == "Distinct": replace("{Distinct}", "DISTINCT {" + node.p.name + "}") elif node.name == "Reduced": @@ -992,7 +1064,9 @@ def translateAlgebra(query_algebra: Query = None): if node.p.name == "values": replace("{ToMultiSet}", "{{" + node.p.name + "}}") else: - replace("{ToMultiSet}", "{-*-SELECT-*- " + "{" + node.p.name + "}" + "}") + replace( + "{ToMultiSet}", "{-*-SELECT-*- " + "{" + node.p.name + "}" + "}" + ) # 18.2 Property Path @@ -1002,22 +1076,40 @@ def translateAlgebra(query_algebra: Query = None): expr = convert_node_arg(node.expr) op = node.op if isinstance(list, type(node.other)): - other = "(" + ", ".join(convert_node_arg(expr) for expr in node.other) + ")" + other = ( + "(" + + ", ".join(convert_node_arg(expr) for expr in node.other) + + ")" + ) else: other = convert_node_arg(node.other) - condition = "{left} {operator} {right}".format(left=expr, operator=op, right=other) + condition = "{left} {operator} {right}".format( + left=expr, operator=op, right=other + ) replace("{RelationalExpression}", condition) elif node.name == "ConditionalAndExpression": - inner_nodes = " && ".join([convert_node_arg(expr) for expr in node.other]) - replace("{ConditionalAndExpression}", convert_node_arg(node.expr) + " && " + inner_nodes) + inner_nodes = " && ".join( + [convert_node_arg(expr) for expr in node.other] + ) + replace( + "{ConditionalAndExpression}", + convert_node_arg(node.expr) + " && " + inner_nodes, + ) elif node.name == "ConditionalOrExpression": - inner_nodes = " || ".join([convert_node_arg(expr) for expr in node.other]) - replace("{ConditionalOrExpression}", "(" + convert_node_arg(node.expr) + " || " + inner_nodes + ")") + inner_nodes = " || ".join( + [convert_node_arg(expr) for expr in node.other] + ) + replace( + "{ConditionalOrExpression}", + "(" + convert_node_arg(node.expr) + " || " + inner_nodes + ")", + ) elif node.name == "MultiplicativeExpression": left_side = convert_node_arg(node.expr) multiplication = left_side for i, operator in enumerate(node.op): - multiplication += operator + " " + convert_node_arg(node.other[i]) + " " + multiplication += ( + operator + " " + convert_node_arg(node.other[i]) + " " + ) replace("{MultiplicativeExpression}", multiplication) elif node.name == "AdditiveExpression": left_side = convert_node_arg(node.expr) @@ -1030,18 +1122,25 @@ def translateAlgebra(query_algebra: Query = None): # # 17.4 Function Definitions # # # 17.4.1 Functional Forms - elif node.name.endswith('BOUND'): + elif node.name.endswith("BOUND"): bound_var = convert_node_arg(node.arg) replace("{Builtin_BOUND}", "bound(" + bound_var + ")") - elif node.name.endswith('IF'): + elif node.name.endswith("IF"): arg2 = convert_node_arg(node.arg2) arg3 = convert_node_arg(node.arg3) - if_expression = "IF(" + "{" + node.arg1.name + "}, " + arg2 + ", " + arg3 + ")" + if_expression = ( + "IF(" + "{" + node.arg1.name + "}, " + arg2 + ", " + arg3 + ")" + ) replace("{Builtin_IF}", if_expression) - elif node.name.endswith('COALESCE'): - replace("{Builtin_COALESCE}", "COALESCE(" + ", ".join(convert_node_arg(arg) for arg in node.arg) + ")") - elif node.name.endswith('Builtin_EXISTS'): + elif node.name.endswith("COALESCE"): + replace( + "{Builtin_COALESCE}", + "COALESCE(" + + ", ".join(convert_node_arg(arg) for arg in node.arg) + + ")", + ) + elif node.name.endswith("Builtin_EXISTS"): # The node's name which we get with node.graph.name returns "Join" instead of GroupGraphPatternSub # According to https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#rExistsFunc # ExistsFunc can only have a GroupGraphPattern as parameter. However, when we print the query algebra @@ -1049,150 +1148,245 @@ def translateAlgebra(query_algebra: Query = None): replace("{Builtin_EXISTS}", "EXISTS " + "{{" + node.graph.name + "}}") traverse(node.graph, visitPre=sparql_query_text) return node.graph - elif node.name.endswith('Builtin_NOTEXISTS'): + elif node.name.endswith("Builtin_NOTEXISTS"): # The node's name which we get with node.graph.name returns "Join" instead of GroupGraphPatternSub # According to https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#rNotExistsFunc # NotExistsFunc can only have a GroupGraphPattern as parameter. However, when we print the query algebra # we get a GroupGraphPatternSub print(node.graph.name) - replace("{Builtin_NOTEXISTS}", "NOT EXISTS " + "{{" + node.graph.name + "}}") + replace( + "{Builtin_NOTEXISTS}", "NOT EXISTS " + "{{" + node.graph.name + "}}" + ) traverse(node.graph, visitPre=sparql_query_text) return node.graph # # # # 17.4.1.5 logical-or: Covered in "RelationalExpression" # # # # 17.4.1.6 logical-and: Covered in "RelationalExpression" # # # # 17.4.1.7 RDFterm-equal: Covered in "RelationalExpression" - elif node.name.endswith('sameTerm'): - replace("{Builtin_sameTerm}", "SAMETERM(" + convert_node_arg(node.arg1) - + ", " + convert_node_arg(node.arg2) + ")") + elif node.name.endswith("sameTerm"): + replace( + "{Builtin_sameTerm}", + "SAMETERM(" + + convert_node_arg(node.arg1) + + ", " + + convert_node_arg(node.arg2) + + ")", + ) # # # # IN: Covered in "RelationalExpression" # # # # NOT IN: Covered in "RelationalExpression" # # # 17.4.2 Functions on RDF Terms - elif node.name.endswith('Builtin_isIRI'): + elif node.name.endswith("Builtin_isIRI"): replace("{Builtin_isIRI}", "isIRI(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('Builtin_isBLANK'): - replace("{Builtin_isBLANK}", "isBLANK(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('Builtin_isLITERAL'): - replace("{Builtin_isLITERAL}", "isLITERAL(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('Builtin_isNUMERIC'): - replace("{Builtin_isNUMERIC}", "isNUMERIC(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('Builtin_STR'): + elif node.name.endswith("Builtin_isBLANK"): + replace( + "{Builtin_isBLANK}", "isBLANK(" + convert_node_arg(node.arg) + ")" + ) + elif node.name.endswith("Builtin_isLITERAL"): + replace( + "{Builtin_isLITERAL}", + "isLITERAL(" + convert_node_arg(node.arg) + ")", + ) + elif node.name.endswith("Builtin_isNUMERIC"): + replace( + "{Builtin_isNUMERIC}", + "isNUMERIC(" + convert_node_arg(node.arg) + ")", + ) + elif node.name.endswith("Builtin_STR"): replace("{Builtin_STR}", "STR(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('Builtin_LANG'): + elif node.name.endswith("Builtin_LANG"): replace("{Builtin_LANG}", "LANG(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('Builtin_DATATYPE'): - replace("{Builtin_DATATYPE}", "DATATYPE(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('Builtin_IRI'): + elif node.name.endswith("Builtin_DATATYPE"): + replace( + "{Builtin_DATATYPE}", "DATATYPE(" + convert_node_arg(node.arg) + ")" + ) + elif node.name.endswith("Builtin_IRI"): replace("{Builtin_IRI}", "IRI(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('Builtin_BNODE'): + elif node.name.endswith("Builtin_BNODE"): replace("{Builtin_BNODE}", "BNODE(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('STRDT'): - replace("{Builtin_STRDT}", "STRDT(" + convert_node_arg(node.arg1) - + ", " + convert_node_arg(node.arg2) + ")") - elif node.name.endswith('Builtin_STRLANG'): - replace("{Builtin_STRLANG}", "STRLANG(" + convert_node_arg(node.arg1) - + ", " + convert_node_arg(node.arg2) + ")") - elif node.name.endswith('Builtin_UUID'): + elif node.name.endswith("STRDT"): + replace( + "{Builtin_STRDT}", + "STRDT(" + + convert_node_arg(node.arg1) + + ", " + + convert_node_arg(node.arg2) + + ")", + ) + elif node.name.endswith("Builtin_STRLANG"): + replace( + "{Builtin_STRLANG}", + "STRLANG(" + + convert_node_arg(node.arg1) + + ", " + + convert_node_arg(node.arg2) + + ")", + ) + elif node.name.endswith("Builtin_UUID"): replace("{Builtin_UUID}", "UUID()") - elif node.name.endswith('Builtin_STRUUID'): + elif node.name.endswith("Builtin_STRUUID"): replace("{Builtin_STRUUID}", "STRUUID()") # # # 17.4.3 Functions on Strings - elif node.name.endswith('Builtin_STRLEN'): - replace("{Builtin_STRLEN}", "STRLEN(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('Builtin_SUBSTR'): + elif node.name.endswith("Builtin_STRLEN"): + replace( + "{Builtin_STRLEN}", "STRLEN(" + convert_node_arg(node.arg) + ")" + ) + elif node.name.endswith("Builtin_SUBSTR"): args = [node.arg.n3(), node.start] if node.length: args.append(node.length) expr = "SUBSTR(" + ", ".join(args) + ")" replace("{Builtin_SUBSTR}", expr) - elif node.name.endswith('Builtin_UCASE'): + elif node.name.endswith("Builtin_UCASE"): replace("{Builtin_UCASE}", "UCASE(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('Builtin_LCASE'): + elif node.name.endswith("Builtin_LCASE"): replace("{Builtin_LCASE}", "LCASE(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('Builtin_STRSTARTS'): - replace("{Builtin_STRSTARTS}", "STRSTARTS(" + convert_node_arg(node.arg1) - + ", " + convert_node_arg(node.arg2) + ")") - elif node.name.endswith('Builtin_STRENDS'): - replace("{Builtin_STRENDS}", "STRENDS(" + convert_node_arg(node.arg1) - + ", " + convert_node_arg(node.arg2) + ")") - elif node.name.endswith('Builtin_CONTAINS'): - replace("{Builtin_CONTAINS}", "CONTAINS(" + convert_node_arg(node.arg1) - + ", " + convert_node_arg(node.arg2) + ")") - elif node.name.endswith('Builtin_STRBEFORE'): - replace("{Builtin_STRBEFORE}", "STRBEFORE(" + convert_node_arg(node.arg1) - + ", " + convert_node_arg(node.arg2) + ")") - elif node.name.endswith('Builtin_STRAFTER'): - replace("{Builtin_STRAFTER}", "STRAFTER(" + convert_node_arg(node.arg1) - + ", " + convert_node_arg(node.arg2) + ")") - elif node.name.endswith('Builtin_ENCODE_FOR_URI'): - replace("{Builtin_ENCODE_FOR_URI}", "ENCODE_FOR_URI(" + convert_node_arg(node.arg) + ")") - elif node.name.endswith('Builtin_CONCAT'): - expr = 'CONCAT({vars})'.format(vars=", ".join(elem.n3() for elem in node.arg)) + elif node.name.endswith("Builtin_STRSTARTS"): + replace( + "{Builtin_STRSTARTS}", + "STRSTARTS(" + + convert_node_arg(node.arg1) + + ", " + + convert_node_arg(node.arg2) + + ")", + ) + elif node.name.endswith("Builtin_STRENDS"): + replace( + "{Builtin_STRENDS}", + "STRENDS(" + + convert_node_arg(node.arg1) + + ", " + + convert_node_arg(node.arg2) + + ")", + ) + elif node.name.endswith("Builtin_CONTAINS"): + replace( + "{Builtin_CONTAINS}", + "CONTAINS(" + + convert_node_arg(node.arg1) + + ", " + + convert_node_arg(node.arg2) + + ")", + ) + elif node.name.endswith("Builtin_STRBEFORE"): + replace( + "{Builtin_STRBEFORE}", + "STRBEFORE(" + + convert_node_arg(node.arg1) + + ", " + + convert_node_arg(node.arg2) + + ")", + ) + elif node.name.endswith("Builtin_STRAFTER"): + replace( + "{Builtin_STRAFTER}", + "STRAFTER(" + + convert_node_arg(node.arg1) + + ", " + + convert_node_arg(node.arg2) + + ")", + ) + elif node.name.endswith("Builtin_ENCODE_FOR_URI"): + replace( + "{Builtin_ENCODE_FOR_URI}", + "ENCODE_FOR_URI(" + convert_node_arg(node.arg) + ")", + ) + elif node.name.endswith("Builtin_CONCAT"): + expr = "CONCAT({vars})".format( + vars=", ".join(elem.n3() for elem in node.arg) + ) replace("{Builtin_CONCAT}", expr) - elif node.name.endswith('Builtin_LANGMATCHES'): - replace("{Builtin_LANGMATCHES}", "LANGMATCHES(" + convert_node_arg(node.arg1) - + ", " + convert_node_arg(node.arg2) + ")") - elif node.name.endswith('REGEX'): + elif node.name.endswith("Builtin_LANGMATCHES"): + replace( + "{Builtin_LANGMATCHES}", + "LANGMATCHES(" + + convert_node_arg(node.arg1) + + ", " + + convert_node_arg(node.arg2) + + ")", + ) + elif node.name.endswith("REGEX"): args = [convert_node_arg(node.text), convert_node_arg(node.pattern)] expr = "REGEX(" + ", ".join(args) + ")" replace("{Builtin_REGEX}", expr) - elif node.name.endswith('REPLACE'): - replace("{Builtin_REPLACE}", "REPLACE(" + convert_node_arg(node.arg) - + ", " + convert_node_arg(node.pattern) + ", " + convert_node_arg(node.replacement) + ")") + elif node.name.endswith("REPLACE"): + replace( + "{Builtin_REPLACE}", + "REPLACE(" + + convert_node_arg(node.arg) + + ", " + + convert_node_arg(node.pattern) + + ", " + + convert_node_arg(node.replacement) + + ")", + ) # # # 17.4.4 Functions on Numerics - elif node.name == 'Builtin_ABS': + elif node.name == "Builtin_ABS": replace("{Builtin_ABS}", "ABS(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_ROUND': + elif node.name == "Builtin_ROUND": replace("{Builtin_ROUND}", "ROUND(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_CEIL': + elif node.name == "Builtin_CEIL": replace("{Builtin_CEIL}", "CEIL(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_FLOOR': + elif node.name == "Builtin_FLOOR": replace("{Builtin_FLOOR}", "FLOOR(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_RAND': + elif node.name == "Builtin_RAND": replace("{Builtin_RAND}", "RAND()") # # # 17.4.5 Functions on Dates and Times - elif node.name == 'Builtin_NOW': + elif node.name == "Builtin_NOW": replace("{Builtin_NOW}", "NOW()") - elif node.name == 'Builtin_YEAR': + elif node.name == "Builtin_YEAR": replace("{Builtin_YEAR}", "YEAR(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_MONTH': + elif node.name == "Builtin_MONTH": replace("{Builtin_MONTH}", "MONTH(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_DAY': + elif node.name == "Builtin_DAY": replace("{Builtin_DAY}", "DAY(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_HOURS': + elif node.name == "Builtin_HOURS": replace("{Builtin_HOURS}", "HOURS(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_MINUTES': - replace("{Builtin_MINUTES}", "MINUTES(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_SECONDS': - replace("{Builtin_SECONDS}", "SECONDS(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_TIMEZONE': - replace("{Builtin_TIMEZONE}", "TIMEZONE(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_TZ': + elif node.name == "Builtin_MINUTES": + replace( + "{Builtin_MINUTES}", "MINUTES(" + convert_node_arg(node.arg) + ")" + ) + elif node.name == "Builtin_SECONDS": + replace( + "{Builtin_SECONDS}", "SECONDS(" + convert_node_arg(node.arg) + ")" + ) + elif node.name == "Builtin_TIMEZONE": + replace( + "{Builtin_TIMEZONE}", "TIMEZONE(" + convert_node_arg(node.arg) + ")" + ) + elif node.name == "Builtin_TZ": replace("{Builtin_TZ}", "TZ(" + convert_node_arg(node.arg) + ")") # # # 17.4.6 Hash functions - elif node.name == 'Builtin_MD5': + elif node.name == "Builtin_MD5": replace("{Builtin_MD5}", "MD5(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_SHA1': + elif node.name == "Builtin_SHA1": replace("{Builtin_SHA1}", "SHA1(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_SHA256': - replace("{Builtin_SHA256}", "SHA256(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_SHA384': - replace("{Builtin_SHA384}", "SHA384(" + convert_node_arg(node.arg) + ")") - elif node.name == 'Builtin_SHA512': - replace("{Builtin_SHA512}", "SHA512(" + convert_node_arg(node.arg) + ")") + elif node.name == "Builtin_SHA256": + replace( + "{Builtin_SHA256}", "SHA256(" + convert_node_arg(node.arg) + ")" + ) + elif node.name == "Builtin_SHA384": + replace( + "{Builtin_SHA384}", "SHA384(" + convert_node_arg(node.arg) + ")" + ) + elif node.name == "Builtin_SHA512": + replace( + "{Builtin_SHA512}", "SHA512(" + convert_node_arg(node.arg) + ")" + ) # Other - elif node.name == 'values': + elif node.name == "values": columns = [] for key in node.res[0].keys(): if isinstance(key, Identifier): columns.append(key.n3()) else: - raise ExpressionNotCoveredException("The expression {0} might not be covered yet.".format(key)) + raise ExpressionNotCoveredException( + "The expression {0} might not be covered yet.".format(key) + ) values = "VALUES (" + " ".join(columns) + ")" rows = "" @@ -1200,18 +1394,29 @@ def translateAlgebra(query_algebra: Query = None): row = [] for term in elem.values(): if isinstance(term, Identifier): - row.append(term.n3()) # n3() is not part of Identifier class but every subclass has it + row.append( + term.n3() + ) # n3() is not part of Identifier class but every subclass has it elif isinstance(term, str): row.append(term) else: raise ExpressionNotCoveredException( - "The expression {0} might not be covered yet.".format(term)) + "The expression {0} might not be covered yet.".format( + term + ) + ) rows += "(" + " ".join(row) + ")" replace("values", values + "{" + rows + "}") - elif node.name == 'ServiceGraphPattern': - replace("{ServiceGraphPattern}", "SERVICE " + convert_node_arg(node.term) - + "{" + node.graph.name + "}") + elif node.name == "ServiceGraphPattern": + replace( + "{ServiceGraphPattern}", + "SERVICE " + + convert_node_arg(node.term) + + "{" + + node.graph.name + + "}", + ) traverse(node.graph, visitPre=sparql_query_text) return node.graph # else: @@ -1236,7 +1441,14 @@ def pprintAlgebra(q): return print("%s(" % (p.name,)) for k in p: - print("%s%s =" % (ind, k,), end=" ") + print( + "%s%s =" + % ( + ind, + k, + ), + end=" ", + ) pp(p[k], ind + " ") print("%s)" % ind) |