has anyone try odoyle on babashka? i can only find one relevant discussion on slack but i can use it with some simple code on babashka without any problem but not sure if it is fully compatible https://clojurians.slack.com/archives/CLX41ASCS/p1605959727433400
That link points to something from four years ago, bb has come a long way since then (and now includes clojure.spec.alpha which tripped it up back then it seems)
gocha, thanks michiel!
leave some working snippet for the future explorer: (generated with gemini and can run with no errors)
(ns bb.my-project.main
(:require [odoyle.rules :as o]))
;; Define the rules for our reactive counter
(def rules
(o/ruleset
{;; Rule to increment the counter when a :tick happens
::increment-counter
[:what
;; Bind the current counter value.
;; {:then not=} ensures this rule only triggers if the counter *value* changes,
;; preventing potential issues if we re-inserted the same value,
;; and makes it react to the initial insertion.
[::counter ::value current-value {:then not=}]
;; This rule requires a :tick fact to exist. Bind its value.
[::tick ::now tick-id]
:then
;; When triggered, insert the incremented value back into the session.
;; Using o/insert! implicitly updates the session within the rule.
(println "-> Tick received! Incrementing counter...")
(o/insert! ::counter ::value (inc current-value))
;; Retract the specific tick fact that triggered this rule instance.
(o/retract! ::tick ::now)]
;; Rule to print the counter whenever its value changes
::print-counter
[:what
;; Bind the counter value. {:then not=} ensures this only triggers
;; when the value is different from the previous match for this rule.
[::counter ::value current-value {:then not=}]
:then
;; Print the new value.
(println (str "<- Counter changed! New value: " current-value))]}))
;; Initialize the session atom with our rules
(def *session
(atom (reduce o/add-rule (o/->session) rules)))
(defn initialize-counter! []
(println "Initializing counter to 0...")
(swap! *session
(fn [session]
(-> session
;; Insert the starting value for the counter
(o/insert ::counter ::value 0)
;; Fire rules to process the initial state
o/fire-rules))))
(defn send-tick! []
(println "Sending tick...")
(swap! *session
(fn [session]
(-> session
;; Insert a :tick fact. Using the current time ensures it's unique
;; each time, guaranteeing the ::increment-counter rule can trigger again.
(o/insert ::tick ::now (System/currentTimeMillis))
;; Fire rules to process the tick and subsequent changes
o/fire-rules))))
;; Main function to run the demo
(defn -main [& _args]
(println "--- O'Doyle Reactivity Demo ---")
(initialize-counter!) ; Set up the initial state
;; Wait a bit for clarity in output
(Thread/sleep 1000)
(send-tick!) ; Send the first tick
(Thread/sleep 1000)
(send-tick!) ; Send another tick
(Thread/sleep 1000)
(send-tick!) ; And one more
(println "--- Demo Finished ---"))
;; You can also run these interactively in a REPL connected to Babashka:
;; (initialize-counter!)
;; (send-tick!)
Thanks :)
It seems all the unit tests pass:
$ bb -Sdeps '{:paths ["src" "test"] :deps {local/local {:local/root "."}}}' test.clj
Testing odoyle.rules-test
Ran 37 tests containing 107 assertions.
0 failures, 0 errors.https://blog.michielborkent.nl/babashka-test-runner.html
Is there a way to test namespaces that don't end with "-test"?
I tried adding :patterns [".*"] in the :exec-args, but then using --nses in the command line doesn't work. 😕
Perhaps patterns takes priority over nses in the cognitect test runner?
I think it does both: https://github.com/cognitect-labs/test-runner/blob/3f288f1f16d167723ad87cc35b1dfee3c1681e10/src/cognitect/test_runner.clj#L16-L17
so you have to undo the patterns I guess
this seems to work for me:
bb test:bb --nses foo --patterns ''or --patterns nil
Ah yes it works! Thank you very much 🙏