|
| 1 | +from rdflib import Dataset, Graph, Literal, URIRef |
| 2 | + |
| 3 | + |
| 4 | +def main(): |
| 5 | + # example for adding a quad |
| 6 | + ds = Dataset() |
| 7 | + g = Graph(identifier=URIRef("http://graph-a")) |
| 8 | + ds.add_graph(g) |
| 9 | + triple = (URIRef("http://subj-a"), URIRef("http://pred-a"), Literal("obj-a")) |
| 10 | + ds.get_context(g.identifier).add(triple) |
| 11 | + result = ds.serialize(format="patch", operation="add") |
| 12 | + print("Add Quad Patch:") |
| 13 | + print(result) |
| 14 | + |
| 15 | + # alternate example for adding a quad |
| 16 | + ds = Dataset() |
| 17 | + quad = ( |
| 18 | + URIRef("http://subj-a"), |
| 19 | + URIRef("http://pred-a"), |
| 20 | + Literal("obj-a"), |
| 21 | + Graph(identifier=URIRef("http://graph-a")), |
| 22 | + ) |
| 23 | + ds.add(quad) |
| 24 | + result = ds.serialize(format="patch", operation="add") |
| 25 | + print("Add Quad Patch:") |
| 26 | + print(result) |
| 27 | + |
| 28 | + # example for adding a triple |
| 29 | + ds = Dataset() |
| 30 | + ds.add(triple) |
| 31 | + result = ds.serialize(format="patch", operation="add") |
| 32 | + print("\nAdd Triple Patch:") |
| 33 | + print(result) |
| 34 | + |
| 35 | + # Example for diff quads |
| 36 | + quad_1 = ( |
| 37 | + URIRef("http://subj-a"), |
| 38 | + URIRef("http://pred-a"), |
| 39 | + Literal("obj-a"), |
| 40 | + Graph(identifier=URIRef("http://graph-a")), |
| 41 | + ) |
| 42 | + quad_2 = ( |
| 43 | + URIRef("http://subj-b"), |
| 44 | + URIRef("http://pred-b"), |
| 45 | + Literal("obj-b"), |
| 46 | + Graph(identifier=URIRef("http://graph-b")), |
| 47 | + ) |
| 48 | + quad_3 = ( |
| 49 | + URIRef("http://subj-c"), |
| 50 | + URIRef("http://pred-c"), |
| 51 | + Literal("obj-c"), |
| 52 | + Graph(identifier=URIRef("http://graph-c")), |
| 53 | + ) |
| 54 | + ds1 = Dataset() |
| 55 | + ds2 = Dataset() |
| 56 | + ds1.addN([quad_1, quad_2]) |
| 57 | + ds2.addN([quad_2, quad_3]) |
| 58 | + result = ds1.serialize(format="patch", target=ds2) |
| 59 | + print("Diff Quad Patch:") |
| 60 | + print(result) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + main() |
0 commit comments