Search in sources :

Example 81 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class MarshallingTest method testSerializeAdd2.

/*
     *  Here I am inserting data points which are not used by any rule (e.g Person).
     *  Later adding rule (e.g. Rule: 'match Person') for those data points.
     *
     *  Result: Pkg/Rule addition is failing with ClassCastException
     */
@Test
public void testSerializeAdd2() throws Exception {
    // Create a rulebase, a session, and test it
    Collection<KiePackage> kpkgs = loadKnowledgePackages("test_Dynamic1_0.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackages(kpkgs);
    List results = new ArrayList();
    KieSession session = kBase.newKieSession();
    session.setGlobal("results", results);
    InternalFactHandle stilton1 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
    session.insert(new Cheese("brie", 10));
    InternalFactHandle bob = (InternalFactHandle) session.insert(new Person("bob", 10));
    // fire rules
    session.fireAllRules();
    // check the results are correct
    assertEquals(1, results.size());
    assertEquals(stilton1.getObject(), results.get(0));
    // serialize session and rulebase
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dynamically add a new package
    kpkgs = loadKnowledgePackages("test_Dynamic3_0.drl");
    kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
    session.insert(new Cheese("brie", 20));
    InternalFactHandle mark = (InternalFactHandle) session.insert(new Person("mark", 20));
    session.fireAllRules();
    results = (List) session.getGlobal("results");
    assertEquals(4, results.size());
    assertEquals(stilton2.getObject(), results.get(1));
    assertEquals(bob.getObject(), results.get(2));
    assertEquals(mark.getObject(), results.get(3));
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dispose session
    session.dispose();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 82 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsModifySameRuleGivesDifferentLogicalInsertion.

@Test(timeout = 10000)
public void testLogicalInsertionsModifySameRuleGivesDifferentLogicalInsertion() throws Exception {
    // TODO JBRULES-1804
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsModifySameRuleGivesDifferentLogicalInsertion.drl", getClass()), ResourceType.DRL);
    Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
    kbase.addPackages(pkgs);
    KieSession session = createKnowledgeSession(kbase);
    try {
        Sensor sensor1 = new Sensor(100, 0);
        FactHandle sensor1Handle = session.insert(sensor1);
        Sensor sensor2 = new Sensor(200, 0);
        FactHandle sensor2Handle = session.insert(sensor2);
        Sensor sensor3 = new Sensor(200, 0);
        FactHandle sensor3Handle = session.insert(sensor3);
        session.fireAllRules();
        session = getSerialisedStatefulKnowledgeSession(session, true);
        List temperatureList = new ArrayList(session.getObjects(new ClassObjectFilter(Integer.class)));
        assertTrue(temperatureList.contains(Integer.valueOf(100)));
        assertTrue(temperatureList.contains(Integer.valueOf(200)));
        assertEquals(2, temperatureList.size());
        sensor1.setTemperature(150);
        sensor1Handle = getFactHandle(sensor1Handle, session);
        session.update(sensor1Handle, sensor1);
        session = getSerialisedStatefulKnowledgeSession(session, true);
        session.fireAllRules();
        temperatureList = new ArrayList(session.getObjects(new ClassObjectFilter(Integer.class)));
        assertFalse(temperatureList.contains(Integer.valueOf(100)));
        assertTrue(temperatureList.contains(Integer.valueOf(150)));
        assertTrue(temperatureList.contains(Integer.valueOf(200)));
        assertEquals(2, temperatureList.size());
    } finally {
        session.dispose();
    }
}
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) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Sensor(org.drools.mvel.compiler.Sensor) Test(org.junit.Test)

Example 83 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase 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);
    KieSession session = createKnowledgeSession(kbase);
    try {
        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.delete(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.delete(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());
    } finally {
        session.dispose();
    }
}
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) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 84 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class UnmarshallingTest method initializeKnowledgeBase.

private KieBase initializeKnowledgeBase(String rule) {
    // Setup knowledge base
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(rule)), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        throw new RuntimeException(kbuilder.getErrors().toString());
    }
    KieBaseConfiguration config = RuleBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(RuleBaseFactory.newRuleBase(config));
    knowledgeBase.addPackages(kbuilder.getKnowledgePackages());
    return knowledgeBase;
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringReader(java.io.StringReader) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 85 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class StreamsTest method testEventExpirationValue.

@Test(timeout = 10000)
public void testEventExpirationValue() {
    final String drl1 = "package org.drools.pkg1\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "rule X\n" + "when\n" + "    StockTick()\n" + "then\n" + "end\n";
    final String drl2 = "package org.drools.pkg2\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "rule X\n" + "when\n" + "    StockTick()\n" + "then\n" + "end\n";
    final InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromKieModuleFromDrl("stream-test", kieBaseTestConfiguration, drl1, drl2);
    final List<ObjectTypeNode> otns = kbase.getRete().getObjectTypeNodes();
    final ObjectType stot = new ClassObjectType(StockTick.class);
    for (final ObjectTypeNode otn : otns) {
        if (otn.getObjectType().isAssignableFrom(stot)) {
            assertEquals(NEVER_EXPIRES, otn.getExpirationOffset());
        }
    }
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)268 Test (org.junit.Test)213 KieSession (org.kie.api.runtime.KieSession)136 ArrayList (java.util.ArrayList)99 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)98 KiePackage (org.kie.api.definition.KiePackage)82 List (java.util.List)55 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)39 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)34 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)33 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 Cheese (org.drools.mvel.compiler.Cheese)24 FactType (org.kie.api.definition.type.FactType)23 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)22 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)21 BetaMemory (org.drools.core.reteoo.BetaMemory)18 PackageDescr (org.drools.drl.ast.descr.PackageDescr)18 Resource (org.kie.api.io.Resource)18 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)17