[rules-users] Accessing maps (hashmaps) keys and values in rules
shaz
shauii at yahoo.com
Tue Nov 6 21:48:32 EST 2007
Hi,
I'll try to explain my case...
I have an Object which has around 90 fields. For each of these fields, there can be unlimited validations that needs to be made. By unlimited, I mean that the user can create rules for each specific field. So not all rules are applicable to all fields. The objects I'm placing on the Map are the field structure of the 90 fields. These are things like the maxlength, mandatory/optional, alphanumeric/numeric of each field.
Here is a sample code wherein I just used the Map in the then clause...
==================
global java.util.HashMap recordTypeMap;
global java.util.TreeMap structMap;
global java.util.ArrayList structList;
function CDRError setCDRError(RaCdrMoc cdr, CDRError cdrError, TreeMap structMap,
String fieldName, String errorType, String editedValue, boolean autoCorrected) {
cdrError = new CDRError();
cdrError.setCdrUploadId(cdr.getId().getCdruploadId());
cdrError.setCdrNo(cdr.getId().getCdrNo());
cdrError.setCdrType(0);
RaCdrStructure cdrStructure = new RaCdrStructure();
cdrStructure = (RaCdrStructure) structMap.get(fieldName);
cdrError.setFieldNo(cdrStructure.getId().getFieldNo());
cdrError.setErrorType(errorType);
cdrError.setEditedFieldValue(editedValue);
cdrError.setAutoCorrected(autoCorrected);
return cdrError;
}
rule "MOC Check Missing Filename"
when
cdr : RaCdrMoc(mocflnm == ""); //this is the Object with 90 fields
cdrError : CDRError(); //the resulting object that will be added to the list
errorList : LinkedList(); //all errors will be placed here
cdrStructure : RaCdrStructure();
then
System.out.println("========No filename================");
cdr.setMocflnm("Testfile.csv");
cdrError = setCDRError(cdr, cdrError, structMap, "MOCFLNM", "01", "Testfile.csv", true);
errorList.addLast(cdrError);
end
=========================
My other rules would require the accessing of Hashmaps from the when clause. IWould there be a need to change my data model
Thanks a lot,
shaz
----- Original Message ----
From: "Anstis, Michael (M.)" <manstis1 at ford.com>
To: Rules Users List <rules-users at lists.jboss.org>
Sent: Friday, November 2, 2007 5:20:38 PM
Subject: RE: [rules-users] Accessing maps (hashmaps) keys and values in rules
DIV {
MARGIN:0px;}
IMHO you should still think about putting the Map values
into working memory.
This would both simplify your rules and make their
execution faster (eval cannot be indexed, if I remember
correctly).
You might need to adapt your data model representation into
one that is more efficient for use in a RETE network.
Can you explain your use case to help understand? Unless
you have very good reason to keep the facts in the Map your life will be more
difficult than it need be.
An "eval" returns a boolean indiciating whether the
"pattern" matched and therefore, whilst you can reference other bound LHS facts,
you cannot bind other objects to variables.
Thanks,
Mike
From: rules-users-bounces at lists.jboss.org
[mailto:rules-users-bounces at lists.jboss.org] On Behalf Of
shaz
Sent: 02 November 2007 02:32
To: Rules Users
List
Subject: Re: [rules-users] Accessing maps (hashmaps) keys and
values in rules
Hi,
I am placing objects in the Hashmap that were retrieved from the
database. There are currently a hundred objects that I'm storing in the
HashMap. I just made a simple example earlier :) . What works for me now is
putting the getting of the values in the eval(). However, I don't know how to
assign the values that I've taken from the Map.
eval( (
(MyClass)mapOfObj.get("keyOfObj")).getId() > 30);
How do I assign
(MyClass)mapOfObj.get("keyOfObj") to a
variable?
tnx,
shaz
-----
Original Message ----
From: "Anstis, Michael (M.)"
<manstis1 at ford.com>
To: Rules Users List
<rules-users at lists.jboss.org>
Sent: Friday, November 2, 2007 12:17:51
AM
Subject: RE: [rules-users] Accessing maps (hashmaps) keys and values in
rules
By way of example (if you misunderstand how the rule
engine should be used):-
rule
chkobj
when
$o :
MyClass( id == "1", name == "name"
)
then
//Do something
else
$o.doSomething();
end
From: rules-users-bounces at lists.jboss.org
[mailto:rules-users-bounces at lists.jboss.org] On Behalf Of Edson
Tirelli
Sent: 01 November 2007 15:48
To: Rules Users
List
Subject: Re: [rules-users] Accessing maps (hashmaps) keys and
values in rules
Shaz,
We strongly advise you
to move to 4.0.3, but this is not related to your problem.
I think you are misunderstanding something. Why are you using the global map
to store your fact? Simply insert it into the working memory and it will
work fine.
[]s
Edson
2007/11/1, Shaz Tumulak <shauii at yahoo.com>:
Hi,
I
can't seem to make this syntax work in Drools 4.0.2. Pls. see my
sample
code below.
________________________________________
class
MyClass {
String
id;
String
name;
MyClass(String
id, String
name){
this.id =
id;
this.name =
name;
}
:
getter,
setter methods
:
}
MyClass
obj = new MyClass("1"", "name");
TreeMap<String, MyClass>
mapOfObj = new TreeMap<String,
MyClass>();
mapOfObj.put("keyOfObj", obj);
________________________________________
DRL
file
-------
global java.util.TreeMap mapOfObj;
rule
chkobj
when
obj :
MyClass(mapOfObj["keyOfObj"].id ==
"1");
eval(obj.getName()
== "name");
________________________________________
I'm getting
this error:
org.drools.RuntimeDroolsException:
java.lang.NullPointerException
at
org.drools.rule.EvalCondition.isAllowed(EvalCondition.java:76)
Is
my syntax correct and has someone used this in 4.0.2 or should I move
on
to Drools 4.0.3? Thanks in
advance.
Regards,
shaz
Edson Tirelli-3
wrote:
>
> Denis,
>
> If
you are using latest build from trunk (I'm not sure it already
works
> in MR3), you can use a simplified MVEL
syntax:
>
> when
> Person(
address["business"].phone == "99999999" )
>
then
> ...
>
end
>
> Although, the above will be
converted into an inline-eval. Not as bad
> as
> a top level
eval, but still more costly then using regular
fields.
>
> []s
> Edson
>
>
2007/7/19, Ryan, Dennis (Dennis) <dennisryan at avaya.com
>:
>>
>> We are using Maps (HashMaps)
in our rules and the only way we have found
>> to access keys and
values in the Map in the "when" clauses is thru use on
>> eval()
which I know is a big no-no by rules purists. Is there a better
>> way,
>> maybe some shorthand I'm not familiar with
to deal with Maps in the when
>>
clause?
>>
>>
>>
>>
Thanks,
>>
>> Dennis
>>
>>
_______________________________________________
>> rules-users
mailing list
>> rules-users at lists.jboss.org
>>
https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>
>
>
--
> Edson Tirelli
> Software Engineer
- JBoss Rules Core Developer
> Office: +55 11 3529-6000
> Mobile: +55 11 9287-5646
> JBoss, a
division of Red Hat @ www.jboss.com
>
>
_______________________________________________
> rules-users
mailing list
> rules-users at lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/rules-users
>
>
shauii at yahoo.com
--
View this
message in context: http://www.nabble.com/Accessing-maps-%28hashmaps%29-keys-and-values-in-rules-tf4112197.html#a13524477
Sent from the drools - user mailing list archive at Nabble.com.
_______________________________________________
rules-users
mailing list
rules-users at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
--
Edson Tirelli
Software
Engineer - JBoss Rules Core Developer
Office: +55 11
3529-6000
Mobile: +55 11 9287-5646
JBoss, a
division of Red Hat @ www.jboss.com
Platonic friends of the opposite sex - the real
deal?
Be a better buddy
Find
out at Yahoo! Answers
____________________________________________________
Organic foods - just how healthy is health food?
Be a better foodie
Find out at Yahoo! Answers - http://ph.answers.yahoo.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20071106/2da9cfd6/attachment.html
More information about the rules-users
mailing list