Hi everyone! Newbie here, I want to use clara with clojure/script in the browser, but I'm having a hard time making it work with figwheel and reagent. I was expecting the following code to run println in the rules, but it doesn't seem to work
(ns ^:figwheel-hooks fallas1.nurse
(:require [goog.dom :as gdom]
[reagent.core :as reagent :refer [atom]]
[reagent.dom :as rdom]
[clara.rules
:refer [insert fire-rules]
:refer-macros [defsession defrule]]))
(defsession session 'clara.example)
(defrecord SupportRequest [client level])
(defrecord ClientRepresentative [name client])
(defrule is-important
"Find important support requests."
[SupportRequest (= :high level)]
=>
(println "High support requested!"))
(defrule notify-client-rep
"Find the client representative and request support."
[SupportRequest (= ?client client)]
[ClientRepresentative (= ?client client) (= ?name name)]
=>
(println "Notify" ?name "that"
?client "has a new support request!"))
(defonce app-state
(atom {:session (-> session
(insert (->ClientRepresentative "Alice" "Acme")
(->SupportRequest "Acme" :high))
(fire-rules))}))
(defn main []
[:div
[:h1 (str (:session @app-state))]])
(defn mount [el]
(rdom/render [main] el))
(defn get-app-element []
(gdom/getElement "app"))
(defn mount-app-element []
(when-let [el (get-app-element)]
(mount el)))
(mount-app-element)
(defn ^:after-load on-reload []
(mount-app-element))
@paulocuneo itโs not clear you have println hooked up to js console. So to eliminate that variable try it with js/console.log instead of println first.
But it is also happening during the defonce init so may also be an odd time to see the output. I still think you should be able to get it to print in the js console though
@mikerod Thank you very much! ๐ I changed it a bit, based on your advise and got it to work.
(ns ^:figwheel-hooks fallas1.nurse
(:require [goog.dom :as gdom]
[reagent.core :as reagent :refer [atom]]
[reagent.dom :as rdom]
[clara.rules
:refer [insert fire-rules]
:refer-macros [defsession defrule]]))
(defrecord SupportRequest [client level])
(defrecord ClientRepresentative [name client])
(defrule is-important
"Find important support requests."
[SupportRequest (= :high level)]
=>
(js/console.log "High support requested!"))
(defrule notify-client-rep
"Find the client representative and request support."
[SupportRequest (= ?client client)]
[ClientRepresentative (= ?client client) (= ?name name)]
=>
(js/console.log "Notify" ?name "that"
?client "has a new support request!"))
(defsession session 'fallas1.nurse)
(defonce app-state
(atom 0))
(defn run-rules [_]
(-> session
(insert (->ClientRepresentative "Alice" "Acme")
(->SupportRequest "Acme" :high))
(fire-rules))
(swap! app-state inc))
(defn main []
[:div
[:div "Rounds:" @app-state]
[:button
{:on-click run-rules}
"run"]])
(defn mount [el]
(rdom/render [main] el))
(defn get-app-element []
(gdom/getElement "app"))
(defn mount-app-element []
(when-let [el (get-app-element)]
(mount el)))
(mount-app-element)
(defn ^:after-load on-reload []
(mount-app-element))
Nice
Yeah above makes more sense to me now in terms of making sure evaluation happens at a normal sort of time
But also you can get println to work to in cljs. But youโd have to enable console print earlier than the rest of this ns
http://clojurescriptmadeeasy.com/blog/easy-console-printing.html
Yeah, I've disabled println by mistake :face_palm: ๐