The simplest would be to create a physical subclass of
DefaultAgendaEventListener rather than the anonymous one used to date:-
MyEventListener myEventListener = new MyEventListener(myGlobal);
ksession.addEventListener(myEventListener);
class MyEventListener extends DefaultAgendaEventListener {
private Set<Rule> rules;
public MyEventListener(Set<Rule> rules) {
this.rules = rules;
}
@Override
public void activationCreated(ActivationCreatedEvent event) {
rules.remove(event.getActivation().getRule());
}
@Override
public void activationCancelled(ActivationCancelledEvent event) {
rules.add((Rule) event.getActivation().getRule());
}
}
Note: you also need to use a Set instead of a List for your "myGlobal".
2010/10/14 Navdeep Kumar <nkumar(a)objectwave.com>
Yes i am using Eclipse IDE, i tried other import statements and
errors are
gone now.
Map<String,String> availablity = new HashMap<String,String>();
*List<Rule>myGlobal = new ArrayList<Rule>();*
//Feature feature = new Feature();
try{
LOG.info("Before calling the function readKnowledgeBase");
//KnowledgeBase kbase = readKnowledgeBase();
LOG.info("Rule file is loaded");
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("AvailabilityRule.drl"),
ResourceType.DRL);
LOG.info("Rule file added to the knowledgebuilder");
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error: errors) {
LOG.error(errors);
LOG.error("in error loop of readKnowledgeBase");
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
Collection<KnowledgePackage>kpackages = kbuilder.getKnowledgePackages();
kbase.addKnowledgePackages(kpackages);
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger =
KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
ksession.setGlobal("availablity", availablity);
*for(KnowledgePackage kpackage : kpackages) {*
* ** for(org.drools.definition.rule.Rule rule : kpackage.getRules()) {*
* ** myGlobal.add((Rule) rule);*
* ** }*
* **}*
ksession.insert(Obj1);
ksession.insert(Obj2);
LOG.info("Rules are fired");
*ksession.addEventListener(new DefaultAgendaEventListener() {*
* ** @Override*
* **public*
* ** void activationCreated(ActivationCreatedEvent event) {*
* ** myGlobal.remove(event.getActivation().getRule());*
* ** }*
*
*
* ** @Override*
* **public*
* ** void activationCancelled(ActivationCancelledEvent event) {*
* ** myGlobal.add((Rule) event.getActivation().getRule());*
* ** }*
* **});*
ksession.addEventListener( new DefaultAgendaEventListener() {
public void afterActivationFired(AfterActivationFiredEvent event) {
super.afterActivationFired( event );
System.out.println( event );
}
});
ksession.fireAllRules();
i declared myGlobal at the top of the program before the rule file is
loading. the added code is in bold. it changed Rule:rule to some import
statement, i guess that is because of casting. but in the both of the action
listeners it is giving me an error saying that myGlobal should be declared
final because of inner class. if i will declare this final i cant do
anything with this. this is the last error which i have now. please suggest
what to do with that.
Thanks
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users