├── kr-core ├── src │ ├── main │ │ └── clojure │ │ │ └── edu │ │ │ └── ucdenver │ │ │ └── ccp │ │ │ ├── kr.clj │ │ │ ├── kr │ │ │ ├── repl_utils.clj │ │ │ ├── variable.clj │ │ │ ├── kb.clj │ │ │ ├── reify.clj │ │ │ ├── unify.clj │ │ │ ├── clj_ify.clj │ │ │ ├── assertion.clj │ │ │ ├── rule_index.clj │ │ │ ├── rule.clj │ │ │ └── forward_rule.clj │ │ │ └── utils.clj │ └── test │ │ └── clojure │ │ └── edu │ │ └── ucdenver │ │ └── ccp │ │ └── test │ │ └── kr │ │ ├── test_sparql_construct.clj │ │ ├── test_kb.clj │ │ ├── test_sparql_property_paths.clj │ │ └── test_sparql.clj └── pom.xml ├── kr-sesame ├── kr-sesame-core │ ├── src │ │ ├── test │ │ │ └── clojure │ │ │ │ └── edu │ │ │ │ └── ucdenver │ │ │ │ └── ccp │ │ │ │ └── test │ │ │ │ └── kr │ │ │ │ └── sesame │ │ │ │ ├── test_rdf.clj │ │ │ │ ├── test_sparql.clj │ │ │ │ ├── test_forward_rule.clj │ │ │ │ ├── test_sparql_construct.clj │ │ │ │ ├── test_sparql_property_paths.clj │ │ │ │ └── test_kb.clj │ │ └── main │ │ │ └── clojure │ │ │ └── edu │ │ │ └── ucdenver │ │ │ └── ccp │ │ │ └── kr │ │ │ └── sesame │ │ │ ├── writer_kb.clj │ │ │ ├── sparql.clj │ │ │ ├── rdf.clj │ │ │ └── kb.clj │ └── pom.xml └── pom.xml ├── kr-jena ├── kr-jena-core │ ├── src │ │ ├── test │ │ │ └── clojure │ │ │ │ └── edu │ │ │ │ └── ucdenver │ │ │ │ └── ccp │ │ │ │ └── test │ │ │ │ └── kr │ │ │ │ └── jena │ │ │ │ ├── test_rdf.clj │ │ │ │ ├── test_sparql.clj │ │ │ │ ├── test_sparql_construct.clj │ │ │ │ ├── test_kb.clj │ │ │ │ └── test_forward_rule.clj │ │ └── main │ │ │ └── clojure │ │ │ └── edu │ │ │ └── ucdenver │ │ │ └── ccp │ │ │ └── kr │ │ │ └── jena │ │ │ ├── sparql.clj │ │ │ └── kb.clj │ └── pom.xml └── pom.xml ├── kr-backend └── pom.xml ├── kr-examples ├── jena-mem-kb │ ├── pom.xml │ └── src │ │ └── main │ │ └── clojure │ │ └── edu │ │ └── ucdenver │ │ └── ccp │ │ └── kr │ │ └── examples │ │ └── jena_mem_kb.clj ├── sesame-remote-kb │ ├── pom.xml │ └── src │ │ └── main │ │ └── clojure │ │ └── edu │ │ └── ucdenver │ │ └── ccp │ │ └── kr │ │ └── examples │ │ └── sesame_remote_kb.clj └── sesame-mem-kb │ ├── pom.xml │ └── src │ └── main │ └── clojure │ └── edu │ └── ucdenver │ └── ccp │ └── kr │ └── examples │ └── sesame_mem_kb.clj ├── README.md ├── pom.xml └── EPLv1.0.html /kr-core/src/main/clojure/edu/ucdenver/ccp/kr.clj: -------------------------------------------------------------------------------- 1 | 2 | (ns edu.ucdenver.ccp.kr 3 | (:use edu.ucdenver.ccp.kr.variable 4 | edu.ucdenver.ccp.kr.unify)) 5 | -------------------------------------------------------------------------------- /kr-sesame/kr-sesame-core/src/test/clojure/edu/ucdenver/ccp/test/kr/sesame/test_rdf.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.sesame.test-rdf 2 | (use clojure.test 3 | [edu.ucdenver.ccp.test.kr.sesame.test-kb :exclude [test-ns-hook]]) 4 | (require edu.ucdenver.ccp.test.kr.test-kb 5 | edu.ucdenver.ccp.test.kr.test-rdf)) 6 | 7 | ;;; -------------------------------------------------------- 8 | ;;; 9 | ;;; -------------------------------------------------------- 10 | 11 | (defn test-ns-hook [] 12 | (binding [edu.ucdenver.ccp.test.kr.test-kb/*kb-creator-fn* 13 | sesame-memory-test-kb] 14 | (run-tests 'edu.ucdenver.ccp.test.kr.test-rdf))) 15 | 16 | ;;; -------------------------------------------------------- 17 | ;;; END 18 | ;;; -------------------------------------------------------- 19 | -------------------------------------------------------------------------------- /kr-sesame/kr-sesame-core/src/test/clojure/edu/ucdenver/ccp/test/kr/sesame/test_sparql.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.sesame.test-sparql 2 | (use clojure.test 3 | [edu.ucdenver.ccp.test.kr.sesame.test-kb :exclude [test-ns-hook]]) 4 | (require edu.ucdenver.ccp.test.kr.test-kb 5 | edu.ucdenver.ccp.test.kr.test-sparql)) 6 | 7 | ;;; -------------------------------------------------------- 8 | ;;; 9 | ;;; -------------------------------------------------------- 10 | 11 | (defn test-ns-hook [] 12 | (binding [edu.ucdenver.ccp.test.kr.test-kb/*kb-creator-fn* 13 | sesame-memory-test-kb] 14 | (run-tests 'edu.ucdenver.ccp.test.kr.test-sparql))) 15 | 16 | ;;; -------------------------------------------------------- 17 | ;;; END 18 | ;;; -------------------------------------------------------- 19 | -------------------------------------------------------------------------------- /kr-sesame/kr-sesame-core/src/test/clojure/edu/ucdenver/ccp/test/kr/sesame/test_forward_rule.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.sesame.test-forward-rule 2 | (use clojure.test 3 | [edu.ucdenver.ccp.test.kr.sesame.test-kb :exclude [test-ns-hook]]) 4 | (require edu.ucdenver.ccp.test.kr.test-kb 5 | edu.ucdenver.ccp.test.kr.test-forward-rule)) 6 | 7 | ;;; -------------------------------------------------------- 8 | ;;; 9 | ;;; -------------------------------------------------------- 10 | 11 | (defn test-ns-hook [] 12 | (binding [edu.ucdenver.ccp.test.kr.test-kb/*kb-creator-fn* 13 | sesame-memory-test-kb] 14 | (run-tests 'edu.ucdenver.ccp.test.kr.test-forward-rule))) 15 | 16 | ;;; -------------------------------------------------------- 17 | ;;; END 18 | ;;; -------------------------------------------------------- 19 | -------------------------------------------------------------------------------- /kr-sesame/kr-sesame-core/src/test/clojure/edu/ucdenver/ccp/test/kr/sesame/test_sparql_construct.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.sesame.test-sparql-construct 2 | (use clojure.test 3 | [edu.ucdenver.ccp.test.kr.sesame.test-kb :exclude [test-ns-hook]]) 4 | (require edu.ucdenver.ccp.test.kr.test-kb 5 | edu.ucdenver.ccp.test.kr.test-sparql-construct)) 6 | 7 | ;;; -------------------------------------------------------- 8 | ;;; 9 | ;;; -------------------------------------------------------- 10 | 11 | (defn test-ns-hook [] 12 | (binding [edu.ucdenver.ccp.test.kr.test-kb/*kb-creator-fn* 13 | sesame-memory-test-kb] 14 | (run-tests 'edu.ucdenver.ccp.test.kr.test-sparql-construct))) 15 | 16 | ;;; -------------------------------------------------------- 17 | ;;; END 18 | ;;; -------------------------------------------------------- 19 | -------------------------------------------------------------------------------- /kr-sesame/kr-sesame-core/src/test/clojure/edu/ucdenver/ccp/test/kr/sesame/test_sparql_property_paths.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.sesame.test-sparql-property-paths 2 | (use clojure.test 3 | [edu.ucdenver.ccp.test.kr.sesame.test-kb :exclude [test-ns-hook]]) 4 | (require edu.ucdenver.ccp.test.kr.test-kb 5 | edu.ucdenver.ccp.test.kr.test-sparql-property-paths)) 6 | 7 | ;;; -------------------------------------------------------- 8 | ;;; 9 | ;;; -------------------------------------------------------- 10 | 11 | (defn test-ns-hook [] 12 | (binding [edu.ucdenver.ccp.test.kr.test-kb/*kb-creator-fn* 13 | sesame-memory-test-kb] 14 | (run-tests 'edu.ucdenver.ccp.test.kr.test-sparql-property-paths))) 15 | 16 | ;;; -------------------------------------------------------- 17 | ;;; END 18 | ;;; -------------------------------------------------------- 19 | -------------------------------------------------------------------------------- /kr-jena/kr-jena-core/src/test/clojure/edu/ucdenver/ccp/test/kr/jena/test_rdf.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.jena.test-rdf 2 | (use clojure.test 3 | [edu.ucdenver.ccp.test.kr.jena.test-kb :exclude [test-ns-hook]]) 4 | (require edu.ucdenver.ccp.test.kr.test-kb 5 | edu.ucdenver.ccp.test.kr.test-rdf)) 6 | 7 | ;;; -------------------------------------------------------- 8 | ;;; 9 | ;;; -------------------------------------------------------- 10 | 11 | (defn test-ns-hook [] 12 | (binding [edu.ucdenver.ccp.test.kr.test-kb/*kb-creator-fn* 13 | jena-memory-test-kb 14 | edu.ucdenver.ccp.kr.jena.rdf/*force-add-named-to-default* 15 | true] 16 | (run-tests 'edu.ucdenver.ccp.test.kr.test-rdf))) 17 | 18 | ;;; -------------------------------------------------------- 19 | ;;; END 20 | ;;; -------------------------------------------------------- 21 | -------------------------------------------------------------------------------- /kr-jena/kr-jena-core/src/test/clojure/edu/ucdenver/ccp/test/kr/jena/test_sparql.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.jena.test-sparql 2 | (use clojure.test 3 | [edu.ucdenver.ccp.test.kr.jena.test-kb :exclude [test-ns-hook]]) 4 | (require edu.ucdenver.ccp.test.kr.test-kb 5 | edu.ucdenver.ccp.test.kr.test-sparql)) 6 | 7 | ;;; -------------------------------------------------------- 8 | ;;; 9 | ;;; -------------------------------------------------------- 10 | 11 | (defn test-ns-hook [] 12 | (binding [edu.ucdenver.ccp.test.kr.test-kb/*kb-creator-fn* 13 | jena-memory-test-kb 14 | edu.ucdenver.ccp.kr.jena.rdf/*force-add-named-to-default* 15 | true] 16 | (run-tests 'edu.ucdenver.ccp.test.kr.test-sparql) 17 | )) 18 | 19 | ;;; -------------------------------------------------------- 20 | ;;; END 21 | ;;; -------------------------------------------------------- 22 | -------------------------------------------------------------------------------- /kr-jena/kr-jena-core/src/test/clojure/edu/ucdenver/ccp/test/kr/jena/test_sparql_construct.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.jena.test-sparql-construct 2 | (use clojure.test 3 | [edu.ucdenver.ccp.test.kr.jena.test-kb :exclude [test-ns-hook]]) 4 | (require edu.ucdenver.ccp.test.kr.test-kb 5 | edu.ucdenver.ccp.test.kr.test-sparql-construct)) 6 | 7 | ;;; -------------------------------------------------------- 8 | ;;; 9 | ;;; -------------------------------------------------------- 10 | 11 | (defn test-ns-hook [] 12 | (binding [edu.ucdenver.ccp.test.kr.test-kb/*kb-creator-fn* 13 | jena-memory-test-kb 14 | edu.ucdenver.ccp.kr.jena.rdf/*force-add-named-to-default* 15 | true] 16 | (run-tests 'edu.ucdenver.ccp.test.kr.test-sparql-construct))) 17 | 18 | ;;; -------------------------------------------------------- 19 | ;;; END 20 | ;;; -------------------------------------------------------- 21 | -------------------------------------------------------------------------------- /kr-sesame/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | 7 | 8 | edu.ucdenver.ccp 9 | kr 10 | 1.4.20-SNAPSHOT 11 | 12 | 13 | kr-sesame 14 | pom 15 | 16 | ${project.artifactId} 17 | KR Sesame bindings. 18 | 19 | 20 | kr-sesame-core 21 | 22 | 23 | 24 | 25 | 26 | ${project.groupId} 27 | kr-core 28 | ${project.version} 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /kr-backend/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | 7 | 8 | edu.ucdenver.ccp 9 | kr 10 | 1.4.20-SNAPSHOT 11 | 12 | 13 | kr-backend 14 | pom 15 | 16 | ${project.artifactId} 17 | KR backend system definitions. 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | ${project.groupId} 27 | kr-core 28 | ${project.version} 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /kr-jena/kr-jena-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | 7 | 8 | edu.ucdenver.ccp 9 | kr-jena 10 | 1.4.20-SNAPSHOT 11 | 12 | 13 | kr-jena-core 14 | jar 15 | 16 | ${project.artifactId} 17 | KR Jena bindings. 18 | 19 | 20 | 21 | 22 | 23 | ${project.groupId} 24 | kr-core 25 | ${project.version} 26 | test-jar 27 | test 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /kr-jena/kr-jena-core/src/test/clojure/edu/ucdenver/ccp/test/kr/jena/test_kb.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.jena.test-kb 2 | (use clojure.test 3 | edu.ucdenver.ccp.kr.kb 4 | edu.ucdenver.ccp.kr.jena.kb) 5 | (require edu.ucdenver.ccp.test.kr.test-kb 6 | edu.ucdenver.ccp.kr.jena.rdf)) 7 | 8 | ;;; -------------------------------------------------------- 9 | ;;; 10 | ;;; -------------------------------------------------------- 11 | 12 | (defn jena-memory-test-kb [] 13 | ;;(edu.ucdenver.ccp.kr.kb/open 14 | (kb :jena-mem)) 15 | ;;(edu.ucdenver.ccp.kr.jena.kb/new-jena-model-kb));) 16 | 17 | (defn test-ns-hook [] 18 | (binding [edu.ucdenver.ccp.test.kr.test-kb/*kb-creator-fn* 19 | jena-memory-test-kb 20 | edu.ucdenver.ccp.kr.jena.rdf/*force-add-named-to-default* 21 | true] 22 | (run-tests 'edu.ucdenver.ccp.test.kr.test-kb))) 23 | 24 | 25 | 26 | ;;; -------------------------------------------------------- 27 | ;;; END 28 | ;;; -------------------------------------------------------- 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /kr-jena/kr-jena-core/src/test/clojure/edu/ucdenver/ccp/test/kr/jena/test_forward_rule.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.jena.test-forward-rule 2 | (use clojure.test 3 | [edu.ucdenver.ccp.test.kr.jena.test-kb :exclude [test-ns-hook]]) 4 | (require edu.ucdenver.ccp.test.kr.test-kb 5 | edu.ucdenver.ccp.test.kr.test-forward-rule)) 6 | 7 | ;;; -------------------------------------------------------- 8 | ;;; 9 | ;;; -------------------------------------------------------- 10 | 11 | (defn test-ns-hook [] 12 | (binding [edu.ucdenver.ccp.test.kr.test-kb/*kb-creator-fn* 13 | jena-memory-test-kb 14 | edu.ucdenver.ccp.kr.jena.rdf/*force-add-named-to-default* 15 | true] 16 | ;;these currently fail because there is a reader and writer 17 | ;; going in the same model causing a failure... 18 | ;; modification during iteration 19 | ;;(run-tests 'edu.ucdenver.ccp.test.kr.test-forward-rule) 20 | )) 21 | 22 | ;;; -------------------------------------------------------- 23 | ;;; END 24 | ;;; -------------------------------------------------------- 25 | -------------------------------------------------------------------------------- /kr-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 9 | 10 | 11 | edu.ucdenver.ccp 12 | kr 13 | 1.4.20-SNAPSHOT 14 | 15 | 16 | kr-core 17 | jar 18 | 19 | ${project.artifactId} 20 | KR core API and tools. 21 | 22 | 23 | 24 | 25 | org.clojure 26 | java.classpath 27 | 28 | 29 | 30 | com.stuartsierra 31 | dependency 32 | 33 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /kr-core/src/main/clojure/edu/ucdenver/ccp/kr/repl_utils.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.repl-utils 2 | (use edu.ucdenver.ccp.kr.kb 3 | edu.ucdenver.ccp.kr.rdf 4 | edu.ucdenver.ccp.kr.sparql 5 | edu.ucdenver.ccp.kr.variable 6 | edu.ucdenver.ccp.kr.unify)) 7 | 8 | ;;; -------------------------------------------------------- 9 | ;;; show-sym 10 | ;;; -------------------------------------------------------- 11 | 12 | (def ^:dynamic *show-sym-limits* [10 10 10]) 13 | 14 | (defn show-take-lim [l limit] 15 | (let [lim (take limit l)] 16 | (dorun (map prn lim)) 17 | (println "Showing: " (min (count lim) limit) " of " (count l)))) 18 | 19 | ;; this could be done with the rdfKB API instead of the sparqlKB API 20 | (defn show-sym [s] 21 | (let [[s-lim p-lim o-lim] *show-sym-limits*] 22 | (println "subject") 23 | (show-take-lim (query-template '(?p ?o) `((~s ?p ?o))) s-lim) 24 | (println "\npredicate") 25 | (show-take-lim (query-template '(?s ?o) `((?s ~s ?o))) p-lim) 26 | (println "\nobject") 27 | (show-take-lim (query-template '(?s ?p) `((?s ?p ~s))) o-lim))) 28 | 29 | 30 | ;;; -------------------------------------------------------- 31 | ;;; END 32 | ;;; -------------------------------------------------------- 33 | 34 | 35 | -------------------------------------------------------------------------------- /kr-sesame/kr-sesame-core/src/test/clojure/edu/ucdenver/ccp/test/kr/sesame/test_kb.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.sesame.test-kb 2 | (use clojure.test 3 | edu.ucdenver.ccp.kr.kb 4 | edu.ucdenver.ccp.kr.sesame.kb 5 | edu.ucdenver.ccp.kr.sesame.writer-kb) 6 | (require edu.ucdenver.ccp.test.kr.test-kb) 7 | (import java.io.ByteArrayOutputStream)) 8 | 9 | ;;; -------------------------------------------------------- 10 | ;;; 11 | ;;; -------------------------------------------------------- 12 | 13 | (defn sesame-memory-test-kb [] 14 | (edu.ucdenver.ccp.kr.kb/open 15 | (edu.ucdenver.ccp.kr.sesame.kb/new-sesame-memory-kb))) 16 | 17 | (defn test-ns-hook [] 18 | (binding [edu.ucdenver.ccp.test.kr.test-kb/*kb-creator-fn* 19 | sesame-memory-test-kb] 20 | (run-tests 'edu.ucdenver.ccp.test.kr.test-kb))) 21 | 22 | 23 | ;;run the kb tests for the writer-kb too 24 | (defn sesame-writer-test-kb [] 25 | (new-sesame-writer-kb (ByteArrayOutputStream.))) 26 | 27 | (defn test-ns-hook [] 28 | (binding [edu.ucdenver.ccp.test.kr.test-kb/*kb-creator-fn* 29 | sesame-writer-test-kb] 30 | (run-tests 'edu.ucdenver.ccp.test.kr.test-kb))) 31 | 32 | ;;; -------------------------------------------------------- 33 | ;;; END 34 | ;;; -------------------------------------------------------- 35 | -------------------------------------------------------------------------------- /kr-jena/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | 7 | 8 | edu.ucdenver.ccp 9 | kr 10 | 1.4.20-SNAPSHOT 11 | 12 | 13 | kr-jena 14 | pom 15 | 16 | ${project.artifactId} 17 | KR Jena bindings. 18 | 19 | 20 | kr-jena-core 21 | 22 | 23 | 24 | 25 | 26 | ${project.groupId} 27 | kr-core 28 | ${project.version} 29 | 30 | 31 | 32 | 33 | org.slf4j 34 | slf4j-log4j12 35 | 36 | 37 | 38 | org.apache.jena 39 | jena-arq 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /kr-core/src/main/clojure/edu/ucdenver/ccp/kr/variable.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.variable 2 | (use edu.ucdenver.ccp.utils)) 3 | 4 | ;;(def variable-prefix \?) 5 | (def variable-ns "?") 6 | 7 | (def temp-variable-prefix "var") 8 | 9 | ;; (defn variable-string? [s] 10 | ;; (= (nth s 0) variable-prefix)) 11 | 12 | 13 | (defn variable? [x] 14 | ;;"Is x a variable (a symbol beginning with '?')?" 15 | "Is x a variable (a symbol in the variable-ns '?/')?" 16 | (and (symbol? x) 17 | (= variable-ns (namespace x)))) 18 | ;;(variable-string? (name x)))) 19 | 20 | 21 | (defn variable [v] 22 | (cond 23 | (string? v) (variable (symbol v)) 24 | (variable? v) v 25 | (symbol? v) (symbol variable-ns (name v)))) 26 | 27 | ;; (symbol? v) (symbol (namespace v) 28 | ;; (str variable-prefix (name v))))) 29 | 30 | (defn temp-variable 31 | ([] (variable (gensym temp-variable-prefix))) 32 | ([prefix] (variable (gensym prefix)))) 33 | 34 | 35 | (defn distinct-elements 36 | ([elem? expr] (distinct-elements elem? expr #'nonempty-seq #'seq)) 37 | ([elem? expr branch? children] 38 | (set (filter elem? (tree-seq branch? children expr))))) 39 | 40 | (defn variables [expr] 41 | (distinct-elements variable? expr)) 42 | ;; ([expr] (list-variables expr #'variable? #'nonempty-seq #'seq)) 43 | ;; ([expr var?] (distinct-elements expr var?)) 44 | ;; ([expr var? branch? children] (distinct-elements expr var? branch? children)) 45 | 46 | (defn symbols [expr] 47 | (distinct-elements symbol? expr)) 48 | 49 | (defn symbols-no-vars [expr] 50 | (distinct-elements #(and (symbol? %) (not (variable? %))) expr)) 51 | 52 | -------------------------------------------------------------------------------- /kr-sesame/kr-sesame-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | 7 | 8 | edu.ucdenver.ccp 9 | kr-sesame 10 | 1.4.20-SNAPSHOT 11 | 12 | 13 | kr-sesame-core 14 | jar 15 | 16 | ${project.artifactId} 17 | KR Sesame bindings. 18 | 19 | 20 | 21 | 24 | 25 | org.openrdf.sesame 26 | sesame-runtime 27 | jar 28 | 29 | 30 | 31 | org.openrdf.sesame 32 | sesame-queryresultio 33 | pom 34 | 35 | 36 | 37 | org.openrdf.sesame 38 | sesame-queryresultio-sparqlxml 39 | jar 40 | 41 | 42 | 43 | org.openrdf.sesame 44 | sesame-queryresultio-binary 45 | jar 46 | 47 | 48 | 49 | 50 | ${project.groupId} 51 | kr-core 52 | ${project.version} 53 | test-jar 54 | test 55 | 56 | 57 | 58 | 59 | log4j 60 | log4j 61 | test 62 | 63 | 64 | commons-logging 65 | commons-logging 66 | 67 | 68 | commons-codec 69 | commons-codec 70 | 71 | 72 | org.slf4j 73 | slf4j-log4j12 74 | test 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /kr-core/src/test/clojure/edu/ucdenver/ccp/test/kr/test_sparql_construct.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.test-sparql-construct 2 | (use clojure.test 3 | edu.ucdenver.ccp.test.kr.test-kb 4 | edu.ucdenver.ccp.test.kr.test-sparql 5 | 6 | edu.ucdenver.ccp.kr.variable 7 | edu.ucdenver.ccp.kr.kb 8 | edu.ucdenver.ccp.kr.rdf 9 | edu.ucdenver.ccp.kr.sparql)) 10 | 11 | 12 | 13 | ;; (def test-triples-6-1 14 | ;; '((ex/a rdf/type foaf/Person ) 15 | ;; (ex/a foaf/name "Alice" ) 16 | ;; (ex/a foaf/mbox "" ) 17 | ;; (ex/a foaf/mbox "" ) 18 | 19 | ;; (ex/b rdf/type foaf/Person ) 20 | ;; (ex/b foaf/name "Bob" ))) 21 | 22 | ;; (def test-triples-numbers-equality 23 | ;; '((ex/a foaf/givenname "Alice" ) 24 | ;; (ex/a foaf/surname "Hacker" ) 25 | ;; (ex/a foaf/age [40 xsd/integer]) 26 | 27 | ;; (ex/b foaf/firstname "Bob" ) 28 | ;; (ex/b foaf/surname "Hacker" ) 29 | ;; (ex/b foaf/age 40) ;the default should be xsd/integer 30 | 31 | ;; (ex/c foaf/firstname "Fred" ) 32 | ;; (ex/c foaf/surname "Hacker" ) 33 | ;; (ex/c foaf/age [50 xsd/integer]))) 34 | 35 | 36 | (kb-test test-construct-pattern test-triples-6-1 37 | (is (= 2 38 | (count 39 | (construct '((?/person rdf/type ex/Human)) 40 | '((?/person rdf/type foaf/Person)))))) 41 | (is (= 4 42 | (count 43 | (construct '((?/person rdf/type ex/Human) 44 | (?/person ex/live-on ex/Earth)) 45 | '((?/person rdf/type foaf/Person))))))) 46 | 47 | 48 | (kb-test test-construct-visit-pattern test-triples-6-1 49 | (let [results (atom '())] 50 | (construct-visit (fn [[s p o :as triple]] 51 | (is (= 3 (count triple))) 52 | (is (= 'rdf/type p)) 53 | (is (= 'ex/Human o)) 54 | (swap! results conj triple)) 55 | '((?/person rdf/type ex/Human)) 56 | '((?/person rdf/type foaf/Person))) 57 | (is (= 2 (count @results))))) 58 | 59 | 60 | (kb-test test-construct-visit-literals test-triples-6-1 61 | (let [results (atom '())] 62 | (construct-visit (fn [[s p o :as triple]] 63 | (is (= 3 (count triple))) 64 | (is (= 'foaf/age p)) 65 | (is (= 40 o)) 66 | (swap! results conj triple)) 67 | '((?/person foaf/age 40)) 68 | '((?/person rdf/type foaf/Person))) 69 | (is (= 2 (count @results))))) 70 | 71 | 72 | (kb-test test-construct-visit-literals-both-sides test-triples-numbers-equality 73 | (let [results (atom '())] 74 | (construct-visit (fn [[s p o :as triple]] 75 | (is (= 3 (count triple))) 76 | (is (= 'ex/age p)) 77 | (is (or (= 40 o) 78 | (= 50 o))) 79 | (swap! results conj triple)) 80 | '((?/person ex/age ?/age)) 81 | '((?/person foaf/surname ?/name) 82 | (?/person foaf/age ?/age))) 83 | (is (= 3 (count @results))))) 84 | -------------------------------------------------------------------------------- /kr-core/src/main/clojure/edu/ucdenver/ccp/kr/kb.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.kb 2 | ;; (use edu.ucdenver.ccp.kr.variable 3 | ;; [clojure.contrib.string :only (dochars)]) 4 | ) 5 | 6 | 7 | ;;; -------------------------------------------------------- 8 | ;;; specials and types 9 | ;;; -------------------------------------------------------- 10 | 11 | ;;; -------------------------------------------------------- 12 | ;;; basic KB protocol 13 | ;;; -------------------------------------------------------- 14 | 15 | (defprotocol KB 16 | ;;(root [kb] "gets the actual underlying api object") 17 | (native [kb] "gets the actual underlying native object") 18 | (initialize [kb] "initializes the kb") 19 | (open [kb] "establishes a connection to the kb") 20 | (close [kb] "closes and invalidates the connection") 21 | (features [kb])) 22 | 23 | 24 | ;;; -------------------------------------------------------- 25 | ;;; kb creator helper 26 | ;;; -------------------------------------------------------- 27 | 28 | ;;(defmulti kb (fn [& args] (and args (first args)))) 29 | 30 | ;; this should either be a class name or a object where it returns it's type 31 | 32 | (defmulti kb 33 | (fn [& args] 34 | (if (and args 35 | (first args)) 36 | (let [a1 (first args)] 37 | (cond 38 | (keyword? a1) a1 39 | (class? a1) a1 40 | :else (type a1)))))) 41 | 42 | ;;; -------------------------------------------------------- 43 | ;;; default KB instance 44 | ;;; -------------------------------------------------------- 45 | 46 | (defonce ^:dynamic *kb* nil) 47 | 48 | (defn set!-kb 49 | "DONOT USE THIS FUNCTION for testing only. sets the root *kb* binding." 50 | [new-kb] 51 | (alter-var-root (var *kb*) 52 | (fn [orig new-kb] new-kb) 53 | new-kb)) 54 | 55 | ;;; -------------------------------------------------------- 56 | ;;; helpers 57 | ;;; -------------------------------------------------------- 58 | 59 | (defn features [kb] 60 | (:kb-features kb)) 61 | 62 | (defn has-feature? [kb feature] 63 | ;; contains doesn't work on a list, which is completely stupid 64 | ;; so we must work around it 65 | ;;(contains? (features kb) feature)) 66 | (some (partial = feature) (features kb))) 67 | 68 | (defn add-feature [kb feature & more-features] 69 | (assoc kb :kb-features (apply conj (features kb) feature more-features))) 70 | 71 | ;;; -------------------------------------------------------- 72 | ;;; connection 73 | ;;; -------------------------------------------------------- 74 | 75 | (defn connection 76 | ([kb] (or (and (:connection kb) kb) 77 | (connection kb true))) 78 | ([kb force-new] (if (or force-new (not (:connection kb))) 79 | (open ^KB kb) 80 | kb))) 81 | 82 | (defmacro with-new-connection [kb conn & body] 83 | `(let [~conn (connection ~kb true)] 84 | (try 85 | ~@body 86 | (finally (close ~conn))))) 87 | ;;(finally (close (connection ~conn)))))) 88 | ;;(finally (.close (:connection ~conn)))))) 89 | 90 | ;;; -------------------------------------------------------- 91 | ;;; copy / cloning 92 | ;;; -------------------------------------------------------- 93 | 94 | (defn copy-kb-slots [target-kb source-kb] 95 | (assoc target-kb 96 | :kb-features (features source-kb))) 97 | 98 | ;;; -------------------------------------------------------- 99 | ;;; end 100 | ;;; -------------------------------------------------------- 101 | 102 | -------------------------------------------------------------------------------- /kr-examples/jena-mem-kb/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | edu.ucdenver.ccp.kr.examples 6 | jena-mem-kb 7 | 1.4.9-SNAPSHOT 8 | jar 9 | 10 | ${project.artifactId} 11 | Example Sesame Memory KB 12 | 13 | 14 | 15 | org.clojure 16 | clojure 17 | 1.4.0 18 | 19 | 20 | 27 | 28 | edu.ucdenver.ccp 29 | kr-jena-core 30 | 1.4.9-SNAPSHOT 31 | 32 | 33 | 34 | swank-clojure 35 | swank-clojure 36 | 1.4.0 37 | 38 | 39 | 57 | 58 | 59 | 60 | 61 | 62 | clojars 63 | http://clojars.org/repo 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | src/main/clojure 72 | 73 | 74 | 75 | 76 | 77 | com.theoryinpractise 78 | clojure-maven-plugin 79 | 1.3.9 80 | true 81 | 82 | 83 | compile-clojure 84 | compile 85 | 86 | compile 87 | 88 | 89 | 90 | test 91 | test 92 | 93 | test 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /kr-examples/sesame-remote-kb/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | edu.ucdenver.ccp.kr.examples 6 | sesame-remote-kb 7 | 1.4.1-SNAPSHOT 8 | jar 9 | 10 | ${project.artifactId} 11 | Example Sesame Remote KB 12 | 13 | 14 | 15 | org.clojure 16 | clojure 17 | 1.4.0 18 | 19 | 20 | 27 | 28 | edu.ucdenver.ccp 29 | kr-sesame-core 30 | 1.4.7 31 | 32 | 33 | 34 | swank-clojure 35 | swank-clojure 36 | 1.4.0 37 | 38 | 39 | 57 | 58 | 59 | 60 | 61 | 62 | clojars 63 | http://clojars.org/repo 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | src/main/clojure 72 | 73 | 74 | 75 | 76 | 77 | com.theoryinpractise 78 | clojure-maven-plugin 79 | 1.3.9 80 | true 81 | 82 | 83 | compile-clojure 84 | compile 85 | 86 | compile 87 | 88 | 89 | 90 | test 91 | test 92 | 93 | test 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /kr-examples/sesame-mem-kb/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | edu.ucdenver.ccp.kr.examples 6 | sesame-mem-kb 7 | 1.4.11-SNAPSHOT 8 | jar 9 | 10 | ${project.artifactId} 11 | Example Sesame Memory KB 12 | 13 | 14 | 15 | org.clojure 16 | clojure 17 | 1.4.0 18 | 19 | 20 | 27 | 28 | edu.ucdenver.ccp 29 | kr-sesame-core 30 | 1.4.11-SNAPSHOT 31 | 32 | 33 | 34 | swank-clojure 35 | swank-clojure 36 | 1.4.0 37 | 38 | 39 | 57 | 58 | 59 | 60 | 61 | 62 | clojars 63 | http://clojars.org/repo 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | src/main/clojure 72 | 73 | 74 | 75 | 76 | 77 | com.theoryinpractise 78 | clojure-maven-plugin 79 | 1.3.9 80 | true 81 | 82 | 83 | compile-clojure 84 | compile 85 | 86 | compile 87 | 88 | 89 | 90 | test 91 | test 92 | 93 | test 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /kr-core/src/main/clojure/edu/ucdenver/ccp/kr/reify.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.reify 2 | (use edu.ucdenver.ccp.utils 3 | edu.ucdenver.ccp.kr.variable 4 | edu.ucdenver.ccp.kr.rdf)) 5 | 6 | ;;; -------------------------------------------------------- 7 | ;;; 8 | ;;; -------------------------------------------------------- 9 | 10 | ;;; -------------------------------------------------------- 11 | ;;; specials 12 | ;;; -------------------------------------------------------- 13 | 14 | (def ^:dynamic *reify-ns* "ex") ; "http://www.example.com/") 15 | (def ^:dynamic *reify-prefix* "G_") 16 | (def ^:dynamic *reify-suffix* "") 17 | 18 | (def ^:dynamic *name_separator* "_") 19 | 20 | ;;; -------------------------------------------------------- 21 | ;;; helpers 22 | ;;; -------------------------------------------------------- 23 | 24 | (defn reify-local-name [name] 25 | (str *reify-prefix* name *reify-suffix*)) 26 | 27 | (defn reify-anon [name] 28 | (anon (reify-local-name name))) 29 | 30 | ;;why would you ever use this? 31 | (defn reify-var [name] 32 | (variable (reify-local-name name))) 33 | 34 | (defn reify-sym 35 | ([name] (symbol *reify-ns* (reify-local-name name))) 36 | ([ns name] (symbol ns (reify-local-name name))) 37 | ([ns name prefix] (reify-sym ns name prefix *reify-suffix*)) 38 | ([ns name prefix suffix] 39 | (binding [*reify-prefix* prefix 40 | *reify-suffix* suffix] 41 | (symbol ns (reify-local-name name))))) 42 | 43 | ;;; -------------------------------------------------------- 44 | ;;; anon with type based mnemonics 45 | ;;; -------------------------------------------------------- 46 | 47 | ;; should probably use a mnemonic from the types, but isn't this exactly 48 | ;; what blank nodes are for?? 49 | ;;GUIDs would be unique, but might make for long URIs? 50 | 51 | ;;TODO should this be blank-node or anon?? 52 | (defn reify-instance [kb types] 53 | (blank-node kb (or (name (first types)) *anon-name*))) 54 | 55 | 56 | ;;; -------------------------------------------------------- 57 | ;;; unique 58 | ;;; -------------------------------------------------------- 59 | 60 | ;; parrot a local name into another ns 61 | (defn reify-unique [] 62 | (reify-sym (uuid))) 63 | 64 | ;;; -------------------------------------------------------- 65 | ;;; localname 66 | ;;; -------------------------------------------------------- 67 | 68 | ;; parrot a local name into another ns 69 | (defn reify-localname [& syms] 70 | (reify-sym (apply str (interpose *name_separator* (map name syms))))) 71 | 72 | ;;; -------------------------------------------------------- 73 | ;;; md5 74 | ;;; -------------------------------------------------------- 75 | 76 | ;;md5 a set of items to produce a sym 77 | (defn reify-md5 [& rest] 78 | (reify-sym (md5 (apply str rest)))) 79 | 80 | 81 | ;;; -------------------------------------------------------- 82 | ;;; regex 83 | ;;; -------------------------------------------------------- 84 | 85 | ;;md5 a set of items to produce a sym 86 | (defn reify-regex [match replace & rest] 87 | (reify-sym (.replaceAll (apply str rest) match replace))) 88 | 89 | ;;; -------------------------------------------------------- 90 | ;;; fn 91 | ;;; -------------------------------------------------------- 92 | 93 | ;;apply a function to a set of bindings to create a sym 94 | ;; (defn reify-fn [fn bindings] 95 | ;; (reify-sym (fn bindings))) 96 | 97 | 98 | ;;; -------------------------------------------------------- 99 | ;;; END 100 | ;;; -------------------------------------------------------- 101 | 102 | 103 | -------------------------------------------------------------------------------- /kr-examples/sesame-remote-kb/src/main/clojure/edu/ucdenver/ccp/kr/examples/sesame_remote_kb.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.examples.sesame-remote-kb 2 | (use edu.ucdenver.ccp.kr.kb 3 | edu.ucdenver.ccp.kr.rdf 4 | edu.ucdenver.ccp.kr.sparql) 5 | (require edu.ucdenver.ccp.kr.sesame.kb) 6 | (import org.openrdf.repository.http.HTTPRepository)) 7 | 8 | ;;; -------------------------------------------------------- 9 | ;;; create kb 10 | ;;; -------------------------------------------------------- 11 | 12 | (defn sesame-remote-test-kb [] 13 | (open 14 | (edu.ucdenver.ccp.kr.sesame.kb/new-sesame-server 15 | :server "http://dbpedia.org/sparql" 16 | :repo-name ""))) 17 | 18 | 19 | (defn add-namespaces [kb] 20 | (update-namespaces kb 21 | '(("ex" "http://www.example.org/") 22 | ("rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#") 23 | ("rdfs" "http://www.w3.org/2000/01/rdf-schema#") 24 | ("owl" "http://www.w3.org/2002/07/owl#") 25 | ("foaf" "http://xmlns.com/foaf/0.1/") 26 | 27 | ("dbpedia-owl" "http://dbpedia.org/ontology/") 28 | ("dbpedia" "http://dbpedia.org/resource/")))) 29 | 30 | 31 | 32 | ;;; -------------------------------------------------------- 33 | ;;; sparql api 34 | ;;; -------------------------------------------------------- 35 | 36 | (def ski-pattern '((?/ski rdf/type dbpedia-owl/SkiArea))) 37 | 38 | (defn count-ski [kb] 39 | (query-count kb ski-pattern)) 40 | 41 | (defn ski-areas [kb] 42 | (query kb '((?/ski rdf/type dbpedia-owl/SkiArea)))) 43 | 44 | (defn visit-ski-areas [kb] 45 | (query-visit kb 46 | (fn [bindings] 47 | (let [area (get bindings '?/ski)] 48 | (println "visiting " (name area)))) 49 | ski-pattern)) 50 | 51 | ;;; -------------------------------------------------------- 52 | ;;; REPL trace: 53 | ;;; -------------------------------------------------------- 54 | 55 | ;; user> (use 'edu.ucdenver.ccp.kr.examples.sesame-remote-kb) 56 | ;; nil 57 | 58 | ;; user> (def a-kb (add-namespaces (sesame-remote-test-kb))) 59 | ;; #'user/a-kb 60 | 61 | ;; user> (count-ski a-kb) 62 | ;; 495 63 | 64 | ;; user> (take 10 (ski-areas a-kb)) 65 | ;; ({?/ski dbpedia/Grouse_Mountain} {?/ski dbpedia/Killington_Ski_Resort} {?/ski dbpedia/Whakapapa_skifield} {?/ski dbpedia/Treble_Cone} {?/ski dbpedia/Invincible_Snowfields} {?/ski dbpedia/Thredbo,_New_South_Wales} {?/ski dbpedia/Tyrol_Basin} {?/ski dbpedia/Squaw_Valley_Ski_Resort} {?/ski dbpedia/Brighton_Ski_Resort} {?/ski dbpedia/Keystone_Resort}) 66 | 67 | 68 | ;; user> (visit-ski-areas a-kb) 69 | ;; visiting Breuil-Cervinia 70 | ;; visiting Snowbasin 71 | ;; visiting Charlotte_Pass,_New_South_Wales 72 | ;; visiting Ski_Dubai 73 | ;; visiting Mont-Sainte-Anne 74 | ;; visiting Sunshine_Village 75 | ;; visiting Madonna_di_Campiglio 76 | ;; visiting Reiteralm 77 | 78 | 79 | ;; user> (use 'edu.ucdenver.ccp.kr.kb) 80 | ;; nil 81 | ;; user> (close a-kb) 82 | ;; #edu.ucdenver.ccp.kr.sesame.kb.SesameKB{:server #, :connection nil, :kb-features (:sparql-1-0 :sparql-1-1), :ns-map-to-long {"dbpedia" "http://dbpedia.org/resource/", "dbpedia-owl" "http://dbpedia.org/ontology/", "foaf" "http://xmlns.com/foaf/0.1/", "owl" "http://www.w3.org/2002/07/owl#", "rdfs" "http://www.w3.org/2000/01/rdf-schema#", "rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "ex" "http://www.example.org/"}, :ns-map-to-short {"http://dbpedia.org/resource/" "dbpedia", "http://dbpedia.org/ontology/" "dbpedia-owl", "http://xmlns.com/foaf/0.1/" "foaf", "http://www.w3.org/2002/07/owl#" "owl", "http://www.w3.org/2000/01/rdf-schema#" "rdfs", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" "rdf", "http://www.example.org/" "ex"}, :value-factory #} 83 | 84 | 85 | ;;; -------------------------------------------------------- 86 | ;;; END 87 | ;;; -------------------------------------------------------- 88 | -------------------------------------------------------------------------------- /kr-core/src/main/clojure/edu/ucdenver/ccp/kr/unify.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.unify 2 | (:use edu.ucdenver.ccp.kr.variable 3 | edu.ucdenver.ccp.utils 4 | clojure.walk) 5 | ;;(:require clojure.core.unify)) 6 | ) 7 | 8 | ;;; based on Norvig PAIP unifier 9 | 10 | (declare unify unify-variable occurs-check) 11 | 12 | (def fail nil) 13 | (def no-bindings {}) 14 | 15 | (def ^:dynamic *occurs-check* true) ;"perform the occurs check?" 16 | 17 | (defn extend-bindings [var val bindings] 18 | (assoc bindings var val)) 19 | 20 | (defn lookup [var bindings] 21 | (get bindings var)) 22 | 23 | (defn has-binding? [var bindings] 24 | (contains? bindings var)) 25 | 26 | ;;; ============================== 27 | 28 | (defn unify 29 | "See if x and y match with given bindings." 30 | ([x y] (unify x y no-bindings)) 31 | ([x y bindings] 32 | (cond 33 | (= bindings fail) fail 34 | (= x y) bindings 35 | (variable? x) (unify-variable x y bindings) 36 | (variable? y) (unify-variable y x bindings) 37 | (and (nonempty-seq x) (nonempty-seq y)) 38 | (unify (next x) (next y) 39 | (unify (first x) (first y) bindings)) 40 | true fail))) 41 | 42 | (defn unify-variable [var x bindings] 43 | "Unify var with x, using (and maybe extending) bindings." 44 | (cond (contains? bindings var) 45 | (unify (lookup var bindings) x bindings) 46 | (and (variable? x) (has-binding? bindings x)) 47 | (unify var (lookup x bindings) bindings) 48 | (and *occurs-check* (occurs-check var x bindings)) 49 | fail 50 | :else (extend-bindings var x bindings))) 51 | 52 | (defn occurs-check [var x bindings] 53 | "Does var occur anywhere inside x?" 54 | (cond (= var x) true 55 | (and (variable? x) (has-binding? bindings x)) 56 | (occurs-check var (lookup x bindings) bindings) 57 | (nonempty-seq x) 58 | (or (occurs-check var (first x) bindings) 59 | (occurs-check var (next x) bindings)) 60 | :else false)) 61 | 62 | ;;; ============================== 63 | 64 | ;; (def unify-occurs (make-occurs-unify-fn variable?)) 65 | ;; (def unify-no-occurs (make-unify-fn variable?)) 66 | ;; (def unify-occurs 67 | ;; (partial #'clojure.core.unify/garner-unifiers 68 | ;; clojure.core.unify/unify-variable 69 | ;; variable?)) 70 | ;; (def unify-no-occurs 71 | ;; (partial #'clojure.core.unify/garner-unifiers 72 | ;; clojure.core.unify/unify-variable- 73 | ;; variable?)) 74 | 75 | ;; (defn unify 76 | ;; ([x y] (unify x y {})) 77 | ;; ([x y binds] (if *occurs-check* 78 | ;; (unify-occurs x y binds) 79 | ;; (unify-no-occurs x y binds)))) 80 | 81 | 82 | ;;; ============================== 83 | 84 | 85 | (defn recursive-replace 86 | ([expr l-bindings] (recursive-replace expr l-bindings #{expr})) 87 | ([expr l-bindings past-vals] 88 | (let [applicable-bindings 89 | (some (fn [b] (if (contains? b expr) b nil)) l-bindings) 90 | bind-val (get applicable-bindings expr)] 91 | (if (or (not applicable-bindings) 92 | (contains? past-vals bind-val)) ;don't enter replace loop 93 | expr 94 | (recur bind-val l-bindings (conj past-vals bind-val)))))) 95 | 96 | (defn subst-bindings-int [x l-bindings] 97 | (prewalk (fn [expr] (recursive-replace expr l-bindings)) x)) 98 | 99 | (defn subst-bindings-list [x list-of-bindings] 100 | (cond (some (partial = fail) list-of-bindings) fail 101 | (every? (partial = no-bindings) list-of-bindings) x 102 | :else (subst-bindings-int x list-of-bindings))) 103 | 104 | (defn subst-bindings [x & bindings] 105 | "Substitute the value of variables in bindings into x, 106 | taking recursively bound variables into account." 107 | (subst-bindings-list x bindings)) 108 | 109 | ;;; ============================== 110 | 111 | (defn unifier [x y] 112 | "Return something that unifies with both x and y (or fail)." 113 | (subst-bindings x (unify x y))) -------------------------------------------------------------------------------- /kr-examples/jena-mem-kb/src/main/clojure/edu/ucdenver/ccp/kr/examples/jena_mem_kb.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.examples.jena-mem-kb 2 | (use edu.ucdenver.ccp.kr.kb 3 | edu.ucdenver.ccp.kr.rdf 4 | edu.ucdenver.ccp.kr.sparql) 5 | (require edu.ucdenver.ccp.kr.jena.kb)) 6 | 7 | ;;; -------------------------------------------------------- 8 | ;;; create kb 9 | ;;; -------------------------------------------------------- 10 | 11 | (defn jena-memory-test-kb [] 12 | (open (kb :jena-mem))) 13 | 14 | 15 | (defn add-namespaces [kb] 16 | (register-namespaces kb 17 | '(("ex" "http://www.example.org/") 18 | ("rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#") 19 | ("rdfs" "http://www.w3.org/2000/01/rdf-schema#") 20 | ("owl" "http://www.w3.org/2002/07/owl#") 21 | ("foaf" "http://xmlns.com/foaf/0.1/")))) 22 | 23 | ;;a couple of ways to add triples 24 | (defn add-triples [kb] 25 | ;;in parts 26 | (add kb 'ex/KevinL 'rdf/type 'ex/Person) 27 | 28 | ;;as a triple 29 | (add kb '(ex/KevinL foaf/name "Kevin Livingston")) 30 | 31 | ;;to the 'default' kb 32 | (binding [*kb* kb] 33 | (add '(ex/KevinL foaf/mbox ""))) 34 | 35 | ;;multiple triples 36 | (add-statements kb 37 | '((ex/BobL rdf/type ex/Person) 38 | (ex/BobL foaf/name "Bob Livingston") 39 | (ex/BobL foaf/mbox "")))) 40 | 41 | 42 | ;;; -------------------------------------------------------- 43 | ;;; rdf api 44 | ;;; -------------------------------------------------------- 45 | 46 | ;;rdf api ask 47 | (defn ask-person [kb] 48 | (ask-rdf kb nil nil 'ex/Person)) 49 | 50 | ;;rdf api query 51 | (defn query-person [kb] 52 | (query-rdf kb nil nil 'ex/Person)) 53 | 54 | 55 | ;;; -------------------------------------------------------- 56 | ;;; sparql api 57 | ;;; -------------------------------------------------------- 58 | 59 | ;; symbols in the ? ns are assumed to be capturing variables 60 | ;; symbols in the _ ns are blank nodes 61 | ;; they function as non-capturing in sparql 62 | (def names-and-email-pattern 63 | '((_/person rdf/type ex/Person) 64 | (_/person foaf/name ?/name) 65 | (_/person foaf/mbox ?/email))) 66 | 67 | ;;sparql api ask 68 | (defn ask-names-with-email [kb] 69 | (ask kb names-and-email-pattern)) 70 | 71 | ;;sparql api ask 72 | (defn query-names-with-email [kb] 73 | (query kb names-and-email-pattern)) 74 | 75 | (defn visit-people [kb] 76 | (query-visit kb 77 | (fn [bindings] 78 | (let [name (get bindings '?/name) 79 | email (get bindings '?/email)] 80 | (println "emailing " name " at: " email))) 81 | names-and-email-pattern)) 82 | 83 | ;;; -------------------------------------------------------- 84 | ;;; REPL trace: 85 | ;;; -------------------------------------------------------- 86 | 87 | ;; user> (use 'edu.ucdenver.ccp.kr.examples.sesame-mem-kb) 88 | ;; nil 89 | 90 | ;; user> (def my-kb (add-namespaces 91 | ;; (sesame-memory-test-kb))) 92 | ;; #'user/my-kb 93 | 94 | ;; user> (add-triples my-kb) 95 | ;; nil 96 | 97 | ;; user> (ask-person my-kb) 98 | ;; true 99 | 100 | ;; user> (query-person my-kb) 101 | ;; ((ex/KevinL rdf/type ex/Person) (ex/BobL rdf/type ex/Person)) 102 | 103 | 104 | ;; user> (ask-names-with-email my-kb) 105 | ;; true 106 | 107 | ;; user> (query-names-with-email my-kb) 108 | ;; ({?/name "Kevin Livingston", ?/email ""} {?/name "Bob Livingston", ?/email ""}) 109 | 110 | ;; user> (visit-people my-kb) 111 | ;; emailing Kevin Livingston at: 112 | ;; emailing Bob Livingston at: 113 | ;; nil 114 | 115 | ;; user> (use 'edu.ucdenver.ccp.kr.kb) 116 | ;; user> (close my-kb) 117 | 118 | ;;; -------------------------------------------------------- 119 | ;;; END 120 | ;;; -------------------------------------------------------- 121 | -------------------------------------------------------------------------------- /kr-examples/sesame-mem-kb/src/main/clojure/edu/ucdenver/ccp/kr/examples/sesame_mem_kb.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.examples.sesame-mem-kb 2 | (use edu.ucdenver.ccp.kr.kb 3 | edu.ucdenver.ccp.kr.rdf 4 | edu.ucdenver.ccp.kr.sparql) 5 | (require edu.ucdenver.ccp.kr.sesame.kb)) 6 | 7 | ;;; -------------------------------------------------------- 8 | ;;; create kb 9 | ;;; -------------------------------------------------------- 10 | 11 | (defn sesame-memory-test-kb [] 12 | (open (kb :sesame-mem))) 13 | 14 | 15 | (defn add-namespaces [kb] 16 | (register-namespaces kb 17 | '(("ex" "http://www.example.org/") 18 | ("rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#") 19 | ("rdfs" "http://www.w3.org/2000/01/rdf-schema#") 20 | ("owl" "http://www.w3.org/2002/07/owl#") 21 | ("foaf" "http://xmlns.com/foaf/0.1/") 22 | ("xsd" "http://www.w3.org/2001/XMLSchema#")))) 23 | 24 | ;;a couple of ways to add triples 25 | (defn add-triples [kb] 26 | ;;in parts 27 | (add kb 'ex/KevinL 'rdf/type 'ex/Person) 28 | 29 | ;;as a triple 30 | (add kb '(ex/KevinL foaf/name "Kevin Livingston")) 31 | 32 | ;;to the 'default' kb 33 | (binding [*kb* kb] 34 | (add '(ex/KevinL foaf/mbox ""))) 35 | 36 | ;;multiple triples 37 | (add-statements kb 38 | '((ex/BobL rdf/type ex/Person) 39 | (ex/BobL foaf/name "Bob Livingston") 40 | (ex/BobL foaf/mbox "")))) 41 | 42 | 43 | ;;; -------------------------------------------------------- 44 | ;;; rdf api 45 | ;;; -------------------------------------------------------- 46 | 47 | ;;rdf api ask 48 | (defn ask-person [kb] 49 | (ask-rdf kb nil nil 'ex/Person)) 50 | 51 | ;;rdf api query 52 | (defn query-person [kb] 53 | (query-rdf kb nil nil 'ex/Person)) 54 | 55 | 56 | ;;; -------------------------------------------------------- 57 | ;;; sparql api 58 | ;;; -------------------------------------------------------- 59 | 60 | ;; symbols in the ? ns are assumed to be capturing variables 61 | ;; symbols in the _ ns are blank nodes 62 | ;; they function as non-capturing in sparql 63 | (def names-and-email-pattern 64 | '((_/person rdf/type ex/Person) 65 | (_/person foaf/name ?/name) 66 | (_/person foaf/mbox ?/email))) 67 | 68 | ;;sparql api ask 69 | (defn ask-names-with-email [kb] 70 | (ask kb names-and-email-pattern)) 71 | 72 | ;;sparql api ask 73 | (defn query-names-with-email [kb] 74 | (query kb names-and-email-pattern)) 75 | 76 | (defn visit-people [kb] 77 | (query-visit kb 78 | (fn [bindings] 79 | (let [name (get bindings '?/name) 80 | email (get bindings '?/email)] 81 | (println "emailing " name " at: " email))) 82 | names-and-email-pattern)) 83 | 84 | ;;; -------------------------------------------------------- 85 | ;;; REPL trace: 86 | ;;; -------------------------------------------------------- 87 | 88 | ;; user> (use 'edu.ucdenver.ccp.kr.examples.sesame-mem-kb) 89 | ;; nil 90 | 91 | ;; user> (def my-kb (add-namespaces 92 | ;; (sesame-memory-test-kb))) 93 | ;; #'user/my-kb 94 | 95 | ;; user> (add-triples my-kb) 96 | ;; nil 97 | 98 | ;; user> (ask-person my-kb) 99 | ;; true 100 | 101 | ;; user> (query-person my-kb) 102 | ;; ((ex/KevinL rdf/type ex/Person) (ex/BobL rdf/type ex/Person)) 103 | 104 | 105 | ;; user> (ask-names-with-email my-kb) 106 | ;; true 107 | 108 | ;; user> (query-names-with-email my-kb) 109 | ;; ({?/name "Kevin Livingston", ?/email ""} {?/name "Bob Livingston", ?/email ""}) 110 | 111 | ;; user> (visit-people my-kb) 112 | ;; emailing Kevin Livingston at: 113 | ;; emailing Bob Livingston at: 114 | ;; nil 115 | 116 | ;; user> (use 'edu.ucdenver.ccp.kr.kb) 117 | ;; user> (close my-kb) 118 | 119 | ;;; -------------------------------------------------------- 120 | ;;; END 121 | ;;; -------------------------------------------------------- 122 | -------------------------------------------------------------------------------- /kr-core/src/test/clojure/edu/ucdenver/ccp/test/kr/test_kb.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.test-kb 2 | (use clojure.test 3 | ;;clojure.test.junit 4 | edu.ucdenver.ccp.kr.variable 5 | edu.ucdenver.ccp.kr.kb 6 | edu.ucdenver.ccp.kr.rdf 7 | edu.ucdenver.ccp.kr.sparql 8 | )) 9 | 10 | ;;; -------------------------------------------------------- 11 | ;;; constansts 12 | ;;; -------------------------------------------------------- 13 | 14 | (defonce ^:dynamic *rcon* nil) 15 | 16 | (def ^:dynamic *namespaces* 17 | '(;;standard namespaces 18 | ("rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#") 19 | ("rdfs" "http://www.w3.org/2000/01/rdf-schema#") 20 | ("owl" "http://www.w3.org/2002/07/owl#") 21 | ("xsd" "http://www.w3.org/2001/XMLSchema#") 22 | 23 | ;;a "regular" namespaces 24 | ("ex" "http://www.example.org/") 25 | ("foaf" "http://xmlns.com/foaf/0.1/") 26 | ("vcard" "http://www.w3.org/2001/vcard-rdf/3.0#") 27 | ("rss" "http://purl.org/rss/1.0/") 28 | ("daml" "http://www.daml.org/2001/03/daml+oil#") 29 | 30 | ("dc" "http://purl.org/dc/elements/1.1/") 31 | ("dc10" "http://purl.org/dc/elements/1.0/") 32 | ("dc11" "http://purl.org/dc/elements/1.1/") 33 | 34 | ("ja" "http://jena.hpl.hp.com/2005/11/Assembler#") 35 | 36 | )) 37 | 38 | (def test-triples 39 | '((ex/a foaf/name "Johnny Lee Outlaw") 40 | (ex/a foaf/mbox "") 41 | (ex/b foaf/name "Peter Goodguy") 42 | (ex/b foaf/mbox "") 43 | (ex/c foaf/mbox ""))) 44 | 45 | ;;; -------------------------------------------------------- 46 | ;;; helpers 47 | ;;; -------------------------------------------------------- 48 | 49 | (def ^:dynamic *kb-creator-fn* nil) 50 | 51 | ;; (defn register-namespaces [kb] 52 | ;; (synch-ns-mappings kb) 53 | ;; (dorun 54 | ;; (map (fn [[short long]] 55 | ;; (update-ns-mapping kb short long)) 56 | ;; *namespaces*)) 57 | ;; (synch-ns-mappings kb)) 58 | 59 | (defn load-test-triples [kb triples] 60 | (dorun 61 | (map (fn [stmt] (add! kb stmt)) triples))) 62 | 63 | (defn new-test-kb 64 | ([triples] (new-test-kb *kb-creator-fn* triples)) 65 | ([kb-creator triples] 66 | (let [kb (register-namespaces (kb-creator) *namespaces*)] 67 | (binding [*kb* kb] 68 | (load-test-triples kb triples)) 69 | kb))) 70 | ;; ([kb-creator triples] (let [kb (kb-creator)] 71 | ;; (register-namespaces kb *namespaces*) 72 | ;; (binding [*kb* kb] 73 | ;; (load-test-triples kb triples)) 74 | ;; kb))) 75 | 76 | (defmacro kb-test [name triples & body] 77 | `(deftest ~name 78 | (when *kb-creator-fn* 79 | (binding [*kb* (new-test-kb ~triples)] 80 | ~@body)))) 81 | 82 | ;; (defmacro kb-test [name triples & body] 83 | ;; `(deftest ~name 84 | ;; (binding [*kb* (new-test-kb ~triples)] 85 | ;; ~@body))) 86 | 87 | ;;; -------------------------------------------------------- 88 | ;;; kb tests 89 | ;;; -------------------------------------------------------- 90 | 91 | (kb-test test-kb-up nil 92 | (is *kb*)) 93 | 94 | (kb-test test-kb-features nil 95 | (let [kb (add-feature *kb* :foo)] 96 | (is (has-feature? kb :foo)) 97 | (is (not (has-feature? kb :bar))))) 98 | 99 | (kb-test test-kb-features-preserve nil 100 | (let [kb (add-feature *kb* :foo)] 101 | (let [open-kb (open kb)] 102 | (is (has-feature? open-kb :foo)) 103 | (let [close-kb (close open-kb)] 104 | (is (has-feature? close-kb :foo)))))) 105 | 106 | (kb-test test-kb-connection nil 107 | (let [kb (add-feature *kb* :foo)] 108 | (let [conn (connection kb true)] 109 | (is (has-feature? conn :foo))))) 110 | 111 | (kb-test test-kb-with-connection nil 112 | (let [kb (add-feature *kb* :foo)] 113 | (with-new-connection kb conn 114 | (is (has-feature? conn :foo))))) 115 | 116 | ;;; -------------------------------------------------------- 117 | ;;; END 118 | ;;; -------------------------------------------------------- 119 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Clojure API for RDF and SPARQL 2 | 3 | The Knowledge Representation and Reasoning Tools library enables easy Clojure use of RDF and SPARQL, provinging a unified interface for both Jena and Sesame. (KR can be extended for other APIs and underlying triplestores.) 4 | 5 | 6 | ## Overview 7 | 8 | Currently it facilitates use of RDF-based representations backed by triple-/quad- stores. It provides a consistent clojure based way of interacting with its backing implementations, which currently include the Jena and Sesame APIs. The library enables easy working with knowledge representations and knowledge bases, and provides support for some common tasks including forward-chaining and reification. 9 | 10 | [Release Notes] 11 | 12 | update: see the note on [Sesame Versions] 13 | 14 | 15 | ## Basic Setup 16 | 17 | The primary api functions you're likely to use come from the kr-core apis: 18 | ```clj 19 | (use 'edu.ucdenver.ccp.kr.kb) 20 | (use 'edu.ucdenver.ccp.kr.rdf) 21 | (use 'edu.ucdenver.ccp.kr.sparql) 22 | ``` 23 | 24 | To actually get a KB instance to work with you'll need to make sure the implementation-specific code is loaded: 25 | ```clj 26 | (require 'edu.ucdenver.ccp.kr.sesame.kb) 27 | ;; OR 28 | (require 'edu.ucdenver.ccp.kr.jena.kb) 29 | ``` 30 | 31 | a kb instance can then be acquired with the kb function, for example: 32 | ```clj 33 | (kb :sesame-mem) ; an in-memory sesame kb 34 | ``` 35 | The `kb` function can take keyword arguments such as `:sesame-mem` or `:jena-mem` or it can take names of several native jena or sesame objects or pre-constructed jena or sesame instances to create a `kb` wrapper around (e.g., a jena `Model` or a sesame `Sail`). 36 | 37 | kb's need some help knowing what the namespace mappings are, the server mappings can be brought down from a third party kb by calling `(synch-ns-mappings my-kb)` or you can add a few: 38 | ```clj 39 | (register-namespaces my-kb 40 | '(("ex" "http://www.example.org/") 41 | ("rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#") 42 | ("foaf" "http://xmlns.com/foaf/0.1/"))) 43 | ;;the return value is the new modified kb - hang onto it 44 | ``` 45 | 46 | ## Basic Use 47 | 48 | Once you have a KB you can load rdf triple or files: 49 | ```clj 50 | ;;in parts 51 | (add my-kb 'ex/KevinL 'rdf/type 'ex/Person) 52 | ;;as a triple 53 | (add my-kb '(ex/KevinL foaf/name "Kevin Livingston")) 54 | ``` 55 | 56 | Query for RDF triples: 57 | ```clj 58 | (ask-rdf my-kb nil nil 'ex/Person) 59 | ;;true 60 | 61 | (query-rdf my-kb nil nil 'ex/Person) 62 | ;;((ex/KevinL rdf/type ex/Person)) 63 | ``` 64 | 65 | Query with triple patterns (SPARQL): 66 | ```clj 67 | (query my-kb '((?/person rdf/type ex/Person) 68 | (?/person foaf/name ?/name) 69 | (:optional ((?/person foaf/mbox ?/email))))) 70 | ;;({?/name "Kevin Livingston", ?/person ex/KevinL}) 71 | ``` 72 | 73 | ## More Details 74 | 75 | The examples also provide details on how to interact with a KB, with run-able poms: 76 | https://github.com/drlivingston/kr/tree/master/kr-examples 77 | 78 | These include examples of connecting to a remote repository and a local in-memory repository. 79 | 80 | 81 | More detailed uses can be found in the test cases for both the KB, RDF, and SPARQL APIs. They are here: 82 | https://github.com/drlivingston/kr/tree/master/kr-core/src/test/clojure/edu/ucdenver/ccp/test/kr 83 | 84 | 85 | ## Maven 86 | 87 | releases are deployed to clojars: 88 | ```xml 89 | 90 | clojars.org 91 | http://clojars.org/repo 92 | 93 | ``` 94 | 95 | the core dependency is kr-core: 96 | ```xml 97 | 98 | edu.ucdenver.ccp 99 | kr-core 100 | 1.4.17 101 | 102 | ``` 103 | 104 | but the core dependency is unnecessary if you are brining in either the sesame or jena implementations: 105 | ```xml 106 | 107 | edu.ucdenver.ccp 108 | kr-sesame-core 109 | 1.4.17 110 | 111 | 112 | 113 | edu.ucdenver.ccp 114 | kr-jena-core 115 | 1.4.17 116 | 117 | ``` 118 | 119 | 120 | ## Acknowledgements 121 | open sourced by:
122 | [CCP Lab][]
123 | [University of Colorado Denver][]
124 | primary developer: [Kevin Livingston][] 125 | 126 | ---- 127 | 128 | 129 | [CCP Lab]: http://compbio.ucdenver.edu/Hunter_lab/CCP_website/index.html 130 | [University of Colorado Denver]: http://www.ucdenver.edu/ 131 | [Kevin Livingston]: https://github.com/drlivingston 132 | [Sesame Versions]:https://github.com/drlivingston/kr/wiki/versions-and-sesame 133 | [Release Notes]:https://github.com/drlivingston/kr/wiki/Release-notes 134 | -------------------------------------------------------------------------------- /kr-core/src/main/clojure/edu/ucdenver/ccp/kr/clj_ify.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.clj-ify 2 | (use edu.ucdenver.ccp.kr.kb)) 3 | 4 | ;;; -------------------------------------------------------- 5 | ;;; clj-ify - convert data to clj structures 6 | ;;; -------------------------------------------------------- 7 | 8 | ;(declare clj-ify) 9 | 10 | (defmulti clj-ify (fn [kb obj] 11 | ;;(println (type obj)) 12 | (class obj))) 13 | ;;(type obj))) 14 | 15 | (defn clj 16 | ([x] (clj-ify *kb* x)) 17 | ([kb x] (clj-ify kb x))) 18 | 19 | ;;TODO clj-ify should be made universally to be a 2 argument funtion 20 | ;; with one one-argument helper that just calls the 2-arg one with *kb* 21 | 22 | ;; collections of clj-ifiable things 23 | ;; should this be made to capture *kb*? if there is a 2 arg clj-ify 24 | (defmethod clj-ify clojure.lang.Seqable [kb s] 25 | (map (partial clj-ify kb) s)) 26 | 27 | (defmethod clj-ify :default [kb x] 28 | (throw (IllegalArgumentException. (str "Unknown Type for: " x)))) 29 | 30 | ;; the above was a fail-fast catch-all for attempting to clj-ify a type 31 | ;; that can't be clj-ified (i.e. has no definition yet) BUT then all 32 | ;; the native types would need a clj-ify definition that is effectively 33 | ;; the identity function (symbols, strings, numbers, etc.) 34 | ;; replacing the default with identity saves implementing that but 35 | ;; passes the buck on identifying in a fail-fast manner things that 36 | ;; don't have a clj-ify implementation yet. 37 | 38 | ;; (defmethod clj-ify :default [x] 39 | ;; x) 40 | 41 | (defmethod clj-ify java.lang.Number [kb n] n) 42 | (defmethod clj-ify java.lang.String [kb str] str) 43 | (defmethod clj-ify java.lang.Boolean [kb bool] bool) 44 | 45 | (defmethod clj-ify clojure.lang.Symbol [kb sym] sym) 46 | 47 | 48 | ;;; -------------------------------------------------------- 49 | ;;; string-literal 50 | ;;; -------------------------------------------------------- 51 | 52 | (defmulti string-ify-literal (fn [kb obj] 53 | (type obj))) 54 | 55 | (defmethod string-ify-literal :default [kb x] 56 | (str x)) 57 | ;;(throw (IllegalArgumentException. (str "Unknown Type for: " x)))) 58 | 59 | 60 | ;;; -------------------------------------------------------- 61 | ;;; clj-ify-literal 62 | ;;; -------------------------------------------------------- 63 | 64 | ;;takes values: 65 | ;; nil or :clj for converting to clj objects 66 | ;; :clj-type to return a vector of [clj-object type-or-language] 67 | ;; :string to return a vector of [string-serialization type-or-language] 68 | ;; :native to return a vector of [native-literal type-or-language] 69 | ;; function that takes [clj-object type-or-language] and 70 | ;; returns one of the above values 71 | 72 | (def ^:dynamic *literal-mode* nil) 73 | 74 | ;; (defn literal-mode [literal type-or-langauge] 75 | ;; (cond (nil? *literal-mode*) nil 76 | ;; (keyword? *literal-mode*) *literal-mode* 77 | ;; :else (*literal-mode* literal type-or-language))) 78 | 79 | ;; (defn clj-ify-literal [literal type-or-langauge] 80 | ;; (case (literal-mode literal type-or-langauge) 81 | ;; nil (clj-ify literal) 82 | ;; :clj (clj-ify literal) 83 | ;; :clj-type (vector (clj-ify literal) type-or-language) 84 | ;; :string (vector (string-ify-literal literal) type-or-language) 85 | ;; :native (vector literal type-or-language))) 86 | 87 | (defn nil-symbol-string? [mode] 88 | (or (nil? mode) 89 | (keyword? mode) 90 | (symbol? mode) 91 | (string? mode))) 92 | 93 | (defn type-or-language-value [kb literal type-or-language] 94 | (if (nil-symbol-string? type-or-language) 95 | type-or-language 96 | (type-or-language kb literal))) 97 | ;; (if (function? type-or-language) 98 | ;; (type-or-langague kb literal) 99 | ;; type-or-language)) 100 | 101 | (defn clj-ify-literal-key [kb mode literal to-value-fn to-string-fn type-or-language] 102 | (case mode 103 | nil (to-value-fn kb literal) 104 | :clj (to-value-fn kb literal) 105 | :clj-type (vector (to-value-fn kb literal) 106 | (type-or-language-value kb literal type-or-language)) 107 | :string (vector (to-string-fn kb literal) 108 | (type-or-language-value kb literal type-or-language)) 109 | :native (vector literal 110 | (type-or-language-value kb literal type-or-language)))) 111 | 112 | 113 | (defn clj-ify-literal [kb literal to-value-fn to-string-fn type-or-language-fn] 114 | (if (nil-symbol-string? *literal-mode*) 115 | (clj-ify-literal-key kb *literal-mode* literal 116 | to-value-fn to-string-fn type-or-language-fn) 117 | (let [type-or-language (type-or-language-fn kb literal) 118 | mode (*literal-mode* literal type-or-language)] 119 | (clj-ify-literal-key kb mode literal 120 | to-value-fn to-string-fn type-or-language)))) 121 | 122 | 123 | 124 | 125 | ;; (cond (nil? *literal-mode*) (to-value-fn literal) 126 | ;; nil (clj-ify literal) 127 | ;; :clj (clj-ify literal) 128 | ;; :clj-type (vector (clj-ify literal) type-or-language) 129 | ;; :string (vector (string-ify-literal literal) type-or-language) 130 | ;; :native (vector literal type-or-language))) 131 | 132 | 133 | ;;; -------------------------------------------------------- 134 | ;;; end 135 | ;;; -------------------------------------------------------- 136 | 137 | -------------------------------------------------------------------------------- /kr-sesame/kr-sesame-core/src/main/clojure/edu/ucdenver/ccp/kr/sesame/writer_kb.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.sesame.writer-kb 2 | (use edu.ucdenver.ccp.kr.kb 3 | [edu.ucdenver.ccp.kr.rdf :exclude (resource)] 4 | edu.ucdenver.ccp.kr.sesame.kb 5 | [edu.ucdenver.ccp.kr.sesame.rdf :exclude (resource)] 6 | [clojure.java.io :exclude (resource)]) 7 | (import ;;org.openrdf.model.impl.ValueFactoryBase 8 | org.openrdf.model.impl.ValueFactoryImpl 9 | java.nio.charset.Charset 10 | (org.openrdf.rio Rio 11 | RDFFormat 12 | RDFWriter 13 | RDFWriterFactory) 14 | org.openrdf.rio.ntriples.NTriplesWriterFactory)) 15 | 16 | ;;; -------------------------------------------------------- 17 | ;;; connections 18 | ;;; -------------------------------------------------------- 19 | 20 | ;;this is nonsese becasue to the circular defintions 21 | ;; and what can and cannot be forward delcared 22 | (declare initialize-sesame-writer 23 | open-sesame-writer 24 | close-sesame-writer 25 | sesame-write-statement 26 | sesame-write-statements) 27 | 28 | ;;; -------------------------------------------------------- 29 | ;;; protocol implementation 30 | ;;; -------------------------------------------------------- 31 | 32 | (defrecord SesameWriterKB [target connection] 33 | KB 34 | 35 | (native [kb] target) 36 | (initialize [kb] kb) ;(initialize-sesame-writer kb)) 37 | (open [kb] (open-sesame-writer kb)) 38 | (close [kb] (close-sesame-writer kb)) 39 | 40 | rdfKB 41 | 42 | ;; (ns-maps [kb] ns-maps-var) 43 | ;; (ns-map-to-short [kb] (:ns-map-to-short (deref ns-maps-var))) 44 | ;; (ns-map-to-long [kb] (:ns-map-to-long (deref ns-maps-var))) 45 | (root-ns-map [kb] (ns-map-to-long kb)) 46 | (register-ns [kb short long] nil) ; no-op 47 | 48 | (create-resource [kb name] (sesame-create-resource kb name)) 49 | (create-property [kb name] (sesame-create-property kb name)) 50 | (create-literal [kb val] (sesame-create-literal kb val)) 51 | (create-literal [kb val type] (sesame-create-literal kb val type)) 52 | 53 | ;;TODO convert to creating proper string literals 54 | ;; (create-string-literal [kb str] (sesame-create-string-iteral kb val)) 55 | ;; (create-string-literal [kb str lang] 56 | ;; (sesame-create-string literal kb val type)) 57 | (create-string-literal [kb str] (sesame-create-literal kb str)) 58 | (create-string-literal [kb str lang] 59 | (sesame-create-literal kb str lang)) 60 | 61 | 62 | (create-blank-node [kb name] (sesame-create-blank-node kb name)) 63 | (create-statement [kb s p o] (sesame-create-statement kb s p o)) 64 | 65 | (add-statement [kb stmt] (sesame-write-statement kb stmt)) 66 | (add-statement [kb stmt context] (sesame-write-statement kb stmt context)) 67 | (add-statement [kb s p o] (sesame-write-statement kb s p o)) 68 | (add-statement [kb s p o context] (sesame-write-statement kb s p o context)) 69 | 70 | (add-statements [kb stmts] (sesame-write-statements kb stmts)) 71 | (add-statements [kb stmts context] (sesame-write-statements kb stmts context)) 72 | 73 | ;; (ask-statement [kb s p o context] (sesame-ask-statement kb s p o context)) 74 | ;; (query-statement [kb s p o context] 75 | ;; (sesame-query-statement kb s p o context)) 76 | 77 | ;; (load-rdf-file [kb file] (sesame-load-rdf-file kb file)) 78 | ;; (load-rdf-file [kb file type] (sesame-load-rdf-file kb file type)) 79 | ;;the following will throw exception for unknown rdf format 80 | ;;(load-rdf-stream [kb stream] (sesame-load-rdf-stream kb stream)) 81 | ;;(load-rdf-stream [kb stream type] (sesame-load-rdf-stream kb stream type)) 82 | ) 83 | 84 | ;;; "constructors" 85 | ;;; -------------------------------------------------------- 86 | 87 | (defn new-writer [out-stream] 88 | (let [writer (Rio/createWriter RDFFormat/NTRIPLES out-stream)] 89 | ;(output-stream target))] 90 | (.startRDF writer) ;side effect function doesn't return itself 91 | writer)) 92 | 93 | (defn open-sesame-writer [kb] 94 | (let [out (output-stream (:target kb)) 95 | writer (new-writer out)] 96 | (copy-sesame-slots (assoc (SesameWriterKB. (:target kb) writer) 97 | ;(new-writer (:target kb))) 98 | :output-stream out 99 | :value-factory (:value-factory kb)) 100 | kb))) 101 | 102 | (defn close-sesame-writer [kb] 103 | (when (:connection kb) 104 | (.endRDF (:connection kb)) 105 | (.close (:output-stream kb))) 106 | (copy-sesame-slots (assoc (SesameWriterKB. (:target kb) 107 | nil) 108 | :value-factory (:value-factory kb)) 109 | kb)) 110 | 111 | 112 | ;;if the target is a zipped output stream it will happily write there 113 | ;; e.g. pass in (GZIPOutputStream. (output-stream ...)) 114 | (defn new-sesame-writer-kb [target] 115 | (initialize-ns-mappings 116 | (assoc (SesameWriterKB. target nil) ;(initial-ns-mappings) nil) 117 | :value-factory (org.openrdf.model.impl.ValueFactoryImpl.)))) 118 | ;;(.getValueFactory repository))) 119 | 120 | 121 | ;;these can't handle graphs ... TODO change to NQUAD writer?? 122 | 123 | (defn sesame-write-statement 124 | ([kb stmt] (.handleStatement (connection! kb) 125 | ^Statment stmt)) 126 | ([kb stmt context] (.handleStatement (connection! kb) 127 | ^Statement stmt)) 128 | ([kb s p o] (.handleStatement (connection! kb) 129 | ^Statement (statement kb s p o))) 130 | ([kb s p o context] (.handleStatement (connection! kb) 131 | ^Statement (statement kb s p o)))) 132 | 133 | 134 | (defn sesame-write-statements 135 | ([kb stmts] (dorun (map (partial sesame-write-statement kb) stmts))) 136 | ([kb stmts context] (dorun (map (partial sesame-write-statement kb) stmts)))) 137 | 138 | 139 | ;;; -------------------------------------------------------- 140 | ;;; END 141 | ;;; -------------------------------------------------------- 142 | -------------------------------------------------------------------------------- /kr-core/src/main/clojure/edu/ucdenver/ccp/kr/assertion.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.assertion 2 | (use [clojure.set :only (union)] 3 | edu.ucdenver.ccp.kr.variable 4 | edu.ucdenver.ccp.kr.reify 5 | edu.ucdenver.ccp.kr.unify 6 | edu.ucdenver.ccp.kr.kb 7 | edu.ucdenver.ccp.kr.rdf 8 | edu.ucdenver.ccp.kr.sparql)) 9 | 10 | ;;; -------------------------------------------------------- 11 | ;;; 12 | ;;; -------------------------------------------------------- 13 | 14 | 15 | ;;; -------------------------------------------------------- 16 | ;;; helpers 17 | ;;; -------------------------------------------------------- 18 | 19 | ;;; -------------------------------------------------------- 20 | ;;; identify more specific types 21 | ;;; -------------------------------------------------------- 22 | 23 | ;;TODO: 24 | ;; this is justing type instead of subClassOf? 25 | ;; also need to worry about subClassOf loops... 26 | 27 | (defn specific-type? [kb child parent] 28 | (ask kb `((~child rdf/type ~parent)))) 29 | 30 | (def mem-specific-type? (memoize specific-type?)) 31 | 32 | (defn spec-of? 33 | ([child parent] (spec-of? *kb* child parent)) 34 | ([kb child parent] (mem-specific-type? kb child parent))) 35 | 36 | ;;; -------------------------------------------------------- 37 | ;;; reduce type sets 38 | ;;; -------------------------------------------------------- 39 | 40 | (defn remove-parent-types 41 | ([kb child parents] (remove-parent-types kb child parents false)) 42 | ([kb child parents return-set altered] 43 | (cond 44 | (not (seq parents)) 45 | [return-set altered] 46 | (spec-of? kb child (first parents)) 47 | (recur kb child (rest parents) return-set true) 48 | :else 49 | (recur kb child (rest parents) 50 | (conj return-set (first parents)) 51 | altered)))) 52 | 53 | (defn seperate-parent-child-neither 54 | ([kb type types] (seperate-parent-child-neither kb type types #{} #{} #{})) 55 | ([kb type types parent child neither] 56 | (cond 57 | (not (seq types)) 58 | [parent child neither] 59 | (= type (first type)) ;skip it 60 | (recur kb type (rest types) parent child neither) 61 | (spec-of? kb type (first types)) ;parent 62 | (recur kb type (rest types) (conj parent (first types)) child neither) 63 | (spec-of? kb (first types) type) ;child 64 | (recur kb type (rest types) parent (conj child (first types)) neither) 65 | :else 66 | (recur kb type (rest types) 67 | parent child (conj neither (first types)))))) 68 | 69 | (defn most-specific-types 70 | ([kb types] (most-specific-types kb types #{})) 71 | ([kb types return-types] 72 | (if (not (seq types)) 73 | return-types 74 | (let [[parent child neither] 75 | (seperate-parent-child-neither (first types) return-types)] 76 | (if (seq child) ; there's already more specific ones in there 77 | (recur kb (rest types) (union child neither)) ; cut this one 78 | ;;else child is nil, who cares about parents, add to neither set 79 | (recur kb (rest types) (conj neither (first types)))))))) 80 | 81 | ;;; -------------------------------------------------------- 82 | ;;; infer types 83 | ;;; -------------------------------------------------------- 84 | 85 | (defn infer-subject-types-from-pred [kb [s p o]] 86 | (query-template kb '?type `((~p rdfs/domain ?type)))) 87 | 88 | (defn infer-subject-types-from-obj [kb [s p o]] 89 | (if (= p 'rdf/type) 90 | (list o) 91 | '())) 92 | 93 | (defn infer-sym-type [kb sym assertion] 94 | (cond 95 | (= sym (first assertion)) 96 | (concat (infer-subject-types-from-pred kb assertion) 97 | (infer-subject-types-from-obj kb assertion)) 98 | ; if pred 99 | ; if object 100 | :else '())) ; it's not in this assertion 101 | 102 | 103 | (defn infer-types 104 | ([sym assertions] (infer-types *kb* sym assertions)) 105 | ([kb sym assertions] 106 | (most-specific-types (reduce concat 107 | (map (partial infer-sym-type kb sym) 108 | assertions))))) 109 | 110 | ;;; -------------------------------------------------------- 111 | ;;; assertion cluster 112 | ;;; -------------------------------------------------------- 113 | 114 | (defn assertion-keys [[sub pred obj :as assertion]] 115 | (list sub obj)) 116 | 117 | ;;list of assertion clusters in 118 | ;; returns list of list of assertion clusters that match and those that don't 119 | (defn matching-clusters [clusters keys] 120 | (let [key-set (set keys)] 121 | (reduce (fn [[match no-match] cluster] 122 | (if (some (partial some key-set) (map assertion-keys cluster)) 123 | [(conj match cluster) no-match] 124 | [match (conj no-match cluster)])) 125 | [() ()] 126 | clusters))) 127 | 128 | 129 | ;;list of assertions in - list of lists of assertions out 130 | (defn cluster-assertions [assertions] 131 | (reduce (fn [clusters assertion] 132 | (let [[matching rest-clusters] 133 | (matching-clusters clusters (assertion-keys assertion))] 134 | (conj rest-clusters 135 | (conj (apply concat matching) 136 | assertion)))) 137 | '() 138 | assertions)) 139 | 140 | (defn disjoint-assertions? [assertions] 141 | (< 1 (count (cluster-assertions assertions)))) 142 | 143 | ;;; -------------------------------------------------------- 144 | ;;; reification 145 | ;;; -------------------------------------------------------- 146 | 147 | 148 | (defn reify-assertions [kb assertions] 149 | (let [vars (variables assertions) 150 | var-instance-map 151 | (reduce conj {} 152 | (map (fn [var] 153 | [var (reify-instance kb 154 | (infer-types kb var assertions))]) 155 | vars))] 156 | (subst-bindings assertions var-instance-map))) 157 | 158 | 159 | 160 | ;;; -------------------------------------------------------- 161 | ;;; END 162 | ;;; -------------------------------------------------------- 163 | 164 | 165 | -------------------------------------------------------------------------------- /kr-jena/kr-jena-core/src/main/clojure/edu/ucdenver/ccp/kr/jena/sparql.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.jena.sparql 2 | (use edu.ucdenver.ccp.kr.variable 3 | ;[edu.ucdenver.ccp.kr.rdf :exclude (resource)] 4 | edu.ucdenver.ccp.kr.kb 5 | edu.ucdenver.ccp.kr.clj-ify 6 | edu.ucdenver.ccp.kr.rdf 7 | edu.ucdenver.ccp.kr.sparql 8 | edu.ucdenver.ccp.kr.jena.rdf) 9 | 10 | (import 11 | ;interfaces 12 | (com.hp.hpl.jena.query QueryFactory 13 | QueryExecutionFactory 14 | QueryExecution 15 | Query 16 | QuerySolution) 17 | 18 | )) 19 | 20 | 21 | ;;; -------------------------------------------------------- 22 | ;;; result processing helpsers 23 | ;;; -------------------------------------------------------- 24 | 25 | ;; (defn count-results [results] 26 | ;; (count (jena-iteration-seq results))) 27 | 28 | ;; (defn result-map [results] 29 | ;; (lazy-seq 30 | ;; ;(when-let [s (results-seq results)] 31 | ;; (when-let [s (seq results)] 32 | ;; (cons 33 | ;; (reduce conj {} (map (fn [binding] 34 | ;; (vector (variable (.getName binding)) 35 | ;; (clj-ify (.getValue binding)))) 36 | ;; (first s))) 37 | ;; (result-map (rest s)))))) 38 | 39 | ;;; -------------------------------------------------------- 40 | ;;; clj-ify 41 | ;;; -------------------------------------------------------- 42 | 43 | ;; (defmethod clj-ify org.openrdf.query.TupleQueryResult [results] 44 | ;; (result-map (jena-iteration-seq results))) 45 | 46 | ;; (defmethod clj-ify com.hp.hpl.jena.rdf.model.impl.LiteralImpl [kb l] 47 | ;; (.getValue l)) 48 | 49 | 50 | ;;; -------------------------------------------------------- 51 | ;;; main query helper 52 | ;;; -------------------------------------------------------- 53 | 54 | (defn jena-query-setup [kb query-string] 55 | (QueryExecutionFactory/create (QueryFactory/create query-string) 56 | (model! kb))) 57 | 58 | (defn jena-result-to-map [kb result] 59 | (let [vars (iterator-seq (.varNames result))] 60 | (reduce conj {} (map (fn [var] 61 | (vector (variable var) 62 | (clj-ify kb (.get result var)))) 63 | vars)))) 64 | 65 | ;;; -------------------------------------------------------- 66 | ;;; main queries 67 | ;;; -------------------------------------------------------- 68 | 69 | ;;this returns a boolean 70 | (defn jena-ask-sparql [kb query-string] 71 | (let [qexec (jena-query-setup kb query-string) 72 | result (.execAsk qexec)] 73 | (.close qexec) 74 | result)) 75 | 76 | ;;this returns a binding set iteration type thingy that can be clj-ified 77 | (defn jena-query-sparql [kb query-string] 78 | (let [qexec (jena-query-setup kb query-string)] 79 | (try 80 | (doall (map (partial jena-result-to-map kb) 81 | (iterator-seq (.execSelect qexec)))) 82 | (finally (.close qexec))))) 83 | 84 | (defn jena-count-sparql [kb query-string] 85 | (let [qexec (jena-query-setup kb query-string)] 86 | (try 87 | (count (iterator-seq (.execSelect qexec))) 88 | (finally (.close qexec))))) 89 | 90 | 91 | ;;TODO verify if this is correct? 92 | (defn jena-visit-sparql [kb visitor query-string] 93 | (let [qexec (jena-query-setup kb query-string)] 94 | (try 95 | (dorun (map (fn [result] 96 | (visitor (jena-result-to-map kb result))) 97 | (iterator-seq (.execSelect qexec)))) 98 | (finally (.close qexec))))) 99 | 100 | 101 | 102 | 103 | (defn jena-construct-sparql [kb sparql-string] 104 | (let [qexec (jena-query-setup kb sparql-string)] 105 | (try 106 | (doall (clj-ify kb (iterator-seq (.execConstructTriples qexec)))) 107 | ;; (map (partial clj-ify kb) 108 | ;; (iterator-seq (.execConstructTriples qexec)))) 109 | (finally (.close qexec))))) 110 | 111 | (defn jena-construct-visit-sparql [kb visitor sparql-string] 112 | (let [qexec (jena-query-setup kb sparql-string)] 113 | (try 114 | (dorun (map (fn [triple] 115 | (visitor (clj-ify kb triple))) 116 | (iterator-seq (.execConstructTriples qexec)))) 117 | (finally (.close qexec))))) 118 | 119 | 120 | 121 | 122 | 123 | 124 | ;;this returns a boolean 125 | (defn jena-ask-pattern [kb pattern & [options]] 126 | (jena-ask-sparql kb (sparql-ask-query pattern options))) 127 | ;; (let [qexec (jena-query-setup kb (sparql-ask-query pattern)) 128 | ;; result (.execAsk qexec)] 129 | ;; (.close qexec) 130 | ;; result)) 131 | 132 | ;;this returns a binding set iteration type thingy that can be clj-ified 133 | (defn jena-query-pattern [kb pattern & [options]] 134 | (jena-query-sparql kb (sparql-select-query pattern options))) 135 | ;; (let [qexec (jena-query-setup kb (sparql-select-query pattern))] 136 | ;; (try 137 | ;; (doall (map jena-result-to-map 138 | ;; (iterator-seq (.execSelect qexec)))) 139 | ;; (finally (.close qexec))))) 140 | 141 | (defn jena-count-pattern [kb pattern & [options]] 142 | (jena-count-sparql kb (sparql-select-query pattern options))) 143 | ;; (let [qexec (jena-query-setup kb (sparql-select-query pattern))] 144 | ;; (try 145 | ;; (count (iterator-seq (.execSelect qexec))) 146 | ;; (finally (.close qexec))))) 147 | ;; (count-results (jena-query-pattern kb pattern))) 148 | 149 | (defn jena-visit-pattern [kb visitor pattern & [options]] 150 | (jena-visit-sparql kb visitor (sparql-select-query pattern options))) 151 | 152 | (defn jena-construct-pattern [kb create-pattern pattern & [options]] 153 | (jena-construct-sparql kb 154 | (sparql-construct-query create-pattern 155 | pattern 156 | options))) 157 | 158 | (defn jena-construct-visit-pattern [kb visitor create-pattern pattern 159 | & [options]] 160 | (jena-construct-visit-sparql kb 161 | visitor 162 | (sparql-construct-query create-pattern 163 | pattern 164 | options))) 165 | 166 | 167 | 168 | ;;; -------------------------------------------------------- 169 | ;;; END 170 | ;;; -------------------------------------------------------- 171 | 172 | 173 | -------------------------------------------------------------------------------- /kr-core/src/main/clojure/edu/ucdenver/ccp/kr/rule_index.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.rule-index 2 | (use edu.ucdenver.ccp.kr.unify 3 | edu.ucdenver.ccp.kr.assertion 4 | edu.ucdenver.ccp.kr.variable 5 | edu.ucdenver.ccp.kr.kb 6 | edu.ucdenver.ccp.kr.clj-ify 7 | edu.ucdenver.ccp.kr.rdf 8 | edu.ucdenver.ccp.kr.sparql)) 9 | 10 | ;;; -------------------------------------------------------- 11 | ;;; 12 | ;;; -------------------------------------------------------- 13 | 14 | 15 | ;; rule standard keys: 16 | ;; :name 17 | ;; :head 18 | ;; :body 19 | ;; :direction 20 | ;; :preferred-reasoner 21 | 22 | ;;; -------------------------------------------------------- 23 | ;;; constants 24 | ;;; -------------------------------------------------------- 25 | 26 | ;;(def ^:dynamic *rule-ns* "http://kabob.ucdenver.edu/kr/rule/") 27 | (def ^:dynamic *rule-ns* "rule") 28 | (def ^:dynamic *var-ns* "var") 29 | 30 | ;;; -------------------------------------------------------- 31 | ;;; helpers 32 | ;;; -------------------------------------------------------- 33 | 34 | (defn rule-sym [name] 35 | (symbol *rule-ns* name)) 36 | 37 | ;;; -------------------------------------------------------- 38 | ;;; rule indexing 39 | ;;; -------------------------------------------------------- 40 | 41 | ;;this should probably be rolled into clj-ify ... 42 | (defn var-to-ns-var [var] 43 | (symbol *var-ns* (name var))) 44 | 45 | ;; (defn uri-to-var [uri] 46 | ;; (symbol 47 | 48 | (defn var-map [{head :head 49 | body :body 50 | :as rule}] 51 | (let [vars (variables (concat head body))] 52 | (reduce (fn [hash v] 53 | (conj hash [v (var-to-ns-var v)])) 54 | {} 55 | vars))) 56 | 57 | (defn var-triples [var-map] 58 | (map (fn [v] 59 | `(~v rdf/type var/Variable)) 60 | (map second var-map))) 61 | 62 | (defn reify-rule-triples [prefix triples] 63 | (map (fn [triple count] 64 | (let [stmt-sym (rule-sym (str prefix count))] 65 | [stmt-sym 66 | (reify-as triple stmt-sym)])) 67 | triples 68 | (range))) 69 | 70 | (defn head-triples [rule-sym 71 | {name :name 72 | head :head 73 | :as rule}] 74 | (mapcat (fn [[stmt triples]] 75 | (conj triples 76 | `(~rule-sym rule/hasHeadTriple ~stmt))) 77 | (reify-rule-triples (str name "-head-") head))) 78 | 79 | (defn body-triples [rule-sym 80 | {name :name 81 | body :body 82 | :as rule}] 83 | (mapcat (fn [[stmt triples]] 84 | (conj triples 85 | `(~rule-sym rule/hasBodyTriple ~stmt))) 86 | (reify-rule-triples (str name "-body-") body))) 87 | 88 | (defn rule-base-triples [rule-sym 89 | {name :name 90 | :as rule}] 91 | `((~rule-sym rdf/type rule/Rule) 92 | (~rule-sym rdfs/label ~name))) 93 | 94 | (defn index-triples [{name :name 95 | head :head 96 | body :body 97 | :as rule}] 98 | (let [rule-sym (rule-sym name) 99 | var-map (var-map rule)] 100 | ;[rule-sym 101 | (concat (rule-base-triples rule-sym rule) 102 | (var-triples var-map) 103 | (head-triples rule-sym 104 | (conj rule [:head (subst-bindings head var-map)])) 105 | (body-triples rule-sym 106 | (conj rule [:body (subst-bindings body var-map)]))) 107 | ;] 108 | )) 109 | 110 | (defn add-to-rule-index [kb rules] 111 | (dorun 112 | (map (fn [rule] 113 | (dorun 114 | (map (partial add kb) (index-triples rule)))) 115 | rules))) 116 | 117 | ;; (defn index-rule? [index {name :name 118 | ;; head :head 119 | ;; body :body 120 | ;; :as rule}] 121 | ;; ) 122 | 123 | ;;; -------------------------------------------------------- 124 | ;;; rule lookup 125 | ;;; -------------------------------------------------------- 126 | 127 | ;;this looks for rules that could generate more specific triples than 128 | ;; the one specified as the lookup triple 129 | ;; (defn triple-re-derive-query-body [[s p o :as triple]] 130 | ;; `((?rule rdf/type rule/Rule) 131 | ;; (?rule rule/hasHeadTriple ?head) 132 | ;; ~@(and (not (variable? s)) 133 | ;; `((?head rdf/subject ?sub) 134 | ;; (:union ~(list '= '?sub s);(= ?sub ~s)) 135 | ;; ((?sub rdf/type var/Variable)) 136 | ;; ((?sub rdf/type ~s)) 137 | ;; ((?sub rdfs/subClassOf ~s))))) 138 | ;; ;; ((~s rdf/type ?sub)) 139 | ;; ;; ((~s rdfs/subClassOf ?sub)) 140 | ;; ~@(and (not (variable? p)) 141 | ;; `((?head rdf/predicate ?pred) 142 | ;; ;;(:union ~(list '= '?pred p);(= ?pred ~p)) 143 | ;; (:union ~(list :sameTerm '?pred p);(= ?pred ~p)) 144 | ;; ((?pred rdf/type var/Variable)) 145 | ;; ((?pred rdfs/subPropertyOf ~p))))) 146 | ;; ~@(and (not (variable? o)) 147 | ;; `((?head rdf/object ?obj) 148 | ;; (:union ~(list '= '?obj o);(= ?obj ~o)) 149 | ;; ((?obj rdf/type var/Variable)) 150 | ;; ((?obj rdf/type ~o)) 151 | ;; ((?obj rdfs/subClassOf ~o))))))) 152 | 153 | (defn triple-re-derive-query-body [[s p o :as triple]] 154 | `((?rule rdf/type rule/Rule) 155 | (?rule rule/hasHeadTriple ?head) 156 | ~@(and (not (variable? s)) 157 | `((?head rdf/subject ?sub) 158 | (:union ((?head rdf/subject ?sub) 159 | (:sameTerm ?sub ~s)) 160 | ((?sub rdf/type var/Variable)) 161 | ((?sub rdf/type ~s)) 162 | ((?sub rdfs/subClassOf ~s))))) 163 | ;; ((~s rdf/type ?sub)) 164 | ;; ((~s rdfs/subClassOf ?sub)) 165 | ~@(and (not (variable? p)) 166 | `((?head rdf/predicate ?pred) 167 | (:union ((?head rdf/predicate ?pred) 168 | (:sameTerm ?pred ~p)) 169 | ((?pred rdf/type var/Variable)) 170 | ((?pred rdfs/subPropertyOf ~p))))) 171 | ~@(and (not (variable? o)) 172 | `((?head rdf/object ?obj) 173 | (:union ((?head rdf/object ?obj) 174 | (:sameTerm ?obj ~o)) 175 | ((?obj rdf/type var/Variable)) 176 | ((?obj rdf/type ~o)) 177 | ((?obj rdfs/subClassOf ~o))))))) 178 | 179 | 180 | ;;; -------------------------------------------------------- 181 | ;;; 182 | ;;; -------------------------------------------------------- 183 | 184 | ;;; -------------------------------------------------------- 185 | ;;; END 186 | ;;; -------------------------------------------------------- 187 | 188 | 189 | -------------------------------------------------------------------------------- /kr-core/src/main/clojure/edu/ucdenver/ccp/kr/rule.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.rule 2 | (use edu.ucdenver.ccp.utils 3 | edu.ucdenver.ccp.kr.unify 4 | edu.ucdenver.ccp.kr.assertion 5 | edu.ucdenver.ccp.kr.variable 6 | edu.ucdenver.ccp.kr.kb 7 | edu.ucdenver.ccp.kr.clj-ify 8 | edu.ucdenver.ccp.kr.rdf 9 | edu.ucdenver.ccp.kr.sparql 10 | [clojure.java.io :exclude (resource)] 11 | clojure.set 12 | clojure.pprint)) 13 | ;; (import java.io.PushbackReader)) 14 | 15 | ;;; -------------------------------------------------------- 16 | ;;; 17 | ;;; -------------------------------------------------------- 18 | 19 | 20 | ;; rule standard keys: 21 | ;; :name 22 | ;; :head 23 | ;; :body 24 | ;; :direction 25 | ;; :preferred-reasoner 26 | 27 | ;;; -------------------------------------------------------- 28 | ;;; constants 29 | ;;; -------------------------------------------------------- 30 | 31 | (def ^:dynamic *verify-on-read* nil) 32 | 33 | (def ^:dynamic *default-ok-ns* #{"_" "?"}) 34 | 35 | (def ^:dynamic *print-bad-rules* true) 36 | 37 | ;;; -------------------------------------------------------- 38 | ;;; helpers 39 | ;;; -------------------------------------------------------- 40 | 41 | ;; (defn read-rule [in] 42 | ;; '()) 43 | 44 | ;;this doesn't have to be a file, it can be anything that can be made reader 45 | (defn load-rules-from-file [file] 46 | (all-input file)) 47 | ;;(read-all-input file)) 48 | 49 | (defn directory? [f] 50 | ;;make sure it's not a string no .isDirectory on strings 51 | (and (instance? java.io.File f) 52 | (.isDirectory f))) 53 | 54 | (defn load-rules-from-directory [dir] 55 | (mapcat all-input 56 | (remove directory? 57 | (directory-seq dir)))) 58 | 59 | 60 | ;;loads from all the files that match the pattern 61 | (defn load-rules-from-classpath [pattern] 62 | ;;(mapcat read-all-input 63 | (mapcat all-input 64 | (remove directory? 65 | (classpath-matching pattern)))) 66 | 67 | 68 | 69 | ;; (with-open [r (PushbackReader. (reader file))] 70 | ;; (loop [form (read r nil r) 71 | ;; results nil] 72 | ;; (if (= form r) ; sentinal value stream can't read itself 73 | ;; results 74 | ;; (recur (read r nil r) (conj results form)))))) 75 | 76 | ;;; -------------------------------------------------------- 77 | ;;; static rule testing 78 | ;;; -------------------------------------------------------- 79 | 80 | (defn connected-rule? [{head :head 81 | body :body 82 | :as rule}] 83 | (and (not (disjoint-assertions? body)) 84 | (not (disjoint-assertions? head)) 85 | ;; can't just test the combination - 86 | ;; the head could make the body connected when it otherwise isn't 87 | (not (disjoint-assertions? (concat head body))))) 88 | 89 | ;;test if there's a var in the head not mentioned in the body 90 | (defn all-head-vars-in-body? [{head :head 91 | body :body 92 | :as rule}] 93 | (let [head-vars (variables head) 94 | body-vars (variables body)] 95 | (every? (set body-vars) head-vars))) 96 | 97 | (defn some-head-vars-in-body? [{head :head 98 | body :body 99 | :as rule}] 100 | (let [head-vars (variables head) 101 | body-vars (variables body)] 102 | (some (set head-vars) body-vars))) 103 | 104 | 105 | (defn all-ns-known? [safe-ns-set {head :head 106 | body :body 107 | :as rule}] 108 | (every? (fn [sym] 109 | ;;either the sym is a keyword or it's ns is in the set 110 | (or (keyword? sym) 111 | ((union *default-ok-ns* (set safe-ns-set)) 112 | (namespace sym)))) 113 | ;;get list of distinct symbols, remove all literals 114 | (remove (complement symbol?) 115 | (distinct (flatten (concat head body)))))) 116 | 117 | 118 | 119 | (defn bad-rules [test rules] 120 | (let [bad (remove test rules)] ; remove if you pass the test 121 | ;;print bad rules and meta data - TODO: this should go to logging 122 | (when *print-bad-rules* 123 | (doseq [rule bad] 124 | (println "rule failed test " (str test)) 125 | (pprint (meta rule)) 126 | (pprint rule))) 127 | bad)) ; return the bad rules 128 | 129 | ;;; -------------------------------------------------------- 130 | ;;; kb rule testing 131 | ;;; -------------------------------------------------------- 132 | 133 | (defn body-in-kb? [kb {body :body 134 | :as rule}] 135 | (ask kb body)) 136 | 137 | 138 | 139 | 140 | 141 | ;;; -------------------------------------------------------- 142 | ;;; 143 | ;;; -------------------------------------------------------- 144 | 145 | 146 | ;;; -------------------------------------------------------- 147 | ;;; simple forward rule 148 | ;;; -------------------------------------------------------- 149 | 150 | 151 | (defn apply-horn-rule 152 | "in prolog: head :- body (head is true if body is)" 153 | [head body] 154 | (map (fn [bindings] 155 | (subst-bindings head bindings)) 156 | (clj-ify (query body)))) 157 | 158 | 159 | 160 | (defn apply-horn-rule-and-refiy 161 | "in prolog: head :- body (head is true if body is)" 162 | [head body] 163 | (map (fn [bindings] 164 | (reify-assertions (subst-bindings head bindings))) 165 | (clj-ify (query body)))) 166 | 167 | 168 | (defn apply-post-processing-rule [kb {name :name 169 | template :query 170 | transform :post-process :as rule}] 171 | ;; print statement below for debugging purposes only - remove at any time 172 | ;;(prn (str "# query hits for rule " name ": " (.size (query kb template)))) 173 | (dorun 174 | (map (fn [bindings count] 175 | ;; this isn't running at the right time... 176 | ;(if (= 0 (mod count 1000)) 177 | ; (println count)) 178 | ;(dorun (map add! 179 | (transform bindings));)) 180 | (query kb template) 181 | (range)))) 182 | 183 | 184 | 185 | 186 | ;; (defn apply-multi-stage-horn-rule 187 | ;; "multiple bodies which are applied sequentially. 188 | ;; reification is done as necessary, however not to the first part. 189 | ;; note the parts are listed in reverse order to be consistent with 190 | ;; prolog head :- body ordering." 191 | ;; [& parts] 192 | ;; (apply-multi-stage-horn-rule-reversed (reverse parts) '())) 193 | 194 | ;; (map (fn [bindings] 195 | ;; (reify-assertions (subst-bindings head bindings))) 196 | ;; (clj-ify (query body)))) 197 | 198 | ;; (defun apply-multi-stage-horn-rule-reversed [parts bindings] 199 | ;; (let [[ 200 | 201 | 202 | 203 | 204 | ;; for things that need to be reified allow for a name template? 205 | ;; so that names that can be found / reused can be generated? 206 | ;; this is dangerously hacky from a KR perspective 207 | 208 | 209 | 210 | ;;; -------------------------------------------------------- 211 | ;;; 212 | ;;; -------------------------------------------------------- 213 | 214 | 215 | ;;; -------------------------------------------------------- 216 | ;;; END 217 | ;;; -------------------------------------------------------- 218 | 219 | 220 | -------------------------------------------------------------------------------- /kr-core/src/main/clojure/edu/ucdenver/ccp/utils.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.utils 2 | (use [clojure.java.io :exclude (resource)] 3 | clojure.java.classpath) 4 | (import java.io.InputStreamReader 5 | (java.util.jar JarFile JarEntry) 6 | java.security.MessageDigest 7 | java.math.BigInteger)) 8 | ;;org.apache.commons.codec.binary.Base64 9 | 10 | 11 | (def ^:dynamic *default-file-encoding* "UTF-8") 12 | 13 | (def ^:dynamic *dynamic-source-fn* (fn [source] 14 | (.endsWith (str source) ".clj"))) 15 | 16 | 17 | (defmacro time-multiple-value 18 | "Evaluates expr and returns vector [result time]." 19 | [expr] 20 | `(let [start# (. System (nanoTime)) 21 | ret# ~expr] 22 | [ret# (/ (double (- (. System (nanoTime)) start#)) 1000000.0)])) 23 | 24 | 25 | (defn uuid [] 26 | (str (java.util.UUID/randomUUID))) 27 | 28 | (defn md5 29 | ([x] (.toString 30 | (BigInteger. 1 31 | (.digest (doto (MessageDigest/getInstance "MD5") 32 | (.reset) 33 | (.update (.getBytes (str x) "UTF-8"))))) 34 | 16)) 35 | ([x & rest] (md5 (apply str x rest)))) 36 | 37 | ;;need a set of flags to control encoding: 38 | ;; hex, 32, 36, 64 (+/- url safe) 39 | ;; 40 | 41 | ;; (defn sha-1 42 | ;; ([x] (Base64/encodeBase64URLSafeString 43 | ;; ^bytes (.digest (doto (MessageDigest/getInstance "SHA1") 44 | ;; (.reset) 45 | ;; (.update (.getBytes (str x) "UTF-8")))))) 46 | ;; ([x & rest] (sha-1 (apply str x rest)))) 47 | 48 | 49 | 50 | (defn read-eval-with-sentinel [reader] 51 | (let [val (read reader nil reader)] 52 | (if (= val reader) 53 | reader 54 | (eval val)))) 55 | 56 | ;;(declare read-eval-with-sentinel) 57 | (def ^:dynamic *dynamic-reader-fn* read-eval-with-sentinel) 58 | 59 | 60 | (defn nonempty-seq [x] 61 | "returns x as a seq if it's a non-empty seq otherwise nil/false" 62 | (and (coll? x) (seq x))) 63 | 64 | 65 | (defmacro defn-special-binding [fn-name default-specials args & body] 66 | (let [special-vars (vec (doall (map gensym default-specials)))] 67 | `(defn ~fn-name 68 | (~args ~@body) 69 | (~(vec (concat special-vars args)) 70 | (binding ~(vec (interleave default-specials special-vars)) 71 | ~@body))))) 72 | 73 | ;; (defn classpath-seq [] 74 | ;; (cond 75 | ;; (jar-file? cp-part) (filenames-in-jar (JarFile. cp-part)) 76 | ;; ;;call recursively? 77 | ;; (.isDirectory cp-part) (let [children (.listFiles cp-part)] 78 | ;; (concat children 79 | ;; (mapcat sub-directories! 80 | ;; children))) 81 | ;; (nil? cp-part) nil 82 | ;; :else (list cp-part))) 83 | 84 | ;; (defn directory-seq [dir] 85 | ;; (lazy-seq 86 | ;; (when-let [children (seq (.listFiles dir))] 87 | ;; (cons dir (mapcat directory-seq children))))) 88 | 89 | (defn directory-seq 90 | ([dir] 91 | (lazy-seq 92 | (let [children (seq (.listFiles (file dir)))] 93 | (concat children (mapcat directory-seq children))))) 94 | ([dir filter-re] 95 | (remove (fn [f] 96 | (not (re-find filter-re (str f)))) 97 | (directory-seq dir)))) 98 | 99 | 100 | 101 | (defn classpath-seq [] 102 | (distinct 103 | (mapcat (fn [cp-part] 104 | (cond 105 | ;;(or 106 | (jar-file? cp-part) 107 | ;; (let [f (file cp-part)] 108 | ;; (and (.isFile f) 109 | ;; (.endsWith (.getName f) "jar")))) 110 | (map (partial str "@") 111 | (filenames-in-jar (JarFile. cp-part))) 112 | (.isDirectory cp-part) (cons cp-part 113 | (directory-seq cp-part)) 114 | (nil? cp-part) nil 115 | :else (list cp-part))) 116 | (classpath)))) 117 | 118 | 119 | (defn classpath-matching [path-part] 120 | (remove (fn [file] 121 | (not (.contains (str file) path-part))) 122 | (classpath-seq))) 123 | 124 | 125 | (defn resource-reader [resource-name] 126 | (let [path (if (.startsWith resource-name "@") 127 | (.substring resource-name 128 | (if (.startsWith resource-name "@/") 2 1)) 129 | resource-name)] 130 | (InputStreamReader. 131 | (ClassLoader/getSystemResourceAsStream path) 132 | *default-file-encoding*))) 133 | 134 | (defn reader! 135 | "makes a reader one way or the other out of it's input. 136 | if it's a string that starts with @ then it's assumed a resource on CP." 137 | [input] 138 | (if (and (string? input) 139 | (.startsWith input "@")) 140 | (resource-reader input) 141 | (reader input))) 142 | 143 | (defn read-all-input [source] 144 | (with-open [r (clojure.lang.LineNumberingPushbackReader. (reader! source))] 145 | (loop [start (.getLineNumber r) 146 | form (read r nil r) 147 | results nil] 148 | (if (= form r) ; sentinal value stream can't read itself 149 | results ; return results 150 | (let [end (.getLineNumber r)] 151 | (recur end 152 | (read r nil r) 153 | (conj results (with-meta form 154 | ;; str-ify source to prevent holding pointer 155 | {:source (str source) 156 | :start-line start 157 | :end-line end})))))))) 158 | 159 | ;; (defn read-eval-with-sentinel [reader] 160 | ;; (let [val (read reader nil reader)] 161 | ;; (if (= val reader) 162 | ;; reader 163 | ;; (eval val)))) 164 | 165 | ;; (defn read-eval-all-input [source dynamic-reader-fn] 166 | ;; (with-open [r (clojure.lang.LineNumberingPushbackReader. (reader! source))] 167 | ;; (loop [start (.getLineNumber r) 168 | ;; form (read-eval-with-sentinel r) 169 | ;; results nil] 170 | ;; (if (= form r) ; sentinal value stream can't read itself 171 | ;; results ; return results 172 | ;; (let [end (.getLineNumber r)] 173 | ;; (recur end 174 | ;; (read-eval-with-sentinel r) 175 | ;; (conj results (with-meta form 176 | ;; ;; str-ify source to prevent holding pointer 177 | ;; {:source (str source) 178 | ;; :start-line start 179 | ;; :end-line end})))))))) 180 | 181 | (defn read-dynamic-all-input [source dynamic-reader-fn] 182 | (with-open [r (clojure.lang.LineNumberingPushbackReader. (reader! source))] 183 | (loop [start (.getLineNumber r) 184 | form (dynamic-reader-fn r) 185 | results nil] 186 | (if (= form r) ; sentinal value stream can't read itself 187 | results ; return results 188 | (let [end (.getLineNumber r)] 189 | (recur end 190 | (dynamic-reader-fn r) 191 | (conj results (with-meta form 192 | ;; str-ify source to prevent holding pointer 193 | {:source (str source) 194 | :start-line start 195 | :end-line end})))))))) 196 | 197 | (defn all-input 198 | ([source] (all-input source *dynamic-reader-fn*)) 199 | ([source modified-dynamic-reader-fn] 200 | (if (*dynamic-source-fn* source) 201 | (read-dynamic-all-input source modified-dynamic-reader-fn) 202 | (read-all-input source)))) 203 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | 7 | Eclipse Public License 1.0 8 | http://opensource.org/licenses/eclipse-1.0.php 9 | repo 10 | 11 | 12 | 13 | 14 | 1.4.0 15 | UTF-8 16 | 17 | 2.10.1 18 | 19 | 2.6.10 20 | 21 | 22 | 1.2.17 23 | 1.6.5 24 | 25 | 26 | edu.ucdenver.ccp 27 | 1.4.20-SNAPSHOT 28 | kr 29 | pom 30 | 31 | ${project.artifactId} 32 | knowledge representation and reasoning tools 33 | 34 | 35 | kr-core 36 | kr-jena 37 | kr-sesame 38 | kr-backend 39 | 40 | 41 | 42 | 43 | 44 | scm:git:https://github.com/drlivingston/kr.git 45 | scm:git:https://github.com/drlivingston/kr.git 46 | https://github.com/drlivingston/kr 47 | HEAD 48 | 49 | 50 | 51 | 58 | 59 | clojars.org 60 | Clojars Repository 61 | https://clojars.org/repo/ 62 | 63 | 64 | 65 | 66 | 67 | 68 | org.clojure 69 | clojure 70 | ${clojure.version} 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | org.clojure 79 | java.classpath 80 | 0.1.0 81 | 82 | 83 | 84 | 85 | org.openrdf.sesame 86 | sesame-runtime 87 | 90 | ${ver.sesame} 91 | jar 92 | 93 | 94 | 95 | org.openrdf.sesame 96 | sesame-queryresultio 97 | ${ver.sesame} 98 | 99 | pom 100 | 101 | 102 | org.openrdf.sesame 103 | sesame-queryresultio-sparqlxml 104 | ${ver.sesame} 105 | 106 | jar 107 | 108 | 109 | org.openrdf.sesame 110 | sesame-queryresultio-binary 111 | ${ver.sesame} 112 | 113 | jar 114 | 115 | 116 | 117 | org.apache.jena 118 | jena-arq 119 | ${ver.arq} 120 | 121 | 122 | 123 | log4j 124 | log4j 125 | ${ver.log4j} 126 | 127 | 128 | 130 | 131 | commons-logging 132 | commons-logging 133 | 1.1.1 134 | 135 | 136 | commons-codec 137 | commons-codec 138 | 1.6 139 | 140 | 141 | 142 | 143 | com.stuartsierra 144 | dependency 145 | 0.1.1 146 | 147 | 148 | 149 | org.slf4j 150 | slf4j-log4j12 151 | ${ver.slf4j} 152 | 153 | 154 | 155 | junit 156 | junit 157 | 3.8.1 158 | test 159 | 160 | 161 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | clojars 176 | http://clojars.org/repo 177 | 178 | 179 | 180 | 181 | 182 | 183 | src/main/clojure 184 | 185 | 186 | 187 | 188 | 189 | com.theoryinpractise 190 | clojure-maven-plugin 191 | 1.3.9 192 | 193 | true 194 | 195 | 196 | compile-clojure 197 | compile 198 | 199 | compile 200 | 201 | 202 | 203 | 204 | test 205 | test 206 | 207 | test 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 221 | 222 | 223 | 224 | org.apache.maven.plugins 225 | maven-jar-plugin 226 | 2.3.1 227 | 228 | 229 | test 230 | 231 | test-jar 232 | 233 | 234 | 235 | 236 | src/test/clojure/ 237 | 238 | 239 | 240 | 241 | org.apache.maven.plugins 242 | maven-release-plugin 243 | 2.4.2 244 | 245 | 246 | org.apache.maven.scm 247 | maven-scm-provider-gitexe 248 | 1.9 249 | 250 | 251 | 252 | false 253 | true 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | -------------------------------------------------------------------------------- /kr-core/src/test/clojure/edu/ucdenver/ccp/test/kr/test_sparql_property_paths.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.test-sparql-property-paths 2 | (use clojure.test 3 | edu.ucdenver.ccp.test.kr.test-kb 4 | edu.ucdenver.ccp.test.kr.test-sparql 5 | 6 | edu.ucdenver.ccp.kr.variable 7 | edu.ucdenver.ccp.kr.kb 8 | edu.ucdenver.ccp.kr.rdf 9 | edu.ucdenver.ccp.kr.sparql) 10 | (import java.net.URI)) 11 | 12 | 13 | 14 | ;; (def test-triples-6-1 15 | ;; '((ex/a rdf/type foaf/Person ) 16 | ;; (ex/a foaf/name "Alice" ) 17 | ;; (ex/a foaf/mbox "" ) 18 | ;; (ex/a foaf/mbox "" ) 19 | 20 | ;; (ex/b rdf/type foaf/Person ) 21 | ;; (ex/b foaf/name "Bob" ))) 22 | 23 | ;; (def test-triples-numbers-equality 24 | ;; '((ex/a foaf/givenname "Alice" ) 25 | ;; (ex/a foaf/surname "Hacker" ) 26 | ;; (ex/a foaf/age [40 xsd/integer]) 27 | 28 | ;; (ex/b foaf/firstname "Bob" ) 29 | ;; (ex/b foaf/surname "Hacker" ) 30 | ;; (ex/b foaf/age 40) ;the default should be xsd/integer 31 | 32 | ;; (ex/c foaf/firstname "Fred" ) 33 | ;; (ex/c foaf/surname "Hacker" ) 34 | ;; (ex/c foaf/age [50 xsd/integer]))) 35 | 36 | (def test-triples-property-paths 37 | '((ex/a foaf/firstname "Alice" ) 38 | (ex/a foaf/surname "Hacker" ) 39 | (ex/a foaf/age [40 xsd/integer]) 40 | 41 | (ex/b foaf/firstname "Bob" ) 42 | (ex/b foaf/surname "Hacker" ) 43 | (ex/b foaf/age 40) ;the default should be xsd/integer 44 | 45 | (ex/c foaf/firstname "Fred" ) 46 | (ex/c foaf/surname "Hacker" ) 47 | (ex/c foaf/age [50 xsd/integer]) 48 | 49 | (ex/a foaf/knows ex/b) 50 | (ex/b foaf/knows ex/c) 51 | )) 52 | 53 | 54 | (kb-test test-basic-symbol-pattern test-triples-property-paths 55 | (is (= 1 56 | (count 57 | (query '((ex/a foaf/knows ?/person)))))) 58 | (is (= 2 59 | (count 60 | (query '((ex/a [foaf/knows +] ?/person)))))) 61 | (is (= 3 ;can bind to self 62 | (count 63 | (query '((ex/a [foaf/knows *] ?/person)))))) 64 | (is (= 2 ;can bind to self 65 | (count 66 | (query '((ex/a [foaf/knows ?] ?/person))))))) 67 | 68 | (kb-test test-basic-sequence-pattern test-triples-property-paths 69 | (is (= 1 70 | (count 71 | (query '((ex/a (foaf/knows foaf/age) ?/age)))))) 72 | 73 | (is (= 2 74 | (count 75 | (query '((ex/a ([foaf/knows +] foaf/age) ?/age)))))) 76 | 77 | (is (= 3 ;; alice knows alice, alice knows bob, bob knows himself 78 | (count 79 | (query '((?/person ([foaf/knows *] foaf/age) 40)))))) 80 | 81 | (is ;; alice knows alice, alice knows bob, 82 | (ask '((ex/a ([foaf/knows *] foaf/age) 40)))) 83 | 84 | (is ;; alice knows bob, 85 | (ask '((ex/a ([foaf/knows +] foaf/age) 40)))) 86 | 87 | (is (= 1 88 | (count 89 | (query '((?/person ([foaf/knows +] foaf/age) 40))))))) 90 | 91 | 92 | (kb-test test-basic-symbol-pattern-uris test-triples-property-paths 93 | (is (= 1 94 | (count 95 | (query `((ex/a ~(URI. "http://xmlns.com/foaf/0.1/knows") ?/person)))))) 96 | (is (= 2 97 | (count 98 | (query `((ex/a 99 | [~(URI. "http://xmlns.com/foaf/0.1/knows") +] 100 | ?/person)))))) 101 | (is (= 3 ;can bind to self 102 | (count 103 | (query `((ex/a 104 | [~(URI. "http://xmlns.com/foaf/0.1/knows") *] 105 | ?/person)))))) 106 | (is (= 2 ;can bind to self 107 | (count 108 | (query `((ex/a 109 | [~(URI. "http://xmlns.com/foaf/0.1/knows") ?] 110 | ?/person))))))) 111 | 112 | (kb-test test-basic-sequence-pattern-uris test-triples-property-paths 113 | (is (= 1 114 | (count 115 | (query `((ex/a 116 | (~(URI. "http://xmlns.com/foaf/0.1/knows") foaf/age) 117 | ?/age)))))) 118 | 119 | (is (= 2 120 | (count 121 | (query `((ex/a 122 | ([~(URI. "http://xmlns.com/foaf/0.1/knows") +] foaf/age) 123 | ?/age)))))) 124 | 125 | (is (= 3 ;; alice knows alice, alice knows bob, bob knows himself 126 | (count 127 | (query `((?/person 128 | ([~(URI. "http://xmlns.com/foaf/0.1/knows") *] foaf/age) 129 | 40)))))) 130 | 131 | (is ;; alice knows alice, alice knows bob, 132 | (ask `((ex/a 133 | ([~(URI. "http://xmlns.com/foaf/0.1/knows") *] foaf/age) 134 | 40)))) 135 | 136 | (is ;; alice knows bob, 137 | (ask `((ex/a 138 | ([~(URI. "http://xmlns.com/foaf/0.1/knows") +] foaf/age) 139 | 40)))) 140 | 141 | (is (= 1 142 | (count 143 | (query `((?/person 144 | ([~(URI. "http://xmlns.com/foaf/0.1/knows") +] foaf/age) 145 | 40))))))) 146 | 147 | (kb-test test-basic-sequence-pattern-uris-2 test-triples-property-paths 148 | (is (= 1 149 | (count 150 | (query `((ex/a 151 | (~(URI. "http://xmlns.com/foaf/0.1/knows") 152 | ~(URI. "http://xmlns.com/foaf/0.1/age")) 153 | ?/age)))))) 154 | 155 | (is (= 2 156 | (count 157 | (query `((ex/a 158 | ([~(URI. "http://xmlns.com/foaf/0.1/knows") +] 159 | ~(URI. "http://xmlns.com/foaf/0.1/age")) 160 | ?/age)))))) 161 | 162 | (is (= 3 ;; alice knows alice, alice knows bob, bob knows himself 163 | (count 164 | (query `((?/person 165 | ([~(URI. "http://xmlns.com/foaf/0.1/knows") *] 166 | ~(URI. "http://xmlns.com/foaf/0.1/age")) 167 | 40)))))) 168 | 169 | (is ;; alice knows alice, alice knows bob, 170 | (ask `((ex/a 171 | ([~(URI. "http://xmlns.com/foaf/0.1/knows") *] 172 | ~(URI. "http://xmlns.com/foaf/0.1/age")) 173 | 40)))) 174 | 175 | (is ;; alice knows bob, 176 | (ask `((ex/a 177 | ([~(URI. "http://xmlns.com/foaf/0.1/knows") +] 178 | ~(URI. "http://xmlns.com/foaf/0.1/age")) 179 | 40)))) 180 | 181 | (is (= 1 182 | (count 183 | (query `((?/person 184 | ([~(URI. "http://xmlns.com/foaf/0.1/knows") +] 185 | ~(URI. "http://xmlns.com/foaf/0.1/age")) 186 | 40))))))) 187 | 188 | 189 | ;; (kb-test test-construct-visit-pattern test-triples-6-1 190 | ;; (let [results (atom '())] 191 | ;; (construct-visit (fn [[s p o :as triple]] 192 | ;; (is (= 3 (count triple))) 193 | ;; (is (= 'rdf/type p)) 194 | ;; (is (= 'ex/Human o)) 195 | ;; (swap! results conj triple)) 196 | ;; '((?/person rdf/type ex/Human)) 197 | ;; '((?/person rdf/type foaf/Person))) 198 | ;; (is (= 2 (count @results))))) 199 | 200 | 201 | ;; (kb-test test-construct-visit-literals test-triples-6-1 202 | ;; (let [results (atom '())] 203 | ;; (construct-visit (fn [[s p o :as triple]] 204 | ;; (is (= 3 (count triple))) 205 | ;; (is (= 'foaf/age p)) 206 | ;; (is (= 40 o)) 207 | ;; (swap! results conj triple)) 208 | ;; '((?/person foaf/age 40)) 209 | ;; '((?/person rdf/type foaf/Person))) 210 | ;; (is (= 2 (count @results))))) 211 | 212 | 213 | ;; (kb-test test-construct-visit-literals-both-sides test-triples-numbers-equality 214 | ;; (let [results (atom '())] 215 | ;; (construct-visit (fn [[s p o :as triple]] 216 | ;; (is (= 3 (count triple))) 217 | ;; (is (= 'ex/age p)) 218 | ;; (is (or (= 40 o) 219 | ;; (= 50 o))) 220 | ;; (swap! results conj triple)) 221 | ;; '((?/person ex/age ?/age)) 222 | ;; '((?/person foaf/surname ?/name) 223 | ;; (?/person foaf/age ?/age))) 224 | ;; (is (= 3 (count @results))))) 225 | -------------------------------------------------------------------------------- /kr-sesame/kr-sesame-core/src/main/clojure/edu/ucdenver/ccp/kr/sesame/sparql.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.sesame.sparql 2 | (use edu.ucdenver.ccp.kr.variable 3 | edu.ucdenver.ccp.kr.kb 4 | edu.ucdenver.ccp.kr.clj-ify 5 | edu.ucdenver.ccp.kr.rdf 6 | edu.ucdenver.ccp.kr.sparql 7 | edu.ucdenver.ccp.kr.sesame.rdf) 8 | 9 | (import org.openrdf.model.impl.StatementImpl 10 | org.openrdf.model.impl.URIImpl 11 | 12 | org.openrdf.repository.Repository 13 | org.openrdf.repository.http.HTTPRepository 14 | org.openrdf.repository.http.HTTPTupleQuery 15 | org.openrdf.repository.http.HTTPBooleanQuery 16 | org.openrdf.repository.RepositoryConnection 17 | org.openrdf.query.BooleanQuery 18 | org.openrdf.query.TupleQuery 19 | org.openrdf.query.GraphQuery 20 | 21 | 22 | org.openrdf.query.TupleQueryResult 23 | org.openrdf.query.QueryLanguage 24 | org.openrdf.query.resultio.TupleQueryResultParserRegistry 25 | org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLParserFactory 26 | org.openrdf.query.resultio.TupleQueryResultFormat 27 | org.openrdf.query.TupleQueryResultHandlerBase 28 | 29 | org.openrdf.rio.helpers.RDFHandlerBase 30 | 31 | )) 32 | 33 | 34 | (defn count-results 35 | ([results] (count-results results 0)) 36 | ([results count] 37 | (if (.hasNext results) 38 | (do (.next results) 39 | (recur results (+ 1 count))) 40 | count))) 41 | 42 | 43 | ;; (defn results-seq [results] 44 | ;; (lazy-seq 45 | ;; (when (.hasNext results) 46 | ;; (cons 47 | ;; (.next results) 48 | ;; (results-seq results))))) 49 | 50 | ;;TODO? are these even useful? 51 | 52 | (defn result-to-map [kb result] 53 | (reduce conj {} (map (fn [binding] 54 | (vector (variable (.getName binding)) 55 | (clj-ify kb (.getValue binding)))) 56 | result))) 57 | 58 | (defn get-results-for-key [key results] 59 | (set 60 | (map (fn [result] 61 | (result key)) 62 | results))) 63 | 64 | ;;; -------------------------------------------------------- 65 | ;;; result processing helpsers 66 | ;;; -------------------------------------------------------- 67 | 68 | (defn count-results [results] 69 | (count (sesame-iteration-seq results))) 70 | 71 | (defn clj-ify-bindings [kb bindings] 72 | (reduce (fn [results binding] 73 | (conj results 74 | (vector (variable (.getName binding)) 75 | (clj-ify kb (.getValue binding))))) 76 | {} 77 | bindings)) 78 | 79 | (defn result-map [kb results] 80 | (lazy-seq 81 | ;(when-let [s (results-seq results)] 82 | (when-let [s (seq results)] 83 | (cons 84 | (reduce conj {} (map (fn [binding] 85 | (vector (variable (.getName binding)) 86 | (clj-ify kb (.getValue binding)))) 87 | (first s))) 88 | (result-map kb (rest s)))))) 89 | 90 | ;;TODO 91 | ;; the above can be made to capture the kb if made like the following 92 | ;; (defn bar 93 | ;; ([l] (bar *foo* l)) 94 | ;; ([x l] 95 | ;; (lazy-seq 96 | ;; (when-let [s (seq l)] 97 | ;; (cons (list x (first s)) 98 | ;; (bar x (rest s))))))) 99 | 100 | 101 | ;;; -------------------------------------------------------- 102 | ;;; clj-ify 103 | ;;; -------------------------------------------------------- 104 | 105 | (defmethod clj-ify org.openrdf.query.TupleQueryResult [kb results] 106 | (result-map kb (sesame-iteration-seq results))) 107 | 108 | 109 | (defmethod clj-ify org.openrdf.query.GraphQueryResult [kb results] 110 | (let [r (doall (map (partial clj-ify kb) (sesame-iteration-seq results)))] 111 | (.close results) 112 | r)) 113 | 114 | 115 | ;;; -------------------------------------------------------- 116 | ;;; main queries 117 | ;;; -------------------------------------------------------- 118 | 119 | ;;this returns a boolean 120 | (defn sesame-ask-sparql [kb query-string] 121 | (.evaluate ^BooleanQuery 122 | (.prepareBooleanQuery ^RepositoryConnection (connection! kb) 123 | QueryLanguage/SPARQL 124 | query-string))) 125 | 126 | (defn sesame-query-sparql [kb query-string] 127 | ;(prn query-string) 128 | (let [tuplequery (.prepareTupleQuery ^RepositoryConnection (connection! kb) 129 | QueryLanguage/SPARQL 130 | query-string)] 131 | (.setIncludeInferred tuplequery *use-inference*) 132 | (clj-ify kb (.evaluate ^TupleQuery tuplequery)))) 133 | 134 | 135 | (defn sesame-visit-sparql [kb visitor query-string] 136 | (let [tuplequery (.prepareTupleQuery ^RepositoryConnection (connection! kb) 137 | QueryLanguage/SPARQL 138 | query-string)] 139 | (.setIncludeInferred tuplequery *use-inference*) 140 | (.evaluate ^TupleQuery tuplequery 141 | (proxy [TupleQueryResultHandlerBase] [] 142 | (handleSolution [bindings] 143 | (visitor (clj-ify-bindings kb bindings))))))) 144 | 145 | (defn sesame-visit-count-sparql [kb query-string] 146 | (let [count (atom 0) 147 | tuplequery (.prepareTupleQuery ^RepositoryConnection (connection! kb) 148 | QueryLanguage/SPARQL 149 | query-string)] 150 | (.setIncludeInferred tuplequery *use-inference*) 151 | (.evaluate ^TupleQuery tuplequery 152 | (proxy [TupleQueryResultHandlerBase] [] 153 | (handleSolution [bindings] 154 | (swap! count inc)))) 155 | @count)) 156 | 157 | 158 | 159 | (defn sesame-count-sparql [kb query-string] 160 | (sesame-visit-count-sparql kb query-string)) 161 | 162 | (defn sesame-count-1-1 [kb pattern options] 163 | ;;TODO this is rediculous, really? there's nothing better? 164 | ;; why is the number a raw string? (is this true of all stores?) 165 | ;;(read-string 166 | (second 167 | (first 168 | (first 169 | (sesame-query-sparql kb 170 | (sparql-1-1-count-query pattern options))))));) 171 | 172 | 173 | (defn sesame-construct-sparql [kb sparql-string] 174 | ;(prn query-string) 175 | (let [graphquery (.prepareGraphQuery ^RepositoryConnection (connection! kb) 176 | QueryLanguage/SPARQL 177 | sparql-string)] 178 | (.setIncludeInferred graphquery *use-inference*) 179 | (clj-ify kb (.evaluate ^GraphQuery graphquery)))) 180 | ;;(.evaluate ^GraphQuery graphquery))) 181 | 182 | 183 | (defn sesame-construct-visit-sparql [kb visitor sparql-string] 184 | (let [graphquery (.prepareGraphQuery ^RepositoryConnection (connection! kb) 185 | QueryLanguage/SPARQL 186 | sparql-string)] 187 | (.setIncludeInferred graphquery *use-inference*) 188 | (.evaluate ^GraphQuery graphquery 189 | (proxy [RDFHandlerBase] [] 190 | (handleStatement [stmt] 191 | (visitor (clj-ify kb stmt))))))) 192 | 193 | 194 | 195 | 196 | ;;this returns a boolean 197 | (defn ^boolean sesame-ask-pattern [kb pattern & [options]] 198 | (sesame-ask-sparql kb (sparql-ask-query pattern options))) 199 | 200 | (defn sesame-query-pattern [kb pattern & [options]] 201 | (sesame-query-sparql kb (sparql-select-query pattern options))) 202 | 203 | (defn sesame-count-pattern [kb pattern & [options]] 204 | ;;check if 1.1 support is available and if so try that way 205 | (if (has-feature? kb sparql-1-1) ;use fast 1.1 query 206 | (sesame-count-1-1 kb pattern options) 207 | ;;otherwise turn and burn 208 | (sesame-count-sparql kb (sparql-select-query pattern options)))) 209 | 210 | (defn sesame-visit-pattern [kb visitor pattern & [options]] 211 | (sesame-visit-sparql kb visitor (sparql-select-query pattern options))) 212 | 213 | (defn sesame-construct-pattern [kb create-pattern pattern & [options]] 214 | (sesame-construct-sparql kb 215 | (sparql-construct-query create-pattern 216 | pattern 217 | options))) 218 | 219 | (defn sesame-construct-visit-pattern [kb visitor create-pattern pattern 220 | & [options]] 221 | (sesame-construct-visit-sparql kb 222 | visitor 223 | (sparql-construct-query create-pattern 224 | pattern 225 | options))) 226 | 227 | ;;; -------------------------------------------------------- 228 | ;;; END 229 | ;;; -------------------------------------------------------- 230 | 231 | 232 | -------------------------------------------------------------------------------- /kr-jena/kr-jena-core/src/main/clojure/edu/ucdenver/ccp/kr/jena/kb.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.jena.kb 2 | (use edu.ucdenver.ccp.kr.kb 3 | edu.ucdenver.ccp.kr.rdf 4 | edu.ucdenver.ccp.kr.sparql 5 | edu.ucdenver.ccp.kr.jena.rdf 6 | edu.ucdenver.ccp.kr.jena.sparql) 7 | (import com.hp.hpl.jena.query.DatasetFactory 8 | com.hp.hpl.jena.sparql.core.DatasetImpl 9 | com.hp.hpl.jena.graph.Graph 10 | com.hp.hpl.jena.rdf.model.Model)) 11 | 12 | ;;; -------------------------------------------------------- 13 | ;;; specials and types 14 | ;;; -------------------------------------------------------- 15 | 16 | (def ^:dynamic *default-model-type* :rdf) ; :rdfs and :owl supported 17 | 18 | ;;; -------------------------------------------------------- 19 | ;;; triple store connection and setup 20 | ;;; -------------------------------------------------------- 21 | 22 | (declare new-jena-model 23 | jena-connection) 24 | 25 | (defn jena-initialize [kb] 26 | (synch-ns-mappings kb) 27 | kb) 28 | 29 | ;;; -------------------------------------------------------- 30 | ;;; protocols and records 31 | ;;; -------------------------------------------------------- 32 | 33 | ;;this is nonsese becasue to the circular defintions 34 | ;; and what can and cannot be forward delcared 35 | (declare new-jena-connection 36 | close-existing-jena-connection) 37 | 38 | (defn jena-connection [kb] 39 | (new-jena-model kb)) 40 | 41 | (defn close-jena-connection [kb] 42 | (close-existing-jena-connection kb)) 43 | 44 | 45 | ;;; KB 46 | ;;; -------------------------------------------------------- 47 | 48 | ;;there could be different types for different backing-stores TDB/SDB 49 | 50 | (defrecord JenaKB [dataset model-type kb-features] 51 | KB 52 | 53 | (native [kb] dataset) 54 | (initialize [kb] (jena-initialize kb)) 55 | (open [kb] (new-jena-connection kb)) 56 | (close [kb] (close-jena-connection kb)) 57 | 58 | rdfKB 59 | 60 | (root-ns-map [kb] (jena-server-ns-map kb)) 61 | ;; (ns-maps [kb] ns-maps-var) 62 | ;; (ns-map-to-short [kb] (:ns-map-to-short (deref ns-maps-var))) 63 | ;; (ns-map-to-long [kb] (:ns-map-to-long (deref ns-maps-var))) 64 | ;; (ns-map-to-short [kb] (:ns-map-to-short (deref (ns-maps kb))))) 65 | ;; (ns-map-to-long [kb] (:ns-map-to-long (deref (ns-maps kb))))) 66 | (register-ns [kb short long] (jena-register-ns kb short long)) 67 | 68 | (create-resource [kb name] (jena-create-resource kb name)) 69 | (create-property [kb name] (jena-create-property kb name)) 70 | (create-literal [kb val] (jena-create-literal kb val)) 71 | (create-literal [kb val type] (jena-create-literal kb val type)) 72 | 73 | ;;TODO convert to creating proper string literals 74 | ;; (create-string-literal [kb str] (jena-create-string-iteral kb val)) 75 | ;; (create-string-literal [kb str lang] 76 | ;; (jena-create-string literal kb val type)) 77 | (create-string-literal [kb str] (jena-create-literal kb str)) 78 | (create-string-literal [kb str lang] 79 | (jena-create-literal kb str lang)) 80 | 81 | 82 | (create-blank-node [kb name] (jena-create-blank-node kb name)) 83 | (create-statement [kb s p o] (jena-create-statement kb s p o)) 84 | 85 | (add-statement [kb stmt] (jena-add-statement kb stmt)) 86 | (add-statement [kb stmt context] (jena-add-statement kb stmt context)) 87 | (add-statement [kb s p o] (jena-add-statement kb s p o)) 88 | (add-statement [kb s p o context] (jena-add-statement kb s p o context)) 89 | (add-statements [kb stmts] (jena-add-statements kb stmts)) 90 | (add-statements [kb stmts context] (jena-add-statements kb stmts context)) 91 | 92 | ;; (ask-statement [kb stmt] (jena-ask-statement kb stmt)) 93 | (ask-statement [kb s p o context] (jena-ask-statement kb s p o context)) 94 | ;; (query-statement [kb stmt] (jena-query-statement kb stmt)) 95 | (query-statement [kb s p o context] (jena-query-statement kb s p o context)) 96 | 97 | 98 | (load-rdf-file [kb file] (jena-load-rdf-file kb file)) 99 | (load-rdf-file [kb file type] (jena-load-rdf-file kb file type)) 100 | 101 | ;;the following will throw exception for unknown rdf format 102 | (load-rdf-stream [kb stream] (jena-load-rdf-stream kb stream)) 103 | 104 | (load-rdf-stream [kb stream type] (jena-load-rdf-stream kb stream type)) 105 | 106 | 107 | sparqlKB 108 | 109 | (ask-pattern [kb pattern] 110 | (jena-ask-pattern kb pattern)) 111 | (ask-pattern [kb pattern options] 112 | (jena-ask-pattern kb pattern options)) 113 | 114 | (query-pattern [kb pattern] 115 | (jena-query-pattern kb pattern)) 116 | (query-pattern [kb pattern options] 117 | (jena-query-pattern kb pattern options)) 118 | 119 | (count-pattern [kb pattern] 120 | (jena-count-pattern kb pattern)) 121 | (count-pattern [kb pattern options] 122 | (jena-count-pattern kb pattern options)) 123 | 124 | (visit-pattern [kb visitor pattern] 125 | (jena-visit-pattern kb visitor pattern)) 126 | (visit-pattern [kb visitor pattern options] 127 | (jena-visit-pattern kb visitor pattern options)) 128 | 129 | (construct-pattern [kb create-pattern pattern] 130 | (jena-construct-pattern kb create-pattern pattern)) 131 | (construct-pattern [kb create-pattern pattern options] 132 | (jena-construct-pattern kb create-pattern pattern options)) 133 | (construct-visit-pattern [kb visitor create-pattern pattern] 134 | (jena-construct-visit-pattern kb visitor create-pattern pattern)) 135 | (construct-visit-pattern [kb visitor create-pattern pattern options] 136 | (jena-construct-visit-pattern kb visitor create-pattern pattern options)) 137 | 138 | 139 | (ask-sparql [kb query-string] 140 | (jena-ask-sparql kb query-string)) 141 | (query-sparql [kb query-string] 142 | (jena-query-sparql kb query-string)) 143 | (count-sparql [kb query-string] 144 | (jena-count-sparql kb query-string)) 145 | (visit-sparql [kb visitor query-string] 146 | (jena-visit-sparql kb visitor query-string)) 147 | 148 | (construct-sparql [kb sparql-string] 149 | (jena-construct-sparql kb sparql-string)) 150 | (construct-visit-sparql [kb visitor sparql-string] 151 | (jena-construct-visit-sparql kb visitor sparql-string)) 152 | 153 | 154 | 155 | ) 156 | 157 | ;;models: 158 | 159 | ;; createOntologyModel() Creates an ontology model which is in-memory and presents OWL ontologies. 160 | 161 | ;; createOntologyModel(OntModelSpec spec, Model base) Creates an ontology model according the OntModelSpec spec which presents the ontology of base. 162 | 163 | ;; createOntologyModel(OntModelSpec spec, ModelMaker maker, Model base) Creates an OWL ontology model according to the spec over the base model. If the ontology model needs to construct additional models (for OWL imports), use the ModelMaker to create them. [The previous method will construct a MemModelMaker for this.] 164 | 165 | ;; Where do OntModelSpecs come from? There's a cluster of constants in the class which provide for common uses; to name but three: - OntModelSpec.OWL_MEM_RDFS_INF OWL ontologies, model stored in memory, using RDFS entailment only 166 | 167 | ;; OntModelSpec.RDFS_MEM RDFS ontologies, in memory, but doing no additional inferences 168 | 169 | ;; OntModelSpec.OWL_DL_MEM_RULE_INF OWL ontologies, in memory, with the full OWL Lite inference 170 | 171 | 172 | 173 | ;;; "constructors" 174 | ;;; -------------------------------------------------------- 175 | 176 | ;; the way new JenaKBConnection is being called it isn't preserving 177 | ;; the additional keys that are added on to the jena server 178 | ;; specifically the :value-factory 179 | 180 | (defn copy-jena-slots [target-kb source-kb] 181 | (copy-rdf-slots (copy-kb-slots target-kb source-kb) 182 | source-kb)) 183 | 184 | 185 | (defn new-jena-kb 186 | ([] (new-jena-kb (com.hp.hpl.jena.query.DatasetFactory/createMem))) 187 | ([dataset] 188 | (jena-initialize 189 | (initialize-ns-mappings 190 | (JenaKB. dataset *default-model-type* nil))))) 191 | 192 | (defn jena-kb-from-model [model] 193 | ;;creates a dataset with this model as the default model 194 | (let [dataset (com.hp.hpl.jena.query.DatasetFactory/create model)] 195 | (new-jena-kb dataset))) 196 | 197 | ;; (defn new-jena-server [model-factory] 198 | ;; (JenaKB. (:server model-factory) (initial-ns-mappings) nil)) 199 | ;; (let [repository (HTTPRepository. *default-server* *repository-name*)] 200 | ;; (.setPreferredTupleQueryResultFormat repository 201 | ;; TupleQueryResultFormat/SPARQL) 202 | ;; (if (and *username* *password*) 203 | ;; (.setUsernameAndPassword repository *username* *password*)) 204 | ;; (assoc (JenaKB. repository (initial-ns-mappings)) 205 | ;; :value-factory (.getValueFactory repository)))) 206 | 207 | ;;Jena uses locking symantics and supports multiple reader single writer 208 | ;; semantics --- this is currently unhandled with un-predicable results 209 | (defn new-jena-connection [kb] 210 | kb) 211 | 212 | (defmethod kb :jena-mem [_] 213 | (new-jena-kb)) 214 | 215 | (defmethod kb com.hp.hpl.jena.sparql.core.DatasetImpl [arg] 216 | (if (class? arg) 217 | (new-jena-kb) 218 | (new-jena-kb arg))) 219 | 220 | (defmethod kb com.hp.hpl.jena.rdf.model.ModelCon [arg] 221 | (if (class? arg) 222 | (new-jena-kb) 223 | (jena-kb-from-model arg))) 224 | 225 | 226 | (defn close-existing-jena-connection [kb] 227 | (when (:dataset kb) 228 | (doseq [model (iterator-seq (.listNames (:dataset kb)))] 229 | (.close (.getNamedModel (:dataset kb) name))) 230 | (.close (.getDefaultModel (:dataset kb))) 231 | (.close (:dataset kb))) 232 | (copy-jena-slots (JenaKB. nil (:model-type kb) nil) 233 | kb)) 234 | 235 | ;;; -------------------------------------------------------- 236 | ;;; END 237 | ;;; -------------------------------------------------------- 238 | 239 | -------------------------------------------------------------------------------- /kr-sesame/kr-sesame-core/src/main/clojure/edu/ucdenver/ccp/kr/sesame/rdf.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.sesame.rdf 2 | (use edu.ucdenver.ccp.kr.variable 3 | edu.ucdenver.ccp.kr.kb 4 | edu.ucdenver.ccp.kr.clj-ify 5 | [edu.ucdenver.ccp.kr.rdf :exclude (resource)] 6 | clojure.java.io) 7 | (import 8 | java.io.IOException 9 | 10 | ;interfaces 11 | org.openrdf.model.URI 12 | org.openrdf.model.Resource 13 | org.openrdf.model.Statement 14 | org.openrdf.model.Literal 15 | 16 | ;Classes 17 | org.openrdf.model.impl.StatementImpl 18 | org.openrdf.model.impl.URIImpl 19 | org.openrdf.model.impl.LiteralImpl 20 | 21 | org.openrdf.repository.Repository 22 | org.openrdf.repository.http.HTTPRepository 23 | org.openrdf.repository.RepositoryConnection 24 | org.openrdf.query.resultio.TupleQueryResultFormat 25 | org.openrdf.rio.RDFFormat)) 26 | 27 | ;;; -------------------------------------------------------- 28 | ;;; specials 29 | ;;; -------------------------------------------------------- 30 | 31 | ;;; -------------------------------------------------------- 32 | ;;; helpers 33 | ;;; -------------------------------------------------------- 34 | 35 | (defn sesame-iteration-seq [results] 36 | (lazy-seq 37 | (when (.hasNext results) 38 | (cons 39 | (.next results) 40 | (sesame-iteration-seq results))))) 41 | 42 | 43 | ;;; -------------------------------------------------------- 44 | ;;; sesame specific connection 45 | ;;; -------------------------------------------------------- 46 | 47 | (defn connection! [kb] 48 | ^RepositoryConnection (:connection (connection kb))) 49 | 50 | ;;; -------------------------------------------------------- 51 | ;;; graphs and models 52 | ;;; -------------------------------------------------------- 53 | 54 | ;;; -------------------------------------------------------- 55 | ;;; namespaces 56 | ;;; -------------------------------------------------------- 57 | 58 | (defn sesame-register-ns [kb short long] 59 | (.setNamespace (connection! kb) short long)) 60 | 61 | ;;TODO? use clj-ify under the hood? 62 | (defn sesame-server-ns-map [kb] 63 | (reduce (fn [m ns] 64 | (assoc m (.getPrefix ns) (.getName ns))) 65 | {} 66 | (doall 67 | (sesame-iteration-seq 68 | (.getNamespaces (connection! kb)))))) 69 | 70 | ;;; -------------------------------------------------------- 71 | ;;; sesame-ify 72 | ;;; -------------------------------------------------------- 73 | 74 | (defn sesame-uri [uri-string] 75 | (URIImpl. uri-string)) 76 | ;;maybe this shoudl be 77 | ;; (.createURI (kb :value-factory) uri-string)) 78 | 79 | (defn sesame-create-resource 80 | [kb r] 81 | ^Resource (sesame-uri r)) 82 | 83 | (defn sesame-create-property 84 | [kb p] 85 | (sesame-uri p)) 86 | 87 | (defn sesame-create-blank-node 88 | [kb id] 89 | (.createBNode (:value-factory kb) id)) 90 | 91 | (defn sesame-create-literal 92 | ([kb l] 93 | (.createLiteral (:value-factory kb) l)) 94 | ([kb s type-or-lang] 95 | ;(println kb s type-or-lang) 96 | (.createLiteral (:value-factory kb) 97 | s 98 | (if (string? type-or-lang) 99 | type-or-lang 100 | (edu.ucdenver.ccp.kr.rdf/resource kb type-or-lang))))) 101 | 102 | 103 | (defn sesame-create-statement 104 | [kb s p o] 105 | (StatementImpl. s p o)) 106 | 107 | (defn sesame-context-array 108 | ([] (make-array Resource 0)) 109 | ([kb c] (if c 110 | (let [a (make-array Resource 1)] 111 | (aset a 0 ^Resource (edu.ucdenver.ccp.kr.rdf/resource kb c)) 112 | ;;(sesame-uri (resource-ify c))) 113 | a) 114 | (sesame-context-array))) 115 | ([kb c & rest] (let [a (make-array Resource (+ 1 (count rest)))] 116 | (map (fn [v i] 117 | (aset a i (edu.ucdenver.ccp.kr.rdf/resource kb v))) 118 | (cons c rest) 119 | (range)) 120 | a))) 121 | 122 | ;;; -------------------------------------------------------- 123 | ;;; clj-ify 124 | ;;; -------------------------------------------------------- 125 | 126 | 127 | ;;; literal-types 128 | ;;; -------------------------------------------------------- 129 | 130 | 131 | (defmulti literal-clj-ify (fn [kb l] 132 | (let [t (.getDatatype l)] 133 | (and t (.toString t))))) 134 | 135 | (defmethod literal-clj-ify :default [kb l] 136 | (.stringValue l)) 137 | 138 | ;;for some reason strings come back as nil which is different than :default 139 | (defmethod literal-clj-ify nil [kb l] 140 | (.stringValue l)) 141 | 142 | ;; need to flesh this out and test... 143 | 144 | ;; (defmethod literal-clj-ify "http://www.w3.org/2001/XMLSchema#integer" [kb l] 145 | ;; (.intValue l)) 146 | 147 | (defmacro lit-clj-ify [type & body] 148 | `(defmethod literal-clj-ify ~type ~(vec '(kb l)) 149 | ~@body)) 150 | 151 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#boolean" (.booleanValue l)) 152 | 153 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#int" (.intValue l)) 154 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#integer" (.integerValue l)) 155 | 156 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#long" (.longValue l)) 157 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#float" (.floatValue l)) 158 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#double" (.doubleValue l)) 159 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#decimal" (.decimalValue l)) 160 | 161 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#dateTime" (.calendarValue l)) 162 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#time" (.calendarValue l)) 163 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#date" (.calendarValue l)) 164 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#gYearMonth" (.calendarValue l)) 165 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#gMonthDay" (.calendarValue l)) 166 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#gYear" (.calendarValue l)) 167 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#gMonth" (.calendarValue l)) 168 | (lit-clj-ify "http://www.w3.org/2001/XMLSchema#gDay" (.calendarValue l)) 169 | 170 | 171 | (defn literal-to-string-value [kb l] 172 | (.stringValue l)) 173 | ;;(str l)) 174 | 175 | (defn literal-to-value [kb l] 176 | (literal-clj-ify kb l)) 177 | 178 | (defn literal-language [l] 179 | (let [lang (.getLanguage l)] 180 | (if (= "" lang) 181 | nil 182 | lang))) 183 | 184 | 185 | (defn literal-type-or-language [kb l] 186 | (or (let [dt (.getDatatype l)] 187 | (and dt 188 | (clj-ify kb dt))) 189 | ;;(convert-string-to-sym kb dt))) 190 | (literal-language l))) 191 | ;;(.getLanguage l))) 192 | 193 | (defn literal-to-clj [kb l] 194 | (clj-ify-literal kb l 195 | literal-to-value 196 | literal-to-string-value 197 | literal-type-or-language)) 198 | 199 | ;;; clj-ify 200 | ;;; -------------------------------------------------------- 201 | 202 | (defmethod clj-ify org.openrdf.model.URI [kb r] 203 | (if (or (= "" (.getLocalName r)) 204 | (= "" (.getNamespace r))) 205 | (convert-string-to-sym kb (.stringValue r)) 206 | (convert-string-to-sym kb 207 | (.stringValue r) 208 | (.getNamespace r) 209 | (.getLocalName r)))) 210 | 211 | (defmethod clj-ify org.openrdf.model.Resource [kb r] 212 | (convert-string-to-sym kb (.stringValue r))) 213 | 214 | (defmethod clj-ify org.openrdf.model.BNode [kb bnode] 215 | (symbol *anon-ns-name* (.stringValue bnode))) 216 | 217 | 218 | ;;need to get numbers back out of literals 219 | ;;(defmethod clj-ify org.openrdf.model.impl.LiteralImpl [kb v] 220 | (defmethod clj-ify org.openrdf.model.Literal [kb v] 221 | (literal-to-clj kb v)) 222 | ;;(literal-clj-ify kb v)) 223 | 224 | 225 | (defmethod clj-ify org.openrdf.model.Value [kb v] 226 | (.stringValue v)) 227 | 228 | (prefer-method clj-ify [org.openrdf.model.Literal] [org.openrdf.model.Value]) 229 | 230 | 231 | (defmethod clj-ify org.openrdf.model.Statement [kb s] 232 | (list (clj-ify kb (.getSubject s)) 233 | (clj-ify kb (.getPredicate s)) 234 | (clj-ify kb (.getObject s)))) 235 | 236 | (defmethod clj-ify org.openrdf.repository.RepositoryResult [kb results] 237 | (map (partial clj-ify kb) 238 | (sesame-iteration-seq results))) 239 | 240 | ;;; -------------------------------------------------------- 241 | ;;; adding 242 | ;;; -------------------------------------------------------- 243 | 244 | (defn sesame-add-statement 245 | ([kb stmt] (.add (connection! kb) 246 | ^Statment stmt 247 | (sesame-context-array))) ;;(make-array Resource 0))) 248 | ([kb stmt context] (.add (connection! kb) 249 | ^Statement stmt 250 | (sesame-context-array kb context))) 251 | ([kb s p o] (.add (connection! kb) 252 | ^Statement (statement kb s p o) 253 | (sesame-context-array))) 254 | ([kb s p o context] (.add (connection! kb) 255 | ^Statement (statement kb s p o) 256 | ;;^Resource s p o 257 | (sesame-context-array kb context)))) 258 | 259 | 260 | (defn sesame-add-statements 261 | ([kb stmts] (.add (connection! kb) 262 | ^Iterable (map (fn [s] 263 | (apply statement kb s)) 264 | stmts) 265 | (sesame-context-array))) 266 | ([kb stmts context] (.add (connection! kb) 267 | ^Iterable (map (fn [s] 268 | (apply statement kb s)) 269 | stmts) 270 | (sesame-context-array kb context)))) 271 | 272 | (defmulti convert-to-sesame-type identity) 273 | (defmethod convert-to-sesame-type :n3 [sym] RDFFormat/N3) 274 | (defmethod convert-to-sesame-type :ntriple [sym] RDFFormat/NTRIPLES) 275 | (defmethod convert-to-sesame-type :rdfxml [sym] RDFFormat/RDFXML) 276 | (defmethod convert-to-sesame-type :trig [sym] RDFFormat/TRIG) 277 | (defmethod convert-to-sesame-type :trix [sym] RDFFormat/TRIX) 278 | (defmethod convert-to-sesame-type :turtle [sym] RDFFormat/TURTLE) 279 | 280 | (defn sesame-load-rdf-file 281 | ([kb file] 282 | (.add (connection! kb) 283 | file 284 | "" ;nil ;"" 285 | (RDFFormat/forFileName (.getName file)) 286 | (sesame-context-array kb *graph*))) 287 | ([kb file type] 288 | (.add (connection! kb) 289 | file 290 | "" ;nil ;"" 291 | (convert-to-sesame-type type) 292 | (sesame-context-array kb *graph*)))) 293 | 294 | (defn sesame-load-rdf-stream 295 | ([kb stream] 296 | (throw (IOException. "Unknown RDF format type for stream."))) 297 | ([kb stream type] 298 | (.add (connection! kb) 299 | stream 300 | "" ;nil ;"" 301 | (convert-to-sesame-type type) 302 | (sesame-context-array kb *graph*)))) 303 | 304 | 305 | ;;; -------------------------------------------------------- 306 | ;;; querying 307 | ;;; -------------------------------------------------------- 308 | 309 | (defn sesame-ask-statement 310 | ([kb s p o context] 311 | (.hasStatement ^RepositoryConnection (connection! kb) 312 | ^Resource (and s (edu.ucdenver.ccp.kr.rdf/resource kb s)) 313 | ^URI (and p (property kb p)) 314 | ^Value (and o (object kb o)) 315 | *use-inference* 316 | (sesame-context-array kb context)))) 317 | 318 | (defn sesame-query-statement 319 | ([kb s p o context] 320 | (clj-ify kb 321 | (.getStatements ^RepositoryConnection (connection! kb) 322 | ^Resource (and s (edu.ucdenver.ccp.kr.rdf/resource kb s)) 323 | ^URI (and p (property kb p)) 324 | ^Value (and o (object kb o)) 325 | *use-inference* 326 | (sesame-context-array kb context))))) 327 | 328 | ;;; -------------------------------------------------------- 329 | ;;; END 330 | ;;; -------------------------------------------------------- 331 | -------------------------------------------------------------------------------- /kr-core/src/main/clojure/edu/ucdenver/ccp/kr/forward_rule.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.forward-rule 2 | (use edu.ucdenver.ccp.utils 3 | edu.ucdenver.ccp.kr.unify 4 | edu.ucdenver.ccp.kr.assertion 5 | edu.ucdenver.ccp.kr.variable 6 | edu.ucdenver.ccp.kr.reify 7 | edu.ucdenver.ccp.kr.kb 8 | edu.ucdenver.ccp.kr.clj-ify 9 | edu.ucdenver.ccp.kr.rdf 10 | edu.ucdenver.ccp.kr.sparql 11 | edu.ucdenver.ccp.kr.rule 12 | [clojure.java.io :exclude (resource)] 13 | clojure.set 14 | clojure.pprint) 15 | (require [com.stuartsierra.dependency :as dep])) 16 | ;; (import java.io.PushbackReader)) 17 | 18 | ;;; -------------------------------------------------------- 19 | ;;; 20 | ;;; -------------------------------------------------------- 21 | 22 | ;;; -------------------------------------------------------- 23 | ;;; constants 24 | ;;; -------------------------------------------------------- 25 | 26 | ;;; -------------------------------------------------------- 27 | ;;; helpers 28 | ;;; -------------------------------------------------------- 29 | 30 | (defn reify-variables [{reify :reify :as rule}] 31 | (map (fn [entry] 32 | (if (sequential? entry) 33 | (first entry) 34 | entry)) 35 | reify)) 36 | 37 | ;;; -------------------------------------------------------- 38 | ;;; static rule testing 39 | ;;; -------------------------------------------------------- 40 | 41 | (defn all-head-vars-in-body-sans-reify-vars? [{head :head 42 | body :body 43 | ;reify :reify 44 | :as rule}] 45 | (let [head-vars (variables head) 46 | body-vars (variables body)] 47 | (every? (set body-vars) 48 | (remove (set (reify-variables rule)) head-vars)))) 49 | 50 | ;;need to add tests - all-reify-vars-in-head 51 | ;; all-head-vars-not-in-body-in-reify 52 | 53 | (defn all-reify-vars-in-head? [{head :head :as rule}] 54 | (let [head-vars (variables head) 55 | reify-vars (reify-variables rule)] 56 | (every? (set head-vars) reify-vars))) 57 | 58 | (defn all-head-vars-not-in-body-in-reify? [{head :head 59 | body :body 60 | :as rule}] 61 | (let [head-vars (variables head) 62 | body-vars (variables body) 63 | reify-vars (reify-variables rule)] 64 | (every? (set reify-vars) 65 | (remove (set body-vars) head-vars)))) 66 | 67 | 68 | (defn forward-safe? [rule] 69 | (every? (fn [test] 70 | (test rule)) 71 | (list connected-rule? 72 | all-head-vars-in-body?))) 73 | 74 | (defn forward-safe-with-reification? [rule] 75 | (every? (fn [test] 76 | (test rule)) 77 | (list connected-rule? 78 | all-reify-vars-in-head? 79 | all-head-vars-not-in-body-in-reify? 80 | all-head-vars-in-body-sans-reify-vars?))) 81 | 82 | 83 | 84 | ;;; -------------------------------------------------------- 85 | ;;; kb rule testing 86 | ;;; -------------------------------------------------------- 87 | 88 | 89 | 90 | ;;; -------------------------------------------------------- 91 | ;;; reification 92 | ;;; -------------------------------------------------------- 93 | 94 | ;;; reify from rule expression 95 | ;;; -------------------------------------------------------- 96 | 97 | (defmacro with-reify-name-bindings [name-bindings & body] 98 | `(binding [*reify-ns* (or (:ns ~name-bindings) *reify-ns*) 99 | *reify-prefix* (or (:prefix ~name-bindings) *reify-prefix*) 100 | *reify-suffix* (or (:suffix ~name-bindings) *reify-suffix*)] 101 | ~@body)) 102 | 103 | 104 | ;;form [var {ns-name :ns 105 | ;; prefix :prefix suffix :suffix 106 | ;; l-name :ln <> 107 | ;; :unique 108 | ;; :localname [vars ..] 109 | ;; :md5 [vars ..] 110 | ;; :regex [match replace vars..] 111 | ;; :fn (fn [bindings] ..) 112 | ;; :as reify-opts} 113 | 114 | ;; this will take the above type of form and return a function 115 | ;; that will function to refify symbols for that form when passed the bindings 116 | 117 | (defmulti reify-rule-form-fn 118 | (fn [rule reify-form] 119 | (cond 120 | (not (sequential? reify-form)) :default 121 | (not (map? (second reify-form))) :default ;is this actually an error? 122 | (keyword? (:ln (second reify-form))) (:ln (second reify-form)) 123 | :else (first (:ln (second reify-form)))))) 124 | 125 | 126 | (defn extend-reify-map [reify-opts fn & [dependency-vars]] 127 | (assoc reify-opts 128 | :reify-fn fn 129 | :dependencies dependency-vars)) 130 | 131 | (defmethod reify-rule-form-fn :default [rule [var reify-opts]] 132 | (extend-reify-map reify-opts 133 | (fn [bindings] 134 | (with-reify-name-bindings reify-opts 135 | (reify-unique))))) 136 | 137 | (defmethod reify-rule-form-fn :unique [rule [var reify-opts]] 138 | (extend-reify-map reify-opts 139 | (fn [bindings] 140 | (with-reify-name-bindings reify-opts 141 | (reify-unique))))) 142 | 143 | (defmethod reify-rule-form-fn :localname [rule 144 | [var {[fn-name & params] :ln 145 | :as reify-opts}]] 146 | (extend-reify-map reify-opts 147 | (fn [bindings] 148 | (with-reify-name-bindings reify-opts 149 | (apply reify-localname (subst-bindings params bindings)))) 150 | (variables params))) 151 | 152 | (defmethod reify-rule-form-fn :md5 [rule 153 | [var {[fn-name & params] :ln 154 | :as reify-opts}]] 155 | (extend-reify-map reify-opts 156 | (fn [bindings] 157 | (with-reify-name-bindings reify-opts 158 | (apply reify-md5 (subst-bindings params bindings)))) 159 | (variables params))) 160 | 161 | (defmethod reify-rule-form-fn :regex [rule 162 | [var {[fn-name match replace & vars] :ln 163 | :as reify-opts}]] 164 | (extend-reify-map reify-opts 165 | (fn [bindings] 166 | (with-reify-name-bindings reify-opts 167 | (apply reify-regex match replace 168 | (subst-bindings vars bindings)))) 169 | ;;(apply reify-regex match replace (map bindings vars)))) 170 | (variables vars))) 171 | 172 | 173 | (defmethod reify-rule-form-fn :fn [rule 174 | [var {[fn-name fcn] :ln 175 | :as reify-opts}]] 176 | (extend-reify-map reify-opts 177 | (fn [bindings] 178 | (with-reify-name-bindings reify-opts 179 | (reify-sym (fcn bindings)))))) 180 | ;; ideally these should be rigged to go last 181 | ;; just move them last after? 182 | ;; pull them all off and put them on at the end 183 | 184 | 185 | (defn default-reify-rule-form-fn [] 186 | (extend-reify-map {} 187 | (fn [bindings] 188 | (reify-unique)))) 189 | 190 | 191 | (defn reification-dependencies [reify-list] 192 | (mapcat (fn [[v {deps :dependencies}]] 193 | (concat `((~v ~nil)) 194 | (map (partial list v) deps))) 195 | reify-list)) 196 | 197 | (defn sort-reification-based-on-dependencies [reify-list] 198 | (let [original-map (reduce (fn [m [v options]] 199 | (assoc m v options)) 200 | {} 201 | reify-list)] 202 | (remove (fn [[var reify-def]] 203 | (nil? reify-def)) 204 | (map (fn [var] 205 | (vector var (original-map var))) 206 | (dep/topo-sort 207 | (reduce (fn [graph [var dependency]] 208 | (dep/depend graph var dependency)) 209 | (dep/graph) 210 | (reification-dependencies reify-list))))))) 211 | 212 | 213 | (defn add-reify-fns [{reify :reify :as rule}] 214 | (assoc rule 215 | :reify 216 | (sort-reification-based-on-dependencies 217 | (map (fn [entry] 218 | (if (sequential? entry) 219 | (let [[var opts :as form] entry] 220 | [var (reify-rule-form-fn rule form)]) 221 | (vector entry (default-reify-rule-form-fn)))) 222 | reify)))) 223 | 224 | ;;; -------------------------------------------------------- 225 | ;;; forward chaining 226 | ;;; -------------------------------------------------------- 227 | 228 | ;; the variables in the reification section aren't actually independent 229 | ;; some of the variables being reified rely on other variables that 230 | ;; need to be reified. so they need to be ordered or managed in some way 231 | 232 | (defn reify-bindings [reify-with-fns bindings] 233 | (reduce (fn [new-bindings [var {reify-fn :reify-fn}]] 234 | ;;check for key in bindings already (rule out optionals) 235 | (if (new-bindings var) 236 | new-bindings 237 | (assoc new-bindings var (reify-fn new-bindings)))) 238 | bindings 239 | reify-with-fns)) 240 | 241 | ;;instantiates a rule and puts the triples in the target kb 242 | (defn run-forward-rule [source-kb target-kb source-rule] 243 | (let [{head :head 244 | body :body 245 | reify :reify 246 | :as rule} (add-reify-fns source-rule)] 247 | (pprint rule) 248 | (query-visit source-kb 249 | (fn [bindings] 250 | (dorun 251 | (map (partial add! target-kb) 252 | (doall 253 | (subst-bindings head 254 | ;;bindings 255 | (reify-bindings reify 256 | bindings)))))) 257 | body ;need to add reify find clauses on optionally 258 | {:select-vars (concat (variables head) 259 | (variables reify))}))) 260 | 261 | 262 | (defn ask-forward-rule [source-kb {head :head 263 | body :body 264 | :as rule}] 265 | (ask source-kb body)) 266 | 267 | (defn count-forward-rule [source-kb {head :head 268 | body :body 269 | :as rule}] 270 | (query-count source-kb 271 | body 272 | {:select-vars (variables head)})) 273 | 274 | 275 | 276 | ;; (query-visit source-kb 277 | ;; (fn [bindings] 278 | ;; (prn bindings) 279 | ;; (dorun (map (fn [triple] 280 | ;; (prn triple) 281 | ;; (add! target-kb triple)) 282 | ;; ;;(partial add! target-kb) 283 | ;; (dorun (map (fn [triple] 284 | ;; (prn triple) 285 | ;; (statement kb triple)) 286 | ;; ;;(partial statement kb) 287 | ;; (reify-assertions 288 | ;; (subst-bindings head bindings))))))) 289 | ;; body)) 290 | 291 | 292 | 293 | ;;; -------------------------------------------------------- 294 | ;;; 295 | ;;; -------------------------------------------------------- 296 | 297 | 298 | 299 | ;;; -------------------------------------------------------- 300 | ;;; 301 | ;;; -------------------------------------------------------- 302 | 303 | 304 | ;;; -------------------------------------------------------- 305 | ;;; END 306 | ;;; -------------------------------------------------------- 307 | 308 | 309 | -------------------------------------------------------------------------------- /kr-sesame/kr-sesame-core/src/main/clojure/edu/ucdenver/ccp/kr/sesame/kb.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.kr.sesame.kb 2 | (use ;edu.ucdenver.ccp.kr.variable 3 | edu.ucdenver.ccp.kr.kb 4 | edu.ucdenver.ccp.kr.rdf 5 | edu.ucdenver.ccp.kr.sparql 6 | edu.ucdenver.ccp.kr.sesame.rdf 7 | edu.ucdenver.ccp.kr.sesame.sparql 8 | ;; [edu.ucdenver.ccp.kr.rdf :exclude (resource)] 9 | ;; clojure.java.io 10 | ) 11 | (import 12 | org.openrdf.model.URI 13 | org.openrdf.model.Resource 14 | org.openrdf.model.Statement 15 | 16 | org.openrdf.model.impl.StatementImpl 17 | org.openrdf.model.impl.URIImpl 18 | 19 | org.openrdf.repository.Repository 20 | org.openrdf.repository.http.HTTPRepository 21 | org.openrdf.repository.RepositoryConnection 22 | 23 | org.openrdf.repository.sail.SailRepository; 24 | org.openrdf.sail.memory.MemoryStore; 25 | 26 | org.openrdf.query.resultio.TupleQueryResultFormat 27 | )) 28 | 29 | ;;; -------------------------------------------------------- 30 | ;;; specials 31 | ;;; -------------------------------------------------------- 32 | 33 | (def ^:dynamic *default-server* nil) 34 | (def ^:dynamic *repository-name* nil) 35 | (def ^:dynamic *username* nil) 36 | (def ^:dynamic *password* nil) 37 | 38 | (def ^:dynamic *kb-features* (list sparql-1-0 sparql-1-1)) 39 | 40 | 41 | ;;; -------------------------------------------------------- 42 | ;;; triple store connection and setup 43 | ;;; -------------------------------------------------------- 44 | 45 | 46 | 47 | ;;; -------------------------------------------------------- 48 | ;;; connections 49 | ;;; -------------------------------------------------------- 50 | 51 | ;;this is nonsese becasue to the circular defintions 52 | ;; and what can and cannot be forward delcared 53 | (declare sesame-initialize 54 | new-sesame-connection 55 | close-existing-sesame-connection) 56 | 57 | (defn sesame-connection [kb] 58 | (new-sesame-connection kb)) 59 | 60 | (defn close-sesame-connection [kb] 61 | (close-existing-sesame-connection kb)) 62 | 63 | 64 | ;;; -------------------------------------------------------- 65 | ;;; namespaces 66 | ;;; -------------------------------------------------------- 67 | 68 | ;;; -------------------------------------------------------- 69 | ;;; protocol implementation 70 | ;;; -------------------------------------------------------- 71 | 72 | ;;TODO seperate server and connection?? 73 | 74 | (defrecord SesameKB [server connection kb-features] 75 | KB 76 | 77 | (native [kb] server) 78 | (initialize [kb] (sesame-initialize kb)) 79 | (open [kb] (new-sesame-connection kb)) 80 | (close [kb] (close-sesame-connection kb)) 81 | (features [kb] kb-features) 82 | 83 | rdfKB 84 | 85 | (root-ns-map [kb] (sesame-server-ns-map kb)) 86 | ;; (ns-maps [kb] ns-maps-var) 87 | ;; (ns-map-to-short [kb] (:ns-map-to-short (deref ns-maps-var))) 88 | ;; (ns-map-to-long [kb] (:ns-map-to-long (deref ns-maps-var))) 89 | (register-ns [kb short long] (sesame-register-ns kb short long)) 90 | 91 | (create-resource [kb name] (sesame-create-resource kb name)) 92 | (create-property [kb name] (sesame-create-property kb name)) 93 | (create-literal [kb val] (sesame-create-literal kb val)) 94 | (create-literal [kb val type] (sesame-create-literal kb val type)) 95 | 96 | ;;TODO convert to creating proper string literals 97 | ;; (create-string-literal [kb str] (sesame-create-string-iteral kb val)) 98 | ;; (create-string-literal [kb str lang] 99 | ;; (sesame-create-string literal kb val type)) 100 | (create-string-literal [kb str] (sesame-create-literal kb str)) 101 | (create-string-literal [kb str lang] 102 | (sesame-create-literal kb str lang)) 103 | 104 | 105 | (create-blank-node [kb name] (sesame-create-blank-node kb name)) 106 | (create-statement [kb s p o] (sesame-create-statement kb s p o)) 107 | 108 | (add-statement [kb stmt] (sesame-add-statement kb stmt)) 109 | (add-statement [kb stmt context] (sesame-add-statement kb stmt context)) 110 | (add-statement [kb s p o] (sesame-add-statement kb s p o)) 111 | (add-statement [kb s p o context] (sesame-add-statement kb s p o context)) 112 | 113 | (add-statements [kb stmts] (sesame-add-statements kb stmts)) 114 | (add-statements [kb stmts context] (sesame-add-statements kb stmts context)) 115 | 116 | (ask-statement [kb s p o context] (sesame-ask-statement kb s p o context)) 117 | (query-statement [kb s p o context] (sesame-query-statement kb s p o context)) 118 | 119 | 120 | (load-rdf-file [kb file] (sesame-load-rdf-file kb file)) 121 | (load-rdf-file [kb file type] (sesame-load-rdf-file kb file type)) 122 | 123 | ;;the following will throw exception for unknown rdf format 124 | (load-rdf-stream [kb stream] (sesame-load-rdf-stream kb stream)) 125 | 126 | (load-rdf-stream [kb stream type] (sesame-load-rdf-stream kb stream type)) 127 | 128 | 129 | 130 | sparqlKB 131 | 132 | (ask-pattern [kb pattern] 133 | (sesame-ask-pattern kb pattern)) 134 | (ask-pattern [kb pattern options] 135 | (sesame-ask-pattern kb pattern options)) 136 | 137 | (query-pattern [kb pattern] 138 | (sesame-query-pattern kb pattern)) 139 | (query-pattern [kb pattern options] 140 | (sesame-query-pattern kb pattern options)) 141 | 142 | (count-pattern [kb pattern] 143 | (sesame-count-pattern kb pattern)) 144 | (count-pattern [kb pattern options] 145 | (sesame-count-pattern kb pattern options)) 146 | 147 | (visit-pattern [kb visitor pattern] 148 | (sesame-visit-pattern kb visitor pattern)) 149 | (visit-pattern [kb visitor pattern options] 150 | (sesame-visit-pattern kb visitor pattern options)) 151 | 152 | (construct-pattern [kb create-pattern pattern] 153 | (sesame-construct-pattern kb create-pattern pattern)) 154 | (construct-pattern [kb create-pattern pattern options] 155 | (sesame-construct-pattern kb create-pattern pattern options)) 156 | (construct-visit-pattern [kb visitor create-pattern pattern] 157 | (sesame-construct-visit-pattern kb visitor create-pattern pattern)) 158 | (construct-visit-pattern [kb visitor create-pattern pattern options] 159 | (sesame-construct-visit-pattern kb visitor create-pattern pattern options)) 160 | 161 | 162 | (ask-sparql [kb query-string] 163 | (sesame-ask-sparql kb query-string)) 164 | (query-sparql [kb query-string] 165 | (sesame-query-sparql kb query-string)) 166 | (count-sparql [kb query-string] 167 | (sesame-count-sparql kb query-string)) 168 | (visit-sparql [kb visitor query-string] 169 | (sesame-visit-sparql kb visitor query-string)) 170 | 171 | (construct-sparql [kb sparql-string] 172 | (sesame-construct-sparql kb sparql-string)) 173 | (construct-visit-sparql [kb visitor sparql-string] 174 | (sesame-construct-visit-sparql kb visitor sparql-string)) 175 | 176 | ) 177 | 178 | ;; TODO factor this out to a "connection" package 179 | 180 | ;;; "constructors" 181 | ;;; -------------------------------------------------------- 182 | 183 | ;; the way new SesameKBConnection is being called it isn't preserving 184 | ;; the additional keys that are added on to the sesame server 185 | ;; specifically the :value-factory 186 | 187 | ;; (defn new-sesame-server [] 188 | ;; (let [repository (HTTPRepository. *default-server* *repository-name*)] 189 | ;; (.setPreferredTupleQueryResultFormat repository 190 | ;; TupleQueryResultFormat/SPARQL) 191 | ;; (if (and *username* *password*) 192 | ;; (.setUsernameAndPassword repository *username* *password*)) 193 | ;; (assoc (SesameKB. repository (initial-ns-mappings) nil) 194 | ;; :value-factory (.getValueFactory repository)))) 195 | 196 | (defn copy-sesame-slots [target-kb source-kb] 197 | (copy-rdf-slots (copy-kb-slots target-kb source-kb) 198 | source-kb)) 199 | 200 | 201 | (defn sesame-kb-helper [repository] 202 | (initialize-ns-mappings 203 | (assoc (SesameKB. repository nil *kb-features*) 204 | :value-factory (.getValueFactory repository)))) 205 | 206 | 207 | ;; (defn new-sesame-server [& {:keys [server repo-name username password] 208 | ;; :or {server *default-server* 209 | ;; repo-name *repository-name* 210 | ;; username *username* 211 | ;; password *password*}}] 212 | ;; (println "server" server " name" repo-name) 213 | ;; (println "username" username " password" password) 214 | ;; (let [repository (HTTPRepository. server repo-name)] 215 | ;; (.setPreferredTupleQueryResultFormat repository 216 | ;; TupleQueryResultFormat/SPARQL) 217 | ;; (if (and username password) 218 | ;; (.setUsernameAndPassword repository username password)) 219 | ;; (assoc (SesameKB. repository (initial-ns-mappings) nil) 220 | ;; :value-factory (.getValueFactory repository)))) 221 | 222 | (defn new-sesame-server [& {:keys [server repo-name username password] 223 | :or {server *default-server* 224 | repo-name *repository-name* 225 | username *username* 226 | password *password*}}] 227 | ;; (println "server" server " name" repo-name) 228 | ;; (println "username" username " password" password) 229 | (let [repository (org.openrdf.repository.http.HTTPRepository. server 230 | repo-name)] 231 | (.setPreferredTupleQueryResultFormat repository 232 | TupleQueryResultFormat/SPARQL) 233 | (if (and username password) 234 | (.setUsernameAndPassword repository username password)) 235 | (sesame-kb-helper repository))) 236 | 237 | ;; (defn new-sesame-server-helper [server & [repo-name username password]] 238 | ;; (apply new-sesame-server 239 | ;; (concat [:server server] 240 | ;; (if repo-name [:repo-name repo-name] nil) 241 | ;; (if username [:username username] nil) 242 | ;; (if password [:password password] nil)))) 243 | 244 | 245 | (defn new-sesame-connection [kb] 246 | (copy-sesame-slots (assoc (SesameKB. (:server kb) 247 | (.getConnection (:server kb)) 248 | (features kb)) 249 | :value-factory (:value-factory kb)) 250 | kb)) 251 | 252 | (defn close-existing-sesame-connection [kb] 253 | (when (:connection kb) 254 | (.close (:connection kb))) 255 | (copy-sesame-slots (assoc (SesameKB. (:server kb) nil (features kb)) 256 | :value-factory (:value-factory kb)) 257 | kb)) 258 | 259 | 260 | ;; (defmethod kb org.openrdf.repository.http.HTTPRepository [class-name] 261 | ;; (new-sesame-server)) 262 | 263 | ;; (defmethod kb org.openrdf.repository.Repository [class-name] 264 | ;; (new-sesame-server)) 265 | 266 | (defmethod kb org.openrdf.repository.http.HTTPRepository [arg] 267 | (if (class? arg) 268 | (new-sesame-server) 269 | (sesame-kb-helper arg))) 270 | 271 | 272 | (defmethod kb org.openrdf.repository.Repository [arg] 273 | (if (class? arg) 274 | (new-sesame-server) 275 | (sesame-kb-helper arg))) 276 | 277 | (defmethod kb org.openrdf.sail.memory.MemoryStore [arg] 278 | (if (class? arg) 279 | (let [repo (SailRepository. (MemoryStore.))] 280 | ;; (.setPreferredTupleQueryResultFormat repo 281 | ;; TupleQueryResultFormat/SPARQL) 282 | (.initialize repo) 283 | (sesame-kb-helper repo)) 284 | (sesame-kb-helper arg))) 285 | 286 | ;; need to add more kb constructors in here for taking in various sesame objects 287 | ;; repository, sail, etc. instances 288 | 289 | 290 | (defn sesame-initialize [kb] 291 | (synch-ns-mappings kb)) 292 | 293 | ;; (defn new-sesame-memory-kb [] 294 | ;; (let [repo (SailRepository. (MemoryStore.))] 295 | ;; (.initialize repo) 296 | ;; ;(.setPreferredTupleQueryResultFormat repo TupleQueryResultFormat/SPARQL) 297 | ;; (assoc (SesameKB. repo (initial-ns-mappings) nil) 298 | ;; :value-factory (.getValueFactory repo)))) 299 | 300 | (defn new-sesame-memory-kb [] 301 | (kb org.openrdf.sail.memory.MemoryStore)) 302 | 303 | 304 | (defmethod kb :sesame-mem [_] 305 | (new-sesame-memory-kb)) 306 | 307 | 308 | ;;; -------------------------------------------------------- 309 | ;;; END 310 | ;;; -------------------------------------------------------- 311 | -------------------------------------------------------------------------------- /EPLv1.0.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Eclipse Public License - Version 1.0 8 | 25 | 26 | 27 | 28 | 29 | 30 |

Eclipse Public License - v 1.0

31 | 32 |

THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE 33 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR 34 | DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS 35 | AGREEMENT.

36 | 37 |

1. DEFINITIONS

38 | 39 |

"Contribution" means:

40 | 41 |

a) in the case of the initial Contributor, the initial 42 | code and documentation distributed under this Agreement, and

43 |

b) in the case of each subsequent Contributor:

44 |

i) changes to the Program, and

45 |

ii) additions to the Program;

46 |

where such changes and/or additions to the Program 47 | originate from and are distributed by that particular Contributor. A 48 | Contribution 'originates' from a Contributor if it was added to the 49 | Program by such Contributor itself or anyone acting on such 50 | Contributor's behalf. Contributions do not include additions to the 51 | Program which: (i) are separate modules of software distributed in 52 | conjunction with the Program under their own license agreement, and (ii) 53 | are not derivative works of the Program.

54 | 55 |

"Contributor" means any person or entity that distributes 56 | the Program.

57 | 58 |

"Licensed Patents" mean patent claims licensable by a 59 | Contributor which are necessarily infringed by the use or sale of its 60 | Contribution alone or when combined with the Program.

61 | 62 |

"Program" means the Contributions distributed in accordance 63 | with this Agreement.

64 | 65 |

"Recipient" means anyone who receives the Program under 66 | this Agreement, including all Contributors.

67 | 68 |

2. GRANT OF RIGHTS

69 | 70 |

a) Subject to the terms of this Agreement, each 71 | Contributor hereby grants Recipient a non-exclusive, worldwide, 72 | royalty-free copyright license to reproduce, prepare derivative works 73 | of, publicly display, publicly perform, distribute and sublicense the 74 | Contribution of such Contributor, if any, and such derivative works, in 75 | source code and object code form.

76 | 77 |

b) Subject to the terms of this Agreement, each 78 | Contributor hereby grants Recipient a non-exclusive, worldwide, 79 | royalty-free patent license under Licensed Patents to make, use, sell, 80 | offer to sell, import and otherwise transfer the Contribution of such 81 | Contributor, if any, in source code and object code form. This patent 82 | license shall apply to the combination of the Contribution and the 83 | Program if, at the time the Contribution is added by the Contributor, 84 | such addition of the Contribution causes such combination to be covered 85 | by the Licensed Patents. The patent license shall not apply to any other 86 | combinations which include the Contribution. No hardware per se is 87 | licensed hereunder.

88 | 89 |

c) Recipient understands that although each Contributor 90 | grants the licenses to its Contributions set forth herein, no assurances 91 | are provided by any Contributor that the Program does not infringe the 92 | patent or other intellectual property rights of any other entity. Each 93 | Contributor disclaims any liability to Recipient for claims brought by 94 | any other entity based on infringement of intellectual property rights 95 | or otherwise. As a condition to exercising the rights and licenses 96 | granted hereunder, each Recipient hereby assumes sole responsibility to 97 | secure any other intellectual property rights needed, if any. For 98 | example, if a third party patent license is required to allow Recipient 99 | to distribute the Program, it is Recipient's responsibility to acquire 100 | that license before distributing the Program.

101 | 102 |

d) Each Contributor represents that to its knowledge it 103 | has sufficient copyright rights in its Contribution, if any, to grant 104 | the copyright license set forth in this Agreement.

105 | 106 |

3. REQUIREMENTS

107 | 108 |

A Contributor may choose to distribute the Program in object code 109 | form under its own license agreement, provided that:

110 | 111 |

a) it complies with the terms and conditions of this 112 | Agreement; and

113 | 114 |

b) its license agreement:

115 | 116 |

i) effectively disclaims on behalf of all Contributors 117 | all warranties and conditions, express and implied, including warranties 118 | or conditions of title and non-infringement, and implied warranties or 119 | conditions of merchantability and fitness for a particular purpose;

120 | 121 |

ii) effectively excludes on behalf of all Contributors 122 | all liability for damages, including direct, indirect, special, 123 | incidental and consequential damages, such as lost profits;

124 | 125 |

iii) states that any provisions which differ from this 126 | Agreement are offered by that Contributor alone and not by any other 127 | party; and

128 | 129 |

iv) states that source code for the Program is available 130 | from such Contributor, and informs licensees how to obtain it in a 131 | reasonable manner on or through a medium customarily used for software 132 | exchange.

133 | 134 |

When the Program is made available in source code form:

135 | 136 |

a) it must be made available under this Agreement; and

137 | 138 |

b) a copy of this Agreement must be included with each 139 | copy of the Program.

140 | 141 |

Contributors may not remove or alter any copyright notices contained 142 | within the Program.

143 | 144 |

Each Contributor must identify itself as the originator of its 145 | Contribution, if any, in a manner that reasonably allows subsequent 146 | Recipients to identify the originator of the Contribution.

147 | 148 |

4. COMMERCIAL DISTRIBUTION

149 | 150 |

Commercial distributors of software may accept certain 151 | responsibilities with respect to end users, business partners and the 152 | like. While this license is intended to facilitate the commercial use of 153 | the Program, the Contributor who includes the Program in a commercial 154 | product offering should do so in a manner which does not create 155 | potential liability for other Contributors. Therefore, if a Contributor 156 | includes the Program in a commercial product offering, such Contributor 157 | ("Commercial Contributor") hereby agrees to defend and 158 | indemnify every other Contributor ("Indemnified Contributor") 159 | against any losses, damages and costs (collectively "Losses") 160 | arising from claims, lawsuits and other legal actions brought by a third 161 | party against the Indemnified Contributor to the extent caused by the 162 | acts or omissions of such Commercial Contributor in connection with its 163 | distribution of the Program in a commercial product offering. The 164 | obligations in this section do not apply to any claims or Losses 165 | relating to any actual or alleged intellectual property infringement. In 166 | order to qualify, an Indemnified Contributor must: a) promptly notify 167 | the Commercial Contributor in writing of such claim, and b) allow the 168 | Commercial Contributor to control, and cooperate with the Commercial 169 | Contributor in, the defense and any related settlement negotiations. The 170 | Indemnified Contributor may participate in any such claim at its own 171 | expense.

172 | 173 |

For example, a Contributor might include the Program in a commercial 174 | product offering, Product X. That Contributor is then a Commercial 175 | Contributor. If that Commercial Contributor then makes performance 176 | claims, or offers warranties related to Product X, those performance 177 | claims and warranties are such Commercial Contributor's responsibility 178 | alone. Under this section, the Commercial Contributor would have to 179 | defend claims against the other Contributors related to those 180 | performance claims and warranties, and if a court requires any other 181 | Contributor to pay any damages as a result, the Commercial Contributor 182 | must pay those damages.

183 | 184 |

5. NO WARRANTY

185 | 186 |

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS 187 | PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 188 | OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 189 | ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY 190 | OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely 191 | responsible for determining the appropriateness of using and 192 | distributing the Program and assumes all risks associated with its 193 | exercise of rights under this Agreement , including but not limited to 194 | the risks and costs of program errors, compliance with applicable laws, 195 | damage to or loss of data, programs or equipment, and unavailability or 196 | interruption of operations.

197 | 198 |

6. DISCLAIMER OF LIABILITY

199 | 200 |

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT 201 | NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, 202 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING 203 | WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF 204 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 205 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR 206 | DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED 207 | HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

208 | 209 |

7. GENERAL

210 | 211 |

If any provision of this Agreement is invalid or unenforceable under 212 | applicable law, it shall not affect the validity or enforceability of 213 | the remainder of the terms of this Agreement, and without further action 214 | by the parties hereto, such provision shall be reformed to the minimum 215 | extent necessary to make such provision valid and enforceable.

216 | 217 |

If Recipient institutes patent litigation against any entity 218 | (including a cross-claim or counterclaim in a lawsuit) alleging that the 219 | Program itself (excluding combinations of the Program with other 220 | software or hardware) infringes such Recipient's patent(s), then such 221 | Recipient's rights granted under Section 2(b) shall terminate as of the 222 | date such litigation is filed.

223 | 224 |

All Recipient's rights under this Agreement shall terminate if it 225 | fails to comply with any of the material terms or conditions of this 226 | Agreement and does not cure such failure in a reasonable period of time 227 | after becoming aware of such noncompliance. If all Recipient's rights 228 | under this Agreement terminate, Recipient agrees to cease use and 229 | distribution of the Program as soon as reasonably practicable. However, 230 | Recipient's obligations under this Agreement and any licenses granted by 231 | Recipient relating to the Program shall continue and survive.

232 | 233 |

Everyone is permitted to copy and distribute copies of this 234 | Agreement, but in order to avoid inconsistency the Agreement is 235 | copyrighted and may only be modified in the following manner. The 236 | Agreement Steward reserves the right to publish new versions (including 237 | revisions) of this Agreement from time to time. No one other than the 238 | Agreement Steward has the right to modify this Agreement. The Eclipse 239 | Foundation is the initial Agreement Steward. The Eclipse Foundation may 240 | assign the responsibility to serve as the Agreement Steward to a 241 | suitable separate entity. Each new version of the Agreement will be 242 | given a distinguishing version number. The Program (including 243 | Contributions) may always be distributed subject to the version of the 244 | Agreement under which it was received. In addition, after a new version 245 | of the Agreement is published, Contributor may elect to distribute the 246 | Program (including its Contributions) under the new version. Except as 247 | expressly stated in Sections 2(a) and 2(b) above, Recipient receives no 248 | rights or licenses to the intellectual property of any Contributor under 249 | this Agreement, whether expressly, by implication, estoppel or 250 | otherwise. All rights in the Program not expressly granted under this 251 | Agreement are reserved.

252 | 253 |

This Agreement is governed by the laws of the State of New York and 254 | the intellectual property laws of the United States of America. No party 255 | to this Agreement will bring a legal action under this Agreement more 256 | than one year after the cause of action arose. Each party waives its 257 | rights to a jury trial in any resulting litigation.

258 | 259 | 260 | 261 | 262 | -------------------------------------------------------------------------------- /kr-core/src/test/clojure/edu/ucdenver/ccp/test/kr/test_sparql.clj: -------------------------------------------------------------------------------- 1 | (ns edu.ucdenver.ccp.test.kr.test-sparql 2 | (use clojure.test 3 | ;;clojure.test.junit 4 | 5 | edu.ucdenver.ccp.test.kr.test-kb 6 | 7 | edu.ucdenver.ccp.kr.variable 8 | edu.ucdenver.ccp.kr.kb 9 | edu.ucdenver.ccp.kr.rdf 10 | edu.ucdenver.ccp.kr.sparql) 11 | (import java.net.URI)) 12 | 13 | ;;; -------------------------------------------------------- 14 | ;;; constansts 15 | ;;; -------------------------------------------------------- 16 | 17 | (def uri-a (URI. "http://www.example.org/a")) 18 | (def uri-p (URI. "http://www.example.org/p")) 19 | (def uri-b (URI. "http://www.example.org/b")) 20 | (def uri-x (URI. "http://www.example.org/x")) 21 | 22 | 23 | (def test-triples-uri 24 | '((ex/a rdf/type foaf/Person ) 25 | (ex/a foaf/name "Alice" ) 26 | (ex/a foaf/mbox "" ) 27 | (ex/a foaf/mbox "" ) 28 | (ex/a foaf/knows ex/b) 29 | (ex/b rdf/type foaf/Person ) 30 | (ex/b foaf/name "Bob" ))) 31 | 32 | 33 | (def test-triples-6-1 34 | '((ex/a rdf/type foaf/Person ) 35 | (ex/a foaf/name "Alice" ) 36 | (ex/a foaf/mbox "" ) 37 | (ex/a foaf/mbox "" ) 38 | 39 | (ex/b rdf/type foaf/Person ) 40 | (ex/b foaf/name "Bob" ))) 41 | 42 | (def test-triples-6-3 43 | '((ex/a foaf/name "Alice" ) 44 | (ex/a foaf/homepage "" ) 45 | 46 | (ex/b foaf/name "Bob" ) 47 | (ex/b foaf/mbox "" ))) 48 | 49 | (def test-triples-7 50 | '((ex/a dc10/title "SPARQL Query Language Tutorial" ) 51 | (ex/a dc10/creator "Alice" ) 52 | 53 | (ex/b dc11/title "SPARQL Protocol Tutorial" ) 54 | (ex/b dc11/creator "Bob" ) 55 | 56 | (ex/c dc10/title "SPARQL" ) 57 | (ex/c dc11/title "SPARQL (updated)" ))) 58 | 59 | (def test-triples-10-2-1 60 | '((ex/a foaf/givenname "Alice" ) 61 | (ex/a foaf/family_name "Hacker" ) 62 | 63 | (ex/b foaf/firstname "Bob" ) 64 | (ex/b foaf/surname "Hacker" ))) 65 | 66 | (def test-triples-numbers-equality 67 | '((ex/a foaf/givenname "Alice" ) 68 | (ex/a foaf/surname "Hacker" ) 69 | (ex/a foaf/age [40 xsd/integer]) 70 | 71 | (ex/b foaf/firstname "Bob" ) 72 | (ex/b foaf/surname "Hacker" ) 73 | (ex/b foaf/age 40) ;the default should be xsd/integer 74 | 75 | (ex/c foaf/firstname "Fred" ) 76 | (ex/c foaf/surname "Hacker" ) 77 | (ex/c foaf/age [50 xsd/integer])));50))) 78 | 79 | (def test-triples-lang 80 | '((ex/a foaf/firstname "Alice" ) 81 | (ex/b foaf/firstname ["Bob" "en"]) 82 | (ex/c foaf/firstname ["Bob"]))) 83 | 84 | (def test-triples-custom-type 85 | '((ex/a ex/p ["foo" ex/custom]) 86 | (ex/b ex/p ["foo" ex/custom2]))) 87 | 88 | (def test-triples-custom-type-uri 89 | `((ex/a ex/p ["foo" ex/custom]) 90 | (ex/b ex/p ["foo" ~(URI. "http://www.example.org/custom")]))) 91 | 92 | 93 | 94 | ;;; -------------------------------------------------------- 95 | ;;; helpers 96 | ;;; -------------------------------------------------------- 97 | 98 | ;;; -------------------------------------------------------- 99 | ;;; tests 100 | ;;; -------------------------------------------------------- 101 | 102 | (kb-test test-kb-loading-triples test-triples 103 | (is *kb*)) 104 | 105 | (kb-test test-simple-ask test-triples 106 | (is (ask '((_/person foaf/name ?/name) 107 | (_/person foaf/mbox ?/email))))) 108 | 109 | (kb-test test-simple-select test-triples 110 | (is (= 2 111 | (count (query '((_/person foaf/name ?/name) 112 | (_/person foaf/mbox ?/email))))))) 113 | 114 | (kb-test test-optional test-triples-6-1 115 | (is (= 3 116 | (count 117 | (query '((?/person foaf/name ?/name) 118 | (:optional 119 | (?/person foaf/mbox ?/email)))))))) 120 | 121 | (kb-test test-count-query test-triples-6-1 122 | (is (= 3 123 | (count-query '((?/person foaf/name ?/name) 124 | (:optional 125 | (?/person foaf/mbox ?/email))))))) 126 | 127 | 128 | (kb-test test-optional-select-6-3 test-triples-6-3 129 | (is (= 2 130 | (count 131 | (query '((?/person foaf/name ?/name) 132 | (:optional (?/person foaf/mbox ?/email)) 133 | (:optional (?/person foaf/homepage ?/hpage)))))))) 134 | 135 | (kb-test test-union-select-7 test-triples-7 136 | (is (= 2 137 | (count 138 | (query 139 | '(:union 140 | ((?/book dc10/title ?/title) 141 | (?/book dc10/creator ?/author)) 142 | ((?/book dc11/title ?/title) 143 | (?/book dc11/creator ?/author)))))))) 144 | 145 | (kb-test test-union-select-10-2-1 test-triples-10-2-1 146 | (is (= 2 147 | (count 148 | (query 149 | '((:union ((?/x foaf/firstname ?/gname)) 150 | ((?/x foaf/givenname ?/gname))) 151 | (:union ((?/x foaf/surname ?/fname)) 152 | ((?/x foaf/family_name ?/fname))))))))) 153 | 154 | (kb-test test-bound-operator test-triples-6-1 155 | (is (= 2 ;this is 2 but should be one if ?/person doesn't capture 156 | (count 157 | (query '((?/person foaf/name ?/name) 158 | (:optional 159 | (?/person foaf/mbox ?/email)) 160 | (:bound ?/email)))))) 161 | (is (= 1 ;just bob 162 | (count 163 | (query '((?/person foaf/name ?/name) 164 | (:optional 165 | (?/person foaf/mbox ?/email)) 166 | (:not (:bound ?/email)))))))) 167 | 168 | (kb-test test-not-operator test-triples-6-1 169 | (is (= 1 ;just bob 170 | (count 171 | (query '((?/person foaf/name ?/name) 172 | (:optional 173 | (?/person foaf/mbox ?/email)) 174 | (:not (:bound ?/email))))))) 175 | (is (= 1 ;just bob 176 | (count 177 | (query '((?/person foaf/name ?/name) 178 | (:optional 179 | (?/person foaf/mbox ?/email)) 180 | (! (:bound ?/email)))))))) 181 | 182 | (kb-test test-numbers test-triples-numbers-equality 183 | (is (= 2 ;two because of reflection 184 | (count (query '((?/person foaf/surname ?/name) 185 | (?/person foaf/age ?/age1) 186 | (?/person2 foaf/surname ?/name) 187 | (?/person2 foaf/age ?/age2) 188 | (= ?/age1 ?/age2) 189 | (!= ?/person ?/person2) 190 | ))))) 191 | (is (= 2 192 | (count (query '((?/person foaf/surname ?/name) 193 | (?/person foaf/age ?/age1) 194 | (?/person2 foaf/surname ?/name) 195 | (?/person2 foaf/age ?/age2) 196 | (> ?/age1 ?/age2) 197 | )))))) 198 | 199 | (kb-test test-n-ary-or test-triples-numbers-equality 200 | (is (= 3 201 | (count (query '((?/person foaf/surname ?/name) 202 | (?/person foaf/age ?/age) 203 | (:or (= ?/age 30) 204 | (= ?/age 40) 205 | (= ?/age 50)))))))) 206 | 207 | (kb-test test-boxed-number test-triples-numbers-equality 208 | (is (= 2 209 | (count (query '((?/person foaf/surname ?/name) 210 | (?/person foaf/age 40)))))) 211 | (is (= 0 212 | (count (query '((?/person foaf/surname ?/name) 213 | (?/person foaf/age [40])))))) 214 | (is (= 2 215 | (count (query '((?/person foaf/surname ?/name) 216 | (?/person foaf/age [40 xsd/integer])))))) 217 | 218 | (is (= 2 219 | (count (query '((?/person foaf/surname ?/name) 220 | (?/person foaf/age ["40" xsd/integer]))))))) 221 | 222 | 223 | (kb-test test-lang test-triples-lang 224 | (is (= 3 225 | (count (query '((?/person foaf/firstname ?/x)))))) 226 | ;;TODO this langague tag is wrong there shouldn't need to be 227 | ;; nested escaped quotes 228 | (is (= 2 229 | (count (query '((?/person foaf/firstname ?/x) 230 | (= (:lang ?/x) ["en"])))))) 231 | ;; the next to are auto-languaged into "en" 232 | (is (= 1 233 | (count (query '((?/person foaf/firstname "Bob")))))) 234 | (is (= 1 235 | (count (query '((?/person foaf/firstname "Alice")))))) 236 | ;; forced "en" 237 | (is (= 1 238 | (count (query '((?/person foaf/firstname ["Alice" "en"])))))) 239 | ;; boxed forcing off auto-language thus missing 240 | (is (= 0 241 | (count (query '((?/person foaf/firstname ["Alice"])))))) 242 | ;;note the lower case 'b' on the failure test 243 | (is (= 0 244 | (count (query '((?/person foaf/firstname "bob"))))))) 245 | 246 | 247 | 248 | 249 | (kb-test test-query-visitor test-triples-lang 250 | (let [count (atom 0)] 251 | (query-visit (fn [bindings] (swap! count inc)) 252 | '((?/person foaf/firstname ?/x))) 253 | (is (= 3 @count))) 254 | 255 | (query-visit (fn [bindings] (is (= 2 (count bindings)))) 256 | '((?/person foaf/firstname ?/x))) 257 | 258 | (query-visit (fn [bindings] 259 | (let [key-set (set (map first bindings))] 260 | (is (key-set '?/x)) 261 | (is (key-set '?/person)))) 262 | '((?/person foaf/firstname ?/x)))) 263 | 264 | ;; there is a bug where "3"^^ 265 | ;; was coming out as "3" instead of 3 266 | (kb-test test-integer-clj-ify test-triples-numbers-equality 267 | (is (= 40 268 | ('?/age (first (query '((?/person foaf/givenname "Alice") 269 | (?/person foaf/age ?/age)))))))) 270 | 271 | 272 | 273 | (kb-test test-backquote-operators test-triples-numbers-equality 274 | (is (= 2 ;two because of reflection 275 | (count (query `((?/person foaf/surname ?/name) 276 | (?/person foaf/age ?/age1) 277 | (?/person2 foaf/surname ?/name) 278 | (?/person2 foaf/age ?/age2) 279 | (= ?/age1 ?/age2) 280 | (!= ?/person ?/person2) 281 | ))))) 282 | (is (= 2 283 | (count (query `((?/person foaf/surname ?/name) 284 | (?/person foaf/age ?/age1) 285 | (?/person2 foaf/surname ?/name) 286 | (?/person2 foaf/age ?/age2) 287 | (> ?/age1 ?/age2) 288 | )))))) 289 | 290 | 291 | (kb-test test-strings-in-operators test-triples-6-3 292 | (is (= 2 293 | (count (query `((?/person foaf/name ?/name)))))) 294 | (is (= 1 295 | (count (query `((?/person foaf/name ?/name) 296 | (= "Bob" ?/name)))))) 297 | ;; box to drop the language tag since none given 298 | (is (= 0 299 | (count (query `((?/person foaf/name ?/name) 300 | (= ["Bob"] ?/name))))))) 301 | 302 | (kb-test test-regex-operator test-triples-6-3 303 | (is (= 2 304 | (count (query `((?/person foaf/name ?/name)))))) 305 | (is (= 1 306 | (count (query `((?/person foaf/name ?/name) 307 | (:regex ?/name "^ali" "i"))))))) 308 | 309 | 310 | (kb-test test-uri-pat test-triples-uri 311 | (is (= 1 312 | (count (query '((?/person1 foaf/knows ?/person2)))))) 313 | (is (= 1 314 | (count (query '((ex/a foaf/knows ?/person2)))))) 315 | (is (= 1 316 | (count (query '((?/person1 foaf/knows ex/b)))))) 317 | (is (= 0 318 | (count (query '((ex/b foaf/knows ?/person2)))))) 319 | (is (= 1 320 | (count (query `((?/person1 foaf/knows ~uri-b)))))) 321 | (is (= 1 322 | (count (query `((~uri-a foaf/knows ?/person2)))))) 323 | (is (ask `((~uri-a foaf/knows ~uri-b)))) 324 | ) 325 | 326 | 327 | (kb-test test-custom-type test-triples-custom-type 328 | (is (= 2 329 | (count (query '((?/person1 ex/p ?/custom)))))) 330 | (is (= 1 331 | (count (query '((?/a ex/p ["foo" ex/custom]))))))) 332 | 333 | (kb-test test-custom-type-uri test-triples-custom-type-uri 334 | (is (= 2 335 | (count (query '((?/person1 ex/p ?/custom)))))) 336 | (is (= 2 337 | (count (query '((?/a ?/p ["foo" ex/custom])))))) 338 | (is (= 2 339 | (count 340 | (query `((?/a 341 | ?/p 342 | ["foo" ~(URI. "http://www.example.org/custom")]))))))) 343 | 344 | 345 | 346 | ;;; -------------------------------------------------------- 347 | ;;; END 348 | ;;; -------------------------------------------------------- 349 | --------------------------------------------------------------------------------