Drools rule "not exists"
Asked Answered
S

2

5

I'm using JBoss Drools to write some business rules. I got a problem on the "not exists" rule.Here is my code.

rule "ATL 27R-A12 Subfleet A319-100 Departure configuration list has flap 1"
    salience 20
    no-loop true
    when
        AircraftConfig(aircraftType=="A319-100")
        RunwayInfo(airport3lCode== "ATL", runwayId == "27R-A12" )
        not (exists (DepartureConfiguration( flap == 1 )))
    then
        throw new RuleNotMatchException("The configurations do not match the rule of this runway.");
end

My facts contains:An AircraftConfig, an RunwayInfo and several DepartureConfigurations. I want to fire the rule when there are no DepartureConfiguration which flap=1. I mean, if there are three DepartureConfigurations, one of them has the flap=1, the others are flap=2 or flap=3, then this rule will not fire. How could I make this work?

Sorption answered 20/3, 2012 at 6:40 Comment(4)
Your code, although with a redundant "exists" in there, should still fire the rule. But you should never raise exceptions in the consequence of a rule. It is not possible to know, from your description alone, what is the problem. Can you add details? Also, which version of Drools are you using?Rosannrosanna
@EdsonTirelli I'm using Drools 5.3.0-final. I'm not sure what details you want, so I just copied my Java code:List<LandingConfiguration> result = new ArrayList<LandingConfiguration>(); KnowledgeBase kbase = readKnowledgeBase("runway.A319.landing.drl"); StatelessKnowledgeSession ksession = kbase .newStatelessKnowledgeSession(); List<Object> facts = new ArrayList<Object>(); facts.add(subFleet); facts.add(runway); facts.addAll(configurations); ksession.execute(facts);Sorption
Is this the only rule in your kbase? try commenting out the conditions to know which one is preventing the rule to fire. You can use the audit log or the eclipse debugger to figure it out, but for a simple rule it is just easier to comment out the conditions to find one which one is failing and then we can try to diagnose the problem.Rosannrosanna
@EdsonTirelli I come to believe their must be sth wrong with my java code, not the rule. I use forall(DepartureConfiguration( flap == 1 )), it not fires, but when I use forall(DepartureConfiguration( flap != 1 ), it fires.Sorption
R
12

The keyword for checking for the non-existence of a fact is not, not not exists. Change the last line of your condition to:

not DepartureConfiguration( flap == 1 )
Radium answered 20/3, 2012 at 12:34 Comment(1)
True, but "not( exists( ... ) )" should also work, just will be redundant and more expensive. I guess the problem is something else.Rosannrosanna
S
1

Actually, I made some conflict in my rules. I used to think the rules should be ran from the top to the end of the drl file. I solved my problem by adding a rule flow. Also thanks to you guys who give me suggestions.

Sorption answered 21/3, 2012 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.