utils.cljs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. (ns microtables-frontend.utils
  2. (:require
  3. ["mathjs" :as mathjs]))
  4. ; to add an npm package to shadow-cljs:
  5. ; https://clojureverse.org/t/guide-on-how-to-use-import-npm-modules-packages-in-clojurescript/2298
  6. ; https://shadow-cljs.github.io/docs/UsersGuide.html#npm
  7. (defn highest [dir data] (apply max (map dir data)))
  8. (defn highest-col
  9. "Return the highest column (letter) for which there is a non-empty cell"
  10. [data]
  11. ; choose the "max" (alphabetical order) value among the longest keys
  12. (apply max (val (apply max-key key (group-by #(.-length %) (keys data))))))
  13. (defn highest-row
  14. "Return the highest row (number) for which there is a non-empty cell"
  15. [data]
  16. ; get all the row keys from all the column objects (and flatten), then pick the max
  17. (apply max (flatten (map keys (vals data)))))
  18. (defn increment-letter-code [s]
  19. (let [l (last s)]
  20. (cond
  21. (empty? s) [65]
  22. (= l 90) (conj (increment-letter-code (subvec s 0 (dec (count s)))) 65)
  23. :else (conj (subvec s 0 (dec (count s))) (inc l)))))
  24. (defn next-letter [lc]
  25. (apply str (map char (increment-letter-code (mapv #(.charCodeAt % 0) lc)))))
  26. (def col-letters (iterate next-letter "A"))
  27. (defn get-datum [data c r]
  28. (some #(if (and (= c (:col %)) (= r (:row %))) %) data))
  29. (def parse-variables (memoize (fn [expression]
  30. (as-> (js->clj (.parse mathjs expression)) $
  31. (.filter $ #(true? (.-isSymbolNode %)))
  32. (map #(.-name %) $)
  33. (map #(.toUpperCase %) $)
  34. (filter #(re-matches #"[A-Z]+[0-9]+" %) $)))))
  35. (def str->rc (memoize (fn [s]
  36. (let [c (re-find #"^[A-Z]+" s)
  37. r (.parseInt js/window (re-find #"[0-9]+$" s))]
  38. {:row r :col c}))))
  39. (defn add-parsed-variables [datum]
  40. (if (= (first (:value datum)) "=")
  41. (let [vars (parse-variables (subs (:value datum) 1))
  42. refs (map str->rc vars)]
  43. (-> datum (assoc :vars vars) (assoc :refs refs) (dissoc :error)))
  44. (-> datum (dissoc :vars) (dissoc :refs) (dissoc :display) (dissoc :error))))
  45. ; leave in the :inbound references, since they probably have not have changed
  46. (defn add-references
  47. "Parses the expression in the value of a datum, and adds vars and refs as necessary"
  48. [datum]
  49. (if (= (first (:value datum)) "=")
  50. (let [vars (parse-variables (subs (:value datum) 1))
  51. refs (map str->rc vars)]
  52. (-> datum
  53. (assoc :vars vars)
  54. (assoc :refs refs)
  55. (dissoc :error)))
  56. (-> datum
  57. (dissoc :vars)
  58. (dissoc :refs)
  59. (dissoc :display)
  60. (dissoc :error))))
  61. (defn add-parsed-variables-to-specific-datum
  62. "Parse variables from the value of a datum and add in :vars and :refs (for swap! data-atom).
  63. If the value does not contain a fomula, remove any :vars and :refs that may have been there."
  64. [c r data] (map #(if (and (= (:col %) c) (= (:row %) r))
  65. (add-parsed-variables %)
  66. %) data))
  67. ; the references in the data are a set of disconnected, doubly-linked trees
  68. ;TODO: rather than denotify all, then re-notify all, maybe use a diff? maybe on small scales it's not worth it?
  69. (defn denotify-references
  70. "Remove references in all cells formerly referenced by this cell"
  71. [data origin refs]
  72. (if (empty? refs)
  73. data
  74. (let [target (first refs)
  75. de-notified (update-in data [(:col target) (:row target) :inbound] (partial filter #(not= % origin)))]
  76. (recur de-notified origin (rest refs)))))
  77. (defn notify-references
  78. "Update references in all cells referenced by this cell"
  79. [data origin refs]
  80. (if (empty? refs)
  81. data
  82. (let [target (first refs)
  83. notified (update-in data [(:col target) (:row target) :inbound] conj origin)]
  84. (recur notified origin (rest refs)))))
  85. (defn create-all-references
  86. "Starting from a clean slate, add in all references. This wipes any references that may have been present."
  87. [data]
  88. (reduce-kv
  89. (fn [columns c curr-column]
  90. (assoc columns c (reduce-kv
  91. (fn [rows r datum]
  92. (assoc rows r (add-references (dissoc (dissoc datum :refs) :inbound))))
  93. {}
  94. curr-column)))
  95. {}
  96. data))
  97. ;TODO: re-write create-all-references to use walk-modify-data instead
  98. (defn walk-modify-data
  99. "Walks through the data map and updates each datum by applying f (a function accepting col, row, datum)."
  100. [data f]
  101. (reduce-kv
  102. (fn [columns c curr-column]
  103. (assoc columns c (reduce-kv
  104. (fn [rows r datum]
  105. (assoc rows r (f c r datum)))
  106. {}
  107. curr-column)))
  108. {}
  109. data))
  110. ;(create-all-back-references (create-all-references {"A" {1 {:value "=B2"}} "B" {2 {:value "=B3"} 3 {:value "=A1"}}}))
  111. ;(= (walk-modify-data (:alt-table-data microtables-frontend.db/default-db) #(-> %3 (dissoc :refs) (dissoc :inbound) (add-references))) (walk-modify-data (:alt-table-data microtables-frontend.db/default-db) #(add-references (dissoc (dissoc %3 :refs) :inbound))) (create-all-references (:alt-table-data microtables-frontend.db/default-db)))
  112. (defn walk-get-refs
  113. "Walks through the data map and returns a list of :col/:row maps for each cell which satisfies the predicate (a function accepting col, row, datum)."
  114. [data pred]
  115. (reduce-kv (fn [l c column] (concat l (map (fn [[r _]] {:col c :row r}) (filter (fn [[r datum]] (pred c r datum)) column)))) '() data))
  116. (defn create-all-back-references
  117. "Assuming all references have been added, insert all back references."
  118. [data]
  119. (loop [data data
  120. formulas (walk-get-refs data #(= (first (:value %3)) "="))]
  121. (if (empty? formulas)
  122. data
  123. (let [origin (first formulas)
  124. refs (get-in data [(:col origin) (:row origin) :refs])
  125. updated-one (notify-references data origin refs)]
  126. (recur updated-one (rest formulas))))))
  127. (defn set-dirty-flags
  128. "Sets the target cell to \"dirty\" and recursively repeat with its back-references all the way up. Returns the new data set."
  129. ([data c r]
  130. (set-dirty-flags data (list {:col c :row r})))
  131. ([data queue]
  132. (if (empty? queue)
  133. data
  134. (let [cur (first queue)
  135. c (:col cur)
  136. r (:row cur)
  137. datum (get-in data [c r])]
  138. (if (true? (:dirty datum))
  139. (recur data (rest queue))
  140. (let [new-data (assoc-in data [c r :dirty] true)
  141. new-queue (concat (rest queue) (:inbound datum))]
  142. (recur new-data new-queue)))))))
  143. ;(walk-get-refs (set-dirty-flags (create-all-back-references (create-all-references (:alt-table-data microtables-frontend.db/default-db))) "C" 5) #(true? (:dirty %3)))
  144. (defn change-datum-value
  145. "Modify the value of a datum in the table, and update all applicable references"
  146. [data c r value]
  147. (let [datum (get-in data [c r])
  148. updated (assoc datum :value value)]
  149. (-> data
  150. (assoc-in [c r :value] value)
  151. (set-dirty-flags c r))))
  152. (defn reset-references
  153. "If there has been a change to which cells are referenced by this cell, then change the necessary back-references to this cell."
  154. [data c r]
  155. (let [datum (get-in data [c r])
  156. parsed (add-references datum)]
  157. (if (= (:refs datum) (:refs parsed))
  158. data
  159. (-> data
  160. (assoc-in [c r] parsed)
  161. (denotify-references {:col c :row r} (:refs datum))
  162. (notify-references {:col c :row r} (:refs parsed))))))
  163. (def evaluate-expression
  164. "Convert (via mathjs) an expression string to a final answer (also a string). A map of variables must also be provided. If there is an error, it will return :calc-error."
  165. (memoize (fn [expression variables]
  166. (try
  167. (.evaluate mathjs expression (clj->js variables))
  168. (catch js/Error e
  169. (println "mathjs evaluation error" (.-message e) e)
  170. :calc-error)))))
  171. ;TODO: deal with lowercase cell references
  172. (defn find-cell [data c r]
  173. (some #(if (and (= (:col %) c) (= (:row %) r)) %) data))
  174. (defn find-ref [data cell-ref]
  175. (some (fn [{:keys [row col] :as datum}] (if (and (= row (:row cell-ref)) (= col (:col cell-ref))) datum)) data))
  176. (defn copy-display-values [data display-values]
  177. (let [original (map #(dissoc % :dirty) data)
  178. removed (map #(-> % (dissoc :found) (dissoc :inputs) (dissoc :dirty)) display-values)]
  179. (into original removed)))
  180. ;TODO: memoize dynamically? probably not worth memoizing directly, and could take up too much memory over time
  181. ; https://stackoverflow.com/a/13123571/8172807
  182. (defn find-cycle
  183. ([data datum] (find-cycle data datum #{}))
  184. ([data datum ances]
  185. (let [cur {:row (:row datum) :col (:col datum)}
  186. this-and-above (conj ances cur)
  187. refs (:refs datum)
  188. found (not (empty? (clojure.set/intersection this-and-above (set refs))))]
  189. (if found
  190. :cycle-error
  191. (some (fn [cell]
  192. (find-cycle data (find-ref data cell) this-and-above)) refs)))))
  193. (defn alt-find-cycle
  194. "Accepts the data and a datum, and peforms a depth-first search to find reference cycles, following back-references."
  195. ([data c r] (alt-find-cycle data c r #{}))
  196. ([data c r ancest]
  197. (let [datum (get-in data [c r])
  198. current {:col c :row r}
  199. this-and-above (conj ancest current)
  200. inbound (:inbound datum)
  201. found-repeat (not (empty? (clojure.set/intersection this-and-above (set inbound))))]
  202. (if found-repeat
  203. :cycle-error
  204. (some #(alt-find-cycle data (:col %) (:row %) this-and-above) inbound)))))
  205. (defn gather-variables-and-evaluate-cell
  206. "Assumes that all the cell's immediate references have been resolved. Collects the final values from them, then evaluates the current cell's expression. Returns the new data map."
  207. [data c r]
  208. (let [datum (dissoc (dissoc (get-in data [c r]) :dirty) :display) ; get rid of the dirty flag right away (it must be included with the returned data to have effect)
  209. refs (:refs datum)
  210. value (:value datum)
  211. formula? (= (first value) "=")
  212. resolved-refs (map #(merge % (get-in data [(:col %) (:row %)])) refs)
  213. evaluated-refs (map #(if (= (first (:value %)) "=") (:display %) (:value % "0")) resolved-refs)
  214. invalid-refs (some nil? resolved-refs)
  215. dirty-refs (some :dirty resolved-refs)
  216. error-refs (some #(= (:display %) :error) resolved-refs)
  217. unevaluated-refs (some nil? evaluated-refs)
  218. cycle-refs (some #(= (:display %) :cycle-error) resolved-refs)
  219. disqualified? (or invalid-refs dirty-refs error-refs)]
  220. (cond
  221. (false? formula?) (assoc-in data [c r] datum) ; if it's not a formula, then return as is (with the dirty flag removed)
  222. cycle-refs (-> data ; if one of its references has a reference cycle, then this one is "poisoned" as well
  223. (assoc-in [c r] datum)
  224. (assoc-in [c r :display] :cycle-error))
  225. unevaluated-refs (assoc-in data [c r :display] :insufficient-data) ; do not un-mark as "dirty", since it has not been evaluated yet
  226. disqualified? (-> data ; some other error is present
  227. (assoc-in [c r] datum)
  228. (assoc-in [c r :display] :error))
  229. (empty? refs) (-> data
  230. (assoc-in [c r] datum)
  231. (assoc-in [c r :display] (evaluate-expression (subs value 1) {})))
  232. :else (let [variables (zipmap (map #(str (:col %) (:row %)) refs) evaluated-refs)
  233. evaluated-value (evaluate-expression (subs value 1) variables)
  234. new-datum (assoc datum :display evaluated-value)]
  235. (assoc-in data [c r] new-datum)))))
  236. ;(time (gather-variables-and-evaluate-cell {"A" {1 {:value "=A2 + 4" :refs '({:col "A" :row 2})} 2 {:value "2"}}} "A" 1))
  237. ;(time (gather-variables-and-evaluate-cell (create-all-back-references (create-all-references (:alt-table-data microtables-frontend.db/default-db))) "B" 7))
  238. ;(time (set-dirty-flags (create-all-back-references (create-all-references (:alt-table-data microtables-frontend.db/default-db))) "B" 7))
  239. ;(zipmap (map #(str (:col %) (:row %)) (list {:col "A" :row 1} {:col "A" :row 2} {:col "A" :row 3} {:col "A" :row 4})) (list 1 3 5 7))
  240. ; THE NEW EVALUATE FUNCTION
  241. ; - check for cycles in the back references, starting from the target cell (if any, use another function to mark it and its back references with :cycle-error and remove :dirty)
  242. ; - if any of the forward references are dirty, mark the cell (and recurse up) with an error (and set a TODO to think about this further)
  243. ; - evaluate (using forward references if necessary)
  244. ; - add all back-references to the queue
  245. ; - recurse
  246. ; - TODO: consider initialization case
  247. ; - TODO: consider multiple cells modified simultaneously
  248. (defn evaluate-from-cell
  249. "Evaluate the final value of a cell, and recursively re-evaluate all the cells that reference it."
  250. [data c r]
  251. (let [cycles? (alt-find-cycle data c r)
  252. new-data (if cycles?
  253. (-> data ; if there are cycles, mark :cycle-error and remove :dirty (rathan than evaluate) - still need to recurse up the tree to mark dependents with :cycle-error
  254. (update-in [c r] dissoc :dirty)
  255. (assoc-in [c r :display] :cycle-error))
  256. (gather-variables-and-evaluate-cell data c r))] ; if there are no cycles, evaluate the cell
  257. (loop [data new-data
  258. queue (get-in new-data [c r :inbound])]
  259. (if (empty? queue)
  260. data ; if the queue is empty, we're done
  261. (let [current (first queue)
  262. cc (:col current)
  263. cr (:row current)
  264. dirty? (get-in data [cc cr :dirty])
  265. re-evaluated-data (if dirty?
  266. (gather-variables-and-evaluate-cell data cc cr)
  267. data)
  268. sufficient? (not= (get-in re-evaluated-data [cc cr :display]) :insufficient-data)
  269. new-queue (if dirty?
  270. (if sufficient?
  271. (concat (rest queue) (get-in re-evaluated-data [cc cr :inbound])) ; if all is well, then add the back-references onto the queue
  272. (concat (rest queue) (list current))) ; if the current cell's dependencies are not satisfied, re-add to the end of the queue
  273. (rest queue))] ; if the current cell is not marked as dirty, then it has already been processed
  274. (recur re-evaluated-data new-queue))))))
  275. (defn alt-re-evaluate
  276. "Evaluate the values of cells that contain formulae, following reference chains if applicable."
  277. [data]
  278. (let [non-empty-cells (flatten (map (fn [[c v]] (map (fn [[r _]] {:col c :row r}) v)) data))
  279. {has-formula true original-values false} (group-by #(= (first (get-in data [(:col %) (:row %) :value])) "=") non-empty-cells)
  280. found-cycles (map #(let [found (alt-find-cycle data (:col %) (:row %))]
  281. (if found (assoc % :error found) %)) has-formula)]
  282. non-empty-cells))
  283. (defn find-val [data c r]
  284. (let [l (find-cell data c r)
  285. v (get l :display (get l :value))
  286. formula? (and (string? v) (= (first v) "="))]
  287. (cond
  288. (nil? v) 0
  289. ;(contains? l :error) :ref-error
  290. formula? :not-yet
  291. :else v)))
  292. ;TODO: ADD DOCSTRINGS TO ALL CONNECTED FUNCTIONS AND RENAME VARIABLES WHERE NEEDED
  293. ;TODO: figure out how to re-evaluate only when the cell modified affects other cells
  294. (defn re-evaluate [data]
  295. (println "re-evaluating" data)
  296. (let [{has-formula true original-values false} (group-by #(= (first (:value %)) "=") data)
  297. found-cycles (map #(let [found (find-cycle data %)] (if found (assoc % :error found) %)) has-formula)
  298. {eligible true ineligible false} (group-by #(not (contains? % :error)) found-cycles)]
  299. ;
  300. (loop [values (into original-values ineligible)
  301. mapped-cell-keys eligible]
  302. (let [search-values (map (fn [datum]
  303. (assoc datum :found (map #(find-val
  304. (concat values mapped-cell-keys)
  305. (:col %)
  306. (:row %))
  307. (:refs datum))))
  308. mapped-cell-keys)
  309. {not-ready true ready nil} (group-by (fn [datum]
  310. (some #(= :not-yet %) (:found datum)))
  311. search-values)
  312. prepped-for-eval (map (fn [datum]
  313. (let [hash-map-of-vars-to-vals (apply hash-map (interleave (:vars datum) (:found datum)))]
  314. (assoc datum :inputs hash-map-of-vars-to-vals)))
  315. ready)
  316. evaluated (map (fn [datum]
  317. (assoc datum :display (evaluate-expression
  318. (subs (:value datum) 1)
  319. (:inputs datum))))
  320. prepped-for-eval)
  321. updated-values (copy-display-values values evaluated)]
  322. (println "EVALUATED" evaluated)
  323. (if (nil? not-ready)
  324. updated-values
  325. (recur updated-values not-ready))))))