[rules-users] How to declare an array attribute within an object in a DRL file?

fulton fulton.gee at LMCO.com
Thu Apr 28 14:36:03 EDT 2011


(email subscribers) In reference to my question on how to declare an array
field within a class defined in the "declare" section of a DRL file (for use
within the Drools Server):

Well, it took me 2 days to figure it out, but here is the current answer.  I
ended up just processing things in reverse with XStream to see what it was
expecting.  That is, I created a Java tester to manually define my Person
and Hobby classes, and then instantiated a Person object and marshalled it
into JSON with XStream to see what it should look like.  I then reversed the
process to unmarshall the JSON back into a Java object to verify that
XStream would parse it correctly.  The trick was to specify the nested Hobby
objects correctly.  There's no way I could have figured this out without
generating the JSON with XStream FIRST.

DRL:
==============================
package  com.foobar

declare Hobby
	name: String
	desc: String
end

declare Person
	name: String
	hobbies: java.util.ArrayList
end

rule "Person" dialect "mvel"

when
    $p : Person();
then
    $p.name = $p.name + " has hobby size of " + $p.hobbies.size();
end
===============================

JSON to be generated from the PHP application:

===============================
{
	'insert':
	{
		'out-identifier':'123',
		'object':
		{
			'com.foobar.Person':
			{
				"name":"Joe",
				"hobbies":
				[
					{
						"com.foobar.Hobby":
						[
							{"name":"Golf","desc":"Sport #1"},
							{"name":"Bowling","desc":"Sport #2"}
						]
					}
				]
			}
		}
	}
}


--
View this message in context: http://drools.46999.n3.nabble.com/How-to-declare-an-array-attribute-within-an-object-in-a-DRL-file-tp2867666p2875778.html
Sent from the Drools: User forum mailing list archive at Nabble.com.



More information about the rules-users mailing list