1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! GRL to RETE Converter
//!
//! This module converts GRL (Grule Rule Language) rules into RETE-UL structures
//! for efficient pattern matching and rule execution.
#![allow(clippy::type_complexity)]
#![allow(deprecated)]
use crate::engine::rule::{Condition, ConditionGroup, Rule};
use crate::errors::{Result, RuleEngineError};
use crate::parser::GRLParser;
use crate::rete::facts::{FactValue, TypedFacts};
use crate::rete::propagation::IncrementalEngine;
use crate::rete::{AlphaNode, ReteUlNode, TypedReteUlRule};
use crate::types::{Operator, Value};
use log::info;
use std::fs;
use std::path::Path;
#[cfg(feature = "streaming")]
use crate::rete::network::{StreamWindowSpec, StreamWindowTypeRete};
/// GRL to RETE Loader
/// Converts GRL rules into RETE-UL structures
pub struct GrlReteLoader;
impl GrlReteLoader {
/// Load rules from a GRL file into RETE engine
pub fn load_from_file<P: AsRef<Path>>(
path: P,
engine: &mut IncrementalEngine,
) -> Result<usize> {
let grl_text =
fs::read_to_string(path.as_ref()).map_err(|e| RuleEngineError::ParseError {
message: format!("Failed to read GRL file: {}", e),
})?;
Self::load_from_string(&grl_text, engine)
}
/// Load rules from GRL string into RETE engine
pub fn load_from_string(grl_text: &str, engine: &mut IncrementalEngine) -> Result<usize> {
// Parse GRL rules
let rules = GRLParser::parse_rules(grl_text)?;
let mut loaded_count = 0;
for rule in rules {
// Convert GRL rule to RETE rule
let rete_rule = Self::convert_rule_to_rete(rule)?;
// Extract dependencies (fact types used in conditions)
let dependencies = Self::extract_dependencies(&rete_rule);
// Add to engine
engine.add_rule(rete_rule, dependencies);
loaded_count += 1;
}
Ok(loaded_count)
}
/// Convert GRL Rule to TypedReteUlRule
fn convert_rule_to_rete(rule: Rule) -> Result<TypedReteUlRule> {
// Convert ConditionGroup to ReteUlNode
let node = Self::convert_condition_group(&rule.conditions)?;
// Create RETE rule
let rete_rule = TypedReteUlRule {
name: rule.name.clone(),
node,
priority: rule.salience,
no_loop: rule.no_loop,
action: Self::create_action_closure(rule.actions),
};
Ok(rete_rule)
}
/// Convert ConditionGroup to ReteUlNode
fn convert_condition_group(group: &ConditionGroup) -> Result<ReteUlNode> {
match group {
ConditionGroup::Single(condition) => Self::convert_condition(condition),
ConditionGroup::Compound {
left,
operator,
right,
} => {
let left_node = Self::convert_condition_group(left)?;
let right_node = Self::convert_condition_group(right)?;
match operator {
crate::types::LogicalOperator::And => {
Ok(ReteUlNode::UlAnd(Box::new(left_node), Box::new(right_node)))
}
crate::types::LogicalOperator::Or => {
Ok(ReteUlNode::UlOr(Box::new(left_node), Box::new(right_node)))
}
crate::types::LogicalOperator::Not => {
// For NOT, we only use left node
Ok(ReteUlNode::UlNot(Box::new(left_node)))
}
}
}
ConditionGroup::Not(inner) => {
let inner_node = Self::convert_condition_group(inner)?;
Ok(ReteUlNode::UlNot(Box::new(inner_node)))
}
ConditionGroup::Exists(inner) => {
let inner_node = Self::convert_condition_group(inner)?;
Ok(ReteUlNode::UlExists(Box::new(inner_node)))
}
ConditionGroup::Forall(inner) => {
let inner_node = Self::convert_condition_group(inner)?;
Ok(ReteUlNode::UlForall(Box::new(inner_node)))
}
ConditionGroup::Accumulate {
result_var,
source_pattern,
extract_field,
source_conditions,
function,
function_arg,
} => Ok(ReteUlNode::UlAccumulate {
result_var: result_var.clone(),
source_pattern: source_pattern.clone(),
extract_field: extract_field.clone(),
source_conditions: source_conditions.clone(),
function: function.clone(),
function_arg: function_arg.clone(),
}),
#[cfg(feature = "streaming")]
ConditionGroup::StreamPattern {
var_name,
event_type,
stream_name,
window,
} => {
// Convert stream pattern to RETE UlStream node
Ok(ReteUlNode::UlStream {
var_name: var_name.clone(),
event_type: event_type.clone(),
stream_name: stream_name.clone(),
window: window.as_ref().map(|w| StreamWindowSpec {
duration: w.duration,
window_type: match &w.window_type {
crate::engine::rule::StreamWindowType::Sliding => {
StreamWindowTypeRete::Sliding
}
crate::engine::rule::StreamWindowType::Tumbling => {
StreamWindowTypeRete::Tumbling
}
crate::engine::rule::StreamWindowType::Session { timeout } => {
StreamWindowTypeRete::Session { timeout: *timeout }
}
},
}),
})
}
}
}
/// Convert single Condition to ReteUlNode (AlphaNode or UlMultiField)
fn convert_condition(condition: &Condition) -> Result<ReteUlNode> {
use crate::engine::rule::ConditionExpression;
// Check if this is a multifield condition
match &condition.expression {
ConditionExpression::MultiField {
field,
operation,
variable: _,
} => {
// Convert to UlMultiField node
let operator_str = Self::operator_to_string(&condition.operator);
let value_str = if !matches!(condition.value, Value::Boolean(_)) {
Some(Self::value_to_string(&condition.value))
} else {
None
};
// Determine if this is a count operation with comparison
let (op, cmp_val) = if operation == "count" && operator_str != "==" {
// Count with comparison: "count > 5"
(Some(operator_str), value_str)
} else {
// Other operations
(None, value_str)
};
Ok(ReteUlNode::UlMultiField {
field: field.clone(),
operation: operation.clone(),
value: if operation == "contains" {
cmp_val.clone()
} else {
None
},
operator: op,
compare_value: if operation == "count" { cmp_val } else { None },
})
}
_ => {
// Standard alpha node for regular conditions
let operator_str = Self::operator_to_string(&condition.operator);
let value_str = Self::value_to_string(&condition.value);
let alpha = AlphaNode {
field: condition.field.clone(),
operator: operator_str,
value: value_str,
};
Ok(ReteUlNode::UlAlpha(alpha))
}
}
}
/// Convert Operator to string
fn operator_to_string(op: &Operator) -> String {
match op {
Operator::Equal => "==".to_string(),
Operator::NotEqual => "!=".to_string(),
Operator::GreaterThan => ">".to_string(),
Operator::GreaterThanOrEqual => ">=".to_string(),
Operator::LessThan => "<".to_string(),
Operator::LessThanOrEqual => "<=".to_string(),
Operator::Contains => "contains".to_string(),
Operator::NotContains => "!contains".to_string(),
Operator::StartsWith => "startsWith".to_string(),
Operator::EndsWith => "endsWith".to_string(),
Operator::Matches => "matches".to_string(),
Operator::In => "in".to_string(),
}
}
/// Convert Value to string for AlphaNode
fn value_to_string(value: &Value) -> String {
match value {
Value::Number(n) => n.to_string(),
Value::Integer(i) => i.to_string(),
Value::String(s) => s.clone(),
Value::Boolean(b) => b.to_string(),
Value::Null => "null".to_string(),
Value::Array(arr) => {
// Convert array to JSON-like string
let items: Vec<String> = arr.iter().map(Self::value_to_string).collect();
format!("[{}]", items.join(","))
}
Value::Object(_) => {
// For objects, we'll use a simplified representation
"object".to_string()
}
Value::Expression(expr) => {
// For expressions, return the expression string
expr.clone()
}
}
}
/// Create action closure from ActionType list
fn create_action_closure(
actions: Vec<crate::types::ActionType>,
) -> std::sync::Arc<dyn Fn(&mut TypedFacts, &mut super::ActionResults) + Send + Sync> {
std::sync::Arc::new(
move |facts: &mut TypedFacts, results: &mut super::ActionResults| {
// Execute actions
for action in &actions {
Self::execute_action(action, facts, results);
}
},
)
}
/// Execute a single action
fn execute_action(
action: &crate::types::ActionType,
facts: &mut TypedFacts,
results: &mut super::ActionResults,
) {
use crate::types::ActionType;
match action {
ActionType::Set { field, value } => {
// Assignment action (from "field = value" syntax in GRL)
// Note: Set() function syntax is NOT supported.
// Use: Player.score = Player.score + 10;
// Check if value is an expression that needs evaluation
let evaluated_value = match value {
Value::Expression(expr) => {
// Evaluate expression with current facts
Self::evaluate_expression_for_rete(expr, facts)
}
_ => value.clone(),
};
// Convert evaluated value to FactValue
let fact_value = Self::value_to_fact_value(&evaluated_value);
facts.set(field, fact_value);
}
ActionType::Log { message } => {
info!("📝 {}", message);
}
ActionType::MethodCall {
object,
method,
args,
} => {
// Method calls can be treated as function calls with object as first arg
let mut all_args = vec![object.clone()];
all_args.extend(args.iter().map(Self::value_to_string));
results.add(super::ActionResult::CallFunction {
function_name: format!("{}.{}", object, method),
args: all_args,
});
println!("� METHOD: {}.{}", object, method);
}
ActionType::Retract { object } => {
// Strip quotes from object name if present
let object_name = object.trim_matches('"');
// Try to get the handle for this fact type from metadata
if let Some(handle) = facts.get_fact_handle(object_name) {
// Retract specific fact by handle
results.add(super::ActionResult::Retract(handle));
println!("🗑️ RETRACT: {} (handle: {:?})", object_name, handle);
} else {
// Fallback: retract by type (first matching fact)
results.add(super::ActionResult::RetractByType(object_name.to_string()));
println!("🗑️ RETRACT: {} (by type, no handle found)", object_name);
}
}
ActionType::Custom {
action_type,
params,
} => {
// Treat custom actions as function calls
let args: Vec<String> = params.values().map(Self::value_to_string).collect();
results.add(super::ActionResult::CallFunction {
function_name: action_type.clone(),
args,
});
println!("🔧 CUSTOM CALL: {}", action_type);
}
ActionType::ActivateAgendaGroup { group } => {
// Queue agenda group activation
results.add(super::ActionResult::ActivateAgendaGroup(group.clone()));
println!("📋 ACTIVATE GROUP: {}", group);
}
ActionType::ScheduleRule {
rule_name,
delay_ms,
} => {
// Queue rule scheduling
results.add(super::ActionResult::ScheduleRule {
rule_name: rule_name.clone(),
delay_ms: *delay_ms,
});
println!("⏰ SCHEDULE: {} (delay: {}ms)", rule_name, delay_ms);
}
ActionType::CompleteWorkflow { workflow_name } => {
// Mark workflow as completed by setting a fact
let completion_key = format!("workflow.{}.completed", workflow_name);
facts.set(&completion_key, FactValue::Boolean(true));
let timestamp_key = format!("workflow.{}.completed_at", workflow_name);
facts.set(
×tamp_key,
FactValue::Integer(chrono::Utc::now().timestamp()),
);
println!("✔️ WORKFLOW COMPLETED: {}", workflow_name);
}
ActionType::SetWorkflowData { key, value } => {
// Store workflow data as facts with "workflow.data." prefix
let data_key = format!("workflow.data.{}", key);
let fact_value = Self::value_to_fact_value(value);
facts.set(&data_key, fact_value);
println!("📊 WORKFLOW DATA SET: {} = {:?}", key, value);
}
ActionType::Append { field, value } => {
// Append to array field
// Get current array or create new one
let current_value = facts.get(field);
let mut array = match current_value {
Some(FactValue::Array(arr)) => arr.clone(),
Some(_) => {
// Field exists but is not an array, create new array
log::warn!("Field {} is not an array, creating new array", field);
Vec::new()
}
None => {
// Field doesn't exist, create new array
Vec::new()
}
};
// Evaluate value if it's an expression
let evaluated_value = match value {
Value::Expression(expr) => Self::evaluate_expression_for_rete(expr, facts),
_ => value.clone(),
};
// Convert to FactValue and append
let fact_value = Self::value_to_fact_value(&evaluated_value);
array.push(fact_value);
// Set the updated array
facts.set(field, FactValue::Array(array));
info!("➕ APPEND: {} += {:?}", field, evaluated_value);
}
}
}
/// Convert Value to FactValue
fn value_to_fact_value(value: &Value) -> FactValue {
match value {
Value::Number(n) => {
// Try integer first, fall back to float
if n.fract() == 0.0 {
FactValue::Integer(*n as i64)
} else {
FactValue::Float(*n)
}
}
Value::Integer(i) => FactValue::Integer(*i),
Value::String(s) => FactValue::String(s.clone()),
Value::Boolean(b) => FactValue::Boolean(*b),
Value::Null => FactValue::Null,
Value::Array(arr) => {
let fact_arr: Vec<FactValue> = arr.iter().map(Self::value_to_fact_value).collect();
FactValue::Array(fact_arr)
}
Value::Object(_) => {
// For now, treat objects as strings
FactValue::String("object".to_string())
}
Value::Expression(expr) => {
// For expressions, store as string - will be evaluated at runtime
FactValue::String(format!("[EXPR: {}]", expr))
}
}
}
/// Extract fact type dependencies from rule
fn extract_dependencies(rule: &TypedReteUlRule) -> Vec<String> {
let mut deps = Vec::new();
Self::extract_deps_from_node(&rule.node, &mut deps);
// Deduplicate
deps.sort();
deps.dedup();
deps
}
/// Recursively extract dependencies from ReteUlNode
fn extract_deps_from_node(node: &ReteUlNode, deps: &mut Vec<String>) {
match node {
ReteUlNode::UlAlpha(alpha) => {
// Extract fact type from field (e.g., "Person.age" -> "Person")
if let Some(dot_pos) = alpha.field.find('.') {
let fact_type = alpha.field[..dot_pos].to_string();
deps.push(fact_type);
}
}
ReteUlNode::UlMultiField { field, .. } => {
// Extract fact type from field (e.g., "Order.items" -> "Order")
if let Some(dot_pos) = field.find('.') {
let fact_type = field[..dot_pos].to_string();
deps.push(fact_type);
}
}
ReteUlNode::UlAnd(left, right) | ReteUlNode::UlOr(left, right) => {
Self::extract_deps_from_node(left, deps);
Self::extract_deps_from_node(right, deps);
}
ReteUlNode::UlNot(inner)
| ReteUlNode::UlExists(inner)
| ReteUlNode::UlForall(inner) => {
Self::extract_deps_from_node(inner, deps);
}
ReteUlNode::UlAccumulate { source_pattern, .. } => {
// Add source pattern as a dependency
deps.push(source_pattern.clone());
}
#[cfg(feature = "streaming")]
ReteUlNode::UlStream { stream_name, .. } => {
// Add stream name as a dependency
deps.push(stream_name.clone());
}
ReteUlNode::UlTerminal(_) => {
// Terminal nodes don't have dependencies
}
}
}
/// Evaluate expression for RETE engine (converts TypedFacts to Facts temporarily)
fn evaluate_expression_for_rete(expr: &str, typed_facts: &TypedFacts) -> Value {
// Convert TypedFacts to Facts for expression evaluation
use crate::engine::facts::Facts;
let facts = Facts::new();
// Copy all facts from TypedFacts to Facts
// RETE stores facts as "quantity" while GRL uses "Order.quantity"
// We need to support both formats
for (key, value) in typed_facts.get_all() {
let converted_value = Self::fact_value_to_value(value);
// Store both with and without prefix
// E.g., "quantity" -> both "quantity" and "Order.quantity"
facts.set(key, converted_value.clone());
// Also try to add with "Order." prefix if not already present
if !key.contains('.') {
facts.set(&format!("Order.{}", key), converted_value);
}
}
// Evaluate expression
match crate::expression::evaluate_expression(expr, &facts) {
Ok(result) => result,
Err(_e) => {
// Silently fallback - this can happen with chained expressions in RETE
// due to working memory complexity
Value::String(expr.to_string())
}
}
}
/// Convert FactValue back to Value (reverse of value_to_fact_value)
fn fact_value_to_value(fact_value: &FactValue) -> Value {
match fact_value {
FactValue::String(s) => {
// Try to parse as number first
if let Ok(i) = s.parse::<i64>() {
Value::Integer(i)
} else if let Ok(f) = s.parse::<f64>() {
Value::Number(f)
} else if s == "true" {
Value::Boolean(true)
} else if s == "false" {
Value::Boolean(false)
} else {
Value::String(s.clone())
}
}
FactValue::Integer(i) => Value::Integer(*i),
FactValue::Float(f) => Value::Number(*f),
FactValue::Boolean(b) => Value::Boolean(*b),
FactValue::Array(arr) => {
Value::Array(arr.iter().map(Self::fact_value_to_value).collect())
}
FactValue::Null => Value::Null,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_convert_simple_rule() {
let grl = r#"
rule "TestRule" salience 10 no-loop {
when
Person.age > 18
then
Person.is_adult = true;
}
"#;
let rules = GRLParser::parse_rules(grl).unwrap();
assert_eq!(rules.len(), 1);
let rete_rule = GrlReteLoader::convert_rule_to_rete(rules[0].clone()).unwrap();
assert_eq!(rete_rule.name, "TestRule");
assert_eq!(rete_rule.priority, 10);
assert!(rete_rule.no_loop);
}
#[test]
fn test_extract_dependencies() {
let grl = r#"
rule "MultiTypeRule" {
when
Person.age > 18 && Order.amount > 1000
then
Person.premium = true;
}
"#;
let rules = GRLParser::parse_rules(grl).unwrap();
let rete_rule = GrlReteLoader::convert_rule_to_rete(rules[0].clone()).unwrap();
let deps = GrlReteLoader::extract_dependencies(&rete_rule);
assert_eq!(deps.len(), 2);
assert!(deps.contains(&"Person".to_string()));
assert!(deps.contains(&"Order".to_string()));
}
#[test]
fn test_load_from_string() {
let grl = r#"
rule "Rule1" {
when
Person.age > 18
then
Person.is_adult = true;
}
rule "Rule2" {
when
Order.amount > 1000
then
Order.high_value = true;
}
"#;
let mut engine = IncrementalEngine::new();
let count = GrlReteLoader::load_from_string(grl, &mut engine).unwrap();
assert_eq!(count, 2);
}
}