Browse files

support for adding properties to RDFS schemas

  • Loading branch information...
1 parent f36768f commit c7c59ae28d784e694f2d0fc613757be97dcc7124 @antoniogarrote committed Jun 17, 2010
Showing with 27 additions and 14 deletions.
  1. +21 −14 src/plaza/rdf/schemas.clj
  2. +6 −0 test/plaza/rdf/schemas_test.clj
@@ -15,6 +15,7 @@
(defprotocol OntologyModel
"Functions that can be applied to a RDF ontology schema"
(type-uri [this] "Returns the URI of this model")
+ (add-property [this alias uri range] "Adds a new property to the model")
(to-pattern [this props] [this subject props] "Builds a pattern suitable to look for instances of this type. A list of properties can be passed optionally")
(to-map [this triples] "Transforms a RDF triple set into a map of properties using the provided keys")
(property-uri [this alias] "Returns the URI for the alias of a property")
@@ -43,33 +44,39 @@
;; Types
(deftype RDFSModel [this-uri properties ranges] OntologyModel
(type-uri [this] this-uri)
- (toString [this] (str this-uri " " properties))
+ (add-property [this alias uri range] (let [prop-val (if (coll? uri) (apply rdf-resource uri) (rdf-resource uri))
+ range-val (if (supported-datatype? range)
+ {:kind :datatype :range (rdf-resource (datatype-uri range))}
+ {:kind :resource :range (if (coll? range) (apply rdf-resource range) range)})]
+ (dosync (alter properties (fn [old-props] (assoc old-props alias prop-val)))
+ (alter ranges (fn [old-ranges] (assoc old-ranges alias range-val))))))
+ (toString [this] (str this-uri " " @properties))
(to-pattern [this subject props] (let [subj (if (instance? plaza.rdf.core.RDFResource subject) subject (if (coll? subject) (apply rdf-resource subject) (rdf-resource subject) ))]
- (build-pattern-for-model this-uri subj props properties)))
- (to-pattern [this props] (build-pattern-for-model this-uri ?s props properties))
+ (build-pattern-for-model this-uri subj props @properties)))
+ (to-pattern [this props] (build-pattern-for-model this-uri ?s props @properties))
(to-map [this triples-or-vector] (let [triples (if (:triples (meta triples-or-vector)) triples-or-vector (make-triples triples-or-vector))]
- (reduce (fn [ac it] (let [prop (str (resource-id (it properties)))
+ (reduce (fn [ac it] (let [prop (str (resource-id (it @properties)))
val (find-property prop triples)]
(if (nil? val) ac (assoc ac it (nth val 2)))))
- (keys properties))))
- (property-uri [this alias] (alias properties))
- (property-alias [this uri] (first (filter #(= (str (get properties %1)) (str uri)) (keys properties))))
- (parse-prop-value [this alias val] (let [{kind :kind range :range} (get ranges alias)]
+ (keys @properties))))
+ (property-uri [this alias] (alias @properties))
+ (property-alias [this uri] (first (filter #(= (str (get @properties %1)) (str uri)) (keys @properties))))
+ (parse-prop-value [this alias val] (let [{kind :kind range :range} (get @ranges alias)]
(if (= kind :resource) (rdf-resource val)
(.parse (find-jena-datatype (str range)) val))))
- (prop-value-to-triple-value [this alias val] (let [{kind :kind range :range} (get ranges alias)]
+ (prop-value-to-triple-value [this alias val] (let [{kind :kind range :range} (get @ranges alias)]
(if (= kind :resource) (rdf-resource val)
(let [jena-type (find-jena-datatype (str range))]
(parse-literal-lexical (str "\"" val "\"^^<" (.getURI jena-type) ">"))))))
(to-rdf-triples [this] (let [subject (rdf-resource (type-uri this))]
- (reduce (fn [ts k] (let [prop (rdf-resource (get properties k))
- range (:range (get ranges k))
+ (reduce (fn [ts k] (let [prop (rdf-resource (get @properties k))
+ range (:range (get @ranges k))
tsp (conj ts [prop (rdf-resource rdfs:range) (rdf-resource range)])]
(conj tsp [prop (rdf-resource rdfs:domain) subject])))
[[subject (rdf-resource rdf:type) (rdf-resource rdfs:Class)]]
- (keys properties))))
- (aliases [this] (keys properties)))
+ (keys @properties))))
+ (aliases [this] (keys @properties)))
;; Type constructor
@@ -85,7 +92,7 @@
{:kind :resource :range (if (coll? range) (apply rdf-resource range) range)})]
[(assoc ac-props it prop-val)
(assoc ac-ranges it range-val)])) [{} {}] (keys props-map-pre))]
- (plaza.rdf.schemas.RDFSModel. typeuri (first maps) (second maps)))))
+ (plaza.rdf.schemas.RDFSModel. typeuri (ref (first maps)) (ref (second maps))))))
;; RDFS schema
@@ -14,6 +14,12 @@
(deftest test-props
(is (= "http://something/Good" (str (type-uri *test-model*)))))
+(deftest test-add-prop
+ (do (add-property *test-model* :wadus "" :float)
+ (add-property *test-model* :foo "" "")
+ (= :foo (property-alias *test-model* ""))
+ (= :wadus (property-alias *test-model* ""))))
(deftest test-to-map
(let [m (to-map *test-model* [[:test ["" :name] "name"] [:test ["" :price] (d 120)] [:test :number (d 10)]])]
(is (= m {:name (rdf-resource "name") :price (d 120) :number (d 10)}))))

0 comments on commit c7c59ae

Please sign in to comment.