Search in sources :

Example 71 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class OutOfMemoryTest method testMemoryLeak.

@Test
@Ignore
public void testMemoryLeak() {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_MemoryLeak.drl", OutOfMemoryTest.class), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSession ksession = createKnowledgeSession(kbase);
    final int pcount = 5;
    Person[] persons = new Person[pcount];
    FactHandle[] pHandles = new FactHandle[pcount];
    for (int i = 0; i < persons.length; i++) {
        persons[i] = new Person("person-0-" + i);
        pHandles[i] = ksession.insert(persons[i]);
    }
    Cheese[] cheeses = new Cheese[pcount];
    FactHandle[] cHandles = new FactHandle[pcount];
    for (int i = 0; i < cheeses.length; i++) {
        cheeses[i] = new Cheese("cheese-0-" + i);
        cHandles[i] = ksession.insert(cheeses[i]);
    }
    ksession.fireAllRules();
    for (int j = 1; j <= 5; j++) {
        for (int i = 0; i < pcount; i++) {
            cheeses[i].setType("cheese-" + j + "-" + i);
            ksession.update(cHandles[i], cheeses[i]);
            persons[i].setName("person-" + j + "-" + i);
            ksession.update(pHandles[i], persons[i]);
        }
        ksession.fireAllRules();
        logger.info("DONE");
    }
    SessionInspector inspector = new SessionInspector(ksession);
    SessionReporter.addNamedTemplate("dump_tuples", getClass().getResourceAsStream("/org/drools/core/util/debug/dump_tuples.mvel"));
    String report = SessionReporter.generateReport("dump_tuples", inspector.getSessionInfo(), new HashMap<String, Object>());
    try {
        FileWriter out = new FileWriter("tupleDump.txt");
        out.write(report);
        out.close();
        logger.info(report);
    } catch (Exception e) {
        e.printStackTrace();
    }
// logicals = getLogicallyInserted( ksession );
// assertEquals( pcount, logicals.size() );
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) FileWriter(java.io.FileWriter) Cheese(org.drools.compiler.Cheese) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) SessionInspector(org.drools.core.util.debug.SessionInspector) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 72 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class HelloWorldTest method readKnowledgeBase.

private KieBase readKnowledgeBase() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl", HelloWorldTest.class), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return kbase;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 73 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class ExtendsTest method testMultipleInheritanceWithPosition6.

@Test
public void testMultipleInheritanceWithPosition6() throws Exception {
    // DROOLS-249
    String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait SecPosTrait\n" + "@propertyReactive\n" + " field3 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait, SecPosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 //@position(0)\n" + " mfield1 : int = 201 //@position(2)\n" + "end";
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
    }
    assertFalse(kBuilder.hasErrors());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
    FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
    assertEquals(6, sw.getFields().size());
    assertEquals("field0", sw.getFields().get(0).getName());
    assertEquals("field1", sw.getFields().get(1).getName());
    assertEquals("field2", sw.getFields().get(2).getName());
    assertEquals("field3", sw.getFields().get(3).getName());
    assertEquals("mfield0", sw.getFields().get(4).getName());
    assertEquals("mfield1", sw.getFields().get(5).getName());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 74 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class ExtendsTest method testMultipleInheritanceWithPosition2.

@Test
public void testMultipleInheritanceWithPosition2() throws Exception {
    // DROOLS-249
    String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end";
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
    }
    assertFalse(kBuilder.hasErrors());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
    FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
    assertEquals(5, sw.getFields().size());
    assertEquals("mfield0", sw.getFields().get(0).getName());
    assertEquals("field0", sw.getFields().get(1).getName());
    assertEquals("mfield1", sw.getFields().get(2).getName());
    assertEquals("field1", sw.getFields().get(3).getName());
    assertEquals("field2", sw.getFields().get(4).getName());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 75 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class ExtendsTest method testDeclareInheritance.

@Test
public void testDeclareInheritance() throws Exception {
    String s1 = "package org.drools;\n" + "import org.drools.compiler.integrationtests.ExtendsTest.*;\n" + "\n" + "declare A \n" + " @role( event )" + " @typesafe( false )\n" + "end\n" + "" + "declare C @role( event ) @typesafe( false ) end \n" + "" + "rule R \n" + "when " + "   $x : C( this.x == 1 ) \n" + "then\n" + "   System.out.println( $x ); \n" + "end\n" + "";
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
    }
    assertFalse(kBuilder.hasErrors());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
    KieSession knowledgeSession = knowledgeBase.newKieSession();
    FactHandle h = knowledgeSession.insert(new X());
    assertTrue(((InternalFactHandle) h).isEvent());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)394 Test (org.junit.Test)321 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)231 KieSession (org.kie.api.runtime.KieSession)221 ArrayList (java.util.ArrayList)173 List (java.util.List)108 KiePackage (org.kie.api.definition.KiePackage)80 FactHandle (org.kie.api.runtime.rule.FactHandle)46 InternalFactHandle (org.drools.core.common.InternalFactHandle)43 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)42 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)41 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)41 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)40 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)39 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)38 Person (org.drools.compiler.Person)37 Cheese (org.drools.compiler.Cheese)36 FactType (org.kie.api.definition.type.FactType)27 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)25 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)24