SlideShare une entreprise Scribd logo
1  sur  19
Ruleby: a Ruby rule engine
Chris McCann
What’s a “rule engine”?


 system for executing IF-THEN statements
   in ruleby these are called “production rules”
What’s wrong with IF-THEN


Complex code
Interdependencies
State transitions
Spaghetti code!
if firstname && lastname
  if type == :group
     if groupname
       ...
     end
  elsif type == :individual
     ...
  end
end

      Care to add a new ‘type’?
Ruleby: a better way

 Rule-based programming
  a rule engine that derives answers from a ‘knowledge
  base’
  the knowledge base contains the rules
  give it facts to compare against the rules
3 Parts of a rule engine


 Facts :: data or objects, aka “working memory”
 Rules :: the IF-THEN statements
 Inference Engine :: an efficient mechanism for
 executing the rules against the facts
The Ruleby inference engine

An implementation of the Rete algorithm that matches
patterns to objects
  pronounced “ree-tee” or “ray-tee”
  from Latin for “net” or Italian for “network”
Proposed by Dr. Charles Forgy in 1974 at CMU
  was his PhD thesis in 1979 and a paper in 1984
Dirt Farmer’s guide to Rete
 Builds a network of nodes from the rules
   each node corresponds to a condition of a rule
 Path from root to leaf defines a complete rule
 As fact are asserted or modified they propagate along
 the network and are evaluated at each node
 When a fact or combination facts match all paterns for
 a rule a leaf node is reached and the rule’s action is
 fired
           So what? It’s FAST, yo!
Using Ruleby, step 1

Build rules in a rule book using the DSL
  rule conditions
  action(s)
  modifications to facts
    modified facts are re-propragated thru network
Using Ruleby, step 2



Create an engine and initialize the rules
Using Ruleby, step 3



Assert facts by adding objects (data) to “working
memory”
Using Ruleby, step 4



Fire the engine to match facts to rules
Building rules

 Rules are defined by the Ruleby DSL (“Ferrari” syntax)
 Create multiple rules inside a single rule book method
   allows you to organize, name and group rules
   each rule is still independent
 Can build rules dynamically (this is Ruby, ya know!)
Typical rule syntax

 rule :identifier, [pattern] do |context|
   # action(s)
 end
Basic pattern syntax
[ClassType, :object_handle, cond_1, cond_n]

notes
 - Class type is required
 - :object_handle is optional (ref in block)
 - 0 to n conditions

          Let’s look at the hello.rb
                  example
Notes on conditions

Call methods on facts (objects) using method.x or m.x
Most examples show ==, there are others (<, >, etc.)
Can pass a block parameter that evals to T/F
  use &condition{ ... } or &c{ ... } for short
  condition is an alias for ‘lambda’
  see cart_rulebook.rb example
Ruleby in a “real” Rails app


 Put rule book in app/rules (don’t forget load_path)
   Inherit from Ruleby::Rulebook
 Define rules with conditions and actions
   assert and/or modify facts (your Ruby objects)
Firing the Engine

 Typically done in a controller
   Create an engine
   Get a rule book and initialize the rules
   Assert facts
   Call engine.match to fire it
References


Ruleby: https://github.com/codeaspects/ruleby
Rete: http://en.wikipedia.org/wiki/Rete_algorithm

Contenu connexe

Tendances

Rules Programming tutorial
Rules Programming tutorialRules Programming tutorial
Rules Programming tutorialSrinath Perera
 
Buenos Aires Drools Expert Presentation
Buenos Aires Drools Expert PresentationBuenos Aires Drools Expert Presentation
Buenos Aires Drools Expert PresentationMark Proctor
 
Rule Engine: Drools .Net
Rule Engine: Drools .NetRule Engine: Drools .Net
Rule Engine: Drools .NetGuo Albert
 
Introducing Drools
Introducing DroolsIntroducing Drools
Introducing DroolsMario Fusco
 
Drools & jBPM Info Sheet
Drools & jBPM Info SheetDrools & jBPM Info Sheet
Drools & jBPM Info SheetMark Proctor
 
Comparing different concurrency models on the JVM
Comparing different concurrency models on the JVMComparing different concurrency models on the JVM
Comparing different concurrency models on the JVMMario Fusco
 
Drools 6 deep dive
Drools 6 deep diveDrools 6 deep dive
Drools 6 deep diveMario Fusco
 
Javascript foundations: Function modules
Javascript foundations: Function modulesJavascript foundations: Function modules
Javascript foundations: Function modulesJohn Hunter
 
SOLID mit Java 8
SOLID mit Java 8SOLID mit Java 8
SOLID mit Java 8Roland Mast
 
The Ring programming language version 1.5.2 book - Part 72 of 181
The Ring programming language version 1.5.2 book - Part 72 of 181The Ring programming language version 1.5.2 book - Part 72 of 181
The Ring programming language version 1.5.2 book - Part 72 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 75 of 185
The Ring programming language version 1.5.4 book - Part 75 of 185The Ring programming language version 1.5.4 book - Part 75 of 185
The Ring programming language version 1.5.4 book - Part 75 of 185Mahmoud Samir Fayed
 
How and why I turned my old Java projects into a first-class serverless compo...
How and why I turned my old Java projects into a first-class serverless compo...How and why I turned my old Java projects into a first-class serverless compo...
How and why I turned my old Java projects into a first-class serverless compo...Mario Fusco
 

Tendances (20)

Rules Programming tutorial
Rules Programming tutorialRules Programming tutorial
Rules Programming tutorial
 
Buenos Aires Drools Expert Presentation
Buenos Aires Drools Expert PresentationBuenos Aires Drools Expert Presentation
Buenos Aires Drools Expert Presentation
 
Rule Engine: Drools .Net
Rule Engine: Drools .NetRule Engine: Drools .Net
Rule Engine: Drools .Net
 
Introducing Drools
Introducing DroolsIntroducing Drools
Introducing Drools
 
Drools & jBPM Info Sheet
Drools & jBPM Info SheetDrools & jBPM Info Sheet
Drools & jBPM Info Sheet
 
Rules With Drools
Rules With DroolsRules With Drools
Rules With Drools
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Drools BeJUG 2010
Drools BeJUG 2010Drools BeJUG 2010
Drools BeJUG 2010
 
Comparing different concurrency models on the JVM
Comparing different concurrency models on the JVMComparing different concurrency models on the JVM
Comparing different concurrency models on the JVM
 
Drools 6 deep dive
Drools 6 deep diveDrools 6 deep dive
Drools 6 deep dive
 
Drools
DroolsDrools
Drools
 
Javascript foundations: Function modules
Javascript foundations: Function modulesJavascript foundations: Function modules
Javascript foundations: Function modules
 
Drools Workshop 2015 - LATAM
Drools Workshop 2015 - LATAMDrools Workshop 2015 - LATAM
Drools Workshop 2015 - LATAM
 
SOLID mit Java 8
SOLID mit Java 8SOLID mit Java 8
SOLID mit Java 8
 
SOLID Java Code
SOLID Java CodeSOLID Java Code
SOLID Java Code
 
The Ring programming language version 1.5.2 book - Part 72 of 181
The Ring programming language version 1.5.2 book - Part 72 of 181The Ring programming language version 1.5.2 book - Part 72 of 181
The Ring programming language version 1.5.2 book - Part 72 of 181
 
The Ring programming language version 1.5.4 book - Part 75 of 185
The Ring programming language version 1.5.4 book - Part 75 of 185The Ring programming language version 1.5.4 book - Part 75 of 185
The Ring programming language version 1.5.4 book - Part 75 of 185
 
G pars
G parsG pars
G pars
 
How and why I turned my old Java projects into a first-class serverless compo...
How and why I turned my old Java projects into a first-class serverless compo...How and why I turned my old Java projects into a first-class serverless compo...
How and why I turned my old Java projects into a first-class serverless compo...
 
JBoss World 2011 - Drools
JBoss World 2011 - DroolsJBoss World 2011 - Drools
JBoss World 2011 - Drools
 

Similaire à Ruleby

EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl Lyndon White
 
SWT Lecture Session 6 - RDFS semantics, inference techniques, sesame rdfs
SWT Lecture Session 6 - RDFS semantics, inference techniques, sesame rdfsSWT Lecture Session 6 - RDFS semantics, inference techniques, sesame rdfs
SWT Lecture Session 6 - RDFS semantics, inference techniques, sesame rdfsMariano Rodriguez-Muro
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsJeff Durta
 
Intro to Ruby on Rails
Intro to Ruby on RailsIntro to Ruby on Rails
Intro to Ruby on Railselpizoch
 
Accurately and Reliably Extracting Data from the Web:
Accurately and Reliably Extracting Data from the Web: Accurately and Reliably Extracting Data from the Web:
Accurately and Reliably Extracting Data from the Web: butest
 
TypeDB Academy | Inference with Rules
TypeDB Academy | Inference with RulesTypeDB Academy | Inference with Rules
TypeDB Academy | Inference with RulesVaticle
 
Large-scale Reasoning with a Complex Cultural Heritage Ontology (CIDOC CRM) ...
 Large-scale Reasoning with a Complex Cultural Heritage Ontology (CIDOC CRM) ... Large-scale Reasoning with a Complex Cultural Heritage Ontology (CIDOC CRM) ...
Large-scale Reasoning with a Complex Cultural Heritage Ontology (CIDOC CRM) ...Vladimir Alexiev, PhD, PMP
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworksMD Sayem Ahmed
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalystdwm042
 
Droolsand Rule Based Systems 2008 Srping
Droolsand Rule Based Systems 2008 SrpingDroolsand Rule Based Systems 2008 Srping
Droolsand Rule Based Systems 2008 SrpingSrinath Perera
 
Ruby On Rails Siddhesh
Ruby On Rails SiddheshRuby On Rails Siddhesh
Ruby On Rails SiddheshSiddhesh Bhobe
 
Information Extraction
Information ExtractionInformation Extraction
Information Extractionbutest
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkAkhil Mittal
 
The Django Book / Chapter 3: Views and URLconfs
The Django Book / Chapter 3: Views and URLconfsThe Django Book / Chapter 3: Views and URLconfs
The Django Book / Chapter 3: Views and URLconfsVincent Chien
 
New_ML_Lecture_9.ppt
New_ML_Lecture_9.pptNew_ML_Lecture_9.ppt
New_ML_Lecture_9.pptEmmaMaria6
 
Four ways to represent computer executable rules
Four ways to represent computer executable rulesFour ways to represent computer executable rules
Four ways to represent computer executable rulesJeff Long
 
CS5393-Korat_Mittal_Akshay_ProjReport
CS5393-Korat_Mittal_Akshay_ProjReportCS5393-Korat_Mittal_Akshay_ProjReport
CS5393-Korat_Mittal_Akshay_ProjReportAkshay Mittal
 

Similaire à Ruleby (20)

EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl
 
SWT Lecture Session 6 - RDFS semantics, inference techniques, sesame rdfs
SWT Lecture Session 6 - RDFS semantics, inference techniques, sesame rdfsSWT Lecture Session 6 - RDFS semantics, inference techniques, sesame rdfs
SWT Lecture Session 6 - RDFS semantics, inference techniques, sesame rdfs
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Intro to Ruby on Rails
Intro to Ruby on RailsIntro to Ruby on Rails
Intro to Ruby on Rails
 
Ruby openfest
Ruby openfestRuby openfest
Ruby openfest
 
Accurately and Reliably Extracting Data from the Web:
Accurately and Reliably Extracting Data from the Web: Accurately and Reliably Extracting Data from the Web:
Accurately and Reliably Extracting Data from the Web:
 
TypeDB Academy | Inference with Rules
TypeDB Academy | Inference with RulesTypeDB Academy | Inference with Rules
TypeDB Academy | Inference with Rules
 
Large-scale Reasoning with a Complex Cultural Heritage Ontology (CIDOC CRM) ...
 Large-scale Reasoning with a Complex Cultural Heritage Ontology (CIDOC CRM) ... Large-scale Reasoning with a Complex Cultural Heritage Ontology (CIDOC CRM) ...
Large-scale Reasoning with a Complex Cultural Heritage Ontology (CIDOC CRM) ...
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworks
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Droolsand Rule Based Systems 2008 Srping
Droolsand Rule Based Systems 2008 SrpingDroolsand Rule Based Systems 2008 Srping
Droolsand Rule Based Systems 2008 Srping
 
Ruby On Rails Siddhesh
Ruby On Rails SiddheshRuby On Rails Siddhesh
Ruby On Rails Siddhesh
 
Data access
Data accessData access
Data access
 
Information Extraction
Information ExtractionInformation Extraction
Information Extraction
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
 
The Django Book / Chapter 3: Views and URLconfs
The Django Book / Chapter 3: Views and URLconfsThe Django Book / Chapter 3: Views and URLconfs
The Django Book / Chapter 3: Views and URLconfs
 
New_ML_Lecture_9.ppt
New_ML_Lecture_9.pptNew_ML_Lecture_9.ppt
New_ML_Lecture_9.ppt
 
Four ways to represent computer executable rules
Four ways to represent computer executable rulesFour ways to represent computer executable rules
Four ways to represent computer executable rules
 
CS5393-Korat_Mittal_Akshay_ProjReport
CS5393-Korat_Mittal_Akshay_ProjReportCS5393-Korat_Mittal_Akshay_ProjReport
CS5393-Korat_Mittal_Akshay_ProjReport
 

Dernier

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 

Dernier (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 

Ruleby

  • 1. Ruleby: a Ruby rule engine Chris McCann
  • 2. What’s a “rule engine”? system for executing IF-THEN statements in ruleby these are called “production rules”
  • 3. What’s wrong with IF-THEN Complex code Interdependencies State transitions Spaghetti code!
  • 4. if firstname && lastname if type == :group if groupname ... end elsif type == :individual ... end end Care to add a new ‘type’?
  • 5. Ruleby: a better way Rule-based programming a rule engine that derives answers from a ‘knowledge base’ the knowledge base contains the rules give it facts to compare against the rules
  • 6. 3 Parts of a rule engine Facts :: data or objects, aka “working memory” Rules :: the IF-THEN statements Inference Engine :: an efficient mechanism for executing the rules against the facts
  • 7. The Ruleby inference engine An implementation of the Rete algorithm that matches patterns to objects pronounced “ree-tee” or “ray-tee” from Latin for “net” or Italian for “network” Proposed by Dr. Charles Forgy in 1974 at CMU was his PhD thesis in 1979 and a paper in 1984
  • 8. Dirt Farmer’s guide to Rete Builds a network of nodes from the rules each node corresponds to a condition of a rule Path from root to leaf defines a complete rule As fact are asserted or modified they propagate along the network and are evaluated at each node When a fact or combination facts match all paterns for a rule a leaf node is reached and the rule’s action is fired So what? It’s FAST, yo!
  • 9. Using Ruleby, step 1 Build rules in a rule book using the DSL rule conditions action(s) modifications to facts modified facts are re-propragated thru network
  • 10. Using Ruleby, step 2 Create an engine and initialize the rules
  • 11. Using Ruleby, step 3 Assert facts by adding objects (data) to “working memory”
  • 12. Using Ruleby, step 4 Fire the engine to match facts to rules
  • 13. Building rules Rules are defined by the Ruleby DSL (“Ferrari” syntax) Create multiple rules inside a single rule book method allows you to organize, name and group rules each rule is still independent Can build rules dynamically (this is Ruby, ya know!)
  • 14. Typical rule syntax rule :identifier, [pattern] do |context| # action(s) end
  • 15. Basic pattern syntax [ClassType, :object_handle, cond_1, cond_n] notes - Class type is required - :object_handle is optional (ref in block) - 0 to n conditions Let’s look at the hello.rb example
  • 16. Notes on conditions Call methods on facts (objects) using method.x or m.x Most examples show ==, there are others (<, >, etc.) Can pass a block parameter that evals to T/F use &condition{ ... } or &c{ ... } for short condition is an alias for ‘lambda’ see cart_rulebook.rb example
  • 17. Ruleby in a “real” Rails app Put rule book in app/rules (don’t forget load_path) Inherit from Ruleby::Rulebook Define rules with conditions and actions assert and/or modify facts (your Ruby objects)
  • 18. Firing the Engine Typically done in a controller Create an engine Get a rule book and initialize the rules Assert facts Call engine.match to fire it

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n