예제 #1
0
def sage_query_construct(config_file, default_graph_uri, query, file, format,
                         limit):
    """
        Execute a SPARQL CONSTRUCT query to a SaGe server.

        Example usage: sage-construct config.yaml http://example.org/swdf-postgres -f queries/construct.sparql
    """
    # assert that we have a query to evaluate
    if query is None and file is None:
        print(
            "Error: you must specificy a query to execute, either with --query or --file. See sage-query --help for more informations."
        )
        exit(1)

    if limit is None:
        limit = inf

    # load query from file if required
    if file is not None:
        with open(file) as query_file:
            query = query_file.read()

    client = TestClient(run_app(config_file))

    nbResults = 0
    nbCalls = 0
    hasNext = True
    next_link = None

    g = Graph()
    start = time()

    while hasNext:
        response = post_sparql(client, query, next_link, default_graph_uri)
        response = response.json()
        nbResults += len(response['bindings'])
        hasNext = response['hasNext']
        next_link = response['next']
        nbCalls += 1
        buffer = ""
        for triple in response['bindings']:
            line = triple['s'] + " " + triple['p'] + " " + triple['o'] + " . \n"
            buffer = buffer + line
        g.parse(data=buffer, format='nt')
        logger.info("{} calls, {} triples, {} triples in graph".format(
            nbCalls, nbResults, len(g)))
        if len(g) >= limit:
            break

    end = time()
    logger.info("finished in {}s".format(end - start))
    logger.info("made {} calls".format(nbCalls))
    logger.info("got {} triples".format(nbResults))
    logger.info("gathered a graph of {} triples".format(len(g)))

    print(g.serialize(format=format).decode('utf8'))
예제 #2
0
 def setup_class(self):
     self._app = run_app('tests/data/test_config.yaml')
     self._client = TestClient(self._app)
예제 #3
0
def start_sage_server(config, port, workers, host, log_level):
    """Launch the Sage server using the CONFIG configuration file"""
    app = run_app(config)
    uvicorn.run(app, port=port, host=host, log_level=log_level)
예제 #4
0
 def setup_method(self):
     self._app = run_app('tests/update/config.yaml')
     self._client = TestClient(self._app)
예제 #5
0
def sage_query(config_file, default_graph_uri, query, file, limit):
    """
        Execute a SPARQL query on an embedded Sage Server.

        Example usage: sage-query config.yaml http://example.org/swdf-postgres -f queries/spo.sparql
    """
    # assert that we have a query to evaluate
    if query is None and file is None:
        print("Error: you must specificy a query to execute, either with --query or --file. See sage-query --help for more informations.")
        exit(1)

    if limit is None:
        limit = inf

    # load query from file if required
    if file is not None:
        with open(file) as query_file:
            query = query_file.read()

    # dataset = load_config(config_file)
    # if not dataset.has_graph(default_graph_uri):
    #     print("Error: the config_file does not define your {default_graph_uri}.")
    client=TestClient(run_app(config_file))

    nbResults = 0
    nbCalls = 0
    hasNext = True
    next_link = None
    count=0
    start = time()

    while hasNext:
        response = post_sparql(client, query, next_link, default_graph_uri)
        response = response.json()
        nbResults += len(response['bindings'])
        hasNext = response['hasNext']
        next_link = response['next']


        nbCalls += 1
        for bindings in response['bindings']:
            print(bindings)
#            for k,v in bindings.items():
#                print(f"{v} ")

        if next_link is not None:
            saved_plan = next_link
            plan = decode_saved_plan(saved_plan)
            root = RootTree()
            root.ParseFromString(plan)
            prog,card=progress(root)
            logger.info(f"progression {prog}/{card}:{prog/card*100}%")


        count += 1
        if count >= limit:
            break

    end= time()
    logger.info("finished in {}s".format(end-start))
    logger.info("made {} calls".format(nbCalls))
    logger.info("got {} mappings".format(nbResults))