Dynamic rules and inheritance
by ventilo35@free.fr
Hi
I'm exploring using dynamic rules (using Drools 6.0.0 Final), and I see a curious behaviour when the knowledge is built using inheritance.
What I notice is
- new rules which have conditions based on subclasses are fired all right
- but new rules which have conditions based on the base class are not always fired
- they are fired only if some initial rules where fired, based on the base class (?!?)
Here is an runnable example for clarity:
// knowledge classes
public static class Person {
private String firstName;
private String lastName;
public void setFirstName(String fname) {
this.firstName = fname;
}
public void setLastName(String lname) {
this.lastName = lname;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
}
public static class Contact extends Person {
private String msisdn;
public void setMsisdn(String m) {
this.msisdn = m;
}
public String getMsisdn() {
return this.msisdn;
}
}
// Drools session
KieServices ks = KieServices.Factory.get();
KieModuleModel kieModuleModel = ks.newKieModuleModel();
KieBaseModel kieBaseModel1 = kieModuleModel.newKieBaseModel( "rules")
.setDefault( true )
.setEqualsBehavior( EqualityBehaviorOption.EQUALITY )
.setEventProcessingMode( EventProcessingOption.STREAM );
KieSessionModel ksessionModel1 = kieBaseModel1.newKieSessionModel( "KSession1" )
.setDefault( true )
.setType( KieSessionModel.KieSessionType.STATEFUL )
.setClockType( ClockTypeOption.get("realtime") );
KieFileSystem kfs = ks.newKieFileSystem();
ReleaseId ri = ks.newReleaseId( "org.default", "artifact", "1.0.0" );
kfs.generateAndWritePomXML(ri);
// initial rules
kfs.write("src/main/resources/rules/initial.drl", ks.getResources().newFileSystemResource("src/main/resources/rules/initial.drl").setResourceType(ResourceType.DRL));
KieBuilder kieBuilder = ks.newKieBuilder( kfs ).buildAll();
KieContainer kContainer = ks.newKieContainer(ri);
KieSession kSession = kContainer.newKieSession();
Contact alice = new Contact();
alice.setFirstName("Alice");
alice.setLastName("Doe");
kSession.insert(alice);
Contact bob = new Contact();
bob.setFirstName("Bob");
bob.setLastName("Tester");
kSession.insert(bob);
kSession.fireAllRules();
// new rules
kfs = ks.newKieFileSystem();
ReleaseId ri2 = ks.newReleaseId( "org.default", "artifact", "1.1.0" );
kfs.generateAndWritePomXML(ri2);
kfs.write("src/main/resources/rules/initial.drl", ks.getResources().newFileSystemResource("src/main/resources/rules/initial.drl"));
kfs.write("src/main/resources/rules/newrules.drl", ks.getResources().newFileSystemResource("src/main/resources/rules/newrules.drl"));
kieBuilder = ks.newKieBuilder( kfs ).buildAll();
kContainer.updateToVersion(ri2);
kSession.fireAllRules();
initial.drl :
package com.sample
import com.sample.DroolsTest.Person;
import com.sample.DroolsTest.Contact;
rule "Hello World 1"
when
c: Contact()
then
System.out.println("initial, working, found Contact "+c.getFirstName());
end
rule "Hello World 2"
when
p: Person()
then
System.out.println("initial, working, found Person "+p.getFirstName());
end
newrules.drl :
package com.sample
import com.sample.DroolsTest.Person;
import com.sample.DroolsTest.Contact;
rule "Hello World 3"
when
c: Contact()
then
System.out.println("new rule, working, found Contact "+c.getFirstName());
end
rule "Hello World 4"
when
p: Person()
then
System.out.println("new rule, working, found Person "+p.getFirstName());
end
This examples produces :
initial, working, found Contact Bob
initial, working, found Contact Alice
initial, working, found Person Bob
initial, working, found Person Alice
new rule, working, found Contact Bob
new rule, working, found Contact Alice
new rule, working, found Person Bob
new rule, working, found Person Alice
But, if you comment out rule #2 in initial.drl, then the output becomes:
initial, working, found Contact Bob
initial, working, found Contact Alice
new rule, working, found Contact Bob
new rule, working, found Contact Alice
i.e. rule #4 isn't fired anymore...
Is this explainable?
Thanks
Francois
11 years, 7 months
Drools Session Persistence
by calcacuervo
Hey Guys.
I would like to persist a ksession in drools 6, but not every time I
interact with the session, as this would impact the performance and we have
lot of events and facts inserted per second.
Is this possible to manually persisting the session (for example, on
undeployment), and then, at the app startup load it?
Thanks!
Demian
--
View this message in context: http://drools.46999.n3.nabble.com/Drools-Session-Persistence-tp4030204.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
11 years, 7 months