[rules-users] Matching strings in two arrays

Swindells, Thomas TSwindells at nds.com
Wed Sep 29 11:17:24 EDT 2010


> Thomas, Thanks for the suggestions.
>
> Swindells, Thomas wrote:
> >
> > Also have you tried just using ==, eg exists( String(this ==
> > "TestString1") ||  String(this == "TestString2") || String(this ==
> > "TestString3") from DclassesList)
> >
>
> This did reduce the time by about 50 ms.
That's a step in the right direction at least.

> Swindells, Thomas wrote:
> >
> >
> > You may also want to consider whether it would be beneficial to
> > restructure your fact class to get rid of the from, and match against
> > ClassName facts instead - this may give you a big performance gain as it
> > means each rule wouldn't have to iterate over the list, if you are
> > updating MyContext then this gain is likely to be even bigger.
> >
>
> Not quite sure about what you mean by "match against ClassName facts instead
> ".  My fact class will have a bunch of instance variables one of which is
> the array of strings.
> Would be great if you could elaborate that a bit.

Have two fact classes,
One is your current fact class (MyContext), the other is to represent each of your classes entry.
Eg:
Class ClassName {
MyContext parent;
String name;
...
}

When inserting you MyContext into the working memory you would also add a ClassName fact for each entry of the classes list (you could either do this in code or have a rule which does it for you).

Your main rules would then look something like the following:


Rule XYZ
$parent : MyContext
exists ( ClassName(parent == $parant, name == "TestString1") or ClassName(parent == $parant, name == "TestString2") or ClassName(parent == $parant, name == "TestString3"))
then
...

If you only ever have one MyContext in working memory you can skip the parent check.


You may also be able to use in to simplify things
exists ( ClassName(parent == $parant, name in {"TestString1", "TestString2", "TestString3"))


Hope that makes sense,

Thomas



**************************************************************************************
This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the postmaster at nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes. To protect the environment please do not print this e-mail unless necessary.

NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603 8808 40-00
**************************************************************************************




More information about the rules-users mailing list