Fork me on GitHub
#clara
<
2023-01-24
>
Adrian Smith11:01:15

Is there a programmatic way of determining why a rule didn't fire?

ethanc23:01:10

in theory, yes. Though it'd likely require knowledge of the rules network structure and a custom listener: https://github.com/cerner/clara-rules/blob/e86dcd4f0224d966c1bd82043e79cf59c96136bf/src/main/clojure/clara/rules/listener.cljc#L5-L27 you have to ascertain the ancestors of a given ProductionNode, from the rulebase retrieved via a components call on the session: https://github.com/cerner/clara-rules/blob/e86dcd4f0224d966c1bd82043e79cf59c96136bf/src/main/clojure/clara/rules/engine.cljc#L2029-L2034 you'd then have to watch for events on the listener for the nodes in question, and the specific elements/token/facts "passing through the nodes". The last node in the subset of nodes for the production, which saw the item in question, could be assumed to be the the reason that it didn't propagate to the ProductionNode(RHS). I use "in theory" because i've never tried doing it... it seems possible given knowledge of the internals, but whether or not its practical is another question...

👍 2
wparker23:01:31

Not exactly a full answer to that problem, but you might the :condition-matches from session inspection helpful: https://www.clara-rules.org/apidocs/0.21.1/clojure/clara.tools.inspect.html#var-inspect That won’t be able to tell you about whether joins match, but might help some for tests against a single fact only (and that Clara recognizes as such, see http://www.clara-rules.org/docs/hash_joins/ for some discussion of that distinction - that page is in the context of performance but the info applies here. Unfortunately I don’t think that aspect of session inspection is quite as polished an experience as the “what rule inserted this fact” and “what facts did this rule insert” aspects but could be still be useful. If you have a particular set of facts that you expect to trigger a rule and don’t, you could try creating a rules session with just that rule and then removing conditions etc. until it does match - keep in mind that while some examples show bringing in an entire namespace of rules you don’t have to do this. Example usage: https://github.com/cerner/clara-examples/blob/343c54e8c2e7522a168b756bbe1e722937a85c09/src/main/clojure/clara/examples/fact_type_options.clj#L20

wparker23:01:26

Is your interest in debugging/tools for debugging in your domain or something else?

Adrian Smith10:01:56

Yeah my use case is around debugging after the session is settled, specifically for domain related reasons We expected a rule to fire and we'd like to know why it didn't

pdmct02:01:21

@UCJCPTW8J I had same issue, and the inspect api is very helpful. Combine it with portal (djblue) to step through the rules and it makes it much easier to what is going on. see this thread from a couple of weeks ago and the similarly useful advice from @U0KRSVDHR https://clojurians.slack.com/archives/C08TC9JCS/p1673960569948969