Skip to content
Advertisement

Checking flag value before inserting flag in drools

I’ve produced a rule and it inserts a question in session. If the question is true, it inserts a FLAG and if the question is not true, it deletes the question and doesn’t update the flag. I need to check the value of the flag before inserting the question in the session. I have tried several ways to do this but not getting the drools thing to do this. Here are my rules:
Inserting question rule

JavaScript

question is true

JavaScript

question is false

JavaScript

Advertisement

Answer

You need to check for the presence of your specific flag in working memory. The commented out syntax in your rule is nearly correct, except you seem to have an extraneous $ present.

Since you’ve not shared what FLAGS is, it’s a little hard to answer your question specifically. I’m going to assume, based on how you’ve formulated your insert statement, that it’s an enum like this:

JavaScript

So if you want to verify that a FLAGS.PUBLIC_READABLE has been inserted into working memory, your rule would include:

JavaScript

I use exists because you’ve not indicated that you need to do anything with the flag, so I’m just checking for its presence.

Note that insert doesn’t re-execute previously evaluated rules. If you need to reevaluate all of working memory, you should use update instead.


Based on comments, here is how you’d implement a simple “quiz” application. A user presents an answer to a question; if the user answers correctly, they are presented with the next question. If the user answers incorrectly, it is “game over” for them and they are given the correct answer to the question they did incorrectly.

I’m using some very simple models:

JavaScript

The user’s answer is a String entered directly into working memory.

JavaScript
Advertisement