Esempio n. 1
0
def make_get_query(update_def):
    """
    Given an update_def, return the sparql query needed to produce a spreadsheet of the data to be managed.
    See do_get
    :return: a sparql query string
    """
    from vivopump import add_qualifiers

    front_query = 'SELECT ?uri ?' + ' ?'.join(update_def['column_defs'].keys()) + '\nWHERE {\n    ' + \
                  update_def['entity_def']['entity_sparql'] + '\n'

    # Fake recursion here to depth 3.  Could be replaced by real recursion to arbitrary path length

    middle_query = ""
    for name, path in update_def['column_defs'].items():
        middle_query += '    OPTIONAL {  ?uri <' + str(path[0]['predicate']['ref']) + '> ?'
        if len(path) == 1:
            middle_query += name + ' . ' + add_qualifiers(path) + ' }\n'
        else:
            middle_query += path[0]['object']['name'] + ' . ?' +\
                path[0]['object']['name'] + ' <' + str(path[1]['predicate']['ref']) + '> ?'
            if len(path) == 2:
                middle_query += name + ' . ' + add_qualifiers(path) + ' }\n'
            else:
                middle_query += path[1]['object']['name'] + ' . ?' +\
                    path[1]['object']['name'] + ' <' + str(path[2]['predicate']['ref']) + '> ?'
                if len(path) == 3:
                    middle_query += name + ' . ' + add_qualifiers(path) + ' }\n'
                else:
                    raise PathLengthException('Path length >3 not supported in do_get')

    back_query = '}\nORDER BY ?' + update_def['entity_def']['order_by']
    return front_query + middle_query + back_query
Esempio n. 2
0
def get_step_triples(update_graph, uri, step_def, debug=True):
    """
    Return the triples matching the criteria defined in the current step of an update
    :param update_graph: the update graph
    :param uri: uri of the entity currently the subject of an update
    :param step_def: step definition from update_def
    :return:  Graph containing one or more triples that match the criteria for the step
    """
    from rdflib import Graph
    from vivopump import vivo_query, add_qualifiers, make_rdf_term
    if 'qualifier' not in step_def['object']:
        g = update_graph.triples((uri, step_def['predicate']['ref'], None))
    else:
        q = 'select (?' + step_def['object']['name'] +' as ?o) where { <' + str(uri) + '> <' + \
            str(step_def['predicate']['ref']) + '> ?' + step_def['object']['name'] + ' .\n' + \
            add_qualifiers([step_def]) + ' }\n'
        if debug:
            print "\nStep Triples Query\n", q
        result_set = vivo_query(q)
        g = Graph()
        for binding in result_set['results']['bindings']:
            o = make_rdf_term(binding['o'])
            g.add((uri, step_def['predicate']['ref'], o))
        if debug:
            print "Step Triples", len(g)
    return g
Esempio n. 3
0
def get_step_triples(update_graph, uri, step_def, debug=True):
    """
    Return the triples matching the criteria defined in the current step of an update
    :param update_graph: the update graph
    :param uri: uri of the entity currently the subject of an update
    :param step_def: step definition from update_def
    :return:  Graph containing one or more triples that match the criteria for the step
    """
    from rdflib import Graph
    from vivopump import vivo_query, add_qualifiers, make_rdf_term
    if 'qualifier' not in step_def['object']:
        g = update_graph.triples((uri, step_def['predicate']['ref'], None))
    else:
        q = 'select (?' + step_def['object']['name'] +' as ?o) where { <' + str(uri) + '> <' + \
            str(step_def['predicate']['ref']) + '> ?' + step_def['object']['name'] + ' .\n' + \
            add_qualifiers([step_def]) + ' }\n'
        if debug:
            print "\nStep Triples Query\n", q
        result_set = vivo_query(q)
        g = Graph()
        for binding in result_set['results']['bindings']:
            o = make_rdf_term(binding['o'])
            g.add((uri, step_def['predicate']['ref'], o))
        if debug:
            print "Step Triples", len(g)
    return g
Esempio n. 4
0
def make_get_query(update_def):
    """
    Given an update_def, return the sparql query needed to produce a spreadsheet of the data to be managed.
    See do_get
    :return: a sparql query string
    """
    from vivopump import add_qualifiers

    front_query = 'SELECT ?uri ?' + ' ?'.join(update_def['column_defs'].keys()) + '\nWHERE {\n    ' + \
                  update_def['entity_def']['entity_sparql'] + '\n'

    # Fake recursion here to depth 3.  Could be replaced by real recursion to arbitrary path length

    middle_query = ""
    for name, path in update_def['column_defs'].items():
        middle_query += '    OPTIONAL {  ?uri <' + str(
            path[0]['predicate']['ref']) + '> ?'
        if len(path) == 1:
            middle_query += name + ' . ' + add_qualifiers(path) + ' }\n'
        else:
            middle_query += path[0]['object']['name'] + ' . ?' +\
                path[0]['object']['name'] + ' <' + str(path[1]['predicate']['ref']) + '> ?'
            if len(path) == 2:
                middle_query += name + ' . ' + add_qualifiers(path) + ' }\n'
            else:
                middle_query += path[1]['object']['name'] + ' . ?' +\
                    path[1]['object']['name'] + ' <' + str(path[2]['predicate']['ref']) + '> ?'
                if len(path) == 3:
                    middle_query += name + ' . ' + add_qualifiers(
                        path) + ' }\n'
                else:
                    raise PathLengthException(
                        'Path length >3 not supported in do_get')

    back_query = '}\nORDER BY ?' + update_def['entity_def']['order_by']
    return front_query + middle_query + back_query
Esempio n. 5
0
    def _get_step_triples(self, uri, step_def):
        """
        Return the triples matching the criteria defined in the current step of an update
        :param uri: uri of the entity currently the subject of an update
        :param step_def: step definition from update_def
        :return:  Graph containing zero or more triples that match the criteria for the step
        """
        from rdflib import Graph, RDF
        from vivopump import add_qualifiers, vivo_query, make_rdf_term

        def step_graph(uris, pred, otype=None, graph=self.update_graph):
            """
            Given a list of uri, a pred and a type, return a graph of the update_graph triples satisfying
                uri pred any   <- these are the returned triples
                any a type
            :param uris: list of uris.
            :param pred: the predicate to use in selecting triples for the step_graph
            :param otype: the object type to use.  default in None, and no type selection will be done.
            :param graph: default is update_graph. Closure sieve requires original_graph
            :return: graph
            """
            sg = Graph()
            for suri in uris:
                for obj in graph.objects(suri, pred):
                    if otype is None:
                        sg.add((suri, pred, obj))
                    elif (obj, RDF.type, otype) in self.update_graph:
                        sg.add((suri, pred, obj))

            return sg

        def sieve_triples(sgc, column_name):
            """
            Given a step graph of triples from a closure (sgc), and the current column_name,
            select the triples from the closure graph that have a path from the entity_uri to
            one or more objects in the closure.  If there is no path, return an empty graph.
            :param sgc:  the step closure graph to be "sieved"
            :param column_name: the name of the column to use
            :return: the sieved closure graph
            """

            print "\nBeginning Closure Graph for", column_name
            for (s, p, o) in sgc.triples((None, None, None)):
                print s, p, o

            if len(sgc) == 0:
                return sgc  # Nothing to sieve
            else:
                pred = self.update_def['column_defs'][column_name][0]['predicate']['ref']
                otype = self.update_def['column_defs'][column_name][0]['object'].get('type', None)
                sg = step_graph([self.entity_uri], pred, otype, graph=self.original_graph)
                if len(sg) == 0 or len(self.update_def['column_defs'][column_name]) == 1:
                    return sg
                print "step 0 graph"
                for (s, p, o) in sg.triples((None, None, None)):
                    print s, p, o
                for step in self.update_def['column_defs'][column_name][1:]:
                    sg = step_graph([y for y in sg.objects(None, None)], step['predicate']['ref'],
                                    step['object'].get('type', None), graph=self.original_graph)
                    print "next step graph"
                    for (s, p, o) in sg.triples((None, None, None)):
                        print s, p, o
                if len(sg) == 0:
                    return sg  # column path is empty, so nothing in the closure can match

                #   Wait for it .... Here's the sieve.  Return triples in the closure graph that have
                #   objects on the column graph

                sgr = Graph()
                for (sgcs, sgcp, sgco) in sgc.triples((None, None, None)):
                    if sgco in sg.objects(None, None):
                        sgr.add((sgcs, sgcp, sgco))

                print "reduced step graph"
                for (s, p, o) in sgr.triples((None, None, None)):
                    print s, p, o

            return sgr
        
        if 'qualifier' not in step_def['object']:

            g = step_graph([uri], step_def['predicate']['ref'], step_def['object'].get('type', None))

            # print "\nStep_triples for", step_def['column_name'], [uri],
            # step_def['predicate']['ref'], step_def['object'].get('type', None)

            for (s, p, o) in g.triples((None, None, None)):
                print unicode(s), unicode(p), unicode(o)

            #   If the step_def is in a closure, and its the last step in the closure, then the
            #   closure triples must be sieved against the objects defined by the column.

            if step_def['closure'] and step_def['last']:

                g = sieve_triples(g, step_def['column_name'])
        else:
        
            #   Handle non-specific predicates qualified by SPARQL (a rare case for VIVO-ISF)
            
            q = 'select (?' + step_def['object']['name'] + ' as ?o) where { <' + str(uri) + '> <' + \
                str(step_def['predicate']['ref']) + '> ?' + step_def['object']['name'] + ' . \n' + \
                add_qualifiers([step_def]) + ' }\n'
            logger.debug(u"Qualified Step Triples Query {}".format(q))
            result_set = vivo_query(q, self.query_parms)  # SLOW
            g = Graph()
            for binding in result_set['results']['bindings']:
                o = make_rdf_term(binding['o'])
                g.add((uri, step_def['predicate']['ref'], o))
        logger.debug(u"Step Triples {}".format(g.serialize(format='nt')))
        return g