I ran into the java.util.ConcurrentModificationException too when trying to iterate over an ArrayList created via "from collect" in Drools 5.5.0 Final. My solution was to copy the list into an array using List.toArray and then iterate
over that. I found that the type information is often not properly maintained in the right-hand side of the rule, requiring casts where they should be unnecessary. Here is a complete, self-contained example that shows how to iterate over the array to modify
or retract the facts.
import java.util.ArrayList;
declare Fact
@propertyReactive
name : String @key
value : String
end
rule
"initialize"
when
then
insert (new
Fact("abc"));
insert (new
Fact("def"));
insert (new
Fact("ghi"));
end
rule
"collect and retract"
salience 1
when
$list : ArrayList(size > 0)
from
collect (Fact(name
in ("abc",
"def")))
then
// This will not compile without the explicit cast.
Fact[] facts = (Fact[]) $list.toArray(new
Fact[0]);
for
(Fact f : facts) {
retract (f);
}
end
rule
"collect and modify"
salience 2
when
$list : ArrayList(size > 0)
from
collect (Fact(name
in ("abc",
"def")))
then
Fact[] facts = (Fact[]) $list.toArray(new
Fact[0]);
for
(Fact f : facts) {
// The modify will not compile without an explicit cast,
// despite the declaration of f in the enhanced for loop.
Fact g = (Fact) f;
modify
(g) {
setValue(g.getName() +
" A");
}
}
// Avoid the cast by using a traditional indexed loop.
for (int
i = 0; i < facts.length; i++) {
// Copy the array element into a new variable
// because "modify (facts[i])" will not compile.
Fact g = facts[i];
modify
(g) {
setValue(g.getName() +
" B");
}
}
end
Best wishes,
Tom
-----Original Message-----
From: rules-users-bounces@lists.jboss.org [mailto:rules-users-bounces@lists.jboss.org] On Behalf Of radhika.inugala
Sent: Tuesday, July 02, 2013 3:20 PM
To: rules-users@lists.jboss.org
Subject: Re: [rules-users] drools migration
Also I am looking at the domain objects for this:
And I need to retract all the objects with locationType == LocationType.ORIGIN the fromState is one of them.
But there would be more of the same.
I think that is the reason the collect is in there.
--
View this message in context:
http://drools.46999.n3.nabble.com/rules-users-drools-migration-tp4024691p4024721.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
_______________________________________________
rules-users mailing list
https://lists.jboss.org/mailman/listinfo/rules-users