File: patch_serializer_example.py

package info (click to toggle)
rdflib 7.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 77,852 kB
  • sloc: python: 59,555; sh: 153; makefile: 83; ruby: 74; xml: 45
file content (64 lines) | stat: -rw-r--r-- 1,721 bytes parent folder | download | duplicates (2)
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
from rdflib import Dataset, Graph, Literal, URIRef


def main():
    # example for adding a quad
    ds = Dataset()
    g = Graph(identifier=URIRef("http://graph-a"))
    ds.add_graph(g)
    triple = (URIRef("http://subj-a"), URIRef("http://pred-a"), Literal("obj-a"))
    ds.get_context(g.identifier).add(triple)
    result = ds.serialize(format="patch", operation="add")
    print("Add Quad Patch:")
    print(result)

    # alternate example for adding a quad
    ds = Dataset()
    quad = (
        URIRef("http://subj-a"),
        URIRef("http://pred-a"),
        Literal("obj-a"),
        Graph(identifier=URIRef("http://graph-a")),
    )
    ds.add(quad)
    result = ds.serialize(format="patch", operation="add")
    print("Add Quad Patch:")
    print(result)

    # example for adding a triple
    ds = Dataset()
    ds.add(triple)
    result = ds.serialize(format="patch", operation="add")
    print("\nAdd Triple Patch:")
    print(result)

    # Example for diff quads
    quad_1 = (
        URIRef("http://subj-a"),
        URIRef("http://pred-a"),
        Literal("obj-a"),
        Graph(identifier=URIRef("http://graph-a")),
    )
    quad_2 = (
        URIRef("http://subj-b"),
        URIRef("http://pred-b"),
        Literal("obj-b"),
        Graph(identifier=URIRef("http://graph-b")),
    )
    quad_3 = (
        URIRef("http://subj-c"),
        URIRef("http://pred-c"),
        Literal("obj-c"),
        Graph(identifier=URIRef("http://graph-c")),
    )
    ds1 = Dataset()
    ds2 = Dataset()
    ds1.addN([quad_1, quad_2])
    ds2.addN([quad_2, quad_3])
    result = ds1.serialize(format="patch", target=ds2)
    print("Diff Quad Patch:")
    print(result)


if __name__ == "__main__":
    main()