We are working on a rule that is structured as follows:
The problem arises when we want to update the original ArrayList of elements. We need to be able to update the non-validated element that has just become validated in its original ArrayList in the working memory so the loop can take the "refreshed" ArrayList into account.
Three questions:
First of all, what is the exact difference between using modify and update in Drools. According to Drools documentation:
"On the right hand side of a rule the modifystatement is recommended, as it makes the changes and notifies the engine in a single statement."
So the difference between one and other would be just simple semantics like:
modify( $sprinkler ) { setOn( true ) };
and
$sprinkler.setOn(true);
update($sprinkler);
Is this assumption correct?
Secondly, is it possible to update or modify a specific element within the ArrayList in the working memory in Drools without using a plain Java iterator (a "for"). In our case each element is identified with a unique ID we obtain from the list so we will have a valid reference for updating its status (validated or non-validated).
Finally, we are aware that updating the working memory would cause the rule to fire again. Let's say we have an ArrayList with two non-validated items. If we were to obtain all non-validated items to validate them, I would create a rule "first" that fires twice, for each non-validated item once:
rule "first"
when
$listOfElements : java.util.ArrayList ( )
$itemsToValidate : Element ( status == "not validated" ) from $listOfElements
then
//do something or not
end
If in a second rule I would validate certain items and wish to change the status of the non-validated element to validated in the working memory like this:
rule "second" extends "first"
when
//we validate attributes of the non-validated element against attributes of the validated elements
then
//my second question is if something like this is actually possible (solution without Java iterator)
$itemsToValidate.setStatus("validated")
modify ($listOfElements) { $itemsToValidate };
end
How would this affect the whole process? The actualisation of the working memory will provoke the first rule to be reevaluated. This rule would already be launched twice because we had encountered two non-validated elements. Will the remaining element be evaluated only once or more than once?
The answer to your first question is that there is no difference between the two snippets. (Statements inserted between the two of the second one would influence my answer.)
The scenario for the second question doesn't use an update in the same form. Inserting a List as a fact but operating on list elements is usually considered as bad. Insert the list elements. The container may or may not be useful as a container holding the list elements to one transaction or whatever.