KBS - Knowledge-Based Systems for Ruby¶
A Ruby implementation of the RETE algorithm for building intelligent, rule-based systems with persistent memory.
What is KBS?¶
KBS (Knowledge-Based Systems) is a powerful Ruby gem that brings production rule systems to your applications. At its core is the RETE algorithm, a highly optimized pattern-matching engine originally developed for expert systems and now used in modern applications ranging from trading systems to IoT automation.
Key Features¶
- π RETE Algorithm: State-of-the-art pattern matching with unlinking optimization
- πΎ Persistent Blackboard Memory: SQLite, Redis, or hybrid storage for facts and audit trails
- π― Declarative DSL: Write rules in natural, readable Ruby syntax
- π Incremental Matching: Process only changes, not entire fact sets
- π« Negation Support: Express "absence of pattern" conditions naturally
- π Multi-Agent Systems: Build collaborative systems with message passing
- π Full Auditability: Complete history of fact changes and rule firings
- β‘ High Performance: Handle millions of facts with sub-millisecond updates
Quick Example¶
require 'kbs'
# Create a rule-based trading system
kb = KBS.knowledge_base do
# Define a rule using the DSL
rule "buy_signal" do
# Stock price is below threshold
on :stock, symbol: :symbol?, price: :price?
on :threshold, symbol: :symbol?, buy_below: :threshold?
# No pending order exists (negation)
without :order, symbol: :symbol?
perform do |facts, bindings|
if bindings[:price?] < bindings[:threshold?]
puts "BUY #{bindings[:symbol?]} at #{bindings[:price?]}"
end
end
end
# Add facts to working memory
fact :stock, symbol: "AAPL", price: 145.50
fact :threshold, symbol: "AAPL", buy_below: 150.0
# Fire matching rules
run # => BUY AAPL at 145.5
end
Why RETE?¶
Traditional rule engines re-evaluate all rules against all facts on every changeβextremely inefficient. RETE solves this through:
- Network Compilation: Rules are compiled into a discrimination network that shares common patterns
- State Preservation: Partial matches are cached between cycles
- Incremental Updates: Only changed facts propagate through the network
- Unlinking Optimization (RETE): Empty nodes automatically disconnect to skip unnecessary work
Result: Near-constant time per fact change, regardless of rule set size.
Use Cases¶
πΉ Algorithmic Trading¶
Real-time market analysis, signal detection, and automated order execution with complex multi-condition rules.
π Industrial Automation¶
IoT sensor monitoring, predictive maintenance, and automated control systems with temporal reasoning.
π₯ Expert Systems¶
Medical diagnosis, troubleshooting assistants, and decision support systems with knowledge representation.
π€ Multi-Agent Systems¶
Collaborative agents with shared blackboard memory for distributed problem-solving.
π§ Business Rules Engines¶
Policy enforcement, workflow automation, and compliance checking with auditable decision trails.
Architecture¶
KBS consists of several integrated components:
- RETE Engine: Core pattern matching and rule execution
- Working Memory: Transient in-memory fact storage
- Blackboard System: Persistent memory with SQLite/Redis backends
- DSL: Natural language rule definition syntax
- Message Queue: Priority-based inter-agent communication
- Audit Log: Complete history for compliance and debugging
See Architecture Overview for details.
Getting Started¶
- Installation - Add KBS to your project
- Quick Start - Build your first rule-based system in 5 minutes
- RETE Algorithm - Deep dive into how it works
- Writing Rules - Master the DSL and pattern matching
- Examples - Learn from real-world applications
Performance¶
KBS is built for production workloads:
- Fact Addition: O(N) where N = activated nodes (typically << total nodes)
- Rule Firing: O(M) where M = matched tokens
- Memory Efficient: Network sharing reduces redundant storage
- Scalable: Tested with millions of facts, thousands of rules
Benchmarks on M2 Max: - Add 100,000 facts: ~500ms - Match complex 5-condition rule: <1ms per fact - Redis backend: 100x faster than SQLite for high-frequency updates
Project Status¶
KBS is actively maintained:
- β Core RETE implementation complete
- β Persistent blackboard with multiple backends
- β Full DSL support with negation
- β Comprehensive test coverage
- β Real-world usage in trading systems
- π§ Additional examples and guides in progress
Community & Support¶
- GitHub: madbomber/kbs
- RubyGems: kbs
- Issues: Report bugs or request features
- Discussions: Ask questions
License¶
KBS is released under the MIT License.
Copyright Β© 2024 Dewayne VanHoozer
Acknowledgments¶
The RETE algorithm was invented by Charles Forgy in 1979. This implementation draws inspiration from:
- Forgy, C. (1982). "Rete: A Fast Algorithm for the Many Pattern/Many Object Pattern Match Problem"
- Doorenbos, R. (1995). "Production Matching for Large Learning Systems" (RETE/UL)
- Modern production rule systems: Drools, Jess, CLIPS
Ready to build intelligent systems? Start with the Quick Start Guide!