[rules-users] Problem in using genrics with drools4.0.1.
Mark Proctor
mproctor at codehaus.org
Tue Oct 9 10:56:55 EDT 2007
Drools, and probably the underlying MVEL that the 'from' uses, has no
understanding of Generics, so I expect thats your problem.
Mark
Gaurav2007 wrote:
> HI ALL,
>
> I am using drool 4.0.1 in my project.
> I am facing problem in using generics feature of jdk1.5.
>
> My objective is to perform validation on the basis of data type of variable.
> for storing values of variable i have one Attribute Class it contains getter
> setter for values of attribute and attributeId which is a string but value
> of attribute is of deferent type so i am thinking to use generics here.
>
> public class Attribute<T> {
>
> public String qualifiedName;
>
> public T newValue;
>
> public T oldValue;
>
> public String getQualifiedName() {
> return qualifiedName;
> }
>
> public void setQualifiedName(String qualifiedName) {
> this.qualifiedName = qualifiedName;
> }
>
> public T getNewValue() {
> return (T)newValue;
> }
>
> public void setNewValue(T newValue) {
> this.newValue = newValue;
> }
>
> public T getOldValue() {
> return oldValue;
> }
>
> public void setOldValue(T oldValue) {
> this.oldValue = oldValue;
> }
> }
>
>
> my rule for date comparison looks like:
>
> //Date Comparison.
> rule "W250.1 DateComparison"
> when
> $dto : DataProvider()
> Attribute(newValue < "29-Oct-2007") from $dto.getValue("W250.1")
> then
> System.out.println ("Error in Date check \t");
> end
>
> DataProvider class it is responsible to give instance of attribute
> corresponding to attributeId:
>
> public class DataProvider {
>
> HashMap<String,Attribute> fieldMap=new HashMap<String, Attribute>();
>
> public DataProvider() {
> populateMap();
> }
>
> public Attribute getValue(String qualifiedName){
> Attribute attribute = fieldMap.get(qualifiedName);
> return attribute;
> }
>
> public void populateMap(){
> Attribute<Date> attribute = new Attribute<Date>();
> SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
> Date date;
> try {
> date = format.parse("27-Oct-2007");
> attribute.setQualifiedName("W250.1");
> attribute.setOldValue(date);
> attribute.setNewValue(date);
> fieldMap.put(attribute.getQualifiedName(),attribute);
>
> }
> catch (ParseException e) {
> e.printStackTrace();
> }
>
> }
> }
>
> in this case i am getting exception:
> Exception in thread "main" java.lang.ClassCastException: java.lang.String
>
> when i am not using generics this rule is working fine.
> but in this case how should i perform validation depending upon data type of
> attribute.
>
> can you please help me in solving this problem.
>
> Thanks,
> Gaurav Joshi
>
>
More information about the rules-users
mailing list