How to write .drl for list type object

2,570 views
Skip to first unread message

Jainath Gupta

unread,
Aug 25, 2017, 5:54:22 AM8/25/17
to Drools Usage
Hi,
    I want to write .drl for List of object. I didn't get any ideas. Below code is autogenerated by drools project, but I made little change. My change is inserting object is List type.
public class DroolsTest {

.java
    public static final void main(String[] args) {
        try {
            // load up the knowledge base
       KieServices ks = KieServices.Factory.get();
       KieContainer kContainer = ks.getKieClasspathContainer();
        KieSession kSession = kContainer.newKieSession("ksession-rules");

            // go !
            Message message = new Message();
            message.setMessage("Hello World");
            message.setStatus(Message.HELLO);
            
            Message message1 = new Message();
            message1.setMessage("Welcome");
            message1.setStatus(Message.HELLO);
            
            List<Message> messageList = new ArrayList<Message>();
            messageList.add(message);
            messageList.add(message1);
            
            kSession.insert(messageList);
            kSession.fireAllRules();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public static class Message {

        public static final int HELLO = 0;
        public static final int GOODBYE = 1;

        private String message;

        private int status;

        public String getMessage() {
            return this.message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public int getStatus() {
            return this.status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

    }

}

.drl:- auto generated and this one is for Message object not for List<Message> object.

rule "Hello World"
    when
        m : Message( status == Message.HELLO, myMessage : message )
    then
        System.out.println( myMessage );
        m.setMessage( "Goodbye cruel world" );
        m.setStatus( Message.GOODBYE );
        update( m );
end


Please Help.

Jainath Gupta

unread,
Aug 28, 2017, 1:46:12 AM8/28/17
to Drools Usage
Hi Team.
  Please answer  this question.

José Filipe Neis

unread,
Aug 28, 2017, 9:40:36 AM8/28/17
to Drools Usage
Which rule are you trying to write? Check if any msg in your list is of HELLO type? All of them? None of them?

Jainath Gupta

unread,
Aug 29, 2017, 1:13:42 AM8/29/17
to Drools Usage
Hi Jose,
   I am inserting List of Message kSession.insert(messageList); So above(m : Message( status == Message.HELLO, myMessage : message ).drl is wrong. Can you Print System.out.println( myMessage ); this message.

Thanks

José Filipe Neis

unread,
Aug 29, 2017, 8:37:45 AM8/29/17
to Drools Usage
Jainath,

Writing the rules will follow the same kind of pattern you used in your example, but remember that now you have a List. What does this influence? That Drools will match your objects if they are equals (and list.equals(list) is not a trivial thing to match).

If I would give you any advice right now is: avoid using a List as a session object, but I really don't know your business requirements as you didn't talk about them at all so far.

Anyway, to write a rule that matches a list property inside another object, you can use contains/memberof or a condition like the one exposed on this SO question:

rule "some rule name"
when
$a
: A($bset : bset)
$b
: B(x == "hello") from $bset
then
//you will have one activation for each of the B objects matching //the second pattern end

Filipe

Jainath Gupta

unread,
Aug 30, 2017, 1:51:07 AM8/30/17
to Drools Usage
If I would give you any advice right now is: avoid using a List as a session object, 
Hi Jose,
In business requirements it's possible that we can insert list as a session object. Which I did in my java class(Is it wrong ? hope so no). So It was drools team responsibility that they should provide provision for writing rules on  List as a session object.

José Filipe Neis

unread,
Aug 30, 2017, 8:26:37 AM8/30/17
to Drools Usage
Drools accept any kind of object as a fact, including a list. It's up to you to define how will this list be matched? How equality works around your list? If all elements are equal?

I tend to think that it would be better to insert the individual elements and match them in an aggregated way, but I don't know which requirements you're dealing with. Again: if you tell what are your requirements, maybe there is a better approach them matching a list that people can suggest you.

--
You received this message because you are subscribed to a topic in the Google Groups "Drools Usage" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/drools-usage/O8AM_0M2CW4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/a73fa3fb-ef42-4d1e-9660-e02e499ba84f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages