(edge: graph.Edge, expand_transitive=EXPAND_TRANSITIVE_RELATIONS)
return sparql_relation_template.format(triples="".join(triples))
Convert a graph edge to a piece of a SPARQL query.

    :param edge: input Edge
    :return: SPARQL piece as a string
    >>> edge_to_sparql(graph.Edge("Q76", None , QUESTION_VAR)).strip()
    '{ GRAPH g:statements { e:Q76 ?r0s ?m0 . ?m0 ?r0v ?qvar .  } }'
    >>> edge_to_sparql(graph.Edge("Q76", None , None, 'P453', QUESTION_VAR)).strip()
    '{ GRAPH g:statements { e:Q76 ?r0s ?m0 . ?m0 e:P453q ?qvar .  } }'
    >>> edge_to_sparql(graph.Edge(QUESTION_VAR,  None , None, 'P453', "Q76")).strip()
    '{ GRAPH g:statements { ?qvar ?r0s ?m0 . ?m0 e:P453q e:Q76 .  } }'
    >>> edge_to_sparql(graph.Edge("Q76", "P36" , QUESTION_VAR)).strip()
    '{ GRAPH g:statements { e:Q76 e:P36s/e:P36v ?qvar .  } }'
    >>> edge_to_sparql(graph.Edge("?e1", "P36" , QUESTION_VAR)).strip()
    '{ GRAPH g:statements { ?e1 e:P36s/e:P36v ?qvar .  } }'
    >>> edge_to_sparql(graph.Edge("?e1", "P131" , QUESTION_VAR)).strip()
    "{ GRAPH g:statements { ?e1 e:P131s/e:P131v ?qvar option (transitive,t_no_cycles, t_min (1), t_max(5), t_step ('step_no') as ?step).  } }"
    >>> edge_to_sparql(graph.Edge(None, None , "Q37876", None, QUESTION_VAR)).strip()
    '{ GRAPH g:statements { ?m0 ?r0v e:Q37876 . ?m0 ?r0q ?qvar .  } }'
    >>> edge_to_sparql(graph.Edge(None, None, "Q37876", "P175", QUESTION_VAR)).strip()
    '{ GRAPH g:statements { ?m0 ?r0v e:Q37876 . ?m0 e:P175q ?qvar .  } }'
    >>> edge_to_sparql(graph.Edge(None, "P161", QUESTION_VAR, None, "Q37876")).strip()
    '{ GRAPH g:statements { ?m0 e:P161v ?qvar . ?m0 ?r0q e:Q37876 .  } }'
    >>> edge_to_sparql(graph.Edge("Q678", None, QUESTION_VAR, None, "2009")).strip()
    '{ GRAPH g:statements { e:Q678 ?r0s ?m0 . ?m0 ?r0v ?qvar . ?m0 ?r0q [base:time ?n0] . FILTER (YEAR(?n0) = 2009) } }'
    >>> edge_to_sparql(graph.Edge("Q678", "P89", QUESTION_VAR, "P453", "Q896")).strip()
    '{ GRAPH g:statements { e:Q678 e:P89s ?m0 . ?m0 e:P89v ?qvar . ?m0 e:P453q e:Q896 .  } }'
    >>> edge_to_sparql(graph.Edge(QUESTION_VAR, None, "Q678", None, "2009")).strip()
    '{ GRAPH g:statements { ?qvar ?r0s ?m0 . ?m0 ?r0v e:Q678 . ?m0 ?r0q [base:time ?n0] . FILTER (YEAR(?n0) = 2009) } }'
    >>> edge_to_sparql(graph.Edge(QUESTION_VAR, None, "2009")).strip()
    '{ GRAPH g:statements { ?qvar ?r0s ?m0 . ?m0 ?r0v [base:time ?n0] . FILTER (YEAR(?n0) = 2009) } }'
    >>> edge_to_sparql(graph.Edge("Q678", None, None, None, "MAX")).strip()
    '{ GRAPH g:statements { e:Q678 ?r0s ?m0 . ?m0 ?r0q [base:time ?n0] .  } }'
    >>> edge_to_sparql(graph.Edge("Q678", None, "MAX")).strip()
    '{ GRAPH g:statements { e:Q678 ?r0s ?m0 . ?m0 ?r0v [base:time ?n0] .  } }'
    >>> edge_to_sparql(graph.Edge(QUESTION_VAR, "class", "Q5",)).strip()
    '{   { GRAPH g:instances { ?qvar rdf:type e:Q5. }}\\n            UNION\\n            {\\n                GRAPH g:statements { ?qvar e:P106s/e:P106v ?m0. } \\n                GRAPH g:instances { ?m0 rdfs:subClassOf? e:Q5. }\\n            }\\n        }'
    >>> edge_to_sparql(graph.Edge(QUESTION_VAR, "iclass")).strip()
    '{VALUES ?r0v { e:P106c e:P31c } \\n        GRAPH g:simple-statements { ?qvar ?r0v ?topic. } }'
Convert a graph edge to a piece of a SPARQL query.
Convert a graph edge to a piece of a SPARQL query .
def edge_to_sparql(edge: graph.Edge, expand_transitive=EXPAND_TRANSITIVE_RELATIONS):
    """
    Convert a graph edge to a piece of a SPARQL query.

    :param edge: input Edge
    :return: SPARQL piece as a string
    >>> edge_to_sparql(graph.Edge("Q76", None , QUESTION_VAR)).strip()
    '{ GRAPH g:statements { e:Q76 ?r0s ?m0 . ?m0 ?r0v ?qvar .  } }'
    >>> edge_to_sparql(graph.Edge("Q76", None , None, 'P453', QUESTION_VAR)).strip()
    '{ GRAPH g:statements { e:Q76 ?r0s ?m0 . ?m0 e:P453q ?qvar .  } }'
    >>> edge_to_sparql(graph.Edge(QUESTION_VAR,  None , None, 'P453', "Q76")).strip()
    '{ GRAPH g:statements { ?qvar ?r0s ?m0 . ?m0 e:P453q e:Q76 .  } }'
    >>> edge_to_sparql(graph.Edge("Q76", "P36" , QUESTION_VAR)).strip()
    '{ GRAPH g:statements { e:Q76 e:P36s/e:P36v ?qvar .  } }'
    >>> edge_to_sparql(graph.Edge("?e1", "P36" , QUESTION_VAR)).strip()
    '{ GRAPH g:statements { ?e1 e:P36s/e:P36v ?qvar .  } }'
    >>> edge_to_sparql(graph.Edge("?e1", "P131" , QUESTION_VAR)).strip()
    "{ GRAPH g:statements { ?e1 e:P131s/e:P131v ?qvar option (transitive,t_no_cycles, t_min (1), t_max(5), t_step ('step_no') as ?step).  } }"
    >>> edge_to_sparql(graph.Edge(None, None , "Q37876", None, QUESTION_VAR)).strip()
    '{ GRAPH g:statements { ?m0 ?r0v e:Q37876 . ?m0 ?r0q ?qvar .  } }'
    >>> edge_to_sparql(graph.Edge(None, None, "Q37876", "P175", QUESTION_VAR)).strip()
    '{ GRAPH g:statements { ?m0 ?r0v e:Q37876 . ?m0 e:P175q ?qvar .  } }'
    >>> edge_to_sparql(graph.Edge(None, "P161", QUESTION_VAR, None, "Q37876")).strip()
    '{ GRAPH g:statements { ?m0 e:P161v ?qvar . ?m0 ?r0q e:Q37876 .  } }'
    >>> edge_to_sparql(graph.Edge("Q678", None, QUESTION_VAR, None, "2009")).strip()
    '{ GRAPH g:statements { e:Q678 ?r0s ?m0 . ?m0 ?r0v ?qvar . ?m0 ?r0q [base:time ?n0] . FILTER (YEAR(?n0) = 2009) } }'
    >>> edge_to_sparql(graph.Edge("Q678", "P89", QUESTION_VAR, "P453", "Q896")).strip()
    '{ GRAPH g:statements { e:Q678 e:P89s ?m0 . ?m0 e:P89v ?qvar . ?m0 e:P453q e:Q896 .  } }'
    >>> edge_to_sparql(graph.Edge(QUESTION_VAR, None, "Q678", None, "2009")).strip()
    '{ GRAPH g:statements { ?qvar ?r0s ?m0 . ?m0 ?r0v e:Q678 . ?m0 ?r0q [base:time ?n0] . FILTER (YEAR(?n0) = 2009) } }'
    >>> edge_to_sparql(graph.Edge(QUESTION_VAR, None, "2009")).strip()
    '{ GRAPH g:statements { ?qvar ?r0s ?m0 . ?m0 ?r0v [base:time ?n0] . FILTER (YEAR(?n0) = 2009) } }'
    >>> edge_to_sparql(graph.Edge("Q678", None, None, None, "MAX")).strip()
    '{ GRAPH g:statements { e:Q678 ?r0s ?m0 . ?m0 ?r0q [base:time ?n0] .  } }'
    >>> edge_to_sparql(graph.Edge("Q678", None, "MAX")).strip()
    '{ GRAPH g:statements { e:Q678 ?r0s ?m0 . ?m0 ?r0v [base:time ?n0] .  } }'
    >>> edge_to_sparql(graph.Edge(QUESTION_VAR, "class", "Q5",)).strip()
    '{   { GRAPH g:instances { ?qvar rdf:type e:Q5. }}\\n            UNION\\n            {\\n                GRAPH g:statements { ?qvar e:P106s/e:P106v ?m0. } \\n                GRAPH g:instances { ?m0 rdfs:subClassOf? e:Q5. }\\n            }\\n        }'
    >>> edge_to_sparql(graph.Edge(QUESTION_VAR, "iclass")).strip()
    '{VALUES ?r0v { e:P106c e:P31c } \\n        GRAPH g:simple-statements { ?qvar ?r0v ?topic. } }'
    """
    relationid = f"e:{edge.relationid}" if edge.relationid is not None else f"?r{edge.edgeid:d}"

    values = {
        'edgeid': edge.edgeid,
        'left': f"e:{edge.leftentityid}" if edge.leftentityid and edge.leftentityid.startswith("Q") else edge.leftentityid,
        'relationid': relationid,
        'right': f"e:{edge.rightentityid}" if edge.rightentityid and edge.rightentityid.startswith("Q") else edge.rightentityid,
        'option': ""
    }
    if edge.relationid in sparql_class_relation:
        return sparql_class_relation[edge.relationid].format(**values)

    triples = []
    if edge.simple:
        if edge.relationid in TRANSITIVE_RELATIONS and expand_transitive:
            values['option'] = queries.sparql_transitive_option
        triples.append(sparql_triple_template['left-to-right'].format(**values))
    else:
        if edge.leftentityid is not None:
            triples.append(sparql_triple_template['left'].format(**values))
        if edge.rightentityid is not None:
            template = sparql_triple_template['right']
            if values['right'].isdigit():
                template = sparql_triple_template['time'] + sparql_triple_template['time-filter']
            elif values['right'] in {"MAX", "MIN"}:
                template = sparql_triple_template['time']
            triples.append(template.format(**{**values, "branch": 'v'}))
        if edge.qualifierentityid is not None:
            relationid = f"e:{edge.qualifierrelationid}" if edge.qualifierrelationid is not None else f"?r{edge.edgeid:d}"
            right = f"e:{edge.qualifierentityid}" if edge.qualifierentityid.startswith("Q") else edge.qualifierentityid
            template = sparql_triple_template['right']
            if right.isdigit():
                template = sparql_triple_template['time'] + sparql_triple_template['time-filter']
            elif right in {"MAX", "MIN"}:
                template = sparql_triple_template['time']
            triples.append(template.format(**{**values,
                                              "branch": 'q',
                                              "relationid": relationid,
                                              "right": right}))

    return sparql_relation_template.format(triples="".join(triples))
def edge_to_sparql ( edge : graph . Edge , expand_transitive = EXPAND_TRANSITIVE_RELATIONS ) : relationid = f"e:{edge.relationid}" if edge . relationid is not None else f"?r{edge.edgeid:d}" values = { 'edgeid' : edge . edgeid , 'left' : f"e:{edge.leftentityid}" if edge . leftentityid and edge . leftentityid . startswith ( "Q" ) else edge . leftentityid , 'relationid' : relationid , 'right' : f"e:{edge.rightentityid}" if edge . rightentityid and edge . rightentityid . startswith ( "Q" ) else edge . rightentityid , 'option' : "" } if edge . relationid in sparql_class_relation : return sparql_class_relation [ edge . relationid ] . format ( * * values ) triples = [ ] if edge . simple : if edge . relationid in TRANSITIVE_RELATIONS and expand_transitive : values [ 'option' ] = queries . sparql_transitive_option triples . append ( sparql_triple_template [ 'left-to-right' ] . format ( * * values ) ) else : if edge . leftentityid is not None : triples . append ( sparql_triple_template [ 'left' ] . format ( * * values ) ) if edge . rightentityid is not None : template = sparql_triple_template [ 'right' ] if values [ 'right' ] . isdigit ( ) : template = sparql_triple_template [ 'time' ] + sparql_triple_template [ 'time-filter' ] elif values [ 'right' ] in { "MAX" , "MIN" } : template = sparql_triple_template [ 'time' ] triples . append ( template . format ( * * { * * values , "branch" : 'v' } ) ) if edge . qualifierentityid is not None : relationid = f"e:{edge.qualifierrelationid}" if edge . qualifierrelationid is not None else f"?r{edge.edgeid:d}" right = f"e:{edge.qualifierentityid}" if edge . qualifierentityid . startswith ( "Q" ) else edge . qualifierentityid template = sparql_triple_template [ 'right' ] if right . isdigit ( ) : template = sparql_triple_template [ 'time' ] + sparql_triple_template [ 'time-filter' ] elif right in { "MAX" , "MIN" } : template = sparql_triple_template [ 'time' ] triples . append ( template . format ( * * { * * values , "branch" : 'q' , "relationid" : relationid , "right" : right } ) ) return sparql_relation_template . format ( triples = "" . join ( triples ) )