summaryrefslogtreecommitdiff
path: root/examples/conjunctive_graphs.py
blob: 433a843f490817718446bb724af0b4610747bd9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
An RDFLib ConjunctiveGraph is an (unnamed) aggregation of all the Named Graphs
within a Store. The :meth:`~rdflib.graph.ConjunctiveGraph.get_context`
method can be used to get a particular named graph for use, such as to add
triples to, or the default graph can be used.

This example shows how to create Named Graphs and work with the
conjunction (union) of all the graphs.
"""

from rdflib import Literal, Namespace, URIRef
from rdflib.graph import ConjunctiveGraph, Graph
from rdflib.plugins.stores.memory import Memory

if __name__ == "__main__":
    LOVE = Namespace("http://love.com#")
    LOVERS = Namespace("http://love.com/lovers/")

    mary = URIRef("http://love.com/lovers/mary")
    john = URIRef("http://love.com/lovers/john")

    cmary = URIRef("http://love.com/lovers/mary")
    cjohn = URIRef("http://love.com/lovers/john")

    store = Memory()

    g = ConjunctiveGraph(store=store)
    g.bind("love", LOVE)
    g.bind("lovers", LOVERS)

    # Add a graph containing Mary's facts to the Conjunctive Graph
    gmary = Graph(store=store, identifier=cmary)
    # Mary's graph only contains the URI of the person she loves, not his cute name
    gmary.add((mary, LOVE.hasName, Literal("Mary")))
    gmary.add((mary, LOVE.loves, john))

    # Add a graph containing John's facts to the Conjunctive Graph
    gjohn = Graph(store=store, identifier=cjohn)
    # John's graph contains his cute name
    gjohn.add((john, LOVE.hasCuteName, Literal("Johnny Boy")))

    # Enumerate contexts
    print("Contexts:")
    for c in g.contexts():
        print(f"-- {c.identifier} ")
    print("===================")
    # Separate graphs
    print("John's Graph:")
    print(gjohn.serialize())
    print("===================")
    print("Mary's Graph:")
    print(gmary.serialize())
    print("===================")

    print("Full Graph")
    print(g.serialize())
    print("===================")

    print("Query the conjunction of all graphs:")
    xx = None
    for x in g[mary : LOVE.loves / LOVE.hasCuteName]:  # type: ignore[misc]
        xx = x
    print("Q: Who does Mary love?")
    print("A: Mary loves {}".format(xx))