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
|
;; 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-database.clj
;;
;; A Clojure implementation of Datalog -- Database
;;
;; straszheimjeffrey (gmail)
;; Created 12 Feburary 2009
(ns clojure.contrib.datalog.tests.test-database
(:use clojure.test
clojure.contrib.datalog.database))
(def test-db
(make-database
(relation :fred [:mary :sue])
(index :fred :mary)
(relation :sally [:jen :becky :joan])
(index :sally :jen)
(index :sally :becky)))
(deftest test-make-database
(is (= test-db
(datalog-database
{:sally (datalog-relation
#{:jen :joan :becky}
#{}
{:becky {}
:jen {}})
:fred (datalog-relation
#{:sue :mary}
#{}
{:mary {}})}))))
(deftest test-ensure-relation
(is (contains? (ensure-relation test-db :bob [:sam :george] [:sam]) :bob))
(is (contains? (ensure-relation test-db :fred [:mary :sue] [:mary]) :fred))
(is (thrown? Exception (ensure-relation test-db :fred [:bob :joe] []))))
(deftest test-add-tuple
(let [new-db (add-tuple test-db :fred {:mary 1 :sue 2})]
(is (= (select new-db :fred {:mary 1}) [{:mary 1 :sue 2}])))
(is (thrown? Exception (add-tuple test-db :fred {:mary 1}))))
(def test-db-1
(add-tuples test-db
[:fred :mary 1 :sue 2]
[:fred :mary 2 :sue 3]
[:sally :jen 1 :becky 2 :joan 0]
[:sally :jen 1 :becky 4 :joan 3]
[:sally :jen 1 :becky 3 :joan 0]
[:sally :jen 1 :becky 2 :joan 3]
[:fred :mary 1 :sue 1]
[:fred :mary 3 :sue 1]))
(deftest test-add-tuples
(is (= test-db-1
(datalog-database
{:sally (datalog-relation
#{:jen :joan :becky}
#{{:jen 1, :joan 0, :becky 3}
{:jen 1, :joan 0, :becky 2}
{:jen 1, :joan 3, :becky 2}
{:jen 1, :joan 3, :becky 4}}
{:becky {3
#{{:jen 1, :joan 0, :becky 3}}
4
#{{:jen 1, :joan 3, :becky 4}}
2
#{{:jen 1, :joan 0, :becky 2}
{:jen 1, :joan 3, :becky 2}}}
:jen {1
#{{:jen 1, :joan 0, :becky 3}
{:jen 1, :joan 0, :becky 2}
{:jen 1, :joan 3, :becky 2}
{:jen 1, :joan 3, :becky 4}}}})
:fred (datalog-relation
#{:sue :mary}
#{{:sue 2, :mary 1}
{:sue 1, :mary 1}
{:sue 3, :mary 2}
{:sue 1, :mary 3}}
{:mary {3
#{{:sue 1, :mary 3}}
2
#{{:sue 3, :mary 2}}
1
#{{:sue 2, :mary 1}
{:sue 1, :mary 1}}}})}))))
(deftest test-remove-tuples
(let [db (reduce #(apply remove-tuple %1 (first %2) (next %2))
test-db-1
[[:fred {:mary 1 :sue 1}]
[:fred {:mary 3 :sue 1}]
[:sally {:jen 1 :becky 2 :joan 0}]
[:sally {:jen 1 :becky 4 :joan 3}]])]
(is (= db
(datalog-database
{:sally (datalog-relation
#{:jen :joan :becky}
#{{:jen 1, :joan 0, :becky 3}
{:jen 1, :joan 3, :becky 2}}
{:becky
{3
#{{:jen 1, :joan 0, :becky 3}}
2
#{{:jen 1, :joan 3, :becky 2}}}
:jen
{1
#{{:jen 1, :joan 0, :becky 3}
{:jen 1, :joan 3, :becky 2}}}})
:fred (datalog-relation
#{:sue :mary}
#{{:sue 2, :mary 1}
{:sue 3, :mary 2}}
{:mary
{2
#{{:sue 3, :mary 2}}
1
#{{:sue 2, :mary 1}}}})})))))
(deftest test-select
(is (= (set (select test-db-1 :sally {:jen 1 :becky 2}))
#{{:jen 1 :joan 0 :becky 2} {:jen 1 :joan 3 :becky 2}}))
(is (= (set (select test-db-1 :fred {:sue 1})))
#{{:mary 3 :sue 1} {:mary 1 :sue 1}})
(is (empty? (select test-db-1 :sally {:joan 5 :jen 1}))))
(deftest test-any-match?
(is (any-match? test-db-1 :fred {:mary 3}))
(is (any-match? test-db-1 :sally {:jen 1 :becky 2 :joan 3}))
(is (not (any-match? test-db-1 :sally {:jen 5})))
(is (not (any-match? test-db-1 :fred {:mary 1 :sue 5}))))
(comment
(run-tests)
)
;; End of file
|