Module grl_query

Module grl_query 

Source
Expand description

GRL Query Syntax Implementation

This module provides parsing and execution of backward chaining queries defined in GRL (Goal-driven Rule Language) syntax. GRL queries allow you to define goal-driven reasoning tasks with configurable search strategies and action handlers.

§Features

  • Declarative query syntax - Define queries in a readable, structured format
  • Multiple search strategies - Choose between depth-first, breadth-first, or iterative deepening
  • Action handlers - Execute actions on query success, failure, or missing facts
  • Conditional execution - Use when clauses to conditionally execute queries
  • Parameterized queries - Support for query parameters (future enhancement)

§GRL Query Syntax

query "QueryName" {
    goal: <expression>                    // Required: Goal to prove
    strategy: <depth-first|breadth-first|iterative>  // Optional: Search strategy
    max-depth: <number>                   // Optional: Maximum search depth
    max-solutions: <number>               // Optional: Maximum solutions to find
    enable-memoization: <true|false>      // Optional: Enable result caching

    when: <condition>                     // Optional: Only execute if condition is true

    on-success: {                         // Optional: Actions on successful proof
        <variable> = <value>;
        <FunctionName>(<args>);
    }

    on-failure: {                         // Optional: Actions on proof failure
        <actions>
    }

    on-missing: {                         // Optional: Actions when facts are missing
        <actions>
    }
}

§Example

use rust_rule_engine::backward::grl_query::{GRLQueryParser, GRLQueryExecutor};
use rust_rule_engine::backward::BackwardEngine;
use rust_rule_engine::{KnowledgeBase, Facts, Value};

let query_str = r#"
query "CheckVIPStatus" {
    goal: User.IsVIP == true
    strategy: depth-first
    max-depth: 10
    on-success: {
        User.DiscountRate = 0.2;
        LogMessage("VIP confirmed");
    }
    on-failure: {
        LogMessage("Not a VIP user");
    }
}
"#;

let query = GRLQueryParser::parse(query_str)?;
let mut bc_engine = BackwardEngine::new(KnowledgeBase::new("kb"));
let mut facts = Facts::new();
facts.set("User.LoyaltyPoints", Value::Number(1500.0));

let result = GRLQueryExecutor::execute(&query, &mut bc_engine, &mut facts)?;

if result.provable {
    println!("Goal proven!");
}

§Supported Functions in Actions

  • LogMessage(message) - Print a log message
  • Request(message) - Send a request message
  • Print(message) - Print output
  • Debug(message) - Print debug output to stderr

Structs§

GRLQuery
A GRL Query definition
GRLQueryExecutor
Executor for GRL queries
GRLQueryParser
Parser for GRL Query syntax
QueryAction
Action to execute based on query result

Enums§

GRLSearchStrategy
Search strategy option for queries