Skip to content

Instantly share code, notes, and snippets.

@oskarkv

oskarkv/queriestest.clj Secret

Last active May 7, 2020
Embed
What would you like to do?
(ns clarax.queriestest
(:require
[clara.rules :as cr]
[clara.rules.accumulators :as acc]))
(defrecord Player [x y z])
(cr/defquery q1
[:?x :?y]
[?p <- (acc/all) :from [Player (= ?x x) (= ?y y)]])
(defn q1test []
(println "with (= ?x x)")
(-> (cr/mk-session [q1] :cache false)
(cr/insert (->Player 1 1 1))
(cr/insert (->Player 2 2 2))
cr/fire-rules
(cr/query q1 :?x 1 :?y 1)))
(cr/defquery q2
[:?x :?y]
[?p <- (acc/all) :from [Player]])
(defn q2test []
(println "with no ?x or ?y in body")
(-> (cr/mk-session [q2] :cache false)
(cr/insert (->Player 1 1 1))
(cr/insert (->Player 2 2 2))
cr/fire-rules
(cr/query q2 :?x 1 :?y 1)))
(cr/defquery q3
[]
[?p <- (acc/all) :from [Player]])
(defn q3test []
(println "with no argsvector")
(-> (cr/mk-session [q3] :cache false)
(cr/insert (->Player 1 1 1))
(cr/insert (->Player 2 2 2))
cr/fire-rules
(cr/query q3 :?x 1 :?y 1)))
(cr/defquery q4
[:?x :?y]
[?p <- (acc/all) :from [Player]])
(defn q4test []
(println "argsvector but no args passed in")
(-> (cr/mk-session [q4] :cache false)
(cr/insert (->Player 1 1 1))
(cr/insert (->Player 2 2 2))
cr/fire-rules
(cr/query q4)))
(cr/defquery q5
[]
[?p <- (acc/all) :from [Player]])
(defn q5test []
(println "no args whatsoever")
(-> (cr/mk-session [q5] :cache false)
(cr/insert (->Player 1 1 1))
(cr/insert (->Player 2 2 2))
cr/fire-rules
(cr/query q5)))
(cr/defquery q6
[:?x :?y]
[?p <- (acc/all) :from [Player (> x ?x) (> y ?y)]])
(defn q6test []
(println "with (> x ?x)")
(-> (cr/mk-session [q6] :cache false)
(cr/insert (->Player 1 1 1))
(cr/insert (->Player 2 2 2))
cr/fire-rules
(cr/query q6 :?x 1 :?y 1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.