[rules-users] How to Capture the rule which does not get executed
Michael Anstis
michael.anstis at gmail.com
Wed Oct 13 16:31:28 EDT 2010
You could store all rules in a global and remove those that execute in an
AgendaEventListener's activtionCreated event (re-adding those that get
retracted by Truth Maintenance in the AgendaEventListeners
activationCancelled event).
//Loads rules
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("myrules.drl"),
ResourceType.DRL);
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
Collection<KnowledgePackage> kpackages = kbuilder.getKnowledgePackages();
kbase.addKnowledgePackages(kpackages);
//Get all rules
for(KnowledgePackage kpackage : kpackages) {
for(Rule rule : kpackage.getRules()) {
myGlobal.add(rule);
}
}
//The usual suspects
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
void activationCreated(ActivationCreatedEvent event) {
myGlobal.remove(event.getActivation().getRule());
}
@Override
void activationCancelled(ActivationCancelledEvent event) {
myGlobal.add(event.getActivation().getRule());
}
});
ksession.fireAllRules();
You could also use an AgendaEventListener to better manage the contents of
the Rule collection for more complex scenarios where Truth Maintenance could
retract an activation - that the simpler solution above wold
2010/10/13 Navdeep Kumar <nkumar at objectwave.com>
> Hi All,
>
> I am new to Drools. I am working on a project where nature of rules are
> dynamic. I want to capture that rule which does not get executed. i need to
> store that rule because i have to display that rule in the UI.
>
> Here is my rule file
>
> rule "FrontAxle:Capacity|FrontSuspension:Capacity"
>
> when
> b:Feature(featureClass.name=="FrontSuspension")
> c:Feature(featureClass.name=="FrontAxle")
> //eval(b.getCapacity() >= c.getCapacity())
> then
> System.out.println("These are compatible");
> System.out.println(b.getFeatureClass().getName());
>
> availablity.put(b.getFeatureClass().getName()+"-"+c.getFeatureClass().getName(),"12.35");
> //availablity.put(b.getName()+"-"+c.getName(),new Double(b.getCapacity()));
> insert(availablity);
> end
> rule "FrontSuspension:Type|FrontAxle:Type"
> when
> b:Feature(featureClass.name=="FrontSuspension")
> c:Feature(featureClass.name=="FrontAxle")
> not Feature(b.attributes.Type=="I Beam" && c.attributes.Type=="Multi Link
> Air")
> //NOT (FrontAxle.type = "I-Beam" AND FrontSuspension.type = "Multi-link
> Air") - mounting restriction
> then
> System.out.println("I am in 2");
> end
> rule "FrontAxle:FeatureCode|FrontSuspension:Type"
> when
> b:Feature(featureClass.name=="FrontSuspension")
> Feature(featureClass.name=="FrontAxle" && code=="002ASW")and not
> Feature(b.attributes.Type=="Multi Link Air" || b.attributes.Type=="Monoleaf
> Spring")
> //FrontAxle.featureCode = "002ASW" AND NOT (FrontSuspension.type =
> "Multi-link Air" OR FrontSuspension.type = "Monoleaf Spring")
> then
> System.out.println("I am in 3");
> end
>
>
>
>
>
>
> now if these two objects named FrontAxle and FrontSuspension are compatible
> then it has to meet all the 3 rules, but if any combination of these objects
> does not meet one of the rule listed above then i have to store the rule for
> the displaying purpose that this combination didn't meet this rule thats why
> they are not compatible. any kind of help will be appreciated.
>
>
>
> Thanks.
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20101013/3cc6634e/attachment.html
More information about the rules-users
mailing list