Search in sources :

Example 41 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class SessionInspectorTest method testGetSessionInfo.

@Test
public void testGetSessionInfo() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_SubNetworks.drl"), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_AccumulateWithFromChaining.drl"), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_CollectResultsBetaConstraint.drl"), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_QueryMemoryLeak.drl"), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.setGlobal("results", new ArrayList<Object>());
    ksession.insert(new Dimension(100, 50));
    ksession.insert(new Dimension(130, 80));
    ksession.insert(new Dimension(50, 40));
    ksession.insert(new Dimension(50, 40));
    Cheesery cheesery = new Cheesery();
    cheesery.addCheese(new Cheese("brie", 10));
    cheesery.addCheese(new Cheese("brie", 10));
    cheesery.addCheese(new Cheese("brie", 10));
    cheesery.addCheese(new Cheese("brie", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    ksession.insert(cheesery);
    ksession.insert(new Person("Bob", "muzzarella"));
    ksession.insert(new Person("Mark", "brie"));
    ksession.insert(new Cheese("brie", 10));
    ksession.insert(new Cheese("brie", 10));
    ksession.insert(new Cheese("brie", 10));
    ksession.insert(new Cheese("brie", 10));
    ksession.insert(new Cheese("muzzarella", 10));
    ksession.insert(new Cheese("muzzarella", 10));
    ksession.insert(new Cheese("muzzarella", 10));
    ksession.insert(new Cheese("muzzarella", 10));
    ksession.insert(new Cheese("Stilton", 10));
    ksession.insert(new Cheese("Stilton", 10));
    ksession.insert(new Cheese("Stilton", 10));
    ksession.insert(new Double(10));
    ksession.insert(new Double(11));
    ksession.insert(new Double(12));
    ksession.insert(new Double(13));
    ksession.insert(new Double(14));
    ksession.insert(new Integer(15));
    ksession.insert(new Integer(16));
    ksession.insert(new Integer(17));
    ksession.insert(new Integer(18));
    FactHandle handle = ksession.insert(new Worker());
    ksession.retract(handle);
    SessionInspector inspector = new SessionInspector(ksession);
    StatefulKnowledgeSessionInfo info = inspector.getSessionInfo();
    String report = SessionReporter.generateReport("simple", info, null);
    assertNotNull(report);
}
Also used : StatefulKnowledgeSessionInfo(org.drools.core.util.debug.StatefulKnowledgeSessionInfo) FactHandle(org.kie.api.runtime.rule.FactHandle) Cheese(org.drools.compiler.Cheese) Dimension(java.awt.Dimension) Cheesery(org.drools.compiler.Cheesery) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Worker(org.drools.compiler.Worker) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) SessionInspector(org.drools.core.util.debug.SessionInspector) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 42 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class AddRuleTest method buildKnowledgeBase.

private InternalKnowledgeBase buildKnowledgeBase(String ruleName, String rule) {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    int i = 0;
    str += "rule " + ruleName + "  when \n";
    str += rule;
    str += "then \n";
    str += " list.add( kcontext.getMatch() );\n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    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 43 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class AddRuleTest method buildKnowledgePackage.

private Collection<KiePackage> buildKnowledgePackage(String ruleName, String rule) {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    int i = 0;
    str += "rule " + ruleName + "  when \n";
    str += rule;
    str += "then \n";
    str += " list.add( kcontext.getMatch() );\n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    return kbuilder.getKnowledgePackages();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder)

Example 44 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class PhreakLiaNodeTest method test.

@Test
public void test() {
    String str = "package org.drools.compiler.test\n" + "\n" + "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "\n" + "rule r1 \n" + "    when \n" + "        $a : A( object == 1 )\n" + "    then \n" + "        System.out.println( $a ); \n" + "end \n" + "rule r2 \n" + "    when \n" + "        $a : A( object == 2 )\n" + "    then \n" + "        System.out.println( $a ); \n" + "end \n " + "rule r3 \n" + "    when \n" + "        $a : A( object == 2 )\n" + "        $b : B( )\n" + "    then \n" + "        System.out.println( $a ); \n" + "end \n " + "rule r4 \n" + "    when \n" + "        $a : A( object == 3 )\n" + "    then \n" + "        System.out.println( $a ); \n" + "end \n";
    KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (builder.hasErrors()) {
        throw new RuntimeException(builder.getErrors().toString());
    }
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(builder.getKnowledgePackages());
    KieSession ksession = knowledgeBase.newKieSession();
    InternalFactHandle fhB = (InternalFactHandle) ksession.insert(B.b(1));
    InternalFactHandle fhA = (InternalFactHandle) ksession.insert(A.a(1));
    ksession.fireAllRules();
    System.out.println("---1---");
    // ksession.update( fhA, a(1) );
    // ksession.fireAllRules();
    // System.out.println( "---2---" );
    // 
    ksession.update(fhA, A.a(2));
    ksession.fireAllRules();
    System.out.println("---3---");
    ksession.update(fhA, A.a(2));
    ksession.fireAllRules();
    System.out.println("---4---");
    ksession.update(fhA, A.a(3));
    ksession.fireAllRules();
    System.out.println("---5---");
    ksession.update(fhB, B.b(1));
    ksession.update(fhA, A.a(3));
    ksession.fireAllRules();
    // ksession.update( fhA, a(1) );
    // ksession.fireAllRules();
    // 
    // ksession.update( fhA, a(1) );
    // ksession.fireAllRules();
    ksession.dispose();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 45 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class RemoveRuleTest method buildKnowledgeBase.

private InternalKnowledgeBase buildKnowledgeBase(String ruleName, String rule) {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    int i = 0;
    str += "rule " + ruleName + "  when \n";
    str += rule;
    str += "then \n";
    str += " list.add( kcontext.getMatch() );\n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return kbase;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Aggregations

KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)376 Test (org.junit.Test)302 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)231 KieSession (org.kie.api.runtime.KieSession)149 ArrayList (java.util.ArrayList)112 List (java.util.List)65 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)52 KiePackage (org.kie.api.definition.KiePackage)39 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)38 FactHandle (org.kie.api.runtime.rule.FactHandle)36 FactType (org.kie.api.definition.type.FactType)27 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)24 Person (org.drools.compiler.Person)23 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)23 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)19 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)17 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)17 StringReader (java.io.StringReader)15 Resource (org.kie.api.io.Resource)15