(in-package :cl-user) (defun maybe-url-filename (thing) (if (keywordp thing) (uri-of-standard-ontology thing) (if (or (pathnamep thing) (not (or (eql 0 (search "file://" thing)) (eql 0 (search "http://" thing))))) (concatenate 'string "file://" (#"replaceAll" (#"replace" (namestring (truename thing)) "C:" "") "\\\\" "/")) thing))) (defun abstract-to-rdfxml (string) (abstract-to-rdfxml-corrected string)) (defun abstract-to-rdfxml-owlapi (string) (let* ((p (new 'AbstractOWLParser)) (rr (new 'owl_rdf.Renderer)) (rsw (new 'StringWriter)) (o (#"parseOntology" p (new 'StringReader string) (new 'java.net.URI "http://localhost/foo")))) (#"renderOntology" rr o rsw) (#"replaceAll" (#"toString" rsw) "rdf\\\.resource" "rdf:resource"))) (defun abstract-to-tptp-owlapi (string) (let* ((p (new 'AbstractOWLParser)) (rr (new 'tptp.Renderer)) (rsw (new 'StringWriter)) (o (#"parseOntology" p (new 'StringReader string) (new 'java.net.URI "http://localhost/foo")))) (#"renderOntology" rr o rsw) (#"toString" rsw))) (defun abstract-to-rdfxml-corrected (string) (let* ((p (new 'org.semanticweb.owl.io.abstract_syntax.AbstractOWLParser)) (rr (new 'CorrectedRDFRenderer)) (rsw (new 'StringWriter)) (o (#"parseOntology" p (new 'StringReader string) (new 'java.net.URI "http://localhost/foo")))) (#"renderOntology" rr o rsw) (let ((rdf (#0"toString" rsw))) (setq rdf (#0"replaceAll" rdf "" "")) ;; compensate for expanding backslashes in value function in lisp syntax. (setq rdf (#0"replaceAll" rdf "\\\\\\\\" "\\\\")) (replace-reused-nodes ;; work around structure sharing bug. (#0"replaceAll" (#0"replaceAll" rdf "rdf:(ID|resource|about)=\"http://blank/(\\S+)\"" "rdf:nodeID=\"_$2\"") "" ""))) ;; work around bug in owl:imports rendering )) ;; Ugh: This generates structure sharing. Fix by replacing re-used nodes. ;; (with-ontology structure-sharing () ;; ((class !Tr :partial) ;; (class !G :partial !Tr) ;; (class !D :partial) ;; (class !S :partial !D) ;; (object-property !p) ;; (property-property !p (object-property !T)) ;; (class !p1 :complete (restriction !p (some-values-from !S))) ;; (class !p2 :complete (restriction '(!p !T (some-values-from !G)) (some-values-from !S))) ;; ) ;; (pprint (multiple-value-list (get-owl-species (kb structure-sharing)))) ;; ) (defvar *shared-structure-workaround* nil) ;; (defun replace-reused-nodes (string) ;; (if *shared-structure-workaround* ;; (let ((description-nodes (remove-duplicates ;; (mapcar 'car (all-matches string "" 1)) ;; :test 'equal))) ;; (loop for nodeid in description-nodes ;; for original = (caar (all-matches string (format nil "(?s)(\\n\\s*)<(\\S*) rdf:nodeID=\"~a\".*?\\1" nodeid) 0)) ;; for patched-original = (#"replaceFirst" original (format nil "rdf:nodeID=\"~a\"" nodeid) "") ;; for replaceme = (format nil "" nodeid) ;; do ;; ; (print-db replaceme original) ;; (setq string (#0"replaceAll" string replaceme patched-original))) ;; (#"replaceAll" string "rdf:nodeID=\"(.*?)\"" "")) ;; (#"toString" string))) ;; more efficient (defun replace-reused-nodes (string) (if *shared-structure-workaround* (let ((replacements (make-hash-table :test 'equal)) (replacement-nodes (all-matches string (format nil "(?s)(\\n\\s*)<(\\S*) rdf:nodeID=\"(.*?)\".*?\\1") 0 3))) (loop for (original nodeid) in replacement-nodes for patched-original = (#"replaceFirst" original (format nil "rdf:nodeID=\"~a\"" nodeid) "") for replaceme = (format nil "" nodeid) do (setf (gethash replaceme replacements) patched-original) (sleep .0001)) (format t "~a blank nodes to replace" (setq *r* (cons replacements string))) (loop for key in (loop for description being the hash-keys of replacements collect description) do (sleep .0001) (loop for old = (gethash key replacements) for new = (replace-all old "" (lambda(description) (or (gethash description replacements) (error "didn't find ~a" description))) 0) while (not (equal old new)) do (setf (gethash key replacements) new))) (setq string (replace-all string "" (lambda(description) (gethash description replacements)) 0)) (#"replaceAll" string "rdf:nodeID=\"(.*?)\"" "")) (#"toString" string))) (defun abstract-file-to-rdfxml (from to) (let* ((p (new 'AbstractOWLParser)) (rr (new 'CorrectedRDFRenderer)) (rsw (new 'FileWriter (namestring (truename to)))) (o (#"parseOntology" p (new 'FileReader (namestring (truename from))) (new 'java.net.URI "http://localhost/foo")))) (print 'parsed) (#"renderOntology" rr o rsw) (#"flush" rsw) (#"close" rsw))) ;(print (multiple-value-list (java-room))) ;(verbose-gc nil) ;; (defun abstract-to-rdfxml (string) ;; (let* ((connection (new 'OWLConnectionImpl)) ;; (p (new 'AbstractOWLParser connection)) ;; (rr (new 'owl_rdf.Renderer)) ;; (rsw (new 'StringWriter)) ;; (o (#"parseOntology" p (new 'StringReader string) (new 'java.net.URI "http://localhost/foo")))) ;; (#"renderOntology" rr o rsw) ;; (#"toString" rsw))) (defun rdfxml-to-abstract (string) (let* ((r (new 'abstract_syntax.Renderer )) (p (new 'OWLRDFParser )) (sw (new 'StringWriter )) (conn (new 'OWLConnectionImpl ))) (#"setConnection" p conn) (let ((o (#"parseOntology" p (new 'StringReader string) (new 'java.net.URI "http://localhost/foo")))) (#"renderOntology" r o sw) (#"toString" sw)))) (defparameter *test-abstract-syntax* "Namespace(a=) Ontology ( DatatypeProperty(a:db) DatatypeProperty(a:id) Individual(a:magnetinstance4) EquivalentClasses (intersectionOf (restriction(a:db value(\"entrez\"^^xsd:string)) restriction(a:id value(\"7157\"^^xsd:string))) oneOf(a:magnetinstance4) ) )") (defun load-kb-abstract (ontology &optional uri) (let ((owl-path (and (probe-file ontology) (merge-pathnames (make-pathname :type "owl") ontology)))) (if (and owl-path (probe-file owl-path) (> (file-write-date owl-path) (file-write-date ontology))) (load-kb-jena (maybe-url-filename owl-path)) (let* ((p (new 'AbstractOWLParser)) (rr (new 'CorrectedRDFRenderer)) ;'owl_rdf.Renderer)) (rsw (new 'StringWriter)) (o (#"parseOntology" p (new 'java.net.URI (or uri (maybe-url-filename ontology)))))) (#"renderOntology" rr o rsw) (with-open-file (f owl-path :direction :output :if-exists :supersede :if-does-not-exist :create) (write-string (#"toString" rsw) f)) (load-kb-jena owl-path) )))) (defun abstract-syntax-of-ontology (ontology) (let* ((r (new 'abstract_syntax.Renderer )) (p (new 'OWLRDFParser )) (sw (new 'StringWriter )) (conn (new 'OWLConnectionImpl ))) (when (keywordp ontology) (setq ontology (uri-of-standard-ontology ontology))) (#"setConnection" p conn) (let ((o (#"parseOntology" p (new 'java.net.URI ontology)))) (#"renderOntology" r o sw) ;(print (#"toString" sw)) (#"toString" sw)))) (defmethod as-turtle ((kb kb)) (let ((sw (new 'stringwriter))) (#"write" (kb-jena-model kb) sw "TURTLE") (#"toString" sw))) ;; turtle : (let ((sw (new 'stringwriter))) (#"write" (kb-jena-model (kb foo)) sw "TURTLE") (#"toString" sw)) ;; http://jena.sourceforge.net/IO/iohowto.html