Mercurial > lasercutter
view src/clojure/contrib/test_contrib/datalog/tests/test_rules.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ;; Copyright (c) Jeffrey Straszheim. All rights reserved. The use and2 ;; distribution terms for this software are covered by the Eclipse Public3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can4 ;; be found in the file epl-v10.html at the root of this distribution. By5 ;; using this software in any fashion, you are agreeing to be bound by the6 ;; terms of this license. You must not remove this notice, or any other,7 ;; from this software.8 ;;9 ;; test-rules.clj10 ;;11 ;; A Clojure implementation of Datalog -- Rule Tests12 ;;13 ;; straszheimjeffrey (gmail)14 ;; Created 12 Feburary 200917 (ns clojure.contrib.datalog.tests.test-rules18 (:use clojure.test19 clojure.contrib.datalog.rules20 clojure.contrib.datalog.literals21 clojure.contrib.datalog.database))24 (def tr-1 (<- (:fred :x ?x :y ?y) (:mary :x ?x :z ?z) (:sally :z ?z :y ?y)))25 (def tr-2 (<- (:fred) (not! :mary :x 3)))26 (def tr-3 (<- (:fred :x ?x :y ?y) (if > ?x ?y) (:mary :x ?x) (:sally :y ?y)))30 (deftest test-rule-safety31 (is (thrown-with-msg? Exception #".*Head vars.*not bound.*"32 (<- (:fred :x ?x) (:sally :y ?y))))33 (is (thrown-with-msg? Exception #".*Body vars.*not bound.*negative position.*"34 (<- (:fred :x ?x) (:becky :x ?x) (not! :sally :y ?y))))35 (is (thrown-with-msg? Exception #".*Body vars.*not bound.*negative position.*"36 (<- (:fred :x ?x) (:becky :x ?x) (if > ?x ?y)))))39 (deftest test-sip40 (is (= (compute-sip #{:x} #{:mary :sally} tr-1)41 (<- ({:pred :fred :bound #{:x}} :x ?x :y ?y)42 ({:pred :mary :bound #{:x}} :z ?z :x ?x)43 ({:pred :sally :bound #{:z}} :y ?y :z ?z))))45 (is (= (compute-sip #{} #{:mary :sally} tr-1)46 (<- (:fred :y ?y :x ?x) (:mary :z ?z :x ?x) ({:pred :sally :bound #{:z}} :y ?y :z ?z))))48 (is (= (compute-sip #{} #{:mary} tr-2)49 (<- (:fred) (not! {:pred :mary :bound #{:x}} :x 3))))51 (is (= (compute-sip #{} #{} tr-2)52 tr-2))54 (is (= (display-rule (compute-sip #{:x} #{:mary :sally} tr-3))55 (display-rule (<- ({:pred :fred :bound #{:x}} :x ?x :y ?y)56 ({:pred :mary :bound #{:x}} :x ?x)57 (:sally :y ?y)58 (if > ?x ?y))))))59 ; Display rule is used because = does not work on60 ; (if > ?x ?y) because it contains a closure63 (def rs64 (rules-set65 (<- (:path :a ?x :b ?y) (:edge :a ?x :b ?y))66 (<- (:path :a ?x :b ?y) (:edge :a ?x :b ?z) (:path :a ?z :b ?y))67 (<- (:edge :a ?x :b ?y) (:route :a ?x :b ?y) (if not= ?x ?y))))69 (deftest test-rules-set70 (is (= (count rs) 3))71 (is (contains? rs (<- (:path :a ?x :b ?y) (:edge :a ?x :b ?z) (:path :a ?z :b ?y)))))73 (deftest test-predicate-map74 (let [pm (predicate-map rs)]75 (is (= (pm :path)76 #{(<- (:path :a ?x :b ?y) (:edge :a ?x :b ?y))77 (<- (:path :a ?x :b ?y) (:edge :a ?x :b ?z) (:path :a ?z :b ?y))}))78 (is (= (-> :edge pm count) 1))))81 (def db1 (make-database82 (relation :fred [:x :y])83 (index :fred :x)84 (relation :sally [:x])85 (relation :ben [:y])))87 (def db2 (add-tuples db188 [:fred :x 1 :y :mary]89 [:fred :x 1 :y :becky]90 [:fred :x 3 :y :sally]91 [:fred :x 4 :y :joe]92 [:fred :x 4 :y :bob]93 [:sally :x 1]94 [:sally :x 2]95 [:sally :x 3]96 [:sally :x 4]97 [:ben :y :bob]))100 (deftest test-apply-rule101 (is (= (apply-rule db2 empty-database (<- (:becky :y ?y) (:sally :x ?x)102 (:fred :x ?x :y ?y)103 (not! :ben :y ?y)104 (if not= ?x 3)))105 (datalog-database106 {107 :becky108 (datalog-relation109 ;; Schema110 #{:y}111 ;; Data112 #{113 {:y :joe}114 {:y :mary}115 {:y :becky}116 }117 ;; Indexes118 {119 })120 }))))125 (comment126 (run-tests)127 )129 ;; End of file