Skip to content

Instantly share code, notes, and snippets.

@lawlesst
Last active December 17, 2015 10:59
Show Gist options
  • Save lawlesst/5598822 to your computer and use it in GitHub Desktop.
Save lawlesst/5598822 to your computer and use it in GitHub Desktop.
Example of creating individuals with multiple types with RDFAlchemy and RDFLib.
"""
Quick example of creating indivduals with multiple types
using RDFAlchemy or just plain RDFLib.
"""
from rdflib import Namespace, Graph, Literal, RDF, URIRef
from rdfalchemy.rdfSubject import rdfSubject
from rdfalchemy import rdfSingle
FOAF = Namespace('http://xmlns.com/foaf/0.1/')
VIVO = Namespace('http://vivoweb.org/ontology/core#')
g = rdfSubject.db = Graph()
g.bind('foaf', FOAF)
g.bind('vivo', VIVO)
class Person(rdfSubject):
rdf_type = FOAF.Person
firstname = rdfSingle(FOAF.firstName)
lastname = rdfSingle(FOAF.lastName)
class FacultyMember(Person):
rdf_type = VIVO.FacultyMember
preferred_title = rdfSingle(VIVO.preferredTitle)
uri = URIRef('http://vivo.edu/individual/n1234')
this_person = Person(uri)
this_person.firstname = "Joe"
this_person.lastname = "Smith"
this_person = FacultyMember(uri)
this_person.preferred_title = "Professor of History"
print g.serialize(format='n3')
#Or just with rdflib
print '\n----Just RDFLib----\n'
g = Graph()
g.add((uri, RDF.type, FOAF.person))
g.add((uri, FOAF.firstName, Literal('Joe')))
g.add((uri, FOAF.lastName, Literal('Smith')))
g.add((uri, RDF.type, VIVO.facultyMember))
g.add((uri, VIVO.preferredTitle, Literal("Professor of History")))
print g.serialize(format='n3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment