rlm@10: ;; Copyright (c) Jeffrey Straszheim. All rights reserved. The use and rlm@10: ;; distribution terms for this software are covered by the Eclipse Public rlm@10: ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can rlm@10: ;; be found in the file epl-v10.html at the root of this distribution. By rlm@10: ;; using this software in any fashion, you are agreeing to be bound by the rlm@10: ;; terms of this license. You must not remove this notice, or any other, rlm@10: ;; from this software. rlm@10: ;; rlm@10: ;; test-literals.clj rlm@10: ;; rlm@10: ;; A Clojure implementation of Datalog -- Literals tests rlm@10: ;; rlm@10: ;; straszheimjeffrey (gmail) rlm@10: ;; Created 25 Feburary 2009 rlm@10: rlm@10: rlm@10: (ns clojure.contrib.datalog.tests.test-literals rlm@10: (:use clojure.test) rlm@10: (:use clojure.contrib.datalog.literals rlm@10: clojure.contrib.datalog.database)) rlm@10: rlm@10: rlm@10: (def pl (eval (build-literal '(:fred :x ?x :y ?y :z 3)))) rlm@10: (def nl (eval (build-literal '(not! :fred :x ?x :y ?y :z 3)))) rlm@10: (def cl (eval (build-literal '(if > ?x 3)))) rlm@10: rlm@10: (def bl (eval (build-literal '(:fred)))) rlm@10: rlm@10: (def bns {:x '?x :y '?y :z 3}) rlm@10: rlm@10: (deftest test-build-literal rlm@10: (is (= (:predicate pl) :fred)) rlm@10: (is (= (:term-bindings pl) bns)) rlm@10: (is (= (:predicate nl) :fred)) rlm@10: (is (= (:term-bindings nl) bns)) rlm@10: (is (= (:symbol cl) '>)) rlm@10: (is (= (:terms cl) '(?x 3))) rlm@10: (is ((:fun cl) [4 3])) rlm@10: (is (not ((:fun cl) [2 4]))) rlm@10: (is (= (:predicate bl) :fred))) rlm@10: rlm@10: (deftest test-literal-predicate rlm@10: (is (= (literal-predicate pl) :fred)) rlm@10: (is (= (literal-predicate nl) :fred)) rlm@10: (is (nil? (literal-predicate cl))) rlm@10: (is (= (literal-predicate bl) :fred))) rlm@10: rlm@10: (deftest test-literal-columns rlm@10: (is (= (literal-columns pl) #{:x :y :z})) rlm@10: (is (= (literal-columns nl) #{:x :y :z})) rlm@10: (is (nil? (literal-columns cl))) rlm@10: (is (empty? (literal-columns bl)))) rlm@10: rlm@10: (deftest test-literal-vars rlm@10: (is (= (literal-vars pl) #{'?x '?y})) rlm@10: (is (= (literal-vars nl) #{'?x '?y})) rlm@10: (is (= (literal-vars cl) #{'?x})) rlm@10: (is (empty? (literal-vars bl)))) rlm@10: rlm@10: (deftest test-positive-vars rlm@10: (is (= (positive-vars pl) (literal-vars pl))) rlm@10: (is (nil? (positive-vars nl))) rlm@10: (is (nil? (positive-vars cl))) rlm@10: (is (empty? (positive-vars bl)))) rlm@10: rlm@10: (deftest test-negative-vars rlm@10: (is (nil? (negative-vars pl))) rlm@10: (is (= (negative-vars nl) (literal-vars nl))) rlm@10: (is (= (negative-vars cl) (literal-vars cl))) rlm@10: (is (empty? (negative-vars bl)))) rlm@10: rlm@10: (deftest test-negated? rlm@10: (is (not (negated? pl))) rlm@10: (is (negated? nl)) rlm@10: (is (not (negated? cl)))) rlm@10: rlm@10: (deftest test-vs-from-cs rlm@10: (is (= (get-vs-from-cs pl #{:x}) #{'?x})) rlm@10: (is (empty? (get-vs-from-cs pl #{:z}))) rlm@10: (is (= (get-vs-from-cs pl #{:x :r}) #{'?x})) rlm@10: (is (empty? (get-vs-from-cs pl #{})))) rlm@10: rlm@10: (deftest test-cs-from-vs rlm@10: (is (= (get-cs-from-vs pl #{'?x}) #{:x})) rlm@10: (is (= (get-cs-from-vs pl #{'?x '?r}) #{:x})) rlm@10: (is (empty? (get-cs-from-vs pl #{})))) rlm@10: rlm@10: (deftest test-literal-appropriate? rlm@10: (is (not (literal-appropriate? #{} pl))) rlm@10: (is (literal-appropriate? #{'?x} pl)) rlm@10: (is (not (literal-appropriate? #{'?x} nl))) rlm@10: (is (literal-appropriate? #{'?x '?y} nl)) rlm@10: (is (not (literal-appropriate? #{'?z} cl))) rlm@10: (is (literal-appropriate? #{'?x} cl))) rlm@10: rlm@10: (deftest test-adorned-literal rlm@10: (is (= (literal-predicate (adorned-literal pl #{:x})) rlm@10: {:pred :fred :bound #{:x}})) rlm@10: (is (= (literal-predicate (adorned-literal nl #{:x :y :q})) rlm@10: {:pred :fred :bound #{:x :y}})) rlm@10: (is (= (:term-bindings (adorned-literal nl #{:x})) rlm@10: {:x '?x :y '?y :z 3})) rlm@10: (is (= (adorned-literal cl #{}) rlm@10: cl))) rlm@10: rlm@10: (deftest test-get-adorned-bindings rlm@10: (is (= (get-adorned-bindings (literal-predicate (adorned-literal pl #{:x}))) rlm@10: #{:x})) rlm@10: (is (= (get-adorned-bindings (literal-predicate pl)) rlm@10: nil))) rlm@10: rlm@10: (deftest test-get-base-predicate rlm@10: (is (= (get-base-predicate (literal-predicate (adorned-literal pl #{:x}))) rlm@10: :fred)) rlm@10: (is (= (get-base-predicate (literal-predicate pl)) rlm@10: :fred))) rlm@10: rlm@10: (deftest test-magic-literal rlm@10: (is (= (magic-literal pl) rlm@10: {:predicate {:pred :fred :magic true}, :term-bindings {}, :literal-type :clojure.contrib.datalog.literals/literal})) rlm@10: (is (= (magic-literal (adorned-literal pl #{:x})) rlm@10: {:predicate {:pred :fred :magic true :bound #{:x}}, rlm@10: :term-bindings {:x '?x}, rlm@10: :literal-type :clojure.contrib.datalog.literals/literal}))) rlm@10: rlm@10: (comment rlm@10: (use 'clojure.contrib.stacktrace) (e) rlm@10: (use :reload 'clojure.contrib.datalog.literals) rlm@10: ) rlm@10: rlm@10: rlm@10: (def db1 (make-database rlm@10: (relation :fred [:x :y]) rlm@10: (index :fred :x) rlm@10: (relation :sally [:x]))) rlm@10: rlm@10: (def db2 (add-tuples db1 rlm@10: [:fred :x 1 :y :mary] rlm@10: [:fred :x 1 :y :becky] rlm@10: [:fred :x 3 :y :sally] rlm@10: [:fred :x 4 :y :joe] rlm@10: [:sally :x 1] rlm@10: [:sally :x 2])) rlm@10: rlm@10: (def lit1 (eval (build-literal '(:fred :x ?x :y ?y)))) rlm@10: (def lit2 (eval (build-literal '(not! :fred :x ?x)))) rlm@10: (def lit3 (eval (build-literal '(if > ?x ?y)))) rlm@10: (def lit4 (adorned-literal (eval (build-literal '(:joan :x ?x :y ?y))) #{:x})) rlm@10: rlm@10: (deftest test-join-literal rlm@10: (is (= (set (join-literal db2 lit1 [{'?x 1} {'?x 2} {'?x 3}])) rlm@10: #{{'?x 1, '?y :mary} {'?x 1, '?y :becky} {'?x 3, '?y :sally}})) rlm@10: (is (= (join-literal db2 lit2 [{'?x 1} {'?x 2} {'?x 3}]) rlm@10: [{'?x 2}])) rlm@10: (is (= (join-literal db2 lit3 [{'?x 1 '?y 2} {'?x 3 '?y 1}]) rlm@10: [{'?x 3 '?y 1}]))) rlm@10: rlm@10: (deftest test-project-literal rlm@10: (is (= ((project-literal db2 lit4 [{'?x 1 '?y 3}{'?x 4 '?y 2}]) {:pred :joan :bound #{:x}}) rlm@10: (datalog-relation rlm@10: ;; Schema rlm@10: #{:y :x} rlm@10: rlm@10: ;; Data rlm@10: #{ rlm@10: {:x 1, :y 3} rlm@10: {:x 4, :y 2} rlm@10: } rlm@10: rlm@10: ;; Indexes rlm@10: { rlm@10: :x rlm@10: { rlm@10: 4 rlm@10: #{{:x 4, :y 2}} rlm@10: 1 rlm@10: #{{:x 1, :y 3}} rlm@10: } rlm@10: })))) rlm@10: rlm@10: rlm@10: rlm@10: (comment rlm@10: (run-tests) rlm@10: ) rlm@10: rlm@10: ;; End of file