Reputation: 2072
I have a table that has conditions
size | type | id | content
1 | 2 | 1 | "hai"
null | null | null | "default content"
1 | null | null | "content for size 1"
I get each and every row in the table into a list of objects.
class A{
private String size="";
private String type="";
private String id="";
private String content = "";
}
The requirement is to show the content corresponding to the row which matches the maximum conditions. else show the default message (corresponding to the 'null,null,null' row).
How can i do this in drools? I am completely lost.
Upvotes: 1
Views: 1500
Reputation: 73
Start by cleaning up the data you're getting as inputs for your content, it looks a little inconsistent from the question.
The next thing you need to do is represent the domain of your problem with objects. For the sake of example, create one pojo called ContentObject with attributes mapping to that defined in your input file (note, you'll need an integer or something that implements Comparable to allow you to do ordering). The next thing you'll need is a pojo to hold your Maximum (containing a single attribute called max).
Create a stateless knowledge session and populate it with objects. Again, for the purpose of example, say:
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
// build the session input objects
ContentObject co1 = new ContentObject(0,"T1","id1", "Some Content 1");
ContentObject co2 = new ContentObject(1,"T2","id2", "Some Content 2");
ContentObject co3 = new ContentObject(3,"T3","id3", "Some Content 3");
Max max = new Max();
// populate a fact set
Set<Object> facts = new HashSet<Object>();
facts.add(co1); facts.add(co2); facts.add(co3); facts.add(max);
// run rules
ksession.execute(facts);
// display result
System.out.println(max.getMax());
in a seperate resouce define the following drool:
package com.yourpackage
import com.yourpackage.ContentObject;
dialect "mvel"
rule "GetMax"
when
m1 : ContentObject ()
mx : Max( m1.size > max )
then
mx.max = m1.size;
update(mx);
end
I think the thing that is not so obvious in Drools is how objects are organised and used in the Working Memory. Its worth searching for the term "cross product" in the drools documentation.
Aslo, bear in mind before you implement this trivial algorithm in drools, that there is a one liner in java that will do the same thing (assuming you're implementing Comparable - c.f. Collections.sort())!
Upvotes: 1