[rules-users] Guvnor and drools implementation - questions

Eddy Hautot eddyhautot at gmail.com
Mon Mar 7 09:09:18 EST 2011


Thanks for your help Vincent.

To be sure i have understood all well.

I've just created 2 class like you show me with some changes to fit and test
all (timestamp are long in fact, i have put int, double and string as well)
:

First class :

import java.util.HashMap;

public class MapPojo extends HashMap<String, Object> {

     public Object getId() {
         return this.get("id");
     }

     @Override
     public boolean equals(Object o) {
         return (o instanceof MapPojo) &&
((MapPojo)o).getId().equals(getId());
     }

     public long getTimeStamp() {
         return (Long) this.get("timeStamp");
     }

     public boolean isBefore(MapPojo other) {
         if (this.getTimeStamp() < other.getTimeStamp())
             return true;
         else
             return false;
     }
}

Second class :

public class MapTest1 extends MapPojo {

    private String     Att1String;
    private String     id;
    private double    Att2Double;
    private int          Att3Int;
    private long       timeStamp;

    public String getId() {
        return (String)this.get("id");
    }
    public void setId(String val) {
        this.put("id",val);
    }

    public String getAtt1String() {
        return (String)this.get("Att1String");
    }
    public void setAtt1String(String val) {
        this.put("Att1String",val);
    }

    public double getAtt2Double() {
        return (Double)this.get("Att2Double");
    }
    public void setAtt2Double(double val) {
        this.put("Att2Double",val);
    }

    public int getAtt3Int() {
        return Integer.parseInt((String)this.get("Att3Int"));
    }
    public void setAtt3Int(int val) {
        this.put("Att3Int",val);
    }

    public long getTimeStamp() {
        return (Long)this.get("timeStamp");
    }
    public void setTimeStamp(Long val) {
        this.put("timeStamp",val);
    }
}

Then i have imported this in Guvnor.

I have made 2 categories. I have put the model (jar) in the 2 categories. I
have created 2 simple rules playing with the attributes id, timestamp...

Then i get some code like this (when i clic on view code or get the drl
file) : 1.|rule "Rule1InCat1" 2.|    dialect "mvel" 3.|    when 4.|        $m1
: MapTest1( att1String == "value1" ) 5.|        $m2 : MapTest1( att1String
== "value1" ) 6.|        MapTest1( att2Double == 3.5 )7. |    then8.|
       MapTest1
fact0 = new MapTest1();9.|         fact0.setId( "id4" );10.|
insert(fact0
); 11.|end
1) For the condition Map $m1 different then Map $m2. I suppose i have to use
the function define in MapPojo? I have to write it by hand using "free from
drl" : eval($m1 != $m2) ? or is there a better way?

The same for testing that $m1 timestamp (Long) is befor $m2. I have to write
it by hand : eval($m1.isBefore($m2))? So they have to know it exist a method
isBefore if not said by the gui?

2) In the code It's written MapTest1(..) instead of Map(...) so i suppose
drools has to do it with the precompiled package  .pkg to know about the
MapPojo and MapTest1.. If i take only the .drl, drools won't know about
class MapTest1.

I think it could be for the biggest file about 100+ rules. And the smallest
about 2-3 only.

I can have : MapTest1, MapTest2, MapTest3, MapTest4 for example with each
their own attributes. And For now if i have 4 drl files (file1.drl go for
MapTest1 objects, file2.drl go for MapTest2 objects...) with their own
rules, i know when to use which file.

Yes i can set a category to each of my rule. Do you mean create 4 categories
(cat1, cat2, cat3, cat4) and for rules of file1.drl, put them it cat1, for
file2.drl, put them in cat2... ? Set a first condition in the rules? Am i
obliged to create a package per file and filter by category or is it
possible to make a package with all and filtering when creating the
knowledgeBase or something like that?


Thanks again for your precious help.



On Fri, Mar 4, 2011 at 7:35 PM, Vincent Legendre <
vincent.legendre at eurodecision.com> wrote:

>
> > I just tried to make a pojo. But i will have to do a single Pojo
> > called "Map" with all the properties? Right now i had 4 Enum. Here i
> > will have to have all in the same POJO with name "Map".
> No. Don't name the Pojo "Map" ... And create as much Pojo that you have
> different enums. Make them extend a common class where are defined
> common behaviour.
> To make that clear once for all, see this code :
>
> public class MapPojo extends HashMap<String, Object> {
>
>     public Object getId() {
>         return this.get("id");
>     }
>
>     @Override
>     public boolean equals(Object o) {
>         return (o instanceof MapPojo)
> && ((MapPojo)o).getId().equals(getId());
>     }
>
>     public Date getTimeStamp() {
>         return (Date) this.get("timeStamp");
>     }
>
>     public boolean isBefore(MapPojo other) {
>         return this.getTimeStamp().before(other.getTimeStamp());
>     }
> }
>
> // for enum 1
> public class MapPojoGroup1 extends MapPojo {
>
>     public String getAtt1() {
>         return (String)this.get("att1");
>     }
>     public void setAtt1(String val) {
>         this.put("att1",val);
>     }
>
>     public Long getAtt2() {
>         return (Long)this.get("att2");
>     }
>     public void setAtt2(Long val) {
>         this.put("att2",val);
>     }
>
> }
>
> ... etc etc etc for other enums
> Easy to generate isn't it ?
>
> >
> > The advantage i suppose is that with the Pojo, we can directly tell
> > this attribute is a String, this one is a Integer, this one a Double
> > and this one a Long and in the rules all will be done automatically
> > without casting?
> Yep. And Guided editor will work "out of the box", providing the
> suitable list of fields, and suitable operators according to the field
> type.
> And if subsequent Pojo generation remove a field, compilation will warn
> you.
>
>
> For the files, please tell how you cut them. Can you set a category ? If
> yes, you can also set a first condition. How many rules do you (plan to)
> have ?
> And yes, you can compile only rules that match a given category. But you
> will have to store it in a snapshot just after that. Please refer to the
> doc for this point.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20110307/55f76d7a/attachment.html 


More information about the rules-users mailing list