如何构建与 DataScript 中的引用向量完全匹配的查询?

How to construct a query that matches exactly a vector of refs in DataScript?

设置 考虑以下电影和演员表的 DataScript 数据库,数据从 learndatalogtoday.org 中窃取:以下代码可以在 JVM/Clojure REPL 中执行或 ClojureScript REPL,只要 project.clj 包含 [datascript "0.15.0"] 作为依赖项。

(ns user
  (:require [datascript.core :as d]))

(def data 
  [["First Blood" ["Sylvester Stallone" "Brian Dennehy" "Richard Crenna"]]
   ["Terminator 2: Judgment Day" ["Linda Hamilton" "Arnold Schwarzenegger" "Edward Furlong" "Robert Patrick"]]
   ["The Terminator" ["Arnold Schwarzenegger" "Linda Hamilton" "Michael Biehn"]]
   ["Rambo III" ["Richard Crenna" "Sylvester Stallone" "Marc de Jonge"]]
   ["Predator 2" ["Gary Busey" "Danny Glover" "Ruben Blades"]]
   ["Lethal Weapon" ["Gary Busey" "Mel Gibson" "Danny Glover"]]
   ["Lethal Weapon 2" ["Mel Gibson" "Joe Pesci" "Danny Glover"]]
   ["Lethal Weapon 3" ["Joe Pesci" "Danny Glover" "Mel Gibson"]]
   ["Alien" ["Tom Skerritt" "Veronica Cartwright" "Sigourney Weaver"]]
   ["Aliens" ["Carrie Henn" "Sigourney Weaver" "Michael Biehn"]]
   ["Die Hard" ["Alan Rickman" "Bruce Willis" "Alexander Godunov"]]
   ["Rambo: First Blood Part II" ["Richard Crenna" "Sylvester Stallone" "Charles Napier"]]
   ["Commando" ["Arnold Schwarzenegger" "Alyssa Milano" "Rae Dawn Chong"]]
   ["Mad Max 2" ["Bruce Spence" "Mel Gibson" "Michael Preston"]]
   ["Mad Max" ["Joanne Samuel" "Steve Bisley" "Mel Gibson"]]
   ["RoboCop" ["Nancy Allen" "Peter Weller" "Ronny Cox"]]
   ["Braveheart" ["Sophie Marceau" "Mel Gibson"]]
   ["Mad Max Beyond Thunderdome" ["Mel Gibson" "Tina Turner"]]
   ["Predator" ["Carl Weathers" "Elpidia Carrillo" "Arnold Schwarzenegger"]]
   ["Terminator 3: Rise of the Machines" ["Nick Stahl" "Arnold Schwarzenegger" "Claire Danes"]]])

(def conn (d/create-conn {:film/cast {:db/valueType :db.type/ref
                                      :db/cardinality :db.cardinality/many}
                          :film/name {:db/unique :db.unique/identity
                                      :db/cardinality :db.cardinality/one}
                          :actor/name {:db/unique :db.unique/identity
                                       :db/cardinality :db.cardinality/one}}))
(def all-datoms (mapcat (fn [[film actors]]
                          (into [{:film/name film}] 
                                (map #(hash-map :actor/name %) actors)))
                        data))
(def all-relations (mapv (fn [[film actors]]
                           {:db/id [:film/name film]
                            :film/cast (mapv #(vector :actor/name %) actors)}) data))

(d/transact! conn all-datoms)
(d/transact! conn all-relations)

描述简而言之,这个数据库中有两种实体——电影和演员(这个词意在去性别化)——以及三种数据:

问题 我想构建一个查询,询问:哪些电影有这些 N 演员,和这些 N 演员单独,作为唯一的星星出现,因为N>=2?

例如,RoboCop 由 Nancy Allen、Peter Weller、Ronny Cox 主演,但 no 电影仅由其中的前两个主演,Allen 和 Weller。因此,我希望以下查询产生空集:

(d/q '[:find ?film-name
       :where
       [?film :film/name ?film-name]
       [?film :film/cast ?actor-1]
       [?film :film/cast ?actor-2]
       [?actor-1 :actor/name "Nancy Allen"]
       [?actor-2 :actor/name "Peter Weller"]]
     @conn)
; => #{["RoboCop"]}

但是,查询是有缺陷的,因为我不知道如何表达任何匹配都应该排除任何不是艾伦或韦勒的演员——再次,我想找到只有艾伦和韦勒没有合作的电影任何其他演员,所以我想修改上面的查询以生成空集。我如何调整此查询以强制执行此要求?

因为 DataScript 没有否定(截至 2016 年 5 月),我认为 'pure' Datalog 中的一个静态查询不可能做到这一点。

我要走的路是:

  1. 以编程方式构建查询以添加声明演员表必须包含 N 个演员的 N 个子句
  2. 添加一个谓词函数,给定一部电影、数据库和一组演员 ID,使用 EAVT 索引查找每部电影是否有不在该组中的演员。

这是一个基本的实现

(defn only-those-actors? [db movie actors]
  (->> (datoms db :eavt movie :film/cast) seq
    (every? (fn [[_ _ actor]]
                (contains? actors actor)))
    ))

(defn find-movies-with-exact-cast [db actors-names]
  (let [actors (set (d/q '[:find [?actor ...] :in $ [?name ...] ?only-those-actors :where
                           [?actor :actor/name ?name]]
                      db actors-names))
        query {:find '[[?movie ...]]
               :in '[$ ?actors ?db]
               :where
               (concat
                 (for [actor actors]
                   ['?movie :film/cast actor])
                 [['(only-those-actors? ?db ?movie ?actors)]])}]
    (d/q query db actors db only-those-actors?)))

您可以将谓词 fun 和 d/entity 一起使用,以通过实体的 :film/cast 字段过滤数据。在 Datascript 不支持否定(not 运算符等)之前,这种方法看起来更加直接。

查看Datascript测试用例中的行(= a (:age (d/entity db e)) here

[{:db/id 1 :name "Ivan" :age 10}
 {:db/id 2 :name "Ivan" :age 20}
 {:db/id 3 :name "Oleg" :age 10}
 {:db/id 4 :name "Oleg" :age 20}]

...

(let [pred (fn [db e a]
             (= a (:age (d/entity db e))))]
  (is (= (q/q '[:find ?e
                :in $ ?pred
                :where [?e :age ?a]
                       [(?pred $ ?e 10)]]
              db pred)
         #{[1] [3]})))))

在你的例子中,谓词主体可能看起来像这样

(clojure.set/subset? actors (:film/cast (d/entity db e))

关于性能,d/entity 调用速度很快,因为它是按索引查找。