i have been trying to find if there are three objects of same class in Drools Fusion.I have a POJO named Tick as below
public class Tick
{
CrossOverPrice crossoverprice;
CrossOverType crossovertype;
public Tick(CrossOverPrice crossoverprice, CrossOverType crossovertype) {
super();
this.crossoverprice = crossoverprice;
this.crossovertype = crossovertype;
}
public CrossOverPrice getCrossoverprice() {
return crossoverprice;
}
public void setCrossoverprice(CrossOverPrice crossoverprice) {
this.crossoverprice = crossoverprice;
}
public CrossOverType getCrossovertype() {
return crossovertype;
}
public void setCrossovertype(CrossOverType crossovertype) {
this.crossovertype = crossovertype;
}
}
and I am trying to find out if there are three consecutive objects of Tick which has crossovertype ==CrossOverType.bear
Below is the rules and main class
public class DroolsTest {
public static final void main(String[] args) {
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
// go !
Tick t1=new Tick(CrossOverPrice.p12,CrossOverType.bear);
Tick t2=new Tick(CrossOverPrice.p23,CrossOverType.bear);
Tick t5=new Tick(CrossOverPrice.p34,CrossOverType.bear);
ksession.insert(t1);
ksession.fireAllRules();
/*ksession.insert(t3);
ksession.fireAllRules();*/
ksession.insert(t2);
ksession.fireAllRules();
ksession.insert(t5);
ksession.fireAllRules();
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error: errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
}
The rules are
declare Tick
@role(event)
end
rule "Check three bear"
when
$t:Tick(crossovertype:CrossOverType.bear ,crossoverprice : CrossOverPrice.p12)
not Tick(this after $t,crossovertype == CrossOverType.bull)
$t1 : Tick(this after $t ,crossovertype == CrossOverType.bear ,crossoverprice ==CrossOverPrice.p23)
not Tick(this after $t1,crossovertype == CrossOverType.bull)
$t3 : Tick(this after $t1 ,crossovertype == CrossOverType.bear ,crossoverprice ==CrossOverPrice.p34)
then
System.out.println("great");
end
I am able to find whether there are two consecutive Tick Objects which has crossovertype==CrossOverType.bear but this is not working for three consecutive objects. Please suggest me something.ThankYou
If you take a look at the 'Check three bear' rule you have posted, you will see that there is a contradiction between the second Conditional Element and the third:
not Tick(this after $t...)
$t1 : Tick(this after $t ...)
This is what I've used in the past to get the latest 3 events from a stream:
$t1:Tick() //the newest
not Tick(this after $t1)
$t2:Tick(this before $t1)
not Tick(this before $t1, this after $t2) //no events between $t1 and $t2
$t3:Tick(this before $t2)
not Tick(this before $t2, this after $t3) //no events between $t2 and $t3
Another solution could be to use sliding windows:
List(size == 3) from collect( Tick(...) over window:length(3) )
In this case, the size == 3 condition is to prevent the activation of the rule when there are only 1 or 2 elements in the window.
Hope it helps,