Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

definition of schemas from RDF models and some utility functions

  • Loading branch information...
commit 8767bc9ab9064572bebb1a9c71efb1e37c6002c8 1 parent 599023f
@antoniogarrote authored
View
26 src/plaza/rdf/implementations/common.clj
@@ -50,9 +50,33 @@
(= "string" (.toLowerCase (keyword-to-string lit))) XSDDatatype/XSDstring
:else (make-custom-type literal)))))
+(defn datatype-symbol
+ "Transforms a XMLSchema datatype URI into a symbol representing the type"
+ ([literal]
+ (let [lit (let [literal-str (keyword-to-string literal)]
+ (if (and (.startsWith literal-str "http://") (not (= -1 (.indexOf literal-str "#"))))
+ (aget (.split literal-str "#") 1)
+ literal))]
+ (condp = lit
+ "xmlliteral" (keyword lit)
+ "literal" (keyword lit)
+ "anyuri" (keyword lit)
+ "boolean" (keyword lit)
+ "byte" (keyword lit)
+ "date" (keyword lit)
+ "datetime" (keyword lit)
+ "decimal" (keyword lit)
+ "double" (keyword lit)
+ "float" (keyword lit)
+ "int" (keyword lit)
+ "integer" (keyword lit)
+ "long" (keyword lit)
+ "string" (keyword lit)
+ nil))))
+
(defn supported-datatype?
- "Returns true if the datatype sym is supported"
+ "Returns true if the datatype sym or URI string is supported"
([sym]
(try (do (find-jena-datatype sym) true)
(catch Exception ex false))))
View
23 src/plaza/rdf/schemas.clj
@@ -3,7 +3,8 @@
;; @date 26.05.2010
(ns plaza.rdf.schemas
- (:use (plaza.rdf core sparql predicates)
+ (:use (plaza utils)
+ (plaza.rdf core sparql predicates)
(plaza.rdf.implementations common)
[clojure.contrib.logging :only [log]]
(plaza utils)))
@@ -126,6 +127,26 @@
([uri props ranges]
(plaza.rdf.schemas.RDFSModel. uri props ranges)))
+(defn- parse-rdf-schema-from-model
+ ([model resource]
+ (let [properties (flatten-1 (model-pattern-apply model [[?s rdfs:domain resource] [?s rdfs:range ?o]]))
+ prop-ranges (reduce (fn [acum [s p o]] (if (= (str p) rdfs:range)
+ (let [prop-val (if (supported-datatype? (str o)) (datatype-symbol (str o)) (str o))]
+ (conj acum {:uri (str s) :range prop-val} ))
+ acum))
+ [] properties)]
+ (reduce (fn [schema prop-map]
+ (let [prop-alias (keyword (extract-local-part-uri (:uri prop-map)))]
+ (add-property schema prop-alias (:uri prop-map) (:range prop-map))))
+ (make-rdfs-schema (str resource)) prop-ranges))))
+
+(defn parse-rdfs-schemas-from-model
+ "Builds RDFS definition of resources from the set of triples in a RDF model"
+ ([model]
+ (let [resources-triples (flatten-1 (model-pattern-apply model [[?s rdf:type rdfs:Class]]))
+ resources (map (fn [[resource _p _o]] (str resource)) resources-triples)]
+ (map (fn [resource] (parse-rdf-schema-from-model model resource)) resources))))
+
;; RDFS schema
(declare-schemas-to-load
View
1  src/plaza/rdf/vocabularies/plaza.clj
@@ -13,6 +13,7 @@
(defonce plz:RestResource "http://plaza.org/vocabularies/restResource")
(defonce plz:restResourceId "http://plaza.org/vocabularies/restResourceId")
(defonce plz:validationError "http://plaza.org/vocabularies/validationError")
+(defonce plz:label "http://plaza.org/vocabularies/label")
(register-rdf-ns :plz plz)
View
7 src/plaza/utils.clj
@@ -126,3 +126,10 @@
(.print pw (. e getMessage))
(. e (printStackTrace pw))
(str sw))))
+
+(defn extract-local-part-uri
+ ([uri]
+ (let [uri-str (str uri)]
+ (if (not (= (.indexOf uri-str "#") -1))
+ (aget (.split uri-str "#") 1)
+ (first (reverse (vec (.split uri-str "/"))))))))
View
6 test/plaza/rdf/implementations/jena_test.clj
@@ -1,6 +1,6 @@
(ns plaza.rdf.implementations.jena-test
(:use [plaza.rdf core] :reload-all)
- (:use [plaza.rdf.implementations jena] :reload-all)
+ (:use [plaza.rdf.implementations jena common] :reload-all)
(:use [clojure.test]))
;; we'll test with jena
@@ -84,3 +84,7 @@
(with-model *m* (model-add-triples [[:a :b :c]]))
(is (= 1 (count (query-triples *m* query-str))))
(is (= 3 (count (ffirst (query-triples *m* query-str)))))))
+
+(deftest test-datatype-symbol
+ (is (= :string (datatype-symbol "http://www.w3.org/2001/XMLSchema#string")))
+ (is (= nil (datatype-symbol "http://www.w3.org/anything/not/a/atatype"))))
View
7 test/plaza/rdf/schemas_test.clj
@@ -54,3 +54,10 @@
(deftest test-schema-to-triples
(let [ts (to-rdf-triples foaf:Agent-schema)]
(is (= 37 (count ts)))))
+
+(deftest test-parse-from-rdf
+ (let [ts (to-rdf-triples foaf:Agent-schema)
+ *m* (build-model)
+ _tmp (with-model *m* (model-add-triples ts))
+ parsed (parse-rdfs-schemas-from-model *m*)]
+ (is (= (sort (aliases foaf:Agent-schema)) (sort (aliases (first parsed)))))))
Please sign in to comment.
Something went wrong with that request. Please try again.