aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/datalog/tests/test_literals.clj
blob: 36ee51471e2616bf3236245e7799a3f4883d9757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
;;  Copyright (c) Jeffrey Straszheim. All rights reserved.  The use and
;;  distribution terms for this software are covered by the Eclipse Public
;;  License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
;;  be found in the file epl-v10.html at the root of this distribution.  By
;;  using this software in any fashion, you are agreeing to be bound by the
;;  terms of this license.  You must not remove this notice, or any other,
;;  from this software.
;;
;;  test-literals.clj
;;
;;  A Clojure implementation of Datalog -- Literals tests
;;
;;  straszheimjeffrey (gmail)
;;  Created 25 Feburary 2009


(ns clojure.contrib.datalog.tests.test-literals
  (:use clojure.test)
  (:use clojure.contrib.datalog.literals
        clojure.contrib.datalog.database))


(def pl (eval (build-literal '(:fred :x ?x :y ?y :z 3))))
(def nl (eval (build-literal '(not! :fred :x ?x :y ?y :z 3))))
(def cl (eval (build-literal '(if > ?x 3))))

(def bl (eval (build-literal '(:fred))))

(def bns {:x '?x :y '?y :z 3})

(deftest test-build-literal
  (is (= (:predicate pl) :fred))
  (is (= (:term-bindings pl) bns))
  (is (= (:predicate nl) :fred))
  (is (= (:term-bindings nl) bns))
  (is (= (:symbol cl) '>))
  (is (= (:terms cl) '(?x 3)))
  (is ((:fun cl) [4 3]))
  (is (not ((:fun cl) [2 4])))
  (is (= (:predicate bl) :fred)))

(deftest test-literal-predicate
  (is (= (literal-predicate pl) :fred))
  (is (= (literal-predicate nl) :fred))
  (is (nil? (literal-predicate cl)))
  (is (= (literal-predicate bl) :fred)))

(deftest test-literal-columns
  (is (= (literal-columns pl) #{:x :y :z}))
  (is (= (literal-columns nl) #{:x :y :z}))
  (is (nil? (literal-columns cl)))
  (is (empty? (literal-columns bl))))

(deftest test-literal-vars
  (is (= (literal-vars pl) #{'?x '?y}))
  (is (= (literal-vars nl) #{'?x '?y}))
  (is (= (literal-vars cl) #{'?x}))
  (is (empty? (literal-vars bl))))

(deftest test-positive-vars
  (is (= (positive-vars pl) (literal-vars pl)))
  (is (nil? (positive-vars nl)))
  (is (nil? (positive-vars cl)))
  (is (empty? (positive-vars bl))))

(deftest test-negative-vars
  (is (nil? (negative-vars pl)))
  (is (= (negative-vars nl) (literal-vars nl)))
  (is (= (negative-vars cl) (literal-vars cl)))
  (is (empty? (negative-vars bl))))

(deftest test-negated?
  (is (not (negated? pl)))
  (is (negated? nl))
  (is (not (negated? cl))))

(deftest test-vs-from-cs
  (is (= (get-vs-from-cs pl #{:x}) #{'?x}))
  (is (empty? (get-vs-from-cs pl #{:z})))
  (is (= (get-vs-from-cs pl #{:x :r}) #{'?x}))
  (is (empty? (get-vs-from-cs pl #{}))))

(deftest test-cs-from-vs
  (is (= (get-cs-from-vs pl #{'?x}) #{:x}))
  (is (= (get-cs-from-vs pl #{'?x '?r}) #{:x}))
  (is (empty? (get-cs-from-vs pl #{}))))

(deftest test-literal-appropriate?
  (is (not (literal-appropriate? #{} pl)))
  (is (literal-appropriate? #{'?x} pl))
  (is (not (literal-appropriate? #{'?x} nl)))
  (is (literal-appropriate? #{'?x '?y} nl))
  (is (not (literal-appropriate? #{'?z} cl)))
  (is (literal-appropriate? #{'?x} cl)))

(deftest test-adorned-literal
  (is (= (literal-predicate (adorned-literal pl #{:x}))
         {:pred :fred :bound #{:x}}))
  (is (= (literal-predicate (adorned-literal nl #{:x :y :q}))
         {:pred :fred :bound #{:x :y}}))
  (is (= (:term-bindings (adorned-literal nl #{:x}))
         {:x '?x :y '?y :z 3}))
  (is (= (adorned-literal cl #{})
         cl)))

(deftest test-get-adorned-bindings
  (is (= (get-adorned-bindings (literal-predicate (adorned-literal pl #{:x})))
         #{:x}))
  (is (= (get-adorned-bindings (literal-predicate pl))
         nil)))

(deftest test-get-base-predicate
  (is (= (get-base-predicate (literal-predicate (adorned-literal pl #{:x})))
         :fred))
  (is (= (get-base-predicate (literal-predicate pl))
         :fred)))

(deftest test-magic-literal
  (is (= (magic-literal pl)
         {:predicate {:pred :fred :magic true}, :term-bindings {}, :literal-type :clojure.contrib.datalog.literals/literal}))
  (is (= (magic-literal (adorned-literal pl #{:x}))
         {:predicate {:pred :fred :magic true :bound #{:x}},
          :term-bindings {:x '?x},
          :literal-type :clojure.contrib.datalog.literals/literal})))

(comment
  (use 'clojure.contrib.stacktrace) (e)
  (use :reload 'clojure.contrib.datalog.literals)
)


(def db1 (make-database
           (relation :fred [:x :y])
           (index :fred :x)
           (relation :sally [:x])))

(def db2 (add-tuples db1
             [:fred :x 1 :y :mary]
             [:fred :x 1 :y :becky]
             [:fred :x 3 :y :sally]
             [:fred :x 4 :y :joe]
             [:sally :x 1]
             [:sally :x 2]))

(def lit1 (eval (build-literal '(:fred :x ?x :y ?y))))
(def lit2 (eval (build-literal '(not! :fred :x ?x))))
(def lit3 (eval (build-literal '(if > ?x ?y))))
(def lit4 (adorned-literal (eval (build-literal '(:joan :x ?x :y ?y))) #{:x}))

(deftest test-join-literal
  (is (= (set (join-literal db2 lit1 [{'?x 1} {'?x 2} {'?x 3}]))
         #{{'?x 1, '?y :mary} {'?x 1, '?y :becky} {'?x 3, '?y :sally}}))
  (is (= (join-literal db2 lit2 [{'?x 1} {'?x 2} {'?x 3}])
         [{'?x 2}]))
  (is (= (join-literal db2 lit3 [{'?x 1 '?y 2} {'?x 3 '?y 1}])
         [{'?x 3 '?y 1}])))
         
(deftest test-project-literal
  (is (= ((project-literal db2 lit4 [{'?x 1 '?y 3}{'?x 4 '?y 2}]) {:pred :joan :bound #{:x}})
         (datalog-relation
          ;; Schema
          #{:y :x}

          ;; Data
          #{
            {:x 1, :y 3}
            {:x 4, :y 2}
            }
          
          ;; Indexes
          {
           :x
           {
            4
            #{{:x 4, :y 2}}
            1
            #{{:x 1, :y 3}}
            }
           }))))



(comment
  (run-tests)
)

;; End of file