Cara Generate File Owl Dari Ontologi Perpustakaan Digital
Cara Generate File OWL dari Ontologi Perpustakaan Digital
1. Install library dulu (kalau belum ada):
pip install rdflib
2. Kode Python untuk membuat file library.owl
:
from rdflib import Graph, Namespace, RDF, RDFS, OWL, Literal
# Buat Graph RDF
g = Graph()
# Definisikan Namespace
LIB = Namespace("http://www.semanticweb.org/ontologies/library#")
g.bind("lib", LIB)
g.bind("rdf", RDF)
g.bind("rdfs", RDFS)
g.bind("owl", OWL)
# Definisikan Kelas
g.add((LIB.Book, RDF.type, OWL.Class))
g.add((LIB.Author, RDF.type, OWL.Class))
g.add((LIB.User, RDF.type, OWL.Class))
g.add((LIB.Publisher, RDF.type, OWL.Class))
g.add((LIB.EBook, RDF.type, OWL.Class))
g.add((LIB.EBook, RDFS.subClassOf, LIB.Book))
# Object Properties
g.add((LIB.hasAuthor, RDF.type, OWL.ObjectProperty))
g.add((LIB.hasAuthor, RDFS.domain, LIB.Book))
g.add((LIB.hasAuthor, RDFS.range, LIB.Author))
g.add((LIB.borrowedBy, RDF.type, OWL.ObjectProperty))
g.add((LIB.borrowedBy, RDFS.domain, LIB.Book))
g.add((LIB.borrowedBy, RDFS.range, LIB.User))
# Data Properties
g.add((LIB.title, RDF.type, OWL.DatatypeProperty))
g.add((LIB.title, RDFS.domain, LIB.Book))
g.add((LIB.title, RDFS.range, RDFS.Literal))
g.add((LIB.publicationYear, RDF.type, OWL.DatatypeProperty))
g.add((LIB.publicationYear, RDFS.domain, LIB.Book))
g.add((LIB.publicationYear, RDFS.range, RDFS.Literal))
g.add((LIB.name, RDF.type, OWL.DatatypeProperty))
g.add((LIB.name, RDFS.domain, LIB.Author))
g.add((LIB.name, RDFS.range, RDFS.Literal))
# Individu
book1 = LIB.Book_001
author1 = LIB.Author_001
user1 = LIB.User_001
g.add((book1, RDF.type, LIB.Book))
g.add((book1, LIB.title, Literal("Introduction to OWL")))
g.add((book1, LIB.publicationYear, Literal(2023)))
g.add((book1, LIB.hasAuthor, author1))
g.add((book1, LIB.borrowedBy, user1))
g.add((author1, RDF.type, LIB.Author))
g.add((author1, LIB.name, Literal("Alice Smith")))
g.add((user1, RDF.type, LIB.User))
# Simpan ke file OWL
g.serialize(destination="library.owl", format='xml')
print("File 'library.owl' berhasil dibuat.")
0 Comments