Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Think about __iadd__, __isub__ etc. for ConjunctiveGraph #225

Open
gromgull opened this issue Jul 21, 2012 · 1 comment
Open

Think about __iadd__, __isub__ etc. for ConjunctiveGraph #225

gromgull opened this issue Jul 21, 2012 · 1 comment
Assignees
Labels
enhancement New feature or request id-as-cntxt tracking related issues

Comments

@gromgull
Copy link
Member

Currently all operations are done on the default graph, i.e. if you add another graph, even if it's a conjunctive graph, all triples are added to the default graph.

It may make sense to check if the other thing added is ALSO a conjunctive graph and merge the contexts:

def __iadd__(self, other):
        """Add all triples in ConjunctiveGraph other to this Graph. 
           BNode IDs are not changed."""
        if isinstance(other, ConjunctiveGraph): 
            self.addN(other.quads((None,None,None)))
        else: 
            self.addN((s,p,o,other.identifier) for s,p,o in other)
        return self

    def __isub__(self, other):
        """Subtract all triples in Graph other from Graph. 
           BNode IDs are not changed."""
        if isinstance(other, ConjunctiveGraph):
            for s,p,o,c in other.quads((None,None,None)):
                self.store.remove((s,p,o),c)
        else:
            for triple in other:
                self.remove(triple)
        return self

    def __add__(self, other) :
        """Set-theoretic union 
           BNode IDs are not changed."""
        retval = ConjunctiveGraph()
        for (prefix, uri) in set(
                list(self.namespaces()) + list(other.namespaces())):
            retval.bind(prefix, uri)
        retval+=self
        retval+=other

        return retval
@gromgull
Copy link
Member Author

The doc for ConjunctiveGraph says:

A ConjunctiveGraph is an (unamed) aggregation of all the named graphs
within the Store. It has a default graph, whose name is associated
with the ConjunctiveGraph throughout its life. All methods work against
this default graph.
Its constructor can take an identifier to use as the
name of this default graph or it will assign a BNode.

"all methods work against this default graph" - sounds good, but isn't quite true.

len will give you the length of the conjunction of all graphs
contains and triples_ will check for triples in all graphs
remove will remove from all grapsh

addN/quads work on quads directly

In fact, the only method to make use of the default_context was add
(and iadd gets delegated to add)

So add works on a single graph only, remove does not.

So:

conjgraph+=g1
conjgraph-=g1

may not be what you want (since isub will be delegated to remove which removes from ALL graphs)

I am not really sure what makes the most sense here. Adding iadd/isub that works for quads seems sensible, but breaks backwards compatability in a fairly subtle way. I would postpone until rdflib 4

I also do not like that len gives you the number of TRIPLES not of quads, but that is another issue.

gromgull added a commit that referenced this issue Jun 26, 2013
discussed in #307

Summary of changes:

 * added methods ```add_graph``` and ```remove_graph``` to the Store
   API, implemented these for Sleepycat and IOMemory. A flag,
   ```graph_awareness``` is set on the store if they methods are
   supported, default implementations will raise an exception.

 * made the dataset require a store with the ```graph_awareness```
   flag set.

 * removed the graph-state kept in the ```Dataset``` class directly.

 * removed ```dataset.add_quads```, ```remove_quads``` methods. The
   ```add/remove``` methods of ```ConjunctiveGraph``` are smart enough
   to work with triples or quads.

 * removed the ```dataset.graphs``` method - it now does exactly the
   same as ```contexts```

 * cleaned up a bit more confusion of whether Graph instance or the
   Graph identifiers are passed to store methods. (#225)
gromgull added a commit that referenced this issue Jul 29, 2013
discussed in #307

Summary of changes:

 * added methods ```add_graph``` and ```remove_graph``` to the Store
   API, implemented these for Sleepycat and IOMemory. A flag,
   ```graph_awareness``` is set on the store if they methods are
   supported, default implementations will raise an exception.

 * made the dataset require a store with the ```graph_awareness```
   flag set.

 * removed the graph-state kept in the ```Dataset``` class directly.

 * removed ```dataset.add_quads```, ```remove_quads``` methods. The
   ```add/remove``` methods of ```ConjunctiveGraph``` are smart enough
   to work with triples or quads.

 * removed the ```dataset.graphs``` method - it now does exactly the
   same as ```contexts```

 * cleaned up a bit more confusion of whether Graph instance or the
   Graph identifiers are passed to store methods. (#225)
@ghost ghost added the id-as-cntxt tracking related issues label Dec 24, 2021
@aucampia aucampia added backwards incompatible will affect backwards compatibility, this includes things like breaking our interface and removed backwards incompatible will affect backwards compatibility, this includes things like breaking our interface labels Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request id-as-cntxt tracking related issues
Projects
None yet
Development

No branches or pull requests

2 participants