function problem

5 messages Options
Embed this post
Permalink
Charlie Holland

function problem

Reply Threaded More More options
Print post
Permalink
I'm fairly new to Drools so there may be an easier way to do this. I need to check to see if any one of a list of tags on an object are in another list of tags and am trying to do that with a function. the compiler tells me it can't compile my eval function:

#list any import classes here.

import com.cp.services.rules.sources.Person;

import com.cp.services.rules.actions.PersonActions;

import com.cp.rules.TagCodes;

import java.util.List;

 

#declare any global variables here

global com.cp.services.rules.actions.PersonActions personActions;

function boolean hasOneOf(List list1, String[] list2){

for (Object obj : list1) {

String string = (String)obj;

for (int j = 0; j < list2.length; j++) {

if (string != null && string.equals(list2[j])) {

return true;

}

}

}

return false;

}

rule "Engineering"

dialect "mvel"

when

#conditions

Person( ageInYears >= 18 ) and

eval ( hasOneOf(Person(tagList), TagCodes(engrTags)) )

then

#actions

personActions.addAction("EngineeringGroup");

end


_______________________________________________
rules-users mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/rules-users
dave sinclair

Re: function problem

Reply Threaded More More options
Print post
Permalink
Charlie,

Can u explain more simply, without the function, what u are trying to do? Is it just to see if there exists a Person, whose tagList property contains a string that is the same as one of the engrTags from a TagCodes?

On Sat, Nov 22, 2008 at 12:25 AM, Charlie Holland <[hidden email]> wrote:
I'm fairly new to Drools so there may be an easier way to do this. I need to check to see if any one of a list of tags on an object are in another list of tags and am trying to do that with a function. the compiler tells me it can't compile my eval function:

#list any import classes here.

import com.cp.services.rules.sources.Person;

import com.cp.services.rules.actions.PersonActions;

import com.cp.rules.TagCodes;

import java.util.List;

 

#declare any global variables here

global com.cp.services.rules.actions.PersonActions personActions;

function boolean hasOneOf(List list1, String[] list2){

for (Object obj : list1) {

String string = (String)obj;

for (int j = 0; j < list2.length; j++) {

if (string != null && string.equals(list2[j])) {

return true;

}

}

}

return false;

}

rule "Engineering"

dialect "mvel"

when

#conditions

Person( ageInYears >= 18 ) and

eval ( hasOneOf(Person(tagList), TagCodes(engrTags)) )

then

#actions

personActions.addAction("EngineeringGroup");

end


_______________________________________________
rules-users mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/rules-users



_______________________________________________
rules-users mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/rules-users
Dan Seaver

Re: function problem

Reply Threaded More More options
Print post
Permalink
In reply to this post by Charlie Holland
Charlie,
I'm not sure why your function doesn't compile as it looks fine to me. Do you have any other details about the message you're getting from the compiler?

In any case, you may want to consider solving the problem a different way. In general, you should try to avoid "eval" statements whenever possible as they can severely hamper performance. Edson wrote up some helpful hints - see "Eval is Evil" at - http://blog.athico.com/2007/03/writing-better-rules.html

As an alternative, try populating working memory with each of the Person's tagList items and then use "memberOf" to determine if it's one of the engineering TagCodes. (You may be able to use the "from" statement with the Person's tagList instead of populating working memory) This allows Drools to do all of the work for you and may very well be faster than using loops.

Let me know if you need further clarification.

Charlie Holland wrote:
I'm fairly new to Drools so there may be an easier way to do this. I need to
check to see if any one of a list of tags on an object are in another list
of tags and am trying to do that with a function. the compiler tells me it
can't compile my eval function:

#list any import classes here.
**

*import* com.cp.services.rules.sources.Person;
**

*import* com.cp.services.rules.actions.PersonActions;
**

*import* com.cp.rules.TagCodes;
**

*import* java.util.List;



#declare any global variables here
**

*global* com.cp.services.rules.actions.PersonActions personActions;

**

*function* *boolean* hasOneOf(List list1, String[] list2){

*for* (Object obj : list1) {

String string = (String)obj;

*for* (*int* j = 0; j < list2.length; j++) {

*if* (string != *null* && string.equals(list2[j])) {

*return* *true*;

}

}

}

*return* *false*;

}

**

*rule* "Engineering"

*dialect* "mvel"

*when*
**

#conditions

Person( ageInYears >= 18 ) *and*

*eval* ( hasOneOf(Person(tagList), TagCodes(engrTags)) )

*then*

#actions

personActions.addAction("EngineeringGroup");

*

end
*

_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
Edson Tirelli-3

Re: function problem

Reply Threaded More More options
Print post
Permalink

   If I understood the problem, one way would be (assuming $tags is a global list):

when
    Person( ageInYears >= 18, $tagList : tagList )
    exists( String( this memberof $tags ) from $tagList )
then
   ...
end

   If $tags is a fact, just bind it before the "exists".

   []s
   Edson


2008/12/1 Dan Seaver <[hidden email]>

Charlie,
I'm not sure why your function doesn't compile as it looks fine to me. Do
you have any other details about the message you're getting from the
compiler?

In any case, you may want to consider solving the problem a different way.
In general, you should try to avoid "eval" statements whenever possible as
they can severely hamper performance. Edson wrote up some helpful hints -
see "Eval is Evil" at -
http://blog.athico.com/2007/03/writing-better-rules.html

As an alternative, try populating working memory with each of the Person's
tagList items and then use "memberOf" to determine if it's one of the
engineering TagCodes. (You may be able to use the "from" statement with the
Person's tagList instead of populating working memory) This allows Drools to
do all of the work for you and may very well be faster than using loops.

Let me know if you need further clarification.


Charlie Holland wrote:
>
> I'm fairly new to Drools so there may be an easier way to do this. I need
> to
> check to see if any one of a list of tags on an object are in another list
> of tags and am trying to do that with a function. the compiler tells me it
> can't compile my eval function:
>
> #list any import classes here.
> **
>
> *import* com.cp.services.rules.sources.Person;
> **
>
> *import* com.cp.services.rules.actions.PersonActions;
> **
>
> *import* com.cp.rules.TagCodes;
> **
>
> *import* java.util.List;
>
>
>
> #declare any global variables here
> **
>
> *global* com.cp.services.rules.actions.PersonActions personActions;
>
> **
>
> *function* *boolean* hasOneOf(List list1, String[] list2){
>
> *for* (Object obj : list1) {
>
> String string = (String)obj;
>
> *for* (*int* j = 0; j < list2.length; j++) {
>
> *if* (string != *null* && string.equals(list2[j])) {
>
> *return* *true*;
>
> }
>
> }
>
> }
>
> *return* *false*;
>
> }
>
> **
>
> *rule* "Engineering"
>
> *dialect* "mvel"
>
> *when*
> **
>
> #conditions
>
> Person( ageInYears >= 18 ) *and*
>
> *eval* ( hasOneOf(Person(tagList), TagCodes(engrTags)) )
>
> *then*
>
> #actions
>
> personActions.addAction("EngineeringGroup");
>
> *
>
> end
> *
>
> _______________________________________________
> rules-users mailing list
> [hidden email]
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>

--
View this message in context: http://www.nabble.com/function-problem-tp20633662p20774756.html
Sent from the drools - user mailing list archive at Nabble.com.

_______________________________________________
rules-users mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/rules-users



--
 Edson Tirelli
 JBoss Drools Core Development
 JBoss, a division of Red Hat @ www.jboss.com

_______________________________________________
rules-users mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/rules-users
Charlie Holland

Re: function problem

Reply Threaded More More options
Print post
Permalink
In reply to this post by Dan Seaver
Thanks, I'll look at the eval article, wasn't sure how to avoid them but will endeavor to find a way. 

I found my way around the original problem, not even sure what it was now.

I'm not sure the memberOf and taglist would generally work for us. The problem is that there are several patterns in the test object that are like this. Here are a couple of examples from the full application. A patient (yup we've switched contexts) has several problems, each is defined by a code. I want to determine if the patient is a diabetes patient, a "diabetes patient" is defined as a patient who as any one of a number of coded problems. Some patients may have 80 or more problem codes, there are about 20 codes that define what could be a diabetes patient. There are multiple lists like this, COPD has another list of codes for example.

Similar problems exist with medications, is this patient on a statin? Statins are a class of medication there are dozens of them any one of them will satisfy the query, and of course the patient may be on dozens of medications.

Not sure that clarified things. I've generally got things working but needless to say there is always room to make things better (like getting rid of the evals)...

On Mon, Dec 1, 2008 at 10:48 AM, Dan Seaver <[hidden email]> wrote:

Charlie,
I'm not sure why your function doesn't compile as it looks fine to me. Do
you have any other details about the message you're getting from the
compiler?

In any case, you may want to consider solving the problem a different way.
In general, you should try to avoid "eval" statements whenever possible as
they can severely hamper performance. Edson wrote up some helpful hints -
see "Eval is Evil" at -
http://blog.athico.com/2007/03/writing-better-rules.html

As an alternative, try populating working memory with each of the Person's
tagList items and then use "memberOf" to determine if it's one of the
engineering TagCodes. (You may be able to use the "from" statement with the
Person's tagList instead of populating working memory) This allows Drools to
do all of the work for you and may very well be faster than using loops.

Let me know if you need further clarification.


Charlie Holland wrote:
>
> I'm fairly new to Drools so there may be an easier way to do this. I need
> to
> check to see if any one of a list of tags on an object are in another list
> of tags and am trying to do that with a function. the compiler tells me it
> can't compile my eval function:
>
> #list any import classes here.
> **
>
> *import* com.cp.services.rules.sources.Person;
> **
>
> *import* com.cp.services.rules.actions.PersonActions;
> **
>
> *import* com.cp.rules.TagCodes;
> **
>
> *import* java.util.List;
>
>
>
> #declare any global variables here
> **
>
> *global* com.cp.services.rules.actions.PersonActions personActions;
>
> **
>
> *function* *boolean* hasOneOf(List list1, String[] list2){
>
> *for* (Object obj : list1) {
>
> String string = (String)obj;
>
> *for* (*int* j = 0; j < list2.length; j++) {
>
> *if* (string != *null* && string.equals(list2[j])) {
>
> *return* *true*;
>
> }
>
> }
>
> }
>
> *return* *false*;
>
> }
>
> **
>
> *rule* "Engineering"
>
> *dialect* "mvel"
>
> *when*
> **
>
> #conditions
>
> Person( ageInYears >= 18 ) *and*
>
> *eval* ( hasOneOf(Person(tagList), TagCodes(engrTags)) )
>
> *then*
>
> #actions
>
> personActions.addAction("EngineeringGroup");
>
> *
>
> end
> *
>
> _______________________________________________
> rules-users mailing list
> [hidden email]
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>

--
View this message in context: http://www.nabble.com/function-problem-tp20633662p20774756.html
Sent from the drools - user mailing list archive at Nabble.com.

_______________________________________________
rules-users mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/rules-users


_______________________________________________
rules-users mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/rules-users