1use crate::engine::rule::{Condition, ConditionGroup, Rule};
2use crate::errors::{Result, RuleEngineError};
3use crate::types::{ActionType, Operator, Value};
4use regex::Regex;
5use std::collections::HashMap;
6
7pub struct GRLParser;
10
11impl GRLParser {
12 pub fn parse_rule(grl_text: &str) -> Result<Rule> {
25 let mut parser = GRLParser;
26 parser.parse_single_rule(grl_text)
27 }
28
29 pub fn parse_rules(grl_text: &str) -> Result<Vec<Rule>> {
31 let mut parser = GRLParser;
32 parser.parse_multiple_rules(grl_text)
33 }
34
35 fn parse_single_rule(&mut self, grl_text: &str) -> Result<Rule> {
36 let cleaned = self.clean_text(grl_text);
37
38 let rule_regex =
40 Regex::new(r#"rule\s+(?:"([^"]+)"|([a-zA-Z_]\w*))\s*(?:salience\s+(\d+))?\s*\{(.+)\}"#)
41 .map_err(|e| RuleEngineError::ParseError {
42 message: format!("Invalid rule regex: {}", e),
43 })?;
44
45 let captures =
46 rule_regex
47 .captures(&cleaned)
48 .ok_or_else(|| RuleEngineError::ParseError {
49 message: format!("Invalid GRL rule format. Input: {}", cleaned),
50 })?;
51
52 let rule_name = if let Some(quoted_name) = captures.get(1) {
54 quoted_name.as_str().to_string()
55 } else if let Some(unquoted_name) = captures.get(2) {
56 unquoted_name.as_str().to_string()
57 } else {
58 return Err(RuleEngineError::ParseError {
59 message: "Could not extract rule name".to_string(),
60 });
61 };
62 let salience = captures
64 .get(3)
65 .and_then(|m| m.as_str().parse::<i32>().ok())
66 .unwrap_or(0);
67
68 let rule_body = captures.get(4).unwrap().as_str();
69
70 let when_then_regex =
72 Regex::new(r"when\s+(.+?)\s+then\s+(.+)").map_err(|e| RuleEngineError::ParseError {
73 message: format!("Invalid when-then regex: {}", e),
74 })?;
75
76 let when_then_captures =
77 when_then_regex
78 .captures(rule_body)
79 .ok_or_else(|| RuleEngineError::ParseError {
80 message: "Missing when or then clause".to_string(),
81 })?;
82
83 let when_clause = when_then_captures.get(1).unwrap().as_str().trim();
84 let then_clause = when_then_captures.get(2).unwrap().as_str().trim();
85
86 let conditions = self.parse_when_clause(when_clause)?;
88
89 let actions = self.parse_then_clause(then_clause)?;
91
92 let mut rule = Rule::new(rule_name, conditions, actions);
94 rule = rule.with_priority(salience);
95
96 Ok(rule)
97 }
98
99 fn parse_multiple_rules(&mut self, grl_text: &str) -> Result<Vec<Rule>> {
100 let rule_regex = Regex::new(r#"(?s)rule\s+(?:"[^"]+"|[a-zA-Z_]\w*).*?\}"#).map_err(|e| {
103 RuleEngineError::ParseError {
104 message: format!("Rule splitting regex error: {}", e),
105 }
106 })?;
107
108 let mut rules = Vec::new();
109
110 for rule_match in rule_regex.find_iter(grl_text) {
111 let rule_text = rule_match.as_str();
112 let rule = self.parse_single_rule(rule_text)?;
113 rules.push(rule);
114 }
115
116 Ok(rules)
117 }
118
119 fn clean_text(&self, text: &str) -> String {
120 text.lines()
121 .map(|line| line.trim())
122 .filter(|line| !line.is_empty() && !line.starts_with("//"))
123 .collect::<Vec<_>>()
124 .join(" ")
125 }
126
127 fn parse_when_clause(&self, when_clause: &str) -> Result<ConditionGroup> {
128 let trimmed = when_clause.trim();
130
131 let clause = if trimmed.starts_with('(') && trimmed.ends_with(')') {
133 let inner = &trimmed[1..trimmed.len()-1];
135 if self.is_balanced_parentheses(inner) {
136 inner
137 } else {
138 trimmed
139 }
140 } else {
141 trimmed
142 };
143
144 if let Some(parts) = self.split_logical_operator(clause, "||") {
146 return self.parse_or_parts(parts);
147 }
148
149 if let Some(parts) = self.split_logical_operator(clause, "&&") {
151 return self.parse_and_parts(parts);
152 }
153
154 if clause.trim_start().starts_with("!") {
156 return self.parse_not_condition(clause);
157 }
158
159 self.parse_single_condition(clause)
161 }
162
163 fn is_balanced_parentheses(&self, text: &str) -> bool {
164 let mut count = 0;
165 for ch in text.chars() {
166 match ch {
167 '(' => count += 1,
168 ')' => {
169 count -= 1;
170 if count < 0 {
171 return false;
172 }
173 }
174 _ => {}
175 }
176 }
177 count == 0
178 }
179
180 fn split_logical_operator(&self, clause: &str, operator: &str) -> Option<Vec<String>> {
181 let mut parts = Vec::new();
182 let mut current_part = String::new();
183 let mut paren_count = 0;
184 let mut chars = clause.chars().peekable();
185
186 while let Some(ch) = chars.next() {
187 match ch {
188 '(' => {
189 paren_count += 1;
190 current_part.push(ch);
191 }
192 ')' => {
193 paren_count -= 1;
194 current_part.push(ch);
195 }
196 '&' if operator == "&&" && paren_count == 0 => {
197 if chars.peek() == Some(&'&') {
198 chars.next(); parts.push(current_part.trim().to_string());
200 current_part.clear();
201 } else {
202 current_part.push(ch);
203 }
204 }
205 '|' if operator == "||" && paren_count == 0 => {
206 if chars.peek() == Some(&'|') {
207 chars.next(); parts.push(current_part.trim().to_string());
209 current_part.clear();
210 } else {
211 current_part.push(ch);
212 }
213 }
214 _ => {
215 current_part.push(ch);
216 }
217 }
218 }
219
220 if !current_part.trim().is_empty() {
221 parts.push(current_part.trim().to_string());
222 }
223
224 if parts.len() > 1 {
225 Some(parts)
226 } else {
227 None
228 }
229 }
230
231 fn parse_or_parts(&self, parts: Vec<String>) -> Result<ConditionGroup> {
232 let mut conditions = Vec::new();
233 for part in parts {
234 let condition = self.parse_when_clause(&part)?;
235 conditions.push(condition);
236 }
237
238 if conditions.is_empty() {
239 return Err(RuleEngineError::ParseError {
240 message: "No conditions found in OR".to_string(),
241 });
242 }
243
244 let mut iter = conditions.into_iter();
245 let mut result = iter.next().unwrap();
246 for condition in iter {
247 result = ConditionGroup::or(result, condition);
248 }
249
250 Ok(result)
251 }
252
253 fn parse_and_parts(&self, parts: Vec<String>) -> Result<ConditionGroup> {
254 let mut conditions = Vec::new();
255 for part in parts {
256 let condition = self.parse_when_clause(&part)?;
257 conditions.push(condition);
258 }
259
260 if conditions.is_empty() {
261 return Err(RuleEngineError::ParseError {
262 message: "No conditions found in AND".to_string(),
263 });
264 }
265
266 let mut iter = conditions.into_iter();
267 let mut result = iter.next().unwrap();
268 for condition in iter {
269 result = ConditionGroup::and(result, condition);
270 }
271
272 Ok(result)
273 }
274
275 fn parse_not_condition(&self, clause: &str) -> Result<ConditionGroup> {
276 let inner_clause = clause.strip_prefix("!").unwrap().trim();
277 let inner_condition = self.parse_when_clause(inner_clause)?;
278 Ok(ConditionGroup::not(inner_condition))
279 }
280
281 fn parse_single_condition(&self, clause: &str) -> Result<ConditionGroup> {
282 let trimmed_clause = clause.trim();
284 let clause_to_parse = if trimmed_clause.starts_with('(') && trimmed_clause.ends_with(')') {
285 &trimmed_clause[1..trimmed_clause.len()-1].trim()
286 } else {
287 trimmed_clause
288 };
289
290 let typed_object_regex =
292 Regex::new(r#"\$(\w+)\s*:\s*(\w+)\s*\(\s*(.+?)\s*\)"#).map_err(|e| {
293 RuleEngineError::ParseError {
294 message: format!("Typed object regex error: {}", e),
295 }
296 })?;
297
298 if let Some(captures) = typed_object_regex.captures(clause_to_parse) {
299 let _object_name = captures.get(1).unwrap().as_str();
300 let _object_type = captures.get(2).unwrap().as_str();
301 let conditions_str = captures.get(3).unwrap().as_str();
302
303 return self.parse_conditions_within_object(conditions_str);
305 }
306
307 let condition_regex = Regex::new(
310 r#"([a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)*)\s*(>=|<=|==|!=|>|<|contains|matches)\s*(.+)"#,
311 )
312 .map_err(|e| RuleEngineError::ParseError {
313 message: format!("Condition regex error: {}", e),
314 })?;
315
316 let captures =
317 condition_regex
318 .captures(clause_to_parse)
319 .ok_or_else(|| RuleEngineError::ParseError {
320 message: format!("Invalid condition format: {}", clause_to_parse),
321 })?;
322
323 let field = captures.get(1).unwrap().as_str().to_string();
324 let operator_str = captures.get(2).unwrap().as_str();
325 let value_str = captures.get(3).unwrap().as_str().trim();
326
327 let operator =
328 Operator::from_str(operator_str).ok_or_else(|| RuleEngineError::InvalidOperator {
329 operator: operator_str.to_string(),
330 })?;
331
332 let value = self.parse_value(value_str)?;
333
334 let condition = Condition::new(field, operator, value);
335 Ok(ConditionGroup::single(condition))
336 }
337
338 fn parse_conditions_within_object(&self, conditions_str: &str) -> Result<ConditionGroup> {
339 let parts: Vec<&str> = conditions_str.split("&&").collect();
341
342 let mut conditions = Vec::new();
343 for part in parts {
344 let trimmed = part.trim();
345 let condition = self.parse_simple_condition(trimmed)?;
346 conditions.push(condition);
347 }
348
349 if conditions.is_empty() {
351 return Err(RuleEngineError::ParseError {
352 message: "No conditions found".to_string(),
353 });
354 }
355
356 let mut iter = conditions.into_iter();
357 let mut result = iter.next().unwrap();
358 for condition in iter {
359 result = ConditionGroup::and(result, condition);
360 }
361
362 Ok(result)
363 }
364
365 fn parse_simple_condition(&self, clause: &str) -> Result<ConditionGroup> {
366 let condition_regex = Regex::new(r#"(\w+)\s*(>=|<=|==|!=|>|<)\s*(.+)"#).map_err(|e| {
368 RuleEngineError::ParseError {
369 message: format!("Simple condition regex error: {}", e),
370 }
371 })?;
372
373 let captures =
374 condition_regex
375 .captures(clause)
376 .ok_or_else(|| RuleEngineError::ParseError {
377 message: format!("Invalid simple condition format: {}", clause),
378 })?;
379
380 let field = captures.get(1).unwrap().as_str().to_string();
381 let operator_str = captures.get(2).unwrap().as_str();
382 let value_str = captures.get(3).unwrap().as_str().trim();
383
384 let operator =
385 Operator::from_str(operator_str).ok_or_else(|| RuleEngineError::InvalidOperator {
386 operator: operator_str.to_string(),
387 })?;
388
389 let value = self.parse_value(value_str)?;
390
391 let condition = Condition::new(field, operator, value);
392 Ok(ConditionGroup::single(condition))
393 }
394
395 fn parse_value(&self, value_str: &str) -> Result<Value> {
396 let trimmed = value_str.trim();
397
398 if (trimmed.starts_with('"') && trimmed.ends_with('"'))
400 || (trimmed.starts_with('\'') && trimmed.ends_with('\''))
401 {
402 let unquoted = &trimmed[1..trimmed.len() - 1];
403 return Ok(Value::String(unquoted.to_string()));
404 }
405
406 if trimmed.eq_ignore_ascii_case("true") {
408 return Ok(Value::Boolean(true));
409 }
410 if trimmed.eq_ignore_ascii_case("false") {
411 return Ok(Value::Boolean(false));
412 }
413
414 if trimmed.eq_ignore_ascii_case("null") {
416 return Ok(Value::Null);
417 }
418
419 if let Ok(int_val) = trimmed.parse::<i64>() {
421 return Ok(Value::Integer(int_val));
422 }
423
424 if let Ok(float_val) = trimmed.parse::<f64>() {
425 return Ok(Value::Number(float_val));
426 }
427
428 if trimmed.contains('.') {
430 return Ok(Value::String(trimmed.to_string()));
431 }
432
433 Ok(Value::String(trimmed.to_string()))
435 }
436
437 fn parse_then_clause(&self, then_clause: &str) -> Result<Vec<ActionType>> {
438 let statements: Vec<&str> = then_clause
439 .split(';')
440 .map(|s| s.trim())
441 .filter(|s| !s.is_empty())
442 .collect();
443
444 let mut actions = Vec::new();
445
446 for statement in statements {
447 let action = self.parse_action_statement(statement)?;
448 actions.push(action);
449 }
450
451 Ok(actions)
452 }
453
454 fn parse_action_statement(&self, statement: &str) -> Result<ActionType> {
455 let trimmed = statement.trim();
456
457 let method_regex = Regex::new(r#"\$(\w+)\.(\w+)\s*\(([^)]*)\)"#).map_err(|e| {
459 RuleEngineError::ParseError {
460 message: format!("Method regex error: {}", e),
461 }
462 })?;
463
464 if let Some(captures) = method_regex.captures(trimmed) {
465 let object = captures.get(1).unwrap().as_str().to_string();
466 let method = captures.get(2).unwrap().as_str().to_string();
467 let args_str = captures.get(3).unwrap().as_str();
468
469 let args = if args_str.trim().is_empty() {
470 Vec::new()
471 } else {
472 self.parse_method_args(args_str)?
473 };
474
475 return Ok(ActionType::MethodCall {
476 object,
477 method,
478 args,
479 });
480 }
481
482 if let Some(eq_pos) = trimmed.find('=') {
484 let field = trimmed[..eq_pos].trim().to_string();
485 let value_str = trimmed[eq_pos + 1..].trim();
486 let value = self.parse_value(value_str)?;
487
488 return Ok(ActionType::Set { field, value });
489 }
490
491 let func_regex =
493 Regex::new(r#"(\w+)\s*\(\s*(.+?)?\s*\)"#).map_err(|e| RuleEngineError::ParseError {
494 message: format!("Function regex error: {}", e),
495 })?;
496
497 if let Some(captures) = func_regex.captures(trimmed) {
498 let function_name = captures.get(1).unwrap().as_str();
499 let args_str = captures.get(2).map(|m| m.as_str()).unwrap_or("");
500
501 match function_name.to_lowercase().as_str() {
502 "update" => {
503 let object_name = if let Some(stripped) = args_str.strip_prefix('$') {
505 stripped.to_string()
506 } else {
507 args_str.to_string()
508 };
509 Ok(ActionType::Update {
510 object: object_name,
511 })
512 }
513 "set" => {
514 let args = if args_str.is_empty() {
516 Vec::new()
517 } else {
518 args_str
519 .split(',')
520 .map(|arg| self.parse_value(arg.trim()))
521 .collect::<Result<Vec<_>>>()?
522 };
523
524 if args.len() >= 2 {
525 let field = args[0].to_string();
526 let value = args[1].clone();
527 Ok(ActionType::Set { field, value })
528 } else if args.len() == 1 {
529 Ok(ActionType::Set {
531 field: args[0].to_string(),
532 value: Value::Boolean(true)
533 })
534 } else {
535 Ok(ActionType::Custom {
536 action_type: "set".to_string(),
537 params: {
538 let mut params = HashMap::new();
539 params.insert("args".to_string(), Value::String(args_str.to_string()));
540 params
541 },
542 })
543 }
544 }
545 "add" => {
546 let value = if args_str.is_empty() {
548 Value::Integer(1) } else {
550 self.parse_value(args_str.trim())?
551 };
552 Ok(ActionType::Custom {
553 action_type: "add".to_string(),
554 params: {
555 let mut params = HashMap::new();
556 params.insert("value".to_string(), value);
557 params
558 },
559 })
560 }
561 "log" => {
562 let message = if args_str.is_empty() {
563 "Log message".to_string()
564 } else {
565 let value = self.parse_value(args_str.trim())?;
566 value.to_string()
567 };
568 Ok(ActionType::Log { message })
569 }
570 _ => {
571 let args = if args_str.is_empty() {
572 Vec::new()
573 } else {
574 args_str
575 .split(',')
576 .map(|arg| self.parse_value(arg.trim()))
577 .collect::<Result<Vec<_>>>()?
578 };
579 Ok(ActionType::Call {
580 function: function_name.to_string(),
581 args,
582 })
583 }
584 }
585 } else {
586 Ok(ActionType::Custom {
588 action_type: "statement".to_string(),
589 params: {
590 let mut params = HashMap::new();
591 params.insert("statement".to_string(), Value::String(trimmed.to_string()));
592 params
593 },
594 })
595 }
596 }
597
598 fn parse_method_args(&self, args_str: &str) -> Result<Vec<Value>> {
599 if args_str.trim().is_empty() {
600 return Ok(Vec::new());
601 }
602
603 let mut args = Vec::new();
605 let parts: Vec<&str> = args_str.split(',').collect();
606
607 for part in parts {
608 let trimmed = part.trim();
609
610 if trimmed.contains('+')
612 || trimmed.contains('-')
613 || trimmed.contains('*')
614 || trimmed.contains('/')
615 {
616 args.push(Value::String(trimmed.to_string()));
618 } else {
619 args.push(self.parse_value(trimmed)?);
620 }
621 }
622
623 Ok(args)
624 }
625}
626
627#[cfg(test)]
628mod tests {
629 use super::GRLParser;
630
631 #[test]
632 fn test_parse_simple_rule() {
633 let grl = r#"
634 rule "CheckAge" salience 10 {
635 when
636 User.Age >= 18
637 then
638 log("User is adult");
639 }
640 "#;
641
642 let rules = GRLParser::parse_rules(grl).unwrap();
643 assert_eq!(rules.len(), 1);
644 let rule = &rules[0];
645 assert_eq!(rule.name, "CheckAge");
646 assert_eq!(rule.salience, 10);
647 assert_eq!(rule.actions.len(), 1);
648 }
649
650 #[test]
651 fn test_parse_complex_condition() {
652 let grl = r#"
653 rule "ComplexRule" {
654 when
655 User.Age >= 18 && User.Country == "US"
656 then
657 User.Qualified = true;
658 }
659 "#;
660
661 let rules = GRLParser::parse_rules(grl).unwrap();
662 assert_eq!(rules.len(), 1);
663 let rule = &rules[0];
664 assert_eq!(rule.name, "ComplexRule");
665 }
666
667 #[test]
668 fn test_parse_new_syntax_with_parentheses() {
669 let grl = r#"
670 rule "Default Rule" salience 10 {
671 when
672 (user.age >= 18)
673 then
674 set(user.status, "approved");
675 }
676 "#;
677
678 let rules = GRLParser::parse_rules(grl).unwrap();
679 assert_eq!(rules.len(), 1);
680 let rule = &rules[0];
681 assert_eq!(rule.name, "Default Rule");
682 assert_eq!(rule.salience, 10);
683 assert_eq!(rule.actions.len(), 1);
684
685 match &rule.actions[0] {
687 crate::types::ActionType::Set { field, value } => {
688 assert_eq!(field, "user.status");
689 assert_eq!(value, &crate::types::Value::String("approved".to_string()));
690 }
691 _ => panic!("Expected Set action, got: {:?}", rule.actions[0]),
692 }
693 }
694
695 #[test]
696 fn test_parse_complex_nested_conditions() {
697 let grl = r#"
698 rule "Complex Business Rule" salience 10 {
699 when
700 (((user.vipStatus == true) && (order.amount > 500)) || ((date.isHoliday == true) && (order.hasCoupon == true)))
701 then
702 apply_discount(20000);
703 }
704 "#;
705
706 let rules = GRLParser::parse_rules(grl).unwrap();
707 assert_eq!(rules.len(), 1);
708 let rule = &rules[0];
709 assert_eq!(rule.name, "Complex Business Rule");
710 assert_eq!(rule.salience, 10);
711 assert_eq!(rule.actions.len(), 1);
712
713 match &rule.actions[0] {
715 crate::types::ActionType::Call { function, args } => {
716 assert_eq!(function, "apply_discount");
717 assert_eq!(args.len(), 1);
718 assert_eq!(args[0], crate::types::Value::Integer(20000));
719 }
720 _ => panic!("Expected Call action, got: {:?}", rule.actions[0]),
721 }
722 }
723}