python-rule-engine icon indicating copy to clipboard operation
python-rule-engine copied to clipboard

Work backwards from results to matching input object

Open matthewdfleming opened this issue 1 year ago • 1 comments

How would you recommend fetching the parts of the evaluated object that triggered a match? I want to be able to iterate over the results of a match and then locate the spot in the object graph that caused the match to be true. This is especially relevant in the one to many case. In the results that come out of the engine, how can I find which child caused the result to be 'true'?

Let's say you have a Parent that has many children and the rule matches when any of the children are named Polo. I'd like to be able to sort out which 'children' matched the rule. In the below case, children id 2 and 3 should be matched.

Rule

{
  "name": "marco_polo",
  "description": "find kids named Polo",
  "conditions": {
    "any": [
        {
          "operator": "equal",
          "value": "Polo",
          "path": "$.Parent.Children[*].Name"
        },
     ]
  }
}

Data

{
  "Parent": {
     "Children": [
        {"Id": 1, "Name": "Marco"},
        {"Id": 2, "Name": "Polo"}
        {"Id": 3, "Name": "Polo"}
      ]   
  }
}

matthewdfleming avatar Nov 19 '24 23:11 matthewdfleming

Hi, sorry about the delay. Let me take a look at this and I'll respond as best I can.

santalvarez avatar Dec 04 '24 12:12 santalvarez