项目作者: jruizgit

项目描述 :
Durable Rules Engine
高级语言: JavaScript
项目地址: git://github.com/jruizgit/rules.git
创建时间: 2013-12-21T23:43:50Z
项目社区:https://github.com/jruizgit/rules

开源协议:MIT License

下载


durable_rules

for real time analytics (a Ruby, Python and Node.js Rules Engine)

Build Status
Gem Version
npm version
PyPI version

durable_rules is a polyglot micro-framework for real-time, consistent and scalable coordination of events. With durable_rules you can track and analyze information about things that happen (events) by combining data from multiple sources to infer more complicated circumstances.

A full forward chaining implementation (Rete) is used to evaluate facts and events in real time. A simple meta-linguistic abstraction lets you define simple and complex rulesets as well as control flow structures such as flowcharts, statecharts, nested statecharts and time driven flows.

The durable_rules core engine is implemented in C, which enables fast rule evaluation as well as muti-language support.

durable_rules can be scaled out by offloading state to a data store out of process such as Redis. State offloading is extensible, so you can integrate the data store of your choice.

In durable_rules V2, less is more: The Rete tree is fully evaluated in C. Thus, the framework is 5x to 10x faster (depending on the scenario) and does not require Redis. The programming model for posting events, asserting and retracting facts is synchronous and does not prescribe any web framework.

Getting Started

Using your scripting language of choice, simply describe the event to match (antecedent) and the action to take (consequent).

Node.js

To install the framework do: npm install durable

  1. var d = require('durable');
  2. d.ruleset('test', function() {
  3. // antecedent
  4. whenAll: m.subject == 'World'
  5. // consequent
  6. run: console.log('Hello ' + m.subject)
  7. });
  8. d.post('test', {subject: 'World'});

Python

To install the framework do: pip install durable_rules

  1. from durable.lang import *
  2. with ruleset('test'):
  3. # antecedent
  4. @when_all(m.subject == 'World')
  5. def say_hello(c):
  6. # consequent
  7. print ('Hello {0}'.format(c.m.subject))
  8. post('test', { 'subject': 'World' })

Ruby

To install the framework do: gem install durable_rules

  1. require "durable"
  2. Durable.ruleset :test do
  3. # antecedent
  4. when_all (m.subject == "World") do
  5. # consequent
  6. puts "Hello #{m.subject}"
  7. end
  8. end
  9. Durable.post :test, { :subject => "World" }

Forward Inference

durable_rules super-power is the foward-chaining evaluation of rules. In other words, the repeated application of logical modus ponens to a set of facts or observed events to derive a conclusion. The example below shows a set of rules applied to a small knowledge base (set of facts).

Node.js

  1. var d = require('durable');
  2. d.ruleset('animal', function() {
  3. whenAll: {
  4. first = m.predicate == 'eats' && m.object == 'flies'
  5. m.predicate == 'lives' && m.object == 'water' && m.subject == first.subject
  6. }
  7. run: assert({ subject: first.subject, predicate: 'is', object: 'frog' })
  8. whenAll: {
  9. first = m.predicate == 'eats' && m.object == 'flies'
  10. m.predicate == 'lives' && m.object == 'land' && m.subject == first.subject
  11. }
  12. run: assert({ subject: first.subject, predicate: 'is', object: 'chameleon' })
  13. whenAll: m.predicate == 'eats' && m.object == 'worms'
  14. run: assert({ subject: m.subject, predicate: 'is', object: 'bird' })
  15. whenAll: m.predicate == 'is' && m.object == 'frog'
  16. run: assert({ subject: m.subject, predicate: 'is', object: 'green' })
  17. whenAll: m.predicate == 'is' && m.object == 'chameleon'
  18. run: assert({ subject: m.subject, predicate: 'is', object: 'green' })
  19. whenAll: m.predicate == 'is' && m.object == 'bird'
  20. run: assert({ subject: m.subject, predicate: 'is', object: 'black' })
  21. whenAll: +m.subject
  22. run: console.log('fact: ' + m.subject + ' ' + m.predicate + ' ' + m.object)
  23. });
  24. d.assert('animal', { subject: 'Kermit', predicate: 'eats', object: 'flies'});
  25. d.assert('animal', { subject: 'Kermit', predicate: 'lives', object: 'water'});
  26. d.assert('animal', { subject: 'Greedy', predicate: 'eats', object: 'flies'});
  27. d.assert('animal', { subject: 'Greedy', predicate: 'lives', object: 'land'});
  28. d.assert('animal', { subject: 'Tweety', predicate: 'eats', object: 'worms'});

Python

  1. from durable.lang import *
  2. with ruleset('animal'):
  3. @when_all(c.first << (m.predicate == 'eats') & (m.object == 'flies'),
  4. (m.predicate == 'lives') & (m.object == 'water') & (m.subject == c.first.subject))
  5. def frog(c):
  6. c.assert_fact({ 'subject': c.first.subject, 'predicate': 'is', 'object': 'frog' })
  7. @when_all(c.first << (m.predicate == 'eats') & (m.object == 'flies'),
  8. (m.predicate == 'lives') & (m.object == 'land') & (m.subject == c.first.subject))
  9. def chameleon(c):
  10. c.assert_fact({ 'subject': c.first.subject, 'predicate': 'is', 'object': 'chameleon' })
  11. @when_all((m.predicate == 'eats') & (m.object == 'worms'))
  12. def bird(c):
  13. c.assert_fact({ 'subject': c.m.subject, 'predicate': 'is', 'object': 'bird' })
  14. @when_all((m.predicate == 'is') & (m.object == 'frog'))
  15. def green(c):
  16. c.assert_fact({ 'subject': c.m.subject, 'predicate': 'is', 'object': 'green' })
  17. @when_all((m.predicate == 'is') & (m.object == 'chameleon'))
  18. def grey(c):
  19. c.assert_fact({ 'subject': c.m.subject, 'predicate': 'is', 'object': 'grey' })
  20. @when_all((m.predicate == 'is') & (m.object == 'bird'))
  21. def black(c):
  22. c.assert_fact({ 'subject': c.m.subject, 'predicate': 'is', 'object': 'black' })
  23. @when_all(+m.subject)
  24. def output(c):
  25. print('Fact: {0} {1} {2}'.format(c.m.subject, c.m.predicate, c.m.object))
  26. assert_fact('animal', { 'subject': 'Kermit', 'predicate': 'eats', 'object': 'flies' })
  27. assert_fact('animal', { 'subject': 'Kermit', 'predicate': 'lives', 'object': 'water' })
  28. assert_fact('animal', { 'subject': 'Greedy', 'predicate': 'eats', 'object': 'flies' })
  29. assert_fact('animal', { 'subject': 'Greedy', 'predicate': 'lives', 'object': 'land' })
  30. assert_fact('animal', { 'subject': 'Tweety', 'predicate': 'eats', 'object': 'worms' })

Ruby

  1. require "durable"
  2. Durable.ruleset :animal do
  3. when_all c.first = (m.predicate == "eats") & (m.object == "flies"),
  4. (m.predicate == "lives") & (m.object == "water") & (m.subject == first.subject) do
  5. assert :subject => first.subject, :predicate => "is", :object => "frog"
  6. end
  7. when_all c.first = (m.predicate == "eats") & (m.object == "flies"),
  8. (m.predicate == "lives") & (m.object == "land") & (m.subject == first.subject) do
  9. assert :subject => first.subject, :predicate => "is", :object => "chameleon"
  10. end
  11. when_all (m.predicate == "eats") & (m.object == "worms") do
  12. assert :subject => m.subject, :predicate => "is", :object => "bird"
  13. end
  14. when_all (m.predicate == "is") & (m.object == "frog") do
  15. assert :subject => m.subject, :predicate => "is", :object => "green"
  16. end
  17. when_all (m.predicate == "is") & (m.object == "chameleon") do
  18. assert :subject => m.subject, :predicate => "is", :object => "green"
  19. end
  20. when_all (m.predicate == "is") & (m.object == "bird") do
  21. assert :subject => m.subject, :predicate => "is", :object => "black"
  22. end
  23. when_all +m.subject do
  24. puts "fact: #{m.subject} #{m.predicate} #{m.object}"
  25. end
  26. end
  27. Durable.assert :animal1, { :subject => "Kermit", :predicate => "eats", :object => "flies" }
  28. Durable.assert :animal1, { :subject => "Kermit", :predicate => "lives", :object => "water" }
  29. Durable.assert :animal1, { :subject => "Greedy", :predicate => "eats", :object => "flies" }
  30. Durable.assert :animal1, { :subject => "Greedy", :predicate => "lives", :object => "land" }
  31. Durable.assert :animal1, { :subject => "Tweety", :predicate => "eats", :object => "worms" }

Pattern Matching

durable_rules provides string pattern matching. Expressions are compiled down to a DFA, guaranteeing linear execution time in the order of single digit nano seconds per character (note: backtracking expressions are not supported).

Node.js

  1. var d = require('durable');
  2. d.ruleset('test', function() {
  3. whenAll: m.subject.matches('3[47][0-9]{13}')
  4. run: console.log('Amex detected in ' + m.subject)
  5. whenAll: m.subject.matches('4[0-9]{12}([0-9]{3})?')
  6. run: console.log('Visa detected in ' + m.subject)
  7. whenAll: m.subject.matches('(5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|2720)[0-9]{12}')
  8. run: console.log('Mastercard detected in ' + m.subject)
  9. });
  10. d.assert('test', { subject: '375678956789765' });
  11. d.assert('test', { subject: '4345634566789888' });
  12. d.assert('test', { subject: '2228345634567898' });

Python

  1. from durable.lang import *
  2. with ruleset('test'):
  3. @when_all(m.subject.matches('3[47][0-9]{13}'))
  4. def amex(c):
  5. print ('Amex detected {0}'.format(c.m.subject))
  6. @when_all(m.subject.matches('4[0-9]{12}([0-9]{3})?'))
  7. def visa(c):
  8. print ('Visa detected {0}'.format(c.m.subject))
  9. @when_all(m.subject.matches('(5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|2720)[0-9]{12}'))
  10. def mastercard(c):
  11. print ('Mastercard detected {0}'.format(c.m.subject))
  12. assert_fact('test', { 'subject': '375678956789765' })
  13. assert_fact('test', { 'subject': '4345634566789888' })
  14. assert_fact('test', { 'subject': '2228345634567898' })

Ruby

  1. require "durable"
  2. Durable.ruleset :test do
  3. when_all m.subject.matches('3[47][0-9]{13}') do
  4. puts "Amex detected in #{m.subject}"
  5. end
  6. when_all m.subject.matches('4[0-9]{12}([0-9]{3})?') do
  7. puts "Visa detected in #{m.subject}"
  8. end
  9. when_all m.subject.matches('(5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|2720)[0-9]{12}') do
  10. puts "Mastercard detected in #{m.subject}"
  11. end
  12. end
  13. Durable.assert :test, { :subject => "375678956789765" }
  14. Durable.assert :test, { :subject => "4345634566789888" }
  15. Durable.assert :test, { :subject => "2228345634567898" }

Business Rules and Miss Manners

durable_rules can also be used to solve traditional Production Business Rules problems. This example is an industry benchmark. Miss Manners has decided to throw a party. She wants to seat her guests such that adjacent people are of opposite sex and share at least one hobby.

Note how the benchmark flow structure is defined using a statechart to improve code readability without sacrificing performance nor altering the combinatorics required by the benchmark. For 128 guests, 438 facts, the execution time is 450 ms in node.js and 600 ms in Python and Ruby.

IMac, 4GHz i7, 32GB 1600MHz DDR3, 1.12 TB Fusion Drive

Image recognition and Waltzdb

Waltzdb is a constraint propagation problem for image recognition: given a set of lines in a 2D space, the system needs to interpret the 3D depth of the image. The first part of the algorithm consists of identifying four types of junctions, then labeling the junctions following Huffman-Clowes notation. Pairs of adjacent junctions constraint each other’s edge labeling. So, after choosing the labeling for an initial junction, the second part of the algorithm iterates through the graph, propagating the labeling constraints by removing inconsistent labels.

In this case too, the benchmark flow structure is defined using a statechart to improve code readability. The benchmark requirements are not altered. The execution time, for the case of 4 regions, is 430 ms in node.js, 654 ms in Python and 552 ms in Ruby.

IMac, 4GHz i7, 32GB 1600MHz DDR3, 1.12 TB Fusion Drive

To Learn More

Reference Manual:

Ruby

Python

Node.js

JSON