Search in sources :

Example 36 with ClassObjectFilter

use of org.drools.core.ClassObjectFilter in project drools by kiegroup.

the class Misc2Test method testPhreakTMS.

@Test
public void testPhreakTMS() {
    // DROOLS-7
    String str = "import org.drools.compiler.Person\n" + "import org.drools.compiler.Cheese\n" + "rule R when\n" + "  Person( $age : age, $name : name == \"A\" )\n" + "  not Person( age == $age + 1 )\n" + "then\n" + "  insertLogical(new Cheese(\"gorgonzola\", 10));\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();
    Person p1 = new Person("A", 31);
    FactHandle fh1 = ksession.insert(p1);
    ksession.fireAllRules();
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(Cheese.class)).size());
    Person p2 = new Person("A", 32);
    FactHandle fh2 = ksession.insert(p2);
    ksession.fireAllRules();
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(Cheese.class)).size());
}
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) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 37 with ClassObjectFilter

use of org.drools.core.ClassObjectFilter in project drools by kiegroup.

the class TruthMaintenanceTest method testStatedDeleteLogicalAssertionFromRule.

@Test
public void testStatedDeleteLogicalAssertionFromRule() {
    // BZ-1317026
    String drl = "global java.util.List list;\n" + "rule R1 when\n" + "then\n" + "    insertLogical( \"test\" ); \n" + "end\n" + "rule R2 when\n" + "    $s : String()\n" + "then\n" + "    list.add( $s ); \n" + "    delete( $s, org.kie.api.runtime.rule.FactHandle.State.STATED ); \n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("test", list.get(0));
    Collection<FactHandle> fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
    assertEquals(1, fhs.size());
}
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) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 38 with ClassObjectFilter

use of org.drools.core.ClassObjectFilter in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsWithModify.

@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsWithModify() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsWithUpdate.drl");
    KieSession ksession = kbase.newKieSession();
    final Person p = new Person("person");
    p.setAge(2);
    FactHandle h = ksession.insert(p);
    assertEquals(1, ksession.getObjects().size());
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
    assertEquals(2, ksession.getObjects().size());
    Collection l = ksession.getObjects(new ClassObjectFilter(CheeseEqual.class));
    assertEquals(1, l.size());
    assertEquals(2, ((CheeseEqual) l.iterator().next()).getPrice());
    h = getFactHandle(h, ksession);
    ksession.retract(h);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
    assertEquals(0, ksession.getObjects().size());
    TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
    final java.lang.reflect.Field field = tms.getClass().getDeclaredField("equalityKeyMap");
    field.setAccessible(true);
    final ObjectHashMap m = (ObjectHashMap) field.get(tms);
    field.setAccessible(false);
    assertEquals("assertMap should be empty", 0, m.size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectHashMap(org.drools.core.util.ObjectHashMap) CheeseEqual(org.drools.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 39 with ClassObjectFilter

use of org.drools.core.ClassObjectFilter in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsBacking.

@Test(timeout = 10000)
public void testLogicalInsertionsBacking() throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsBacking.drl", getClass()), ResourceType.DRL);
    Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    KieSession session = createKnowledgeSession(kbase);
    final Cheese cheese1 = new Cheese("c", 1);
    final Cheese cheese2 = new Cheese(cheese1.getType(), 1);
    FactHandle h1 = session.insert(cheese1);
    session.fireAllRules();
    session = getSerialisedStatefulKnowledgeSession(session, true);
    Collection<?> list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
    assertEquals(1, list.size());
    // probably dangerous, as contains works with equals, not identity
    assertEquals(cheese1.getType(), list.iterator().next());
    FactHandle h2 = session.insert(cheese2);
    session.fireAllRules();
    session = getSerialisedStatefulKnowledgeSession(session, true);
    list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
    assertEquals(1, list.size());
    assertEquals(cheese1.getType(), list.iterator().next());
    assertEquals(3, session.getObjects().size());
    h1 = getFactHandle(h1, session);
    session.retract(h1);
    session = getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    session = getSerialisedStatefulKnowledgeSession(session, true);
    list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
    assertEquals("cheese-type " + cheese1.getType() + " was deleted, but should not. Backed by cheese2 => type.", 1, list.size());
    assertEquals("cheese-type " + cheese1.getType() + " was deleted, but should not. Backed by cheese2 => type.", cheese1.getType(), list.iterator().next());
    h2 = getFactHandle(h2, session);
    session.retract(h2);
    session = getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    session = getSerialisedStatefulKnowledgeSession(session, true);
    list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
    assertEquals("cheese-type " + cheese1.getType() + " was not deleted, but should have. Neither  cheese1 => type nor cheese2 => type is true.", 0, list.size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ClassObjectFilter(org.drools.core.ClassObjectFilter) KiePackage(org.kie.api.definition.KiePackage) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 40 with ClassObjectFilter

use of org.drools.core.ClassObjectFilter in project drools by kiegroup.

the class TruthMaintenanceTest method testDeleteLogicalAssertion.

@Test
public void testDeleteLogicalAssertion() {
    // BZ-1317026
    String drl = "rule R1 when\n" + "then\n" + "    insertLogical( \"test\" ); \n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    ksession.fireAllRules();
    Collection<FactHandle> fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
    assertEquals(1, fhs.size());
    for (FactHandle fh : fhs) {
        ksession.delete(fh);
    }
    fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
    assertEquals(0, fhs.size());
}
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) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

ClassObjectFilter (org.drools.core.ClassObjectFilter)55 KieSession (org.kie.api.runtime.KieSession)44 Test (org.junit.Test)40 FactHandle (org.kie.api.runtime.rule.FactHandle)29 InternalFactHandle (org.drools.core.common.InternalFactHandle)24 KieBase (org.kie.api.KieBase)19 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)17 ArrayList (java.util.ArrayList)15 List (java.util.List)14 Collection (java.util.Collection)12 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)11 Person (org.drools.compiler.Person)8 KiePackage (org.kie.api.definition.KiePackage)8 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)8 HashMap (java.util.HashMap)7 KieHelper (org.kie.internal.utils.KieHelper)6 Cheese (org.drools.compiler.Cheese)5 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 RegistryContext (org.drools.core.command.impl.RegistryContext)4 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)4