[rules-users] How to handle generics in Drools?

Welsh, Armand AWelsh at StateStreet.com
Thu Nov 17 17:25:59 EST 2011


Type Generics are used to control type cast conversion errors at compile time.  In runtime, it's just a List, not a List<T>.  What is unique is that all the contents of the List are of the same type.  To define the global use:

Global List tasks

In rules, you can evaluate this as:

Rule 'some-rule'
When
	$pds : ProjectDataSet() from tasks


Or you can add your contraints to the ProjectDataSet fact as needed.  Collection tasks could conceivable return several different object classes, but the Query Criteria states to only inspect those Objects that are an instance of ProjectDataSet.

This would be synonymous to the following java process:

	List<ProjectDataSet> pds = new ArrayList<ProjectDataSet>()
	for(Object o : tasks) {
		if ( o instanceof ProjectDataSet )
			pds.add( (ProjectDataSet) o );
	}
	return pds;


However, in your java code that loads the fact, you are not setting a global, you are inserting all members of the tasks collection into drools working memory.
This means, that your rule would evaluate those facts as:

Import com.some.project.path.ProjectDataSet

Rule 'some-rule'
When
	$pds : ProjectDataSet()

Then
	// some action
End


So in this case, you do not have a global, and you do not need to be concerned with the fact you are loading in a List, because the function call you are calling is a helper function that essentially does this:

	for (Collection c : tasks) {
		commands.add(CommandFactory.newInsert( c );
	}

So what is in working memory, is a whole lot of ProjectDataSet objects, but not the list itself.  In reality,  the actual code implementation of the function is not likely to be what I have shown, but this is how you can think of the command's implementation.

Regards,
Armand


-----Original Message-----
From: rules-users-bounces at lists.jboss.org [mailto:rules-users-bounces at lists.jboss.org] On Behalf Of Adrigan
Sent: Thursday, November 17, 2011 1:56 PM
To: rules-users at lists.jboss.org
Subject: Re: [rules-users] How to handle generics in Drools?

Hello Edson,

	If the generic *<T>* is not allowed, and drools appears to be written to operate over collections of objects, how do you insert a List of items like
this:
*List<ProjectDataSet.Task> tasks*?  In my code I insert the tasks as facts like this:
*commands.add(CommandFactory.newInsertElements(tasks));*
I use a decision table and have as one of the variables: (in the variable
row)
*List<ProjectDataSet.Task> tasks*.

	The error I receive is this:
/org.drools.template.parser.DecisionTableParseException: The format for global variables is incorrect. It should be: [Class name, Class otherName].
But it was: [List<ProjectDataSet.Task> tasks]
	at
org.drools.decisiontable.parser.RuleSheetParserUtil.getVariableList(RuleSheetParserUtil.java:84)/

	I desire the rules in the table to iterate through all the tasks in that list and identify tasks that are due soon (a part of my condition column *TASKACTSTART< today* … etc.  The TASKACTSTART is a field named by Microsoft in the ProjectDataSet.Task class.)

	Have I misunderstood the power of drools?  How do you address a collection of a type like List<T> in a rule?

Thank you in advance for your help,
	Adrigan




--
View this message in context: http://drools.46999.n3.nabble.com/rules-users-How-to-handle-generics-in-Drools-tp3427723p3517186.html
Sent from the Drools: User forum mailing list archive at Nabble.com.

_______________________________________________
rules-users mailing list
rules-users at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users




More information about the rules-users mailing list