The world of clara-rules (1)

A library called clara-rules was suddenly talked about in me, so I summarized it quickly.

Overview / features

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

rule? engine?

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

What can be solved

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

Query?

--Check when you are fine! --Edit request too. .. .. ??

Other miscellaneous feelings, etc.

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

Sequel

Recommended Posts

The world of clara-rules (2)
The world of clara-rules (4)
The world of clara-rules (1)
The world of clara-rules (3)
The world of clara-rules (5)
Judgment of the calendar
The idea of quicksort
The idea of jQuery
About the handling of Null
Docker monitoring-explaining the basics of basics-
About the description of Docker-compose.yml
Understand the basics of docker
The play of instantiating java.lang.Void
Explanation of the FizzBuzz problem
The basics of Swift's TableView
Median of the three values
The illusion of object orientation
Switch the version of bundler
About the behavior of ruby Hash # ==
[Java] Delete the elements of List
Qualify only part of the text
Understand the basic mechanism of log4j2.xml
About the basics of Android development
'% 02d' What is the percentage of% 2?
[Rails] Check the contents of the object
Replace the contents of the Jar file
[Java version] The story of serialization
Check the version of Cent OS
[Swift] Change the textColor of UIDatePicker
[Ruby] See the essence of ArgumentError
Explanation of the order of rails routes
I read the source of ArrayList I read
The basics of SpringBoot + MyBatis + MySQL
Try Hello World with the minimum configuration of Heroku Java spring-boot
The basic basis of Swift dialogs
The basic basis of Swift's Delegate
I read the source of Integer
This and that of the JDK
Check the migration status of rails
The story of @ViewScoped consuming memory
Filter the fluctuations of raw data
Explaining the columns of Spree :: Taxonomy
A memorandum of the FizzBuzz problem
I read the source of Long
Official logo list of the service
Explain the column of Spree :: Product
Various methods of the String class
About the role of the initialize method
Think about the 7 rules of Optional
Get the ID of automatic numbering
I read the source of Short
I read the source of Byte
Order of processing in the program
I read the source of String
The origin of Java lambda expressions
[Ruby] Display the contents of variables
Filter the result of BindingResult [Spring]
Summary about the introduction of Device
Remote debugging of the Cognos SDK
About the log level of java.util.logging.Logger
Get the acceleration and bearing of the world coordinate system on Android