Search in sources :

Example 46 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();
    try {
        final Person p = new Person("person");
        p.setAge(2);
        FactHandle h = ksession.insert(p);
        assertEquals(1, ksession.getObjects().size());
        ksession.fireAllRules();
        ksession = 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.delete(h);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, false);
        assertEquals(0, ksession.getObjects().size());
        TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) ksession);
        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());
    } finally {
        ksession.dispose();
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) CheeseEqual(org.drools.mvel.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) ObjectHashMap(org.drools.core.util.ObjectHashMap) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 47 with ClassObjectFilter

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

the class DefeasibilityTest method testManyDefeasibles.

@Test(timeout = 10000)
public void testManyDefeasibles() {
    String drl = "package org.drools.defeasible; " + "declare Fact " + "     fact: String @key " + "end " + " " + "rule init " + "     when " + "     then " + "         insert( new Fact( 'one' ) ); " + "         insert( new Fact( 'two' ) ); " + "         insert( new Fact( 'two' ) ); " + "end " + " " + "rule rule1 " + "     @Defeasible " + "     enabled true " + "     when " + "         Fact( \"one\"; ) " + "     then " + "         System.out.println(\"one causes wibble\"); " + "         insertLogical( new Fact( \"wibble\") ); " + "end " + " " + "rule rule2 " + "     @Defeasible " + "     when " + "         Fact( \"two\"; ) " + "     then " + "         System.out.println(\"two causes wibble\"); " + "         insertLogical( new Fact( \"wibble\") ); " + "end " + " " + "rule rule3 " + "     @Defeater " + "     @Defeats( \"rule2\" ) " + "     when " + "         Fact( \"two\"; ) " + "     then " + "         System.out.println(\"two negates wibble\"); " + "         insertLogical( new Fact( \"wibble\"), \"neg\" ); " + "end";
    KieSession session = getSessionFromString(drl);
    session.fireAllRules();
    FactType factType = session.getKieBase().getFactType("org.drools.defeasible", "Fact");
    for (Object o : session.getObjects(new ClassObjectFilter(factType.getFactClass()))) {
        if ("wibble".equals(factType.get(o, "fact"))) {
            InternalFactHandle handle = (InternalFactHandle) session.getFactHandle(o);
            DefeasibleBeliefSet dbs = (DefeasibleBeliefSet) ((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).getBeliefSet();
            assertEquals(3, dbs.size());
            assertTrue(dbs.isConflicting());
        }
    }
}
Also used : DefeasibleBeliefSet(org.drools.tms.beliefsystem.defeasible.DefeasibleBeliefSet) ClassObjectFilter(org.drools.core.ClassObjectFilter) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 48 with ClassObjectFilter

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

the class IncrementalCompilationTest method testRemoveRulesWithAccumulateAndLogicalAssertions.

@Test
public void testRemoveRulesWithAccumulateAndLogicalAssertions() {
    // DROOLS-3554
    final String DRL1 = "rule R1 when\n" + "    accumulate(String($l : length > 1); $s : sum($l))\n" + "then\n" + "    insertLogical($s);\n" + "end";
    KieServices ks = KieServices.get();
    final ReleaseId id = ks.newReleaseId("org.test", "logical", "1.0.0");
    KieModuleModel model = ks.newKieModuleModel();
    model.newKieBaseModel("kbase").newKieSessionModel("ksession").setDefault(true);
    String kproj = model.toXML();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.generateAndWritePomXML(id);
    kfs.writeKModuleXML(kproj);
    kfs.write(ks.getResources().newReaderResource(new StringReader(DRL1)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    kieBuilder.buildAll(DrlProject.class);
    KieModule kieModule = kieBuilder.getKieModule();
    KieContainer kieContainer = ks.newKieContainer(kieModule.getReleaseId());
    KieSession kieSession = kieContainer.newKieSession();
    kieSession.insert("test");
    kieSession.insert("test2");
    assertEquals(2, kieSession.getObjects().size());
    kieSession.fireAllRules();
    assertEquals(3, kieSession.getObjects().size());
    assertEquals(9, kieSession.getObjects(new ClassObjectFilter(Integer.class)).iterator().next());
    ReleaseId id2 = ks.newReleaseId("org.test", "logical", "2.0.0");
    KieFileSystem kfs2 = ks.newKieFileSystem();
    KieBuilder kieBuilder2 = ks.newKieBuilder(kfs2);
    kfs2.generateAndWritePomXML(id2);
    kfs2.writeKModuleXML(kproj);
    kieBuilder = ks.newKieBuilder(kfs2);
    kieBuilder.buildAll(DrlProject.class);
    kieModule = kieBuilder.getKieModule();
    kieContainer.updateToVersion(id2);
    kieSession.fireAllRules();
    assertEquals(2, kieSession.getObjects().size());
    assertTrue(kieSession.getObjects(new ClassObjectFilter(Integer.class)).isEmpty());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieFileSystem(org.kie.api.builder.KieFileSystem) ClassObjectFilter(org.drools.core.ClassObjectFilter) KieModuleModel(org.kie.api.builder.model.KieModuleModel) StringReader(java.io.StringReader) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 49 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 " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\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 = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, 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.mvel.compiler.Cheese) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 50 with ClassObjectFilter

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

the class StatefulSessionTest method testIterateObjects.

@Test
public void testIterateObjects() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_IterateObjects.drl");
    KieSession ksession = kbase.newKieSession();
    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.mvel.compiler.Cheese) 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