The world of clara-rules (3)

Last time

I learned from the video of the lecture that clara-rules can be used for join operations between different entities, so I implemented a simple example myself.

problem

code

(ns  clara-rules.join
  (:require [clara.rules :refer [defrule fire-rules insert insert! mk-session]]))

(defrecord TypeA [attr-a join-key])

(defrecord TypeB [attr-b join-key])

(defrecord TypeC [attr-a attr-b join-key])

(defrule Join
  [TypeA (= ?jk-a join-key) (= ?a attr-a)]
  [TypeB (= join-key ?jk-a) (= ?b attr-b)]
  =>
  (insert! (->TypeC ?a ?b ?jk-a)))

(defrule Print
  [?result <- TypeC];; Will always match
  =>
  (println ?result))

(-> (mk-session)
    (insert (->TypeA "a1" :foo)
            (->TypeA "a2" :bar)
            (->TypeA "a3" :baz)
            (->TypeB "b1" :foo)
            (->TypeB "b2" :bar)
            (->TypeB "b3" :baz)
            (->TypeB "b4" :qux))
    (fire-rules))
;;#clara_rules.join.TypeC{:attr-a a1, :attr-b b1, :join-key :foo}
;;#clara_rules.join.TypeC{:attr-a a2, :attr-b b2, :join-key :bar}
;;#clara_rules.join.TypeC{:attr-a a3, :attr-b b3, :join-key :baz}

--Type C is printed for Type A and Type B pairs that have a common : join-key as intended. You did it! --(-> TypeB" b4 ": qux) does not appear in the results because there is no TypeA fact with the same : join-key: qux. I see.

Explanation of <- on the left side of defrule

Bind the fact itself to a variable, not the attribute of the fact. Taking [? result <-TypeC] as an example, the entire matched TypeC instance is bound to? result.

Impressions

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)
The idea of quicksort
The idea of jQuery
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
Continuation: The parable of OOP (omitted)
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?
[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
Note on the path of request.getRequestDispatcher
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
The story of encountering Spring custom annotation
Display text on top of the image