;; references http://owlapi.svn.sourceforge.net/viewvc/owlapi/owl1_1/trunk/examples/src/main/java/org/coode/owlapi/examples/Example10.java?view=markup ;; classpath includes all the jars below lib in http://pellet.owldl.com/downloads/pellet-1.5.2.zip (defstruct (kb2 (:print-function print-kb2-struct)) loaded-from manager ontology factory is-changed ) (defun print-kb2-struct (kb stream depth) (print-unreadable-object (kb stream) (format stream "owlapi manager on ~a~a" (or (kb2-loaded-from kb) (#"toString" (#"getURI" (kb2-ontology kb)))) (if (kb2-is-changed kb) "*" "")))) (defmethod imports ((kb2 kb2)) (mapcar 'make-uri (mapcar #"toString" (mapcar #"getIRI" (set-to-list (#"getImportsClosure" (kb2-manager kb) (kb2-ontology kb))))))) (defmethod annotation-properties ((kb2 kb2)) (remove-duplicates (append-over-import-closure kb2 (lambda(o) (mapcar 'make-uri (mapcar #"toString" (set-to-list (#"getAnnotationURIs" o)))))))) (defmethod append-over-import-closure ((kb2 kb2) fn) (loop for o in (set-to-list (#"getOntologies" (kb2-manager kb2))) append (funcall fn o))) (defun to-iri (thing) (cond ((stringp thing) (new 'uk.ac.manchester.cs.owl.IRIImpl (new 'java.net.URI thing))) ((uri-p thing) (new 'uk.ac.manchester.cs.owl.IRIImpl (new 'java.net.URI (uri-full thing)))) ((java-object-p thing) (if (equal (#"getClass" thing) (find-java-class 'uk.ac.manchester.cs.owl.IRIImpl)) thing (new 'uk.ac.manchester.cs.owl.IRIImpl thing))))) (defmethod to-owlapi-class ((kb kb2) class) (#"getOWLClass" (kb2-factory kb) (#"create" 'java.net.uri (uri-full class)))) (defmethod sub-class-of ((kb kb2) sub super) (let ((axiom (#"getOWLSubClassOfAxiom" (kb2-factory kb) (to-owlapi-class kb sub) (to-owlapi-class kb super)))) (#"applyChange" (kb2-manager kb) (new 'AddAxiom (kb2-ontology kb) axiom)) axiom )) (defmethod annotation-uri-annotations ((kb kb2) uri) (remove-duplicates (loop with iri = (to-iri uri) for io in (set-to-list (#"getImportsClosure" (kb2-manager m) (kb2-ontology m))) append (mapcar (lambda(ann) (list (make-uri (#"toString" (#"getIRI" (#"getProperty" ann)))) (let* ((value (#"getValue" ann)) (valuetype (#"getClass" value))) (cond ((equal valuetype (find-java-class 'uk.ac.manchester.cs.owl.OWLRDFTextLiteralImpl)) `(:literal ,(#"getLiteral" value) ,(#"getLang" value))) ((equal valuetype (find-java-class 'uk.ac.manchester.cs.owl.IRIImpl)) (make-uri (#"toString" value ))))))) (set-to-list (#"getAnnotationAssertionAxioms" io iri)))) :test 'equalp)) (defmethod axioms-for-class ((kb kb2) uri) (loop with iri = (to-iri uri) for io in (set-to-list (#"getImportsClosure" (kb2-manager m) (kb2-ontology m))) append (set-to-list (#"getClasseAxioms" io iri)))) (defun load-kb-owlapi2 (ontology) (let* ((manager (#"createOWLOntologyManager" 'org.semanticweb.owl.apibinding.OWLManager)) (ont (#"loadOntologyFromPhysicalURI" manager (new 'net.uri ontology))) (reasoner '(new 'org.mindswap.pellet.owlapi.Reasoner manager))) ; (#"loadOntologies" reasoner (#"getImportsClosure" manager ont)) ; (#"classify" reasoner) ; (values manager ont reasoner) (make-kb2 :loaded-from ontology :ontology ont :manager manager) )) (defun new-ontology(uri) (let* ((manager (#"createOWLOntologyManager" 'org.semanticweb.owl.apibinding.OWLManager)) (ont (#"createOntology" manager (#"create" 'java.net.URI (uri-full uri)))) (factory (#"getOWLDataFactory" manager))) (make-kb2 :loaded-from nil :ontology ont :factory factory :is-changed t :manager manager))) (defmethod write-rdfxml ((kb kb2) &optional path) (setq path (or path "file:///Users/alanr/Desktop/owl2test.owl")) (#"storeOntology" (new 'rdfxmlontologystorer) (kb2-manager kb) (kb2-ontology kb) (#"create" 'java.net.URI path) (new 'rdfxmlontologyformat))) (defmethod annotate ((kb kb2) thing property value) (#"applyChange" (kb2-manager kb) (new 'AddAxiom (kb2-ontology kb) (#"getOWLAnnotationAssertionAxiom" (kb2-factory kb) thing (#"getOWLAnnotationProperty" (kb2-factory kb) (#"create" 'java.net.URI (uri-full property))) (#"getRDFTextLiteral" (kb2-factory kb) value "en"))))) (defvar *labels* nil) ;; annoying thing here is that you have to search all the imports individually for annotations :( ;; tostring includes @en or ^^xsd:string - need to fix (defun get-class-labels (reasoner ont) (let ((table (make-hash-table :test 'equal)) (classes (set-to-list (#"getClasses" reasoner))) (label (#"getURI" (get-java-field 'org.semanticweb.owl.vocab.OWLRDFVocabulary "RDFS_LABEL"))) (preferred-label (#"create" 'java.net.uri "http://purl.obofoundry.org/obo/OBI_0000288"))) (loop for iont in (set-to-list (#"getImportsClosure" manager ont)) do (loop for class in classes for uri = (#"toString" (#"getURI" class)) do (loop for annot in (set-to-list (#"getAnnotations" class iont label)) do (pushnew (#"toString" (#"getAnnotationValueAsConstant" annot)) (gethash uri table) :test 'equal)) (loop for annot in (set-to-list (#"getAnnotations" class iont preferred-label)) do (pushnew (#"toString" (#"getAnnotationValueAsConstant" annot)) (gethash uri table) :test 'equal)))) (setq *labels* table))) ;; print out the subclasses and superclasses of class (defun test-sub-supers (reasoner manager &optional (class "http://purl.obofoundry.org/obo/OBI_0000142")) (let ((clazz (#"getOWLClass" (#"getOWLDataFactory" manager) (#"create" 'java.net.uri class)))) (format t "Supers: ~{~% ~a~}~%Subs:~{~% ~a~}~%" (loop for super in (mapcar #"toString" (mapcar #"getURI" (mapcan 'set-to-list (set-to-list (#"getAncestorClasses" reasoner clazz))))) collect (or (car (gethash super *labels*)) super)) (loop for sub in (mapcar #"toString" (mapcar #"getURI" (mapcan 'set-to-list (set-to-list (#"getDescendantClasses" reasoner clazz))))) collect (or (car (gethash sub *labels*)) sub))))) (defun test (&optional (obiuri "file:///Users/alanr/repos/obi/trunk/src/ontology/branches/obil.owl")) (let ((kb2 (load-kb-owlapi2 obiuri))) ;; ; (get-class-labels reasoner ont) ; (test-sub-supers reasoner manager)) kb2)) ;; take a single ontology, a list of them, or a java collection of them, and create a java collection. (defun to-ontology-collection (ontologies) (if (typep ontologies 'sequence) (loop with set = (new 'hashset) for i below (length ontologies) do (#"add" set (elt ontologies i)) finally (return set)) (if (jinstance-of-p ontologies (find-java-class 'java.util.collection)) ontologies (#"singleton" 'collections ontologies)))) ;; create a functon walk ontology that is like what is done in ;; http://owlapi.svn.sourceforge.net/viewvc/owlapi/owl1_1/trunk/examples/src/main/java/org/coode/owlapi/examples/Example13.java?view=markup ;; Difference 1: we don't specialize the visitor instance, so it is up to the function to check the type of node visited. ;; Difference 2: The class OWLObjectSomeRestriction is now called OWLObjectSomeValuesFrom ;; Argument ontology can be either a single ontology or a java collection of them, or a list of them (define-owl-visitor walk-ontology (function ontology) (:caller (new 'OWLOntologyWalker (to-ontology-collection ontology)) :invoker #"walkStructure") ) (defun test-walk-ontology () (let ((manager (load-kb-owlapi2 "file:///Users/alanr/repos/obi/trunk/src/ontology/branches/obil.owl"))) (walk-ontology (lambda(walker visitor thing &aux (somerestriction (find-java-class 'OWLObjectSomeValuesFrom))) (when (jinstance-of-p thing somerestriction) (format t "~a in ~a~%" thing (#"getCurrentAxiom" walker)))) (#"getImportsClosure" (kb2-manager manager) (kb2-ontology manager)))))