Search code examples
drools

In drools is there a way to detect endless loops and halt a session programmatically?


in short my questions are:

  • Is there anything built-in into drools that allows/facilitates detection of endless loops?
  • Is there a way to programmatically halt sessions (e.g. for the case of a detected endless loop)?

More details:

I'm planning to have drools (6.2 or higher) run within a server/platform where users will create and execute their own rules. One of the issues I'm facing is that carelessly/faulty rule design can easily result in endless loops (whether its just a forgotten "no-loop true" statement or the more complex rule1 triggers rule2 triggers rule3 (re)triggers rule1 circles that lead to endless loops. If this happens, drools basically slows down my server/platform to a halt.

I'm currently looking into how to detect and/or terminate sessions that run in an endless loop. Now as a (seemingly) endless loop is nothing that is per-se invalid or in certain cases maybe even desired I can imagine that there is not a lot of built-in detection mechanism for this case (if any). But as I am not an expert I'd be happy to know if there is anything built-in to detect endless loops? In my use case I would be ok to determine a session as "endlessly looped" based on a threshold of how often any rule might have been activated. As I understand I could use maybe AgendaEventListeners that keep track of how often any rule has been fired and if a threshold is met either insert a control fact or somehow trigger a rule that contains the drools.halt() for this session.

I wonder (and couldn't find a lot of details) if it is possible to programmatically halt/terminate sessions. I've only come across a fireUntilHalt() method but that didn't seem like the way to go (or I didnt understand it really).

Also, at this point I was only planning to use stateless session (but if it's well encapsulated I could also work with stateful sessions if that makes my goal easier to achieve).

Any answers/ideas/feedback to my initial approach is highly welcome :)

Thanks!


Solution

  • A fundamental breaking point of any RBS implementation is created where the design lets "users create and design their own rules". I don't know why some marketing hype opens the door for non-programmers to write what is program code, without any safeguarding.

    Detecting whether a session halts is theoretically impossible. Google "Halting problem".

    For certain contexts you might come up with a limit of the number of rules that might be executed at most or something similar. And you can use listeners to count and raise an exception, etc etc.

    Basically you have very bad cards once you succumb to the execution of untested code created by amateurs.