Examples from the lectures: Difference between revisions

From info216
(Replaced content with "This page will be updated with code examples from the lectures.")
Tag: Replaced
Line 1: Line 1:
This page contains code examples from the lectures.
This page will be updated with code examples from the lectures.
 
==S02==
 
===Getting started with RDFlib===
<syntaxhighlight>
from inspect import formatargspec
from rdflib import Graph, Literal, RDF, FOAF, Namespace
 
g = Graph()
 
EX = Namespace('http://ex.org/')
 
g.add((EX.Bob, RDF.type, FOAF.Person))
g.add((EX.Alice, RDF.type, FOAF.Person))
g.add((EX.Carol, RDF.type, FOAF.Person))
g.add((EX.Bob, FOAF.knows, EX.Alice))
g.add((EX.Bob, FOAF.knows, EX.Carol))
 
g.add((EX.Bob, FOAF.name, Literal('Bob')))
g.add((EX.Alice, FOAF.name, Literal('Alice')))
g.add((EX.Carol, FOAF.name, Literal('Carol')))
 
g.namespace_manager.bind('ex', EX)
 
print(g.serialize(format='json-ld'))
 
for p, o in g[ EX.Bob : : ]:
    print(p, o)
 
 
g2 = Graph()
g2.parse('https://www.wikidata.org/entity/Q935079.ttl')
print(g2.serialize())
</syntaxhighlight>
 
==S03==
 
===Local query with RDFlib===
<syntaxhighlight>
from rdflib import Graph, Namespace
 
REX = Namespace('http://example.org/royal#')
 
g = Graph()
g.parse('family.ttl', format='ttl')
res = g.query("""
    CONSTRUCT {
        ?child fam:aunt ?aunt .
    } WHERE {
        ?child fam:hasParent / fam:hasSister ?aunt .
    }
""",
    initNs={
        'fam': 'http://example.org/family#'
    },
    initBindings={
        'child': REX.SverreMagnus
    })
</syntaxhighlight>
 
==S04==
 
===Remote query with SPARQLWrapper===
<syntaxhighlight>
import SPARQLWrapper
 
endpoint = 'http://sandbox.i2s.uib.no/bigdata/namespace/kb/sparql'
 
paper_uri = 'http://semanticscholar.org/paper/c93a37e6922e09f34fc093f7e4f9675775d4557d'
 
client = SPARQLWrapper.SPARQLWrapper(endpoint=endpoint)
client.setReturnFormat('json')
client.setQuery(f'''
    DESCRIBE <{paper_uri}>
''')
res = client.queryAndConvert()
</syntaxhighlight>
 
===Remote update with SPARQLWrapper===
<syntaxhighlight>
client.setReturnFormat('xml')
client.setMethod('POST')
client.setQuery('''
    DELETE DATA {
        <http://example.org/Abra> <http://example.org/ka> <http://example.org/Dabra> .
    }
''')
res = client.queryAndConvert()
client.setMethod('GET')
</syntaxhighlight>
 
===Local update with RDFlib===
<syntaxhighlight>
from rdflib import Graph, Namespace
 
g = Graph()
g.parse('family.ttl', format='turtle')
 
FAM = Namespace('http://example.org/family#')
g.update('''
    DELETE {
        ?child fam:hasAunt ?parent .
    } INSERT {
        ?child fam:hasAunt ?sister .
    } WHERE {
        ?child fam:hasParent ?parent .
        ?parent fam:hasSister ?sister .
    }
    ''',
    initNs={
        'fam': FAM
    })
 
print(g.serialize(format='turtle'))
</syntaxhighlight>
 
== S07 ==
 
RDFS programming with owlrl .
 
=== Syllogism (rule rdfs9) ===
 
<syntaxhighlight>
from rdflib import Graph, RDF, RDFS, OWL, Namespace
import owlrl
 
EX = Namespace('http://example.org#')
 
g = Graph()
g.bind('', EX)
NS = {
    '': EX,
    'rdf': RDF,
    'rdfs': RDFS,
}
g.update("""
INSERT DATA {
    :Socrates rdf:type :Man .
    :Man rdfs:subClassOf :Mortal .
}
""", initNs=NS)
 
rdfs_engine = owlrl.RDFSClosure.RDFS_Semantics(g, True, False, False)
rdfs_engine.closure()
rdfs_engine.flush_stored_triples()
 
res = g.query("""
ASK { :Socrates rdf:type :Mortal . }
""", initNs=NS)
print(res.askAnswer)
</syntaxhighlight>
 
=== Domain and range (rules rdfs2-3) ===
 
<syntaxhighlight>
from rdflib import Graph, RDF, RDFS, OWL, Namespace
import owlrl
 
EX = Namespace('http://example.org#')
 
g = Graph()
g.bind('', EX)
NS = {
    '': EX,
    'rdf': RDF,
    'rdfs': RDFS,
}
g.update("""
INSERT DATA {
    :Socrates :husbandOf :Xantippe .
    :husbandOf rdfs:domain :Man .
    :husbandOf rdfs:range :Woman .
}
""", initNs=NS)
 
rdfs_engine = owlrl.RDFSClosure.RDFS_Semantics(g, True, False, False)
rdfs_engine.closure()
rdfs_engine.flush_stored_triples()
 
res = g.query("""
ASK { :Xantippe rdf:type :Woman . }
""", initNs=NS)
print(res.askAnswer)
</syntaxhighlight>
 
==S08==
 
===owl:inverseOf===
 
<syntaxhighlight>
from rdflib import Graph, Namespace, RDF, RDFS, FOAF
import owlrl
 
 
EX = Namespace('http://ex.org/')
 
g = Graph()
g.bind('', EX)
g.add((EX.Andreas, RDF.type, EX.Teacher))
g.add((EX.Martin, RDF.type, EX.Teacher))
g.add((EX.Tobias, RDF.type, EX.Teacher))
g.add((EX.Martin, RDF.type, EX.Student))
g.add((EX.Tobias, RDF.type, EX.Student))
g.add((EX.Mariah, RDF.type, EX.Student))
g.add((EX.Bahareh, RDF.type, EX.Student))
# g.add((EX.Teacher, RDFS.subClassOf, FOAF.Person))
# g.add((EX.Student, RDFS.subClassOf, FOAF.Person))
 
 
g.update("""
INSERT DATA {
    :Martin :hasSupervisor :Andreas .
    :hasSupervisor owl:inverseOf :supervisorOf .
}
""")
</syntaxhighlight>
 
This gives no response:
<syntaxhighlight>
res = g.query("""
SELECT ?person WHERE {
    :Andreas :supervisorOf ?person .
}
""")
print(res.serialize(format='txt').decode())
</syntaxhighlight>
 
Not this either:
<syntaxhighlight>
engine = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)
engine.closure()
engine.flush_stored_triples()
 
res = g.query("""
SELECT ?person WHERE {
    :Andreas :supervisorOf ?person .
}
""")
print(res.serialize(format='txt').decode())
</syntaxhighlight>
 
But this prints out a response:
<syntaxhighlight>
engine = owlrl.CombinedClosure.RDFS_OWLRL_Semantics(g, False, False, False)
engine.closure()
engine.flush_stored_triples()
 
res = g.query("""
SELECT ?person WHERE {
    :Andreas :supervisorOf ?person .
}
""")
print(res.serialize(format='txt').decode())
</syntaxhighlight>
 
==S11==
 
===Simple DL reasoning in Protégé===
 
Download the [[:file:ex-prop-res.owl.txt | OWL file]], and remove the ".txt" suffix (required by the wiki platform).
 
Load the renamed file into Protege-OWL. From the "Reasoner" menu, choose "HermiT ..." and then "Start reasoner" to see the results of reasoning. Whenever you change the ontology, use "Synchronize reasoner" to update the reasoning results.
 
===Simple DL reasoning in Python's owlready2===
 
First you need to
 
    pip install owlready2
 
then you can run this:
 
<syntaxhighlight>
import os
 
from owlready2 import *
 
 
# on Windows, the HermiT reasoner needs to find Java
TEMP_DIR = 'temp_owlready2/'
if os.name=='nt':
    JAVA_EXE = 'C:\\Program Files\\Java\\jre1.8.0_251'
    # JAVA_EXE = os.getenv('JRE_HOME')
 
# load the ontology we have saved from Protege-OWL (save in RDF-XML or OWL-XML format)
EX = 'file://ex-prop-res.owl'
onto = get_ontology(EX)
onto.load('file://ex-prop-res.owl')
 
# pick out all the triples inside owlready2 as an rdflib Graph
g = default_world.as_rdflib_graph()
print(g.serialize(format='ttl'))
 
# use an owlready2 method to loop through all the individuals in the ontology
for ind in onto.individuals():
    print(ind, ind.is_a)
 
# run the built-in HermiT reasoner
sync_reasoner()
 
# loop through the individuals again to see the new types added by HermiT
for ind in onto.individuals():
    print(ind, ind.is_a)
</syntaxhighlight>
 
===Simple ontology creation in Python's owlready2===
The code below does the same thing, but instead of reading the ontology from a file, it defines it using owlready2:
<syntaxhighlight>
import os
 
from owlready2 import *
 
 
TEMP_DIR = 'temp_owlready2/'
if os.name=='nt':
    JAVA_EXE = 'C:\\Program Files\\Java\\jre1.8.0_251'
    # JAVA_EXE = os.getenv('JRE_HOME')
 
EX = 'http://info216.uib.no/ex-prop-res/'
onto = get_ontology(EX)
 
# get_ontology(EX) will reload the ontology if it already exists in your workspace,
# so we need to empty it
for ind in onto.individuals():
    destroy_entity(ind)
for cls in onto.classes():
    destroy_entity(cls)
 
with onto:
    # add classes, properties and individuals to the 'onto' ontology
    class Person(Thing): pass
    class Woman(Person): pass
    class hasSibling(SymmetricProperty):
        domain = [Person]
        range = [Person]
 
    marthaLouise = Woman('MarthaLouise')
    ingridAlexandra = Woman('IngridAlexandra')
    haakonMagnus = Person('HaakonMagnus')
    sverreMagnus = Person('SverreMagnus')
 
    haakonMagnus.hasSibling = [marthaLouise]
    ingridAlexandra.hasSibling = [sverreMagnus]
 
# have a look at the triples in Turtle
g = default_world.as_rdflib_graph()
print(g.serialize(format='ttl'))
</syntaxhighlight>
 
Add the restriction classes too:
<syntaxhighlight>
with onto:
    class Sister(Person):
        equivalent_to = [Woman & hasSibling.some(Thing)]
 
    class Parent(Person): pass
    class Aunt(Person):
        equivalent_to = [Sister & hasSibling.some(Parent)]
 
    marthaLouise.is_a.append(Parent)
    haakonMagnus.is_a.append(Parent)
</syntaxhighlight>
 
Now you can look at the turtle again, and list all the individuals and their types (classes):
<syntaxhighlight>
g = default_world.as_rdflib_graph()
print(g.serialize(format='ttl'))
 
for ind in onto.individuals():
    print(ind, ind.is_a)
</syntaxhighlight>
 
Run the reasoner:
<syntaxhighlight>
sync_reasoner()
</syntaxhighlight>
 
List the individuals and their types again to see (some of) the results of reasoning.

Revision as of 20:35, 11 January 2023

This page will be updated with code examples from the lectures.