You really shouldn't be matching an entire collection of objects like that.
Think of each rule as capturing one "example" of an object in a given state, and
doing something when that object is found. So think of your first rule like this:
rule "Check for null State"
when
obj : MyObj(state == null || state == "")
then
obj.addError("State must have a value");
end
Where all MyObj instances are inserted into working memory.
If you also insert all entries in the state2CodeMap into working memory, that rule becomes
equally simple. Let's say a State2Code object has String state and
ArrayList<String>> codes properties, then the rule would look like this:
rule "Check for valid code"
when
obj : MyObj(objState : state, objCode : code)
codes : State2Code(state == objState, objCode not memberOf codes)
then
obj.addError("Code is not valid for the State");
end
Make sense?
--- On Tue, 1/25/11, mviens <mike(a)viens.com> wrote:
From: mviens <mike(a)viens.com>
Subject: [rules-users] Question about Drools accessing a global HashMap and comparing it
to an object value
To: rules-users(a)lists.jboss.org
Date: Tuesday, January 25, 2011, 3:37 PM
MyObj is a Java object that contains:
String state;
String code;
public void addError(String message);
myObjList is an ArrayList<MyObj> containing from 2 to
20,000 objects
state2CodeMap is a HashMap<String,
ArrayList<String>> containing an entry
for each US state. The associated ArrayList contains a
minimum of 1 entry,
up to 200 entries.
I need to create a rule that checks all of the MyObj
objects in myObjList,
gets the appropriate ArrayList from state2CodeMap based on
myObj.state and
then if myObj.code not memberOf $codeList, run the "then"
portion of the
rule.
I have created about 60 fairly simple rules, that are
working just fine, but
this one is a little too complex for me to userstand how to
write it in
Drools. Looking at this page (
http://blog.athico.com/2007/06/chained-from-accumulate-collect.html
http://blog.athico.com/2007/06/chained-from-accumulate-collect.html
), I can
see that nested "from" seems to be supported, but the page
seems to be for
Drools 4. Plus, I have had to teach myself Drools in the
past week. I could
really use some help with this. Any assistance is greatly
appreciated!
Mike V.
global List myObjList;
global HashMap state2CodeMap;
# this rule is working just fine!
rule "Check for null State"
when
$objs :
ArrayList() from collect(MyObj(state == null || state ==
"") from myObjList)
then
for (MyObj myObj
: (ArrayList<MyObj>)$objs)
{
myObj.addError("State must have a value");
}
end
# I really have no idea how to write this
rule
rule "Check for valid code"
when
$objs :
ArrayList() from collect(MyObj())
then
for (MyObj myObj
: (ArrayList<MyObj>)$objs)
{
myObj.addError("Code is not valid for the State");
}
end
--
View this message in context:
http://drools-java-rules-engine.46999.n3.nabble.com/Question-about-Drools...
Sent from the Drools - User mailing list archive at
Nabble.com.
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users