Clara

核心理念

Clara is based on two core ideas:

  • Expert systems are a good way to represent domain knowledge and untangle complex logic.
  • Expert systems have been hijacked into enterprise software offering limited tooling to business users.

Clara avoids this pitfall. It is not targeting business users, but instead aims to retake the advantages of rule engines to simplify the job of developers. Clara rules are just code. They live a Clojure namespace, are made of Clojure expressions, and can trivially invoke any function or external library on the platform. Clara just allows developers to write business code in a simpler way.

Note that Clara can be used as a core engine to support tools built for a business user. There are several users who are generating rules from business logic, using Clara as the underlying engine. This makes sense, since simple, user-facing tools can be effective if you restrict them to a single domain. But this is outside of Clara itself, where we focus on a developer audience.

如何表达复杂的逻辑,同时适应不断变化的需求?

传统的方式:面向非开发者 - 定义有限的语言规则,允许非开发者定义决策规则
Clara的方式:面向开发者 - 使用Clojure语言来定义决策规则,使开发者表达复杂决策规则的同时适用不断变化的需求

规则引擎的功能需求

  • 规则定义
  • 知识推断
  • 知识查询

安装Clara

  1. ;; Leiningen
  2. [com.cerner/clara-rules "0.21.0"]

规则引擎示例

image.png

  1. ;; 规则定义
  2. (ns clara.example
  3. (:require [clara.rules :refer :all]))
  4. (defrecord SupportRequest [client level])
  5. (defrecord ClientRepresentative [name client])
  6. ;; 基于Record定义规则
  7. (defrule is-important
  8. "Find important support requests."
  9. [SupportRequest (= :high level)]
  10. =>
  11. (println "High support requested!"))
  12. ;; 绑定查询结果,?client与?name
  13. (defrule notify-client-rep
  14. "Find the client representative and request support."
  15. [SupportRequest (= ?client client)]
  16. [ClientRepresentative (= ?client client) (= ?name name)]
  17. =>
  18. (println "Notify" ?name "that"
  19. ?client "has a new support request!"))
  20. ;; 规则执行
  21. (-> (mk-session 'clara.example)
  22. (insert (->ClientRepresentative "Alice" "Acme")
  23. (->SupportRequest "Acme" :high))
  24. (fire-rules))

规则引擎使用场景

语雀内容