Ejemplo n.º 1
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
Ejemplo 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
Ejemplo n.º 3
0
 def test_uriref_case(self):
     from rdflib import URIRef
     input_dict = {
         "type": "uri",
         "value": "http://vivo.ufl.edu/individual/n531532305"
     }
     rdf_term = make_rdf_term(input_dict)
     print rdf_term
     self.assertTrue(type(rdf_term) is URIRef)
Ejemplo n.º 4
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