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

Last time

If you use the accumulator function of clara-rules, you can realize something like an aggregate function in sql, so try it this time. I will see it.

Overview

--Use the one in the clara.rules.accumulators namespace --Min / max / sum etc. are provided by default --You can also create your own accumulator (accum function) --Can be used in defquery on the left side of defrule

grammar

[<variable name to bind> <-<accumulator>: from [<fact> + <constraint>]]

Try to move

I tried the following code.


(ns clara-rules.accumulator
  (:require [clara.rules :refer [fire-rules insert mk-session query defquery]]
            [clara.rules.accumulators :as acc]))

(defrecord SomeAmount [id amt])

(defquery test-query
  []
  [?largest-amt <- (acc/max :amt) :from [SomeAmount]]
  [?total <- (acc/sum :amt) :from [SomeAmount]]
  [?grouped <- (acc/grouping-by :id) :from [SomeAmount]]
  ;;Total by groups
  [?grouped-and-summed
   <- (acc/grouping-by :id (fn [m]
                             ;;Accepts map key=field for grouping
                             ;;value=seq of maps of records that were grouped
                             (map (fn [[k seq-of-records]]
                                    {k (reduce + (map :amt seq-of-records))})
                                  m)))
   :from [SomeAmount]])

(-> (mk-session)
    (insert (->SomeAmount :a 4)
            (->SomeAmount :a 8)
            (->SomeAmount :b 32)
            (->SomeAmount :b 21)
            (->SomeAmount :c 1)
            (->SomeAmount :c 9))
    (fire-rules)
    (query test-query))
;; => ({:?largest-amt 32,
;;      :?total 75,
;;      :?grouped {:a [#clara_rules.accumulator.SomeAmount{:id :a, :amt 4}
;;                     #clara_rules.accumulator.SomeAmount{:id :a, :amt 8}],
;;                 :b [#clara_rules.accumulator.SomeAmount{:id :b, :amt 32}
;;                     #clara_rules.accumulator.SomeAmount{:id :b, :amt 21}],
;;                 :c [#clara_rules.accumulator.SomeAmount{:id :c, :amt 1}
;;                     #clara_rules.accumulator.SomeAmount{:id :c, :amt 9}]},
;;      :?grouped-and-summed ({:a 12} {:b 53} {:c 10})})

Recommended Posts

The world of clara-rules (2)
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
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
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
Explain the column of Spree :: Product
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
[Challenge CircleCI from 0] Learn the basics of CircleCI
Understand the basics of Android Audio Record
The secret to the success of IntelliJ IDEA
Check the contents of the Java certificate store
Automatically adjust the height of Xib files