A library called clara-rules was suddenly talked about in me, so I summarized it quickly.
--A library that provides an implementation of the rule engine --Separate domain knowledge from code --Available for both clj / cljs --Naturally available from Java --There is also a video of the lecture by the author himself [1] [[2]](https://www.youtube.com/watch?v = zs5Rueo42TA) -There is also a video of a more recent lecture
--The origin of the expert system ――It was popular for a while because it was attractive that business users could solve problems without going through programmers when there were not enough programmers, but there was a limit. --clara-rules targets programmers, not business users, from the perspective that practically any rule is code.
--If you have some logic and express it as a function call, you need to explicitly write that it passes / receives the information that the logic needs. --The information required by logic changes → Inevitably refactoring is required --There is no need to write information to be passed / received via rules.
session --Representing the state --Insert / retract to add or remove facts --The fact is represented by an instance of the Clojure record --Apply rules to facts added to the session with fire-rules --Immutable value (insert operation returns new session, old session remains) --Like Clojure --In fact, it is designed to be operated with a threading macro.
defrule
macro--Macro for writing rules
--There is a right side and a left side
--Separated by =>
--Write some judgment logic on the left side (multiple can be written)
--Describe what happens when the judgment becomes truthy on the right side
--Can cause side effects
--You can also add facts to the current session
Simple example
(defrecord Num [number])
(defrule is-two
[Num (= number 2)] => (println "Two!"))
(-> (mk-session)
(insert (Num 4)))
(fire-rules))
;;Two!
--Check when you are fine! --Edit request too. .. .. ??
――Although it is not specified, I feel that there is something I want to make called clara-rules first, and I chose Clojure for its implementation. ――I don't know how happy I am with just a simple example, so I still need to learn. --It seems that JavaScript has nools and Ruby has wongi. ――I felt that dynamically generating rules was not supported. ――I was wondering if you can use the protocol method of the type on the left side in defrule, but the author told me that it is possible with slack.
Thanks for the clarification ryanbrush!
(defprotocol IFoo
(test-foo [this]))
(deftype Foo []
IFoo
(test-foo [this] true))
(defrule is-foo
[Foo (test-foo this;;It looks like a symbol that is automatically bound within the scope
)]
=> (println "is a foo"))
(-> (mk-session)
(insert (Foo.))
(fire-rules))
;;is a foo
Recommended Posts