New cell history feature

This commit is contained in:
Simon Brooke 2023-07-22 21:16:33 +01:00
commit 4b1472d311
2 changed files with 54 additions and 7 deletions

View file

@ -22,8 +22,7 @@
further rules can be applied to that cell." further rules can be applied to that cell."
:author "Simon Brooke"} :author "Simon Brooke"}
mw-engine.core mw-engine.core
(:require [clojure.string :refer [starts-with?]] (:require [mw-engine.flow :refer [flow-world]]
[mw-engine.flow :refer [flow-world]]
[mw-engine.utils :refer [get-int-or-zero map-world rule-type]] [mw-engine.utils :refer [get-int-or-zero map-world rule-type]]
[taoensso.timbre :as l])) [taoensso.timbre :as l]))
@ -67,9 +66,17 @@
e e
(.getMessage e) (.getMessage e)
(-> rule meta :lisp) (-> rule meta :lisp)
cell))))] cell))))
rule-meta (meta rule)]
(when result (when result
(merge result (meta rule))))) (merge result
{:history (concat
(:history result)
(list {:rule (:source rule-meta)
:rule-type (:rule-type rule-meta)
:generation (get-int-or-zero
result
:generation)}))}))))
(defn- apply-rules (defn- apply-rules
"Derive a cell from this `cell` of this `world` by applying these `rules`." "Derive a cell from this `cell` of this `world` by applying these `rules`."
@ -84,7 +91,7 @@
(l/warn e (l/warn e
(format (format
"Error in `apply-rules`: `%s` (%s) while executing rules on cell `%s`" "Error in `apply-rules`: `%s` (%s) while executing rules on cell `%s`"
e (-> e .getClass .getName)
(.getMessage e) (.getMessage e)
cell)))))) cell))))))
cell)) cell))

View file

@ -2,8 +2,8 @@
interpretation of MicroWorld rule." interpretation of MicroWorld rule."
:author "Simon Brooke"} :author "Simon Brooke"}
mw-engine.utils mw-engine.utils
(:require (:require [clojure.math.combinatorics :as combo]
[clojure.math.combinatorics :as combo])) [clojure.string :refer [join]]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
@ -304,3 +304,43 @@
"Return the rule-type of this compiled `rule`." "Return the rule-type of this compiled `rule`."
[rule] [rule]
(:rule-type (meta rule))) (:rule-type (meta rule)))
(defn history-string
"Return the history of this `cell` as a string for presentation to the user."
[cell]
(join "\n"
(map #(format "%6d: %s" (:generation %) (:rule %))
(:history cell))))
(defn- extend-summary [summary rs rl event]
(str summary
(if rs (format "%d-%d (%d occurances): %s\n" rs
(:generation event)
rl
(:rule event))
(format "%d: %s\n" (:generation event)
(:rule event)))))
(defn summarise-history
"Return, as a string, a shorter summary of the history of this cell"
[cell]
(loop [history (rest (:history cell))
event (first (:history cell))
prev nil
rs nil
rl 0
summary ""]
(cond (nil? event) (extend-summary summary rs rl prev)
(= (:rule event) (:rule prev)) (recur
(rest history)
(first history)
event
(or rs (:generation event))
(inc rl)
summary)
:else (recur (rest history)
(first history)
event
nil
0
(extend-summary summary rs (inc rl) event)))))