Search in sources :

Example 96 with KiePackage

use of org.kie.api.definition.KiePackage 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 97 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class ImportsTest method testImportColision.

@Test
public void testImportColision() throws Exception {
    final Collection<KiePackage> kpkgs1 = loadKnowledgePackages("nested1.drl");
    final Collection<KiePackage> kpkgs2 = loadKnowledgePackages("nested2.drl");
    final InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
    kbase.addPackages(kpkgs1);
    kbase.addPackages(kpkgs2);
    final KieSession ksession = createKnowledgeSession(kbase);
    SerializationHelper.serializeObject(kbase);
    ksession.insert(new FirstClass());
    ksession.insert(new SecondClass());
    ksession.insert(new FirstClass.AlternativeKey());
    ksession.insert(new SecondClass.AlternativeKey());
    ksession.fireAllRules();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) FirstClass(org.drools.compiler.FirstClass) SecondClass(org.drools.compiler.SecondClass) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 98 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class SerializedPackageMergeTwoSteps1Test method testBuildAndSerializePackagesInTwoSteps1.

@Test
public void testBuildAndSerializePackagesInTwoSteps1() throws IOException, ClassNotFoundException {
    String str1 = "package com.sample.packageA\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R1 when\n" + "  $p : Person( name == \"John\" )\n" + "then\n" + "  list.add($p);" + "end\n";
    String str2 = "package com.sample.packageB\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R2 when\n" + "  $p : Person()\n" + "then\n" + "  list.add($p);" + "end\n";
    // Create 2 knowledgePackages separately
    KnowledgeBuilder builder1 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder1.add(ResourceFactory.newByteArrayResource(str1.getBytes()), ResourceType.DRL);
    Collection<KiePackage> knowledgePackages1 = builder1.getKnowledgePackages();
    // serialize the first package to a file
    writeKnowledgePackage(knowledgePackages1, BINPKG[0]);
    KnowledgeBuilder builder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder2.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL);
    Collection<KiePackage> knowledgePackages2 = builder2.getKnowledgePackages();
    // serialize the second package to a file
    writeKnowledgePackage(knowledgePackages2, BINPKG[1]);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) Test(org.junit.Test)

Example 99 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class SerializedPackageMergeTest method getSession.

private StatelessKieSession getSession(boolean serialize) throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    for (String drl : DRLs) {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream(drl)), ResourceType.DRL);
        assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
        Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
        Collection<KiePackage> newCollection = null;
        if (serialize) {
            newCollection = new ArrayList<KiePackage>();
            for (KiePackage kpkg : kpkgs) {
                kpkg = SerializationHelper.serializeObject(kpkg);
                newCollection.add(kpkg);
            }
        } else {
            newCollection = kpkgs;
        }
        kbase.addPackages(newCollection);
    }
    return kbase.newStatelessKieSession();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 100 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class SerializedPackageMergeTwoSteps2Test method _deserializeFromBytes.

private Collection<KiePackage> _deserializeFromBytes(byte[] byteCode) {
    Collection<KiePackage> ret = null;
    ByteArrayInputStream bis = null;
    ObjectInput in = null;
    try {
        bis = new ByteArrayInputStream(byteCode);
        in = new DroolsObjectInputStream(bis);
        ret = (Collection<KiePackage>) in.readObject();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (bis != null)
                bis.close();
            if (in != null)
                in.close();
        } catch (IOException e) {
        }
    }
    return ret;
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) KiePackage(org.kie.api.definition.KiePackage) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Aggregations

KiePackage (org.kie.api.definition.KiePackage)124 Test (org.junit.Test)93 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)80 KieSession (org.kie.api.runtime.KieSession)70 ArrayList (java.util.ArrayList)44 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)39 List (java.util.List)25 Cheese (org.drools.compiler.Cheese)22 Person (org.drools.compiler.Person)18 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)17 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)11 EntryPoint (org.kie.api.runtime.rule.EntryPoint)10 Resource (org.kie.api.io.Resource)9 StringReader (java.io.StringReader)8 HashMap (java.util.HashMap)8 FactHandle (org.kie.api.runtime.rule.FactHandle)8 Map (java.util.Map)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 KieBase (org.kie.api.KieBase)7