use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class TruthMaintenanceTest method testTMSWithLateUpdate.
@Test(timeout = 10000)
public void testTMSWithLateUpdate() {
// JBRULES-3416
String str = "" + "package org.drools.compiler.test;\n" + "\n" + "import org.drools.compiler.Father;\n" + "import org.drools.compiler.YoungestFather;\n" + "\n" + "rule \"findMarriedCouple\"\n" + "when\n" + " $h: Father()\n" + " not Father(father == $h)\n" + "then\n" + " insertLogical(new YoungestFather($h));\n" + "end";
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession kSession = createKnowledgeSession(kbase);
Father abraham = new Father("abraham");
Father bart = new Father("bart");
Collection<? extends Object> youngestFathers;
bart.setFather(abraham);
FactHandle abrahamHandle = kSession.insert(abraham);
FactHandle bartHandle = kSession.insert(bart);
kSession.fireAllRules();
youngestFathers = kSession.getObjects(new ClassObjectFilter(YoungestFather.class));
assertEquals(1, youngestFathers.size());
assertEquals(bart, ((YoungestFather) youngestFathers.iterator().next()).getMan());
Father homer = new Father("homer");
FactHandle homerHandle = kSession.insert(homer);
homer.setFather(abraham);
// If we do kSession.update(homerHandle, homer) here instead of after bart.setFather(homer) it works
// But in some use cases we cannot do this because fact fields are actually called
// while the facts are in an invalid temporary state
bart.setFather(homer);
// Late update call for homer, after bart has been changed too, but before fireAllRules
kSession.update(homerHandle, homer);
kSession.update(bartHandle, bart);
kSession.fireAllRules();
youngestFathers = kSession.getObjects(new ClassObjectFilter(YoungestFather.class));
assertEquals(1, youngestFathers.size());
assertEquals(bart, ((YoungestFather) youngestFathers.iterator().next()).getMan());
// System.err.println(reportWMObjects(kSession));
}
use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class StatefulSessionTest method testIterateObjects.
@Test
public void testIterateObjects() throws Exception {
final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_IterateObjects.drl"));
final KieSession ksession = createKnowledgeSession(kbase);
final List results = new ArrayList();
ksession.setGlobal("results", results);
ksession.insert(new Cheese("stilton", 10));
ksession.fireAllRules();
final Iterator events = ksession.getObjects(new ClassObjectFilter(PersonInterface.class)).iterator();
assertTrue(events.hasNext());
assertEquals(1, results.size());
assertEquals(results.get(0), events.next());
}
use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class ExpirationTest method testSoftExpirationWithDeclaration.
@Test
public void testSoftExpirationWithDeclaration() {
// DROOLS-1483
String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "import " + C.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(10s) end\n" + "declare B @role( event ) @expires(value = 30s, policy = TIME_SOFT) end\n" + "declare C @role( event ) @expires(value = 30s, policy = TIME_SOFT) end\n" + "rule Ra when\n" + " $e : A() over window:time(20s)\n" + "then end\n " + "rule Rb when\n" + " $e : B() over window:time(20s)\n" + "then end\n " + "rule Rc when\n" + " $e : C()\n" + "then end\n";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler clock = ksession.getSessionClock();
ksession.insert(new A(1));
ksession.insert(new B(2));
ksession.insert(new C(3));
ksession.fireAllRules();
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, ksession.getObjects(new ClassObjectFilter(A.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(B.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(C.class)).size());
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
// t=15 -> hard expiration of A
assertEquals(0, ksession.getObjects(new ClassObjectFilter(A.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(B.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(C.class)).size());
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
// t=25 -> implicit expiration of B
assertEquals(0, ksession.getObjects(new ClassObjectFilter(A.class)).size());
assertEquals(0, ksession.getObjects(new ClassObjectFilter(B.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(C.class)).size());
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
// t=35 -> soft expiration of C
assertEquals(0, ksession.getObjects(new ClassObjectFilter(A.class)).size());
assertEquals(0, ksession.getObjects(new ClassObjectFilter(B.class)).size());
assertEquals(0, ksession.getObjects(new ClassObjectFilter(C.class)).size());
}
use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class ExpirationTest method testSoftExpiration.
@Test
public void testSoftExpiration() {
// DROOLS-1483
String drl = "import " + ExpiringEventA.class.getCanonicalName() + "\n" + "import " + ExpiringEventB.class.getCanonicalName() + "\n" + "import " + ExpiringEventC.class.getCanonicalName() + "\n" + "rule Ra when\n" + " $e : ExpiringEventA() over window:time(20s)\n" + "then end\n " + "rule Rb when\n" + " $e : ExpiringEventB() over window:time(20s)\n" + "then end\n " + "rule Rc when\n" + " $e : ExpiringEventC()\n" + "then end\n";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler clock = ksession.getSessionClock();
ksession.insert(new ExpiringEventA());
ksession.insert(new ExpiringEventB());
ksession.insert(new ExpiringEventC());
ksession.fireAllRules();
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventA.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventB.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventC.class)).size());
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
// t=15 -> hard expiration of A
assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventA.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventB.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventC.class)).size());
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
// t=25 -> implicit expiration of B
assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventA.class)).size());
assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventB.class)).size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventC.class)).size());
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
// t=35 -> soft expiration of C
assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventA.class)).size());
assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventB.class)).size());
assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventC.class)).size());
}
use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class TraitMapCoreTest method donCustomMapTest.
@Test()
public void donCustomMapTest() {
String source = "package org.drools.traits.test; \n" + "import java.util.*\n;" + "import " + TraitMapCoreTest.DomainMap.class.getCanonicalName() + ";\n" + "" + "global List list; \n" + "" + "" + "declare trait PersonMap" + "@propertyReactive \n" + " name : String \n" + " age : int \n" + " height : Double \n" + "end\n" + "" + "" + "rule Don \n" + "when \n" + " $m : Map( this[ \"age\"] == 18 ) " + "then \n" + " don( $m, PersonMap.class );\n" + "end \n" + "" + "rule Log \n" + "when \n" + " $p : PersonMap( name == \"john\", age > 10 ) \n" + "then \n" + " modify ( $p ) { \n" + " setHeight( 184.0 ); \n" + " }" + "end \n";
KieSession ksession = loadKnowledgeBaseFromString(source).newKieSession();
TraitFactory.setMode(VirtualPropertyMode.MAP, ksession.getKieBase());
List list = new ArrayList();
ksession.setGlobal("list", list);
HashMap map = new DomainMap();
map.put("name", "john");
map.put("age", 18);
ksession.insert(map);
ksession.fireAllRules();
assertTrue(map.containsKey("height"));
assertEquals(map.get("height"), 184.0);
assertEquals(2, ksession.getObjects().size());
assertEquals(1, ksession.getObjects(new ClassObjectFilter(DomainMap.class)).size());
}
Aggregations