Exemple #1
0
 def force_state_refresh(self, parameters=None):
     """I override `.cores.hosted.HostedCore.force_state_refresh`.
     """
     # nothing to do, there is no cache involved
     self.check_parameters(parameters, parameters, "force_state_refresh")
     if self._state is not None:
         self._state.remove((None, None, None))
         bounded_description(self.uri, self.home.get_state(), self._state)
     return
Exemple #2
0
 def delete(self, parameters=None, _trust=False):
     """I implement :meth:`.cores.ICore.delete`.
     """
     self.check_parameters(parameters, parameters, "delete")
     cbd = bounded_description(self.uri, self.home.get_state())
     with self.home.edit(_trust=_trust) as editable:
         for triple in cbd:
             editable.remove(triple)
Exemple #3
0
    def post_graph(self,
                   graph,
                   parameters=None,
                   _trust=False,
                   _created=None,
                   _rdf_type=None):
        """I override :meth:`rdfrest.util.GraphPostableMixin.post_graph`.

        I allow for multiple obsels to be posted at the same time.
        """
        base = self.get_base()
        post_single_obsel = super(StoredTrace, self).post_graph
        binding = {"trace": self.uri}
        ret = []
        candidates = [
            i[0] for i in graph.query(_SELECT_CANDIDATE_OBSELS,
                                      initBindings=binding)
        ]
        bnode_candidates = {i for i in candidates if isinstance(i, BNode)}
        with base.lock(self), self.obsel_collection.edit(
            {"add_obsels_only": 1}, _trust=True):
            for candidate in candidates:
                if isinstance(candidate, BNode):
                    bnode_candidates.remove(candidate)
                obs_graph = bounded_description(candidate,
                                                graph,
                                                prune=bnode_candidates)
                for other in bnode_candidates:
                    obs_graph.remove((candidate, None, other))
                    obs_graph.remove((other, None, candidate))

                ret1 = post_single_obsel(obs_graph, parameters, _trust,
                                         candidate, KTBS.Obsel)
                if ret1:
                    assert len(ret1) == 1
                    new_obs = ret1[0]
                    ret.append(new_obs)
                    if new_obs != candidate:
                        replace_node_sparse(graph, candidate, new_obs)

        assert not bnode_candidates, bnode_candidates
        if not ret:
            raise InvalidDataError("No obsel found in posted graph")

        stats = self.trace_statistics
        if stats:
            # Traces created before @stats was introduced have no trace_statistics
            stats.metadata.set((stats.uri, METADATA.dirty, YES))
        return ret
Exemple #4
0
def get_obsel_bounded_description(node, graph, fill=None):
    """I override :func:`rdfrest.util.bounded_description` for obsels.

    In order to clearly differenciate attributes from relations,
    related obsels must be linked to the trace by the ktbs:hasTrace.

    :param node: the node (uri or blank) to return a description of
    :param graph: the graph from which to retrieve the description
    :param fill: if provided, fill this graph rather than a fresh one, and return it
    """
    ret = bounded_description(node, graph, fill)
    trace_uri = ret.value(node, KTBS.hasTrace)
    add = ret.add
    for other, _, in graph.query(_RELATED_OBSELS, initBindings = { "obs": node }):
        add((other, KTBS.hasTrace, trace_uri))
    return ret
Exemple #5
0
def test_bounded_description():
    g1 = Graph()
    g1.parse(
        StringIO(
            """@prefix : <http://example.org/> .
    :node
        :p1 :succ1 ;
        :p2 42 ;
        :p3 (101 :succ2 [ :p3 "blank list item" ] ) ;
        .
    :succ1 :p4 :out1 .
    :succ2 :p5 :out2 .

    :pred1 :p6 :node .
    :pred2 :p7 [ :p8 :node ] ;
           :p8 [ :p9 "out3" ] ;
           :p9 :out4 .

    [ :pA :pred3 ] :pB :node .

    :out5 :pC :pred3 .
    :pred1 :pD :succ1 .
    """
        ),
        format="n3",
    )

    gref = Graph()
    gref.parse(
        StringIO(
            """@prefix : <http://example.org/> .
    :node
        :p1 :succ1 ;
        :p2 42 ;
        :p3 (101 :succ2 [ :p3 "blank list item" ] ) ;
        .
    :pred1 :p6 :node .
    :pred2 :p7 [ :p8 :node ] .

    [ :pA :pred3 ] :pB :node .
    """
        ),
        format="n3",
    )

    gbd = bounded_description(URIRef("http://example.org/node"), g1)
    assert isomorphic(gref, gbd)
Exemple #6
0
    def post_graph(self, graph, parameters=None,
                   _trust=False, _created=None, _rdf_type=None):
        """I override :meth:`rdfrest.util.GraphPostableMixin.post_graph`.

        I allow for multiple obsels to be posted at the same time.
        """
        base = self.get_base()
        post_single_obsel = super(StoredTrace, self).post_graph
        binding = { "trace": self.uri }
        ret = []
        candidates = [ i[0] for i in graph.query(_SELECT_CANDIDATE_OBSELS,
                                                 initBindings=binding) ]
        bnode_candidates = { i for i in candidates
                               if isinstance(i, BNode) }
        with self.obsel_collection.edit({"add_obsels_only":1}, _trust=True):
            for candidate in candidates:
                if isinstance(candidate, BNode):
                    bnode_candidates.remove(candidate)
                obs_graph = bounded_description(candidate, graph, prune=bnode_candidates)
                for other in bnode_candidates:
                    obs_graph.remove((candidate, None, other))
                    obs_graph.remove((other, None, candidate))

                ret1 = post_single_obsel(obs_graph, parameters, _trust, candidate,
                                         KTBS.Obsel)
                if ret1:
                    assert len(ret1) == 1
                    new_obs = ret1[0]
                    ret.append(new_obs)
                    if new_obs != candidate:
                        replace_node_sparse(graph, candidate, new_obs)

        assert not bnode_candidates, bnode_candidates
        if not ret:
            raise InvalidDataError("No obsel found in posted graph")

        stats = self.trace_statistics
        if stats:
            # Traces created before @stats was introduced have no trace_statistics
            stats.metadata.set((stats.uri, METADATA.dirty, YES))
        return ret
Exemple #7
0
def test_bounded_description():
    g1 = Graph()
    g1.parse(StringIO("""@prefix : <http://example.org/> .
    :node
        :p1 :succ1 ;
        :p2 42 ;
        :p3 (101 :succ2 [ :p3 "blank list item" ] ) ;
        .
    :succ1 :p4 :out1 .
    :succ2 :p5 :out2 .

    :pred1 :p6 :node .
    :pred2 :p7 [ :p8 :node ] ;
           :p8 [ :p9 "out3" ] ;
           :p9 :out4 .

    [ :pA :pred3 ] :pB :node .

    :out5 :pC :pred3 .
    :pred1 :pD :succ1 .
    """),
             format="n3")

    gref = Graph()
    gref.parse(StringIO("""@prefix : <http://example.org/> .
    :node
        :p1 :succ1 ;
        :p2 42 ;
        :p3 (101 :succ2 [ :p3 "blank list item" ] ) ;
        .
    :pred1 :p6 :node .
    :pred2 :p7 [ :p8 :node ] .

    [ :pA :pred3 ] :pB :node .
    """),
               format="n3")

    gbd = bounded_description(URIRef("http://example.org/node"), g1)
    assert isomorphic(gref, gbd)