Пример #1
0
    def remove(self, statement, context=None):
        """Remove a triple from the graph"""
        log.debug("remove(%s) from %s" % (statement, context))
        _context = _get_context(context)
        s,p,o = skolemise(statement)
        bindings = (
            s or Variable("s"),
            p or Variable("p"),
            o or Variable("o")
        )

        construct = u"CONSTRUCT { " + _n3(bindings) + u" } WHERE { " 
        if _context != "local:": construct += u"GRAPH <%s> { " % _context
        construct += _n3(bindings)
        if _context != "local:": construct += u" }"
        construct += u" }"

        result = self.cursor().execute(construct)
        _skg = SkolemGraph(result)

        print result.serialize(format="n3")
        if len(result) > 0:
            delete = u"DELETE DATA { "
            if _context != "local:": delete += u"GRAPH <%s> {\n" % _context
            delete += _skg.serialize(format="nt")
            if _context != "local:": delete += u"}"
            delete += u" }"

            print delete

            self.cursor().update(delete)
Пример #2
0
    def triples(self, statement, context=None, **kw):
        """Return triples matching (s,p,o) pattern"""

        ## shortcut if we are just checking the existence
        if all(statement):
            if self.__contains__(statement, context):
                yield statement, context
            return

        _context = _get_context(context)

        s,p,o = skolemise(statement)
        bindings = (
            s or Variable("s"),
            p or Variable("p"),
            o or Variable("o")
        )
        query = u"SELECT DISTINCT " + u" ".join([x.n3() for x in bindings if isinstance(x, Variable)])
        query += u" WHERE { "
        if _context != "local:": query += u"GRAPH <%s> { " % _context
        query += u" ".join([x.n3() for x in bindings])
        if _context != "local:": query += u" }"
        query += u" }"
        results = self.cursor().execute(query, **kw)
        for row in results:
            triple = []
            for b in bindings:
                if isinstance(b, Variable):
                    triple.append(row[b])
                else:
                    triple.append(b)
            yield (deskolemise(triple), context)
Пример #3
0
    def add(self, statement, context=None, **kw):
        """Add triples to the graph"""
        context = _get_context(context)

        if not self.exists(statement, context):
            cursor = self.cursor()
            g = Graph(identifier=context)
            g.add(skolemise(statement))
            cursor.add_graph(g, replace=False)
Пример #4
0
 def addN(self, slist, **kw):
     """Add a list of quads to the graph"""
     graphs = {}
     for s,p,o,c in slist:
         c = _get_context(c)
         g = graphs.get(c, Graph(identifier=c))
         if not self.exists((s,p,o), c):
             g.add(skolemise((s,p,o)))
     cursor = self.cursor()
     for g in graphs.values():
         cursor.add_graph(g, replace=False)
Пример #5
0
 def exists(self, statement, context=None):
     context = _get_context(context)
     s,p,o = skolemise(statement)
     q = u"ASK WHERE { GRAPH <%s> { %s %s %s } }" % (context, s.n3(), p.n3(), o.n3())
     return bool(self.query(q))