Drools Fusion に同じクラスの 3 つのオブジェクトがあるかどうかを調べようとしています。以下のように Tick という名前の POJO があります。
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;
}
}
そして、 crossovertype == CrossOverType.bear を持つ Tick の3つの連続したオブジェクトがあるかどうかを調べようとしています
以下はルールとメインクラスです
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;
}
}
ルールは
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
crossovertype==CrossOverType.bear を持つ 2 つの連続する Tick オブジェクトがあるかどうかを確認できますが、これは 3 つの連続するオブジェクトでは機能しません。私に何か提案してください.ThankYou