[JAVA] The world of clara-rules (4)

Last time

There is a query function for checking the contents of the clara-rules session from the outside, so I tried to find out how to use it.

--Basically, only the left side of the rule is defined.

Difference in role with rules

--Rules are used to derive new facts from existing facts added to the session --Query is used purely to look inside the session --Debug --Intermediate step to extract information from the calculated session and pass it to another process

Code example

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

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

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

(defquery find-all-As
  [?a <- TypeA];;Matching condition

(defquery find-As-by-attr
  [:?value];; Parameter needs to be a keyword
  [?a <- TypeA (= attr-a ?value)])

(defquery find-join-key-of-As
  [TypeA (= ?jk join-key)])

(defquery find-A-and-B-pair-by-join-key
  [?a <- TypeA (= join-key ?value)]
  [?b <- TypeB (= join-key ?value)])

(def sess (-> (mk-session)
              (insert (->TypeA "a1" :foo)
                      (->TypeA "a2" :bar)
                      (->TypeA "a3" :baz)
                      (->TypeB "b1" :foo)
                      (->TypeB "b2" :bar)
                      (->TypeB "b3" :baz)
                      (->TypeB "b4" :qux))

(query sess find-all-As)
;; ({:?a {:attr-a "a1", :join-key :foo}}
;;  {:?a {:attr-a "a2", :join-key :bar}}
;;  {:?a {:attr-a "a3", :join-key :baz}})

(query sess find-As-by-attr :?value "a1")
;; => ({:?a #clara_rules.join.TypeA{:attr-a "a1", :join-key :foo}, :?value "a1"})

(query sess find-join-key-of-As)
;; => ({:?jk :foo} {:?jk :bar} {:?jk :baz})

(query sess find-A-and-B-pair-by-join-key :?value :foo)
;; => ({:?a {:attr-a "a1", :join-key :foo},
;;      :?value :foo,
;;      :?b {:attr-b "b1", :join-key :foo}})

Points of interest in the above code

--defquery can be written in the same way as the left side of defrule, except that parameters can be received externally. --Variable binding etc --query returns the variable bound in defquery as the key of the resulting map --query returns the match as a sequence of maps


In the previous examples, the side effect (println) was only used on the right side of the rule to check the result. I'm glad that I was able to solve one of the questions I had since I touched on whether there was a way to check the results without using side effects.

Next time

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-
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)
Understand the basic mechanism of log4j2.xml
About the basics of Android development
[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
Note on the path of request.getRequestDispatcher
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
Try using || instead of the ternary operator
About the version of Docker's Node.js image