view src/clojure/contrib/datalog.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 and
2 ;; distribution terms for this software are covered by the Eclipse Public
3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
4 ;; be found in the file epl-v10.html at the root of this distribution. By
5 ;; using this software in any fashion, you are agreeing to be bound by the
6 ;; terms of this license. You must not remove this notice, or any other,
7 ;; from this software.
8 ;;
9 ;; datalog.clj
10 ;;
11 ;; A Clojure implementation of Datalog
12 ;;
13 ;; straszheimjeffrey (gmail)
14 ;; Created 2 March 2009
17 ;;; Please see the example.clj file in the datalog folder
20 (ns
21 ^{:author "Jeffrey Straszheim",
22 :doc "A Clojure implementation of Datalog"}
23 clojure.contrib.datalog
24 (:use clojure.contrib.datalog.rules
25 clojure.contrib.datalog.softstrat
26 clojure.contrib.datalog.database)
27 (:use [clojure.set :only (intersection)]
28 [clojure.contrib.except :only (throwf)]))
30 (defstruct work-plan
31 :work-plan ; The underlying structure
32 :rules ; The original rules
33 :query ; The original query
34 :work-plan-type) ; The type of plan
36 (defn- validate-work-plan
37 "Ensure any top level semantics are not violated"
38 [work-plan database]
39 (let [common-relations (-> work-plan :rules (intersection (-> database keys set)))]
40 (when (-> common-relations
41 empty?
42 not)
43 (throwf "The rules and database define the same relation(s): %s" common-relations))))
44 ; More will follow
46 (defn build-work-plan
47 "Given a list of rules and a query, build a work plan that can be
48 used to execute the query."
49 [rules query]
50 (struct-map work-plan
51 :work-plan (build-soft-strat-work-plan rules query)
52 :rules rules
53 :query query
54 :work-plan-type ::soft-stratified))
56 (defn run-work-plan
57 "Given a work plan, a database, and some query bindings, run the
58 work plan and return the results."
59 [work-plan database query-bindings]
60 (validate-work-plan work-plan database)
61 (evaluate-soft-work-set (:work-plan work-plan) database query-bindings))
64 ;; End of file