看起来nippy可以与标准序列化一起使用,因为逻辑事实数据库是用已可序列化的 Clojure 类型实现的。
(ns logos.serial
(:require [taoensso.nippy :as nippy]
[clojure.core.logic.pldb :as pldb]
[clojure.core.logic :as log]
[clojure.java.io]))
(defn freeze-to! [o target & {:keys [compressor] :or {compressor nippy/lz4hc-compressor}}]
(with-open [buff (clojure.java.io/output-stream target)
dos (java.io.DataOutputStream. buff)]
(nippy/freeze-to-out! dos o)))
(defn thaw-from! [target & {:keys [compressor] :or {compressor nippy/lz4hc-compressor}}]
(with-open [fis (clojure.java.io/input-stream target)
dis (java.io.DataInputStream. fis)]
(nippy/thaw-from-in! dis )))
(pldb/db-rel man p)
(pldb/db-rel woman p)
(pldb/db-rel likes p1 p2)
(pldb/db-rel fun p)
(def facts0
(pldb/db
[man 'Bob]
[man 'John]
[man 'Ricky]
[woman 'Mary]
[woman 'Martha]
[woman 'Lucy]
[likes 'Bob 'Mary]
[likes 'John 'Martha]
[likes 'Ricky 'Lucy]))
(def facts1 (-> facts0 (pldb/db-fact fun 'Lucy)))
查询和测试
(defn simple-query [db]
(pldb/with-db db
(log/run* [q]
(log/fresh [x y]
(fun y)
(likes x y)
(log/== q [x y])))))
;;logos.serial>(simple-query facts1)
;;([Ricky Lucy])
(freeze-to! facts1 "facts.lz4")
(def facts-thawed (thaw-from! "facts.lz4"))
;;logos.serial>(= facts1 facts-thawed)
;;logos.serial> (simple-query facts-thawed)
;;([Ricky Lucy])