Created
June 28, 2022 16:49
-
-
Save hiredman/3fa9bbd53b02413a2c3301453d625d80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn generate-state-machine [init-state inputs states state transition? accum] | |
(gen/sized | |
(fn [size] | |
(if (zero? size) | |
(let [prev (into {} (for [[a b] (partion-all 2 1 (sort inputs))] [b a]))] | |
((fn f [events] | |
(gen/gen-pure | |
(rose/make-rose | |
events | |
(when (seq events) | |
(map | |
(fn [events] | |
(rose events (f events))) | |
(filter | |
(fn [events] | |
(reduce | |
(fn [state event] | |
(if-let [n (seq (for [s state | |
x states | |
:when (transition? s event x)] | |
x))] | |
(set n) | |
(reduced false))) | |
#{init-state} | |
events)) | |
(concat | |
(for [i (range (count events)) | |
:let [events (vec (keep-indexed (fn [w e] (when (not= w i) e)) events))]] | |
events) | |
(for [i (range (count events)) | |
:let [n (ord (nth events i))] | |
:when n] | |
(assoc events i n))))))))) | |
accum)) | |
(gen/bind (gen/one-of | |
(vec (for [i inputs | |
end-state states | |
:when (transition? state i end-state)] | |
(gen/return [i end-state])))) | |
(fn [[input end-state]] | |
(gen/scale | |
dec | |
(generate-state-machine init-state inputs states end-state transition? (conj accum input))))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment