summaryrefslogtreecommitdiff
path: root/examples/swap_primer.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/swap_primer.py')
-rw-r--r--examples/swap_primer.py68
1 files changed, 40 insertions, 28 deletions
diff --git a/examples/swap_primer.py b/examples/swap_primer.py
index dacb92e4..e3adc298 100644
--- a/examples/swap_primer.py
+++ b/examples/swap_primer.py
@@ -1,65 +1,70 @@
"""
-
This is a simple primer using some of the
example stuff in the Primer on N3:
http://www.w3.org/2000/10/swap/Primer
-
"""
-# Load up RDFLib
-
-from rdflib import ConjunctiveGraph, Namespace, Literal
+from rdflib import ConjunctiveGraph, Namespace, Literal, URIRef
from rdflib.namespace import OWL, DC
-if __name__ == '__main__':
+if __name__ == "__main__":
# Firstly, it doesn't have to be so complex.
# Here we create a "Graph" of our work.
# Think of it as a blank piece of graph paper!
primer = ConjunctiveGraph()
- myNS = Namespace('#')
+ myNS = Namespace("#")
primer.add((myNS.pat, myNS.knows, myNS.jo))
# or:
- primer.add((myNS['pat'], myNS['age'], Literal(24)))
+ primer.add((myNS["pat"], myNS["age"], Literal(24)))
# Now, with just that, lets see how the system
# recorded *way* too many details about what
# you just asserted as fact.
- #
from pprint import pprint
+
+ print("All the things in the Graph:")
pprint(list(primer))
# just think .whatever((s, p, o))
# here we report on what we know
+ print("==================")
+
+ print("Subjects:")
pprint(list(primer.subjects()))
+ print("Predicates:")
pprint(list(primer.predicates()))
+ print("Objects:")
pprint(list(primer.objects()))
+ print("==================")
# and other things that make sense
- # what do we know about pat?
+ print("What we know about pat:")
pprint(list(primer.predicate_objects(myNS.pat)))
- # who is what age?
+ print("Who is what age?")
pprint(list(primer.subject_objects(myNS.age)))
+ print("==================")
+ print("==================")
+
# Okay, so lets now work with a bigger
# dataset from the example, and start
# with a fresh new graph.
+ del(primer)
primer = ConjunctiveGraph()
# Lets start with a verbatim string straight from the primer text:
mySource = """
-
-
- @prefix : <http://www.w3.org/2000/10/swap/Primer#>.
+ @prefix : <#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@@ -92,9 +97,6 @@ if __name__ == '__main__':
:Woman = foo:FemaleAdult .
:Title a rdf:Property; = dc:title .
-
-
-
""" # --- End of primer code
# To make this go easier to spit back out...
@@ -103,28 +105,38 @@ if __name__ == '__main__':
# By default, your main namespace is the URI of your
# current working directory, so lets make that simpler:
- myNS = Namespace('http://www.w3.org/2000/10/swap/Primer#')
- primer.bind('', myNS)
- primer.bind('owl', OWL)
- primer.bind('dc', DC)
- primer.bind('swap', 'http://www.w3.org/2000/10/swap/')
+ primer.bind("owl", OWL)
+ primer.bind("dc", DC)
+ primer.bind("swap", "http://www.w3.org/2000/10/swap/")
# Lets load it up!
- primer.parse(data=mySource, format='n3')
+ primer.parse(data=mySource, format="n3")
# Now you can query, either directly straight into a list:
- [(x, y, z) for x, y, z in primer]
+ print()
+ print("Printing bigger example's triples:")
+ for i in [(x, y, z) for x, y, z in primer]:
+ print(i)
# or spit it back out (mostly) the way we created it:
- print(primer.serialize(format='n3'))
+ print()
+ print("Printing bigger example as N3:")
+ print(primer.serialize(format="n3").decode("utf-8"))
# for more insight into things already done, lets see the namespaces
- list(primer.namespaces())
+ print()
+ print("Printing bigger example's namespaces:")
+ for n in list(primer.namespaces()):
+ print(n)
- # lets ask something about the data
+ # lets ask something about the data, using a SPARQL query
- list(primer.objects(myNS.pat, myNS.child))
+ print()
+ print("Who are pat's children?")
+ q = "SELECT ?child WHERE { :pat :child ?child }"
+ for r in primer.query(q):
+ print(r)