(g: SemanticGraph, ask=False, limit=endpoint_access.GLOBAL_RESULT_LIMIT)
return query
Convert graph to a SPARQL query.

    :param ask: if the a simple existence of the graph should be checked instead of returning variable values.
    :param g: a graph as a dictionary with non-empty edgeSet
    :param return_var_values: if True the denotations for free variables will be returned
    :param limit: limit on the result list size
    :return: a SPARQL query as a string
    >>> print(graph_to_query(SemanticGraph(edges=[graph.Edge(0, "Q76", None , QUESTION_VAR)]) ))
Convert graph to a SPARQL query.
Convert graph to a SPARQL query .
def graph_to_query(g: SemanticGraph, ask=False, limit=endpoint_access.GLOBAL_RESULT_LIMIT):
    """
    Convert graph to a SPARQL query.

    :param ask: if the a simple existence of the graph should be checked instead of returning variable values.
    :param g: a graph as a dictionary with non-empty edgeSet
    :param return_var_values: if True the denotations for free variables will be returned
    :param limit: limit on the result list size
    :return: a SPARQL query as a string
    >>> print(graph_to_query(SemanticGraph(edges=[graph.Edge(0, "Q76", None , QUESTION_VAR)]) ))

    """
    variables = set()
    order_by = []
    edges = []

    for i, edge in enumerate(g.edges):
        edges.append(edge_to_sparql(edge, expand_transitive=not ask))
        if not edge.grounded:
            variables.add(f"?r{edge.edgeid:d}v")
        if edge.qualifierentityid in {'MAX', 'MIN'}:
            order_by.append(f"{'DESC' if edge.qualifierentityid=='MAX' else 'ASC'}(?n{edge.edgeid:d})")
        if edge.relationid == 'iclass':
            variables.add("?topic")
        if edge.relationid == 'iclass':
            variables.add("?topic")
        if edge.simple and edge.relationid in TRANSITIVE_RELATIONS and not ask\
                and QUESTION_VAR not in edge.nodes():
            variables.add("?step")

    query = queries.sparql_prefix + (
        queries.sparql_select if not ask else queries.sparql_ask)
    if any(edge.relationid == 'class' for edge in g.edges):
        query = queries.sparql_inference_clause + query
    order_by_pattern = ""
    if len(variables - {'?step'}) == 0 and not ask:
        variables.add(QUESTION_VAR)
        if order_by:
            order_by_pattern = queries.sparql_close_order.format(" ".join(order_by))
            limit = 1
    else:
        variables = variables - {'?step'}
    query = query.format(queryvariables=" ".join(variables))
    query += "{{ {} }}".format('\n'.join(edges))
    if not ask:
        query += order_by_pattern + queries.sparql_close.format(limit)

    return query
def graph_to_query ( g : SemanticGraph , ask = False , limit = endpoint_access . GLOBAL_RESULT_LIMIT ) : variables = set ( ) order_by = [ ] edges = [ ] for i , edge in enumerate ( g . edges ) : edges . append ( edge_to_sparql ( edge , expand_transitive = not ask ) ) if not edge . grounded : variables . add ( f"?r{edge.edgeid:d}v" ) if edge . qualifierentityid in { 'MAX' , 'MIN' } : order_by . append ( f"{'DESC' if edge.qualifierentityid=='MAX' else 'ASC'}(?n{edge.edgeid:d})" ) if edge . relationid == 'iclass' : variables . add ( "?topic" ) if edge . relationid == 'iclass' : variables . add ( "?topic" ) if edge . simple and edge . relationid in TRANSITIVE_RELATIONS and not ask and QUESTION_VAR not in edge . nodes ( ) : variables . add ( "?step" ) query = queries . sparql_prefix + ( queries . sparql_select if not ask else queries . sparql_ask ) if any ( edge . relationid == 'class' for edge in g . edges ) : query = queries . sparql_inference_clause + query order_by_pattern = "" if len ( variables - { '?step' } ) == 0 and not ask : variables . add ( QUESTION_VAR ) if order_by : order_by_pattern = queries . sparql_close_order . format ( " " . join ( order_by ) ) limit = 1 else : variables = variables - { '?step' } query = query . format ( queryvariables = " " . join ( variables ) ) query += "{{ {} }}" . format ( '\n' . join ( edges ) ) if not ask : query += order_by_pattern + queries . sparql_close . format ( limit ) return query