clara

http://www.clara-rules.org/
paulocuneo 2021-06-05T18:05:36.038600Z

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))

2021-06-05T21:54:16.040Z

@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.

2021-06-05T21:54:51.041100Z

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

paulocuneo 2021-06-05T22:06:00.042600Z

@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))

2021-06-05T22:06:50.042900Z

Nice

2021-06-05T22:07:07.043500Z

Yeah above makes more sense to me now in terms of making sure evaluation happens at a normal sort of time

2021-06-05T22:07:34.044300Z

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

paulocuneo 2021-06-05T22:10:53.045500Z

Yeah, I've disabled println by mistake :face_palm: ๐Ÿ˜…

๐Ÿ˜ฑ 1