Search in sources :

Example 11 with ClassObjectFilter

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));
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Father(org.drools.compiler.Father) YoungestFather(org.drools.compiler.YoungestFather) Test(org.junit.Test)

Example 12 with ClassObjectFilter

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());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 13 with ClassObjectFilter

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());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 14 with ClassObjectFilter

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());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 15 with ClassObjectFilter

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());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

ClassObjectFilter (org.drools.core.ClassObjectFilter)25 Test (org.junit.Test)25 KieSession (org.kie.api.runtime.KieSession)24 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)15 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 FactHandle (org.kie.api.runtime.rule.FactHandle)15 KieBase (org.kie.api.KieBase)11 ArrayList (java.util.ArrayList)9 List (java.util.List)8 Person (org.drools.compiler.Person)8 KieHelper (org.kie.internal.utils.KieHelper)6 Cheese (org.drools.compiler.Cheese)5 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 KiePackage (org.kie.api.definition.KiePackage)5 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)5 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 CheeseEqual (org.drools.compiler.CheeseEqual)3 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)3 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)2