RDFLib: How to create ontology in Python?

Viewed 3337

I am defining an ontology for Urban system using rdflib in Python. I defined the class urbanSystem and the subClass name.

import rdflib
from rdflib.graph import Graph, ConjunctiveGraph
from rdflib import Graph, URIRef, BNode, Literal
from rdflib import Namespace
from rdflib.namespace import OWL, RDF, RDFS, FOAF
myOntology =  Namespace("http://www.semanticweb.org/myOntology#")
g.bind("myOntology", myOntology)

# Create the graph 
g = Graph()
# Create the node to add to the Graph
urbanSystem = URIRef(myOntology["urbanSystem"])

# Add the OWL data to the graph
g.add((urbanSystem, RDF.type, OWL.Class))
g.add((urbanSystem, RDFS.subClassOf, OWL.Thing))

name = URIRef(myOntology["name"])
g.add((name, RDF.type, OWL.Class))
g.add((name, RDFS.subClassOf, urbanSystem))

Now I want to add a city to my ontology, for instance Paris.

label = URIRef(myOntology["Paris"])

what is the best way to say Paris is an urbanSystem and with name Paris

This is what I am doing.

g.add( (label, RDF.type, myOntology.urbanSystem) )
g.add( (label, myOntology.name, Literal['Paris']) )
1 Answers

You define name as a Class,but then use it as an Object Property. You need

g.add((name, RDF.type, OWL.ObjectProperty))

Otherwise looks OK

Related