Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial support for RDFS models

  • Loading branch information...
commit f629de2255ffa41c10cd0e0b40859354a07041df 1 parent bde795e
@antoniogarrote authored
View
3  chat.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+java -jar plaza-standalone.jar examples chat $1 -ts-host 172.21.1.3 -ts-port 7555 -host 172.21.1.3
View
9 src/plaza/examples/chat.clj
@@ -32,10 +32,11 @@
(defn- update-users
"Updates the list of connected users"
([triples]
- (let [author (literal-value (o (first (filter (tc (predicate? (uri? nick))) (first triples)))))]
- (println (str "*** agent user-login - got triples: " triples))
- (send set-userlist-text (fn [ta] (let [old-text (.getText ta)]
- (.setText ta (str old-text "\r\n " author)) ta))))))
+ (doseq [ts triples]
+ (let [author (literal-value (o (first (filter (tc (predicate? (uri? nick))) ts))))]
+ (println (str "*** agent user-login - got triples: " author))
+ (send set-userlist-text (fn [ta] (let [old-text (.getText ta)]
+ (.setText ta (str old-text "\r\n " author)) ta)))))))
;; Local triple spaces
View
2  src/plaza/rdf/implementations/jena.clj
@@ -42,7 +42,7 @@
([model query query-string]
; (let [query (if (string? query-or-string) (sparql-to-query query-or-string) query-or-string)
; query-string (if (string? query-or-string) query-or-string (str (build-query *sparql-framework* query)))]
- (println (str "QUERYING JENA WITH: " query-string))
+; (println (str "QUERYING JENA WITH: " query-string))
(model-critical-read model
(let [qexec (QueryExecutionFactory/create query-string (to-java model))
; (let [qexec (QueryExecutionFactory/create (build-query query) @model)
View
57 src/plaza/rdf/models.clj
@@ -0,0 +1,57 @@
+;; @author Antonio Garote
+;; @email antoniogarrote@gmail.com
+;; @date 26.05.2010
+
+(ns plaza.rdf.models
+ (:use (plaza.rdf core sparql predicates)
+ (plaza utils)))
+
+;; auxiliary functions
+(defn find-property [prop triples]
+ (first (filter (tc (predicate? (uri? prop))) triples)))
+
+;; Protocols
+(defprotocol OntologyModel
+ "Functions that can be applied to a RDF ontology schema"
+ (type-uri [model] "Returns the URI of this model")
+ (to-pattern [model props] [model subject props] "Builds a pattern suitable to look for instances of this type. A list of properties can be passed optionally")
+ (to-map [model triples] "Transforms a RDF triple set into a map of properties using the provided keys"))
+
+(defn- build-pattern-for-model
+ ([type-uri subj props properties]
+ (let [opt-props-to-add (vec (clojure.set/difference (set (keys properties)) (set props)))
+ props-to-add (if (empty? props) (keys properties) props)
+ mandatory-pattern (reduce (fn [ac it]
+ (when (nil? (it properties)) (throw (Exception. (str "Unknown property: " it " for type " type-uri " " properties))))
+ (conj ac [subj (it properties) (keyword (str "?" (keyword-to-string it)))]))
+ [(opt [subj rdf:type (resource-id type-uri)])]
+ props-to-add)
+ optional-pattern (reduce (fn [ac it]
+ (when (nil? (it properties)) (throw (Exception. (str "Unknown property: " it " for type " type-uri " " properties))))
+ (conj ac (opt [subj (it properties) (keyword (str "?" (keyword-to-string it)))])))
+ []
+ opt-props-to-add)]
+ (make-pattern (concat mandatory-pattern optional-pattern)))))
+
+;; Types
+(deftype RDFSModel [type-uri properties] OntologyModel
+ (type-uri [this] type-uri)
+ (toString [this] (str type-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 type-uri subj props properties)))
+ (to-pattern [this props] (build-pattern-for-model type-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)))
+ val (find-property prop triples)]
+ (println (str "found: " val " for prop " prop))
+ (if (nil? val) ac (assoc ac it (nth val 2)))))
+ {}
+ (keys properties)))))
+
+;; Type constructor
+(defn def-rdfs-model
+ ( [type-uri-pre & properties]
+ (let [type-uri (apply rdf-resource type-uri-pre)
+ props-map-pre (apply hash-map properties)
+ props-map (reduce (fn [ac it] (assoc ac it (if (coll? (it props-map-pre)) (apply rdf-resource (it props-map-pre)) (rdf-resource (it props-map-pre))))) {} (keys props-map-pre))]
+ (plaza.rdf.models.RDFSModel. type-uri props-map))))
View
26 test/plaza/rdf/models_test.clj
@@ -0,0 +1,26 @@
+(ns plaza.rdf.models-test
+ (:use [plaza.rdf core sparql models] :reload-all)
+ (:use [plaza.rdf.implementations jena] :reload-all)
+ (:use [clojure.test]))
+
+(init-jena-framework)
+
+(defonce *test-model* (def-rdfs-model ["http://something/" "Good"] :name "http://test.com/name" :price ["http://test.com/" :price] :number :number))
+
+(deftest test-props
+ (is (= "http://something/Good") (type-uri *test-model*)))
+
+(deftest test-to-map
+ (let [m (to-map *test-model* [[:test ["http://test.com/" :name] "name"] [:test ["http://test.com/" :price] (d 120)] [:test :number (d 10)]])]
+ (is (= m {:name (rdf-resource "name") :price (d 120) :number (d 10)}))))
+
+(deftest test-to-pattern
+ (let [p (to-pattern *test-model* [:name :price])]
+ (is (= 4 (count p)))
+ (is (= 2 (count (filter #(:optional (meta %1)) p))))))
+
+(deftest test-to-pattern-2
+ (let [p (to-pattern *test-model* "http://test.com/Test" [:name :price])]
+ (is (= 4 (count p)))
+ (is (= 2 (count (filter #(:optional (meta %1)) p))))
+ (doseq [[s _p _o] p] (is (= "http://test.com/Test" (resource-id s))))))
View
5 test/plaza/triple_spaces/server/rabbit_test.clj
@@ -5,11 +5,6 @@
(deftest test-default-rabbit-parameters
(is (= "guest" (:username *default-rabbit-parameters*))))
-(deftest test-check-default-values
- (let [optsp (check-default-values {:password "foo"})]
- (is (= "foo" (:password optsp)))
- (is (= "guest" (:username optsp)))))
-
(deftest test-connect
(let [rabbit (connect)]
(is (instance? com.rabbitmq.client.impl.AMQConnection (:connection rabbit)))
View
5 test/plaza/utils_test.clj
@@ -19,3 +19,8 @@
(is (= "hola" (cmd-param-to-keyword "hola")))
(is (= :hola (cmd-param-to-keyword :hola)))
(is (= :hola (cmd-param-to-keyword "-hola"))))
+
+(deftest test-check-default-values
+ (let [optsp (check-default-values {:password "foo"} {:password "bar" :username "guest"})]
+ (is (= "foo" (:password optsp)))
+ (is (= "guest" (:username optsp)))))
Please sign in to comment.
Something went wrong with that request. Please try again.