This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-12-07
Channels
- # adventofcode (114)
- # announcements (3)
- # aws (5)
- # babashka (62)
- # beginners (111)
- # calva (4)
- # cider (20)
- # clara (5)
- # clj-kondo (1)
- # cljs-dev (9)
- # clojure (255)
- # clojure-europe (75)
- # clojure-italy (10)
- # clojure-nl (3)
- # clojure-norway (5)
- # clojure-uk (6)
- # clojuredesign-podcast (5)
- # clojurescript (34)
- # community-development (28)
- # conjure (1)
- # cursive (3)
- # data-science (1)
- # datavis (1)
- # datomic (4)
- # figwheel-main (1)
- # fulcro (14)
- # graalvm (1)
- # graphql (8)
- # integrant (4)
- # introduce-yourself (2)
- # jobs (2)
- # juxt (4)
- # kaocha (2)
- # malli (6)
- # membrane-term (53)
- # mount (2)
- # nextjournal (2)
- # off-topic (27)
- # pathom (11)
- # polylith (3)
- # portal (11)
- # reagent (4)
- # reitit (4)
- # remote-jobs (1)
- # reveal (14)
- # shadow-cljs (22)
- # tools-deps (24)
- # vim (6)
- # xtdb (19)
Hi, I’m very new to clara and trying to use it to build an hierarchical data structure. I am having some trouble grasping the basics obviously. I have this following simple rule that I want to create one widget:
(defrecord Widget [material type num-gadgets])
(defn pick-type [type]
"intermediate")
(defn get-num-gadgets []
1)
(defrule add-widget
[?c <- (accum/count) :from (Widget (= material "steel"))]
[:test (= 0 ?c)]
=>
(println "adding widget")
(rules/insert! (->Widget "steel" (pick-type "foo") (get-num-gadgets))))
I expect that the test should resolve to false
once it has created one widget, however this seems to loop forever. What am I doing wrong here?
I am also not sure if this ‘right’ way to create facts in the session?Ok, after scrolling back … seems this is a common trap that newbies fall into with truth maintenance. Walking through an example of something like this would make a great example in the getting started documentation!
I am trying to build an object generator using the rules, here is a example:
(defrecord Widget [name material length parts])
(defrule add-widget
[:not [Widget]]
=>
(println "adding widget")
(rules/insert-unconditional! (->Widget "w1" "unknown" nil [])))
(defn pick-material []
(first (shuffle ["steel" "silver" "gold"])))
(defn pick-length [mat]
(condp = mat
"steel" (+ 10 (rand-int 20))
"silver" (+ 5 (rand-int 10))
"gold" (rand-int 10)))
(defrule set-widget-attrs
[?widget <- Widget (= ?name name) (= ?material material) (= ?length length) (= ?parts parts)]
[:test (and (nil? ?length) (= ?material "unknown"))]
=>
(println "setting material")
(let [new-mat (pick-material)
len (pick-length new-mat)]
(rules/insert-unconditional! (->Widget ?name new-mat len ?parts)))
(rules/retract! ?widget))
this runs but produces 2 widgets (fires twice) and I am not sure why.
I am trying to effectively update the widget attributes by creating a new one and retracting the old one. This is the output:
(def session (-> (rules/mk-session 'user)
(rules/fire-rules)))
adding widget
setting material
adding widget
setting material
(rules/query session widget?)
=>List(2)(
0: Map{:?widget: #user.Widget Map{:name: "w1", :material: "gold", :length: 4, :parts: Vector(0)}},
1: Map{:?widget: #user.Widget Map{:name: "w1", :material: "gold", :length: 0, :parts: Vector(0)}}
)