Skip to content

Instantly share code, notes, and snippets.

@pieter-van-prooijen
Last active May 25, 2017 08:44
Show Gist options
  • Save pieter-van-prooijen/9fb49a76899240a15bee9d4f8615afb9 to your computer and use it in GitHub Desktop.
Save pieter-van-prooijen/9fb49a76899240a15bee9d4f8615afb9 to your computer and use it in GitHub Desktop.
AMS Clojure Meetup 24-05-2017 Clara rule engine presentation, drop this index.html into a reveal.js distribution and open in the browser
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Clara Rule Engine (Clojure Meetup 24-05-2017)</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/white.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-markdown>
<textarea data-template>
# Clara Rule Engine
(clara-rules.org)
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Use Case: Risk Engine
- Payment app backend
- Block fraudulent customers and transactions
* Simple blacklisting of IBANs, phone numbers etc.
* Detecting use of savings account IBANs
* Detect fraudulent spending patterns.
- Operates async from main backend, coupled via queues.
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Current Implementation
- Node.js based, with various hardcoded rules and a naive implementation of rule execution.
- Higher level rule expression is needed.
(allowing domain specialists to write rules ?)
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Clara
- Implements a system of _facts_, _rules_ and _queries_ as a Clojure(script) library.
- Geared toward developers, not business analyst like most rule engine because:
_'You end up writing Java in Excel spreadsheet cells'_
- Uses Clojure macros and the Rete algorithm to efficiently compile and execute rules
- Explicit Java interop (Java Beans as facts)
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Facts
- Examples:
1. "A customer with IBAN NL00INGB..."
2. "A payment from customer A to customer B for amount n euros"
3. "a banned IBAN BE00123..."
- Represented as records (with built-in syntax support) or any other destructurable data type
- Prototype uses maps with associated spec definitions.
_(type function needed)_
_customer example_
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Rules
#### Left Hand Side
Facts and conditions needed for this rule to fire.
#### Right Hand Side
Consequences of a rule firing:
- insert / retract facts
- side effects (outside of the session)
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Left Hand Side
- Binds _special variables_ (?foo) to a fact or part of a fact
- Selects facts using the supplied type function
- Extract relevant fact parts using destructuring
- Constrain facts by conditional s-expressions
- Tries to *unify* the special variables
_Banned IBAN rule example_
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### RHS (Rule Firing)
- Only when Clara succeeds in unification
- Fires for all the facts that match the LHS
- Executes the RHS with the unified bindings.
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Queries
- Retrieves facts using LHS syntax
_Customer Query_
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Truth Maintenance
- Clara tracks which facts in the LHS cause insertions in the RHS
- When a LHS fact is retracted, the RHS insertions are also removed.
- Detect retractions using listeners.
- Does *not* do the opposite: re-insertions of retracted facts because of rule firings.
_Banned IBAN rule retraction_
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Library Use
- create a session, with rule ns, type fn and options:
```clojure
(r/mk-session 'risk-engine.domain.core :fact-type-fn ::type
:cache false)
```
- insert facts and fire the rules, session is immutable, so thread it
```clojure
(-> session
(r/insert fact1 fact2 ...)
(r/fire-rules))
```
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Rough Edges
- Retracted fact detection is cumbersome
- Needs a fire-rules invocation after each manually inserted fact ?
- Looping when RHS doesn't prevent the LHS from matching again.
</textarea>
</section>
<section data-markdown>
<textarea data-template>
### Further Explorations
- Replace duplication of blocked / unblocked customer facts ?
- Code in facts ? (IBAN bank identifier extraction)
- Speed tests for large fact bases
- Serialization of working memory
- Kafka / Avro as input for facts (already working)
- Complex transaction rules / spending patterns
</textarea>
</section>
<section data-markdown>
<textarea data-template>
Links:
- Website: http://www.clara-rules.org
- Github: https://github.com/cerner/clara-rules
- Strange Loop 2014: Retaking Rules for Developers
https://www.youtube.com/watch?v=Z6oVuYmRgkk
- Clojure Conj 2016: Clarifying Rule Engines with Clara Rules
https://www.youtube.com/watch?v=Q_k5MkZmd-o
</textarea>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
],
transition: 'none'
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment