Skip to content

Instantly share code, notes, and snippets.

@ashleysommer
Created September 20, 2018 00:40
Show Gist options
  • Save ashleysommer/87f0b9660a71de380889f98745af2f74 to your computer and use it in GitHub Desktop.
Save ashleysommer/87f0b9660a71de380889f98745af2f74 to your computer and use it in GitHub Desktop.
import rdflib
import rdflib_jsonld
from rdflib.namespace import Namespace, XSD
EX = Namespace("http://example.org/")
false_bool = rdflib.Literal("false", datatype=XSD.boolean)
true_bool = rdflib.Literal("true", datatype=XSD.boolean)
test_turtle = '''
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.org/> .
:test1 :isAlive "false"^^xsd:boolean .
:test2 :isAlive "true"^^xsd:boolean .
:test3 :isAlive "0"^^xsd:boolean .
:test4 :isAlive "1"^^xsd:boolean .
'''
g = rdflib.Graph()
g.parse(data=test_turtle, format='turtle')
result1 = g.value(EX.test1, EX.isAlive)
result2 = g.value(EX.test2, EX.isAlive)
result3 = g.value(EX.test3, EX.isAlive)
result4 = g.value(EX.test4, EX.isAlive)
print("\nReading values from Turtle-parsed graph:")
for r in (result1, result2, result3, result4):
print("value: {} , datatype: {} ".format(
str(r._value), str(r.datatype)))
try:
assert not (result4 == true_bool), "\"1\" should not equal \"true\""
except AssertionError as a:
print("assertion fail: \n{}".format(str(a)))
test_xml = '''<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:ns0="http://example.org/">
<rdf:Description rdf:about="http://example.org/test1">
<ns0:isAlive rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">false</ns0:isAlive>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/test2">
<ns0:isAlive rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</ns0:isAlive>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/test3">
<ns0:isAlive rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">0</ns0:isAlive>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/test4">
<ns0:isAlive rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">1</ns0:isAlive>
</rdf:Description>
</rdf:RDF>'''
g = rdflib.Graph()
g.parse(data=test_xml, format='xml')
result1 = g.value(EX.test1, EX.isAlive)
result2 = g.value(EX.test2, EX.isAlive)
result3 = g.value(EX.test3, EX.isAlive)
result4 = g.value(EX.test4, EX.isAlive)
print("\nReading values from RDF/XML-parsed graph:")
for r in (result1, result2, result3, result4):
print("value: {} , datatype: {} ".format(
str(r._value), str(r.datatype)))
try:
assert not (result4 == true_bool), "\"1\" should not equal \"true\""
except AssertionError as a:
print("assertion fail: \n{}".format(str(a)))
test_json = '''
[
{"@id":"http://example.org/test1",
"http://example.org/isAlive":[{"@value":"false","@type":"http://www.w3.org/2001/XMLSchema#boolean"}]},
{"@id":"http://example.org/test2",
"http://example.org/isAlive":[{"@value":"true","@type":"http://www.w3.org/2001/XMLSchema#boolean"}]},
{"@id":"http://example.org/test3",
"http://example.org/isAlive":[{"@value":"0","@type":"http://www.w3.org/2001/XMLSchema#boolean"}]},
{"@id":"http://example.org/test4",
"http://example.org/isAlive":[{"@value":"1","@type":"http://www.w3.org/2001/XMLSchema#boolean"}]}
]
'''
g = rdflib.Graph()
g.parse(data=test_json, format='json-ld')
result1 = g.value(EX.test1, EX.isAlive)
result2 = g.value(EX.test2, EX.isAlive)
result3 = g.value(EX.test3, EX.isAlive)
result4 = g.value(EX.test4, EX.isAlive)
print("\nReading values from JSON-LD parsed graph:")
for r in (result1, result2, result3, result4):
print("value: {} , datatype: {} ".format(
str(r._value), str(r.datatype)))
try:
assert not (result4 == true_bool), "\"1\" should not equal \"true\""
except AssertionError as a:
print("assertion fail: \n{}".format(str(a)))
print("\nTesting Raw literals.")
fail_true = rdflib.Literal("1", datatype=XSD.boolean)
fail_false = rdflib.Literal("0", datatype=XSD.boolean)
for f in (fail_true, fail_false):
print("value: {} , datatype: {} ".format(
str(f._value), str(f.datatype)))
try:
assert not (fail_true == true_bool), "\"1\" should not equal \"true\""
except AssertionError as a:
print("assertion fail: \n{}".format(str(a)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment