aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/datalog/seminaive.clj
blob: b013996df29e3fba69a86588f5bd0608393414ee (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
;;  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.
;;
;;  seminaive.clj
;;
;;  A Clojure implementation of Datalog -- Semi Naive Evaluation
;;
;;  straszheimjeffrey (gmail)
;;  Created 5 March 2009


(ns clojure.contrib.datalog.seminaive
  (:use clojure.contrib.datalog.util
        clojure.contrib.datalog.database
        clojure.contrib.datalog.literals
        clojure.contrib.datalog.rules
        clojure.contrib.datalog.magic)
  (:use [clojure.set :only (union intersection difference)])
  (:use [clojure.contrib.seq-utils :only (indexed)])
  (:require [clojure.contrib.graph :as graph]))


;;;;  WORK IN PROGRESS !!!


;;; Delta Rules

(defn- compute-delta-rules*
  "Perform the delta computation of a rule, where i-rules is the set
   of intensional predicates."
  [r i-preds]
  (let [head (:head r)
        head-negated (negated-literal head)
        delta-head (delta-literal head)
        body (:body r)
        build-body (fn [left lit right]
                     (assoc r :head delta-head
                              :body (concat left
                                            [(delta-literal lit)]
                                            right
                                            [head-negated])))
        new-rules (loop [lit (first body)
                        left []
                        right (next body)
                        results []]
                   (if (nil? lit)
                     results
                     (let [new-results (if (-> lit literal-predicate i-preds)
                                         (conj results (build-body left lit right))
                                         results)]
                       (recur (first right)
                              (conj left lit)
                              (next right)
                              new-results))))]
    (if (empty? new-rules)
      [(assoc r :head delta-head :body (conj (vec body) head-negated))]
      new-rules)))


(defn- compute-delta-rules
  "Compute the delta rules of a rules set, where i-rules is the set of
   intensional predicates."
  [rs i-preds]
  (mapcat #(compute-delta-rules* % i-preds) rs))


(comment
  (compute-delta-rules* (<- (:anc :x ?x :y ?y) (:anc :x ?x :y ?z) (:anc :x ?z :y ?y)) #{:anc})

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


;; End of file