[rules-dev] Enum types in rules template

Florian Beckmann florian.beckmann at camunda.com
Thu May 13 12:51:06 EDT 2010


Hi all,

I got a question relating to value substitution of enum types  in rule 
templates.

Given the enum type:

public enum MyEnum
{
	A("a");
	B("b);

	private String shortName;

	private MyEnum(string shortName)
	{
		this.shortName = shortName;
	}

	public String shortName()
	{
		return shortName;
	}

	public String toString()
	{
		return shortName;
	}
}

an object for the template

public class RuleObject
{
	private MyEnum myEnum;
	public TemplateObject(MyEnum myEnum)
	{
		this.myEnum = myEnum;
	}

	public void setMyEnum(MyEnum myEnum){this.myEnum = myEnum;}
	public MyEnum getMyEnum(){ return myEnum;}
	
}

and a fact model object

public class FactObject
{
	private MyEnum myEnum;
	public void setMyEnum(MyEnum myEnum){this.myEnum = myEnum;}
	public MyEnum getMyEnum(){ return myEnum;}
}

with an instance 
TemplateObject templateObject = new TemplateObject(MyEnum.A);
 
and the template
----------------------------------
template header
name
myEnum
package my.package

import my.package.FactObject
import my.package.MyEnum

template "MyTemplate"

rule "@{name}"
	when
		$factObject: FactObject(myEnum == MyEnum.@{myEnum}} //<------------ 
toString() is used here
	then
		...
end

end template
----------------------------

It seems that for the substitution of @{myEnum} the toString() method of the 
myEnum-Object is used. Without providing a toString() method in the declared 
enum class the name() method is invoked implicitly. Hence the expression would 
be substituted with 'A' ($factObject: FactObject(myEnum == A) . Therefore it's 
necessary to put the Enums-Classname with a dot - "MyEnum." - in front so it 
would be MyEnum.A in the generated rule  ($factObject: FactObject(myEnum == 
MyEnum.A).

Trouble starts if I provide a custom toString() method in my enum type. With 
the example above the expression @{myEnum} would be substituted with 'a' 
($factObject: FactObject(myEnum == MyEnum.a) hence I get 
MyEnum.a in my generated rule -> Rule error because type doesn't exist.

I don't know anything about the implementation details of the template to rule 
generation -  but wouldn't it make more sense 

1. to substitute @{myEnum} with the enums classname and name() value 
(@{myEnum} -> MyEnum.A) if the given field is an Enum instance?
or
2. At least use the name() method (which is final) to always get the enums 
Name.

Maybe there are reasons why this makes no sense or can't be done? Your 
thoughts please.

Cheers
Florian Beckmann


		


--
--------------------------------------------
camunda services GmbH - The Business Process Company
Zossener Straße 55-58 - 10961 Berlin
www.camunda.com - info at camunda.com
---------------------------------------------
Florian Beckmann
Entwickler
Telefon +49 30 664040 900
florian.beckmann at camunda.com
---------------------------------------------
Amtsgericht Charlottenburg: HRB 113230 B
Geschäftsführer: Jakob Freund, Bernd Rücker
---------------------------------------------



More information about the rules-dev mailing list