Skip to content

KBS - Knowledge-Based System

KBS - Knowledge-Based Systems for Ruby

A Ruby implementation of the RETE algorithm for building intelligent, rule-based systems with persistent memory.

Gem Version Ruby License

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:

  1. Network Compilation: Rules are compiled into a discrimination network that shares common patterns
  2. State Preservation: Partial matches are cached between cycles
  3. Incremental Updates: Only changed facts propagate through the network
  4. 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

  1. Installation - Add KBS to your project
  2. Quick Start - Build your first rule-based system in 5 minutes
  3. RETE Algorithm - Deep dive into how it works
  4. Writing Rules - Master the DSL and pattern matching
  5. 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

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!