Search in sources :

Example 41 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class PerformanceExample method main.

public static void main(final String[] args) throws Exception {
    final long numberOfRulesToBuild = 10;
    boolean useAccumulate = true;
    // noticed performance difference between java and mvel dialects
    String dialect = "mvel";
    boolean usekjars = false;
    boolean collectionBasedRules = true;
    System.out.println("********* Numbers of rules: " + numberOfRulesToBuild + " kjars: " + usekjars + " accumulate: " + useAccumulate + " dialect: " + dialect + " *********");
    String rules = getRules(numberOfRulesToBuild, useAccumulate, dialect, collectionBasedRules);
    // System.out.println(rules);
    long startTime = System.currentTimeMillis();
    StatelessKieSession kSession;
    FactType ft;
    if (usekjars) {
        KieContainer kContainer = loadContainerFromString(rules);
        kSession = kContainer.newStatelessKieSession();
        ft = kContainer.getKieBase().getFactType("org.drools.examples.performance", "TransactionC");
    } else {
        /* Alternative way to load knowledge base without using kjars.
            Found slowness issue with internalInvalidateSegmentPrototype() when number of rules are increased.*/
        KieBase kbase = loadKnowledgeBaseFromString(rules);
        kSession = kbase.newStatelessKieSession();
        ft = kbase.getFactType("org.drools.examples.performance", "TransactionC");
    }
    long endTime = System.currentTimeMillis();
    System.out.println("Total time to build and load knowledgebase: " + (endTime - startTime) + " ms");
    ArrayList output = new ArrayList();
    kSession.setGlobal("mo", output);
    Object o = ft.newInstance();
    Gson gConverter = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
    Object fo = gConverter.fromJson(getFact(), o.getClass());
    // initial execute
    kSession.execute(fo);
    startTime = System.currentTimeMillis();
    kSession.execute(fo);
    endTime = System.currentTimeMillis();
    System.out.println("Execution time: " + (endTime - startTime) + " ms");
    String rulesOutput = gConverter.toJson(output);
    System.out.println(rulesOutput);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Gson(com.google.gson.Gson) FactType(org.kie.api.definition.type.FactType) KieContainer(org.kie.api.runtime.KieContainer)

Example 42 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class SequentialTest method testSequentialPlusPhreakOperationComplex.

@Test
public void testSequentialPlusPhreakOperationComplex() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test\n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "global  " + List.class.getCanonicalName() + " list\n";
    // Focus is done as g1, g2, g1 to demonstrate that groups will not re-activate
    str += "rule r0 when\n";
    str += "then\n";
    str += "    drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g1' ).setFocus();\n";
    str += "    drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g2' ).setFocus();\n";
    str += "    drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g1' ).setFocus();\n";
    str += "end\n";
    str += "rule r1 agenda-group 'g1' when\n";
    str += "    a : A( object > 0 )\n";
    str += "then\n";
    str += "    list.add( drools.getRule().getName() );\n";
    str += "    modify(a) { setObject( 3 ) };\n";
    str += "end\n";
    // r1_x is here to show they do not react when g2.r9 changes A o=2,
    // i.e. checking that re-activating g1 won't cause it to pick up previous non evaluated rules.
    // this is mostly checking that the no linking doesn't interfere with the expected results.
    str += "rule r1_x agenda-group 'g1' when\n";
    str += "    a : A( object == 2 )\n";
    str += "then\n";
    str += "    list.add( drools.getRule().getName() );\n";
    str += "end\n";
    // r1_y is here to show it does not react when A is changed to o=5 in r3
    str += "rule r1_y agenda-group 'g1' when\n";
    str += "    a : A( object == 5 )\n";
    str += "then\n";
    str += "    list.add( drools.getRule().getName() );\n";
    str += "end\n";
    str += "rule r2 agenda-group 'g1' when\n";
    str += "    a : A( object < 3 )\n";
    str += "then\n";
    str += "    list.add( drools.getRule().getName() );\n";
    str += "end\n";
    str += "rule r3 agenda-group 'g1' when\n";
    str += "    a : A(object >= 3  )\n";
    str += "then\n";
    str += "    modify(a) { setObject( 5 ) };\n";
    str += "    list.add( drools.getRule().getName() );\n";
    str += "end\n";
    // Checks that itself, f3 and r1_y do not react as they are higher up
    str += "rule r4 agenda-group 'g1' when\n";
    str += "    a : A(object >= 3  )\n";
    str += "then\n";
    str += "    modify(a) { setObject( 5 ) };\n";
    str += "    list.add( drools.getRule().getName() );\n";
    str += "end\n";
    // Checks that while this at one point matches, it does not match by the time g2 is entered
    // nor does it react when r9 changes a o=2
    str += "rule r6 agenda-group 'g2' when\n";
    str += "    a : A(object < 5  )\n";
    str += "then\n";
    str += "    list.add( drools.getRule().getName() );\n";
    str += "end\n";
    str += "rule r7 agenda-group 'g2' when\n";
    str += "    a : A(object >= 3  )\n";
    str += "then\n";
    str += "    list.add( drools.getRule().getName() );\n";
    str += "end\n";
    str += "rule r8 agenda-group 'g2' when\n";
    str += "    a : A(object >= 5  )\n";
    str += "then\n";
    str += "    list.add( drools.getRule().getName() );\n";
    str += "end\n";
    // This changes A o=2 to check if g1.r1_x incorrect reacts when g1 is re-entered
    str += "rule r9 agenda-group 'g2' when\n";
    str += "    a : A(object >= 5  )\n";
    str += "then\n";
    str += "    modify(a) { setObject( 2 ) };\n";
    str += "    list.add( drools.getRule().getName() );\n";
    str += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.execute(CommandFactory.newInsertElements(Arrays.asList(new Object[] { new A(1) })));
    assertEquals(6, list.size());
    assertEquals("r1", list.get(0));
    assertEquals("r3", list.get(1));
    assertEquals("r4", list.get(2));
    assertEquals("r7", list.get(3));
    assertEquals("r8", list.get(4));
    assertEquals("r9", list.get(5));
}
Also used : A(org.drools.compiler.phreak.A) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ArrayList(java.util.ArrayList) List(java.util.List) DynamicRulesTest(org.drools.compiler.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 43 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class SequentialTest method testSequentialWithRulebaseUpdate.

// JBRULES-1567 - ArrayIndexOutOfBoundsException in sequential execution after calling RuleBase.addPackage(..)
@Test
public void testSequentialWithRulebaseUpdate() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase(kconf, "simpleSalience.drl");
    StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.execute(new Person("pob"));
    kbase.addPackages(loadKnowledgePackagesFromString(new String(IoUtils.readBytesFromInputStream(DynamicRulesTest.class.getResource("test_Dynamic3.drl").openStream()))));
    ksession = kbase.newStatelessKieSession();
    ksession.setGlobal("list", list);
    Person person = new Person("bop");
    ksession.execute(person);
    assertEquals(7, list.size());
    assertEquals("rule 3", list.get(0));
    assertEquals("rule 2", list.get(1));
    assertEquals("rule 1", list.get(2));
    assertEquals("rule 3", list.get(3));
    assertEquals("rule 2", list.get(4));
    assertEquals("rule 1", list.get(5));
    assertEquals(person, list.get(6));
}
Also used : DynamicRulesTest(org.drools.compiler.integrationtests.DynamicRulesTest) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) DynamicRulesTest(org.drools.compiler.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 44 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class SequentialTest method testKnowledgeRuntimeAccess.

@Test
public void testKnowledgeRuntimeAccess() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test\n";
    str += "import org.drools.compiler.Message\n";
    str += "rule \"Hello World\"\n";
    str += "when\n";
    str += "    Message( )\n";
    str += "then\n";
    str += "    System.out.println( drools.getKieRuntime() );\n";
    str += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
    ksession.execute(new Message("help"));
}
Also used : Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) DynamicRulesTest(org.drools.compiler.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 45 with StatelessKieSession

use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.

the class SequentialTest method runTestProfileManyRulesAndFacts.

private void runTestProfileManyRulesAndFacts(boolean sequentialMode, String message, int timetoMeasureIterations, String file) throws DroolsParserException, IOException, Exception {
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    if (sequentialMode) {
        kconf.setOption(SequentialOption.YES);
    } else {
        kconf.setOption(SequentialOption.NO);
    }
    KieBase kbase = loadKnowledgeBase(kconf, file);
    StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    Object[] data = new Object[50000];
    for (int i = 0; i < data.length; i++) {
        if (i % 2 == 0) {
            final Person p = new Person("p" + i, "stilton");
            data[i] = p;
        } else {
            data[i] = new Cheese("cheddar", i);
        }
    }
    if (timetoMeasureIterations == 0) {
        // one shot measure
        long start = System.currentTimeMillis();
        ksession.execute(CommandFactory.newInsertElements(Arrays.asList(data)));
        System.out.println("Time for " + message + ":" + (System.currentTimeMillis() - start));
        assertTrue(list.size() > 0);
    } else {
        // lots of shots
        // test throughput
        long start = System.currentTimeMillis();
        long end = start + timetoMeasureIterations;
        int count = 0;
        while (System.currentTimeMillis() < end) {
            StatelessKieSession sess2 = createStatelessKnowledgeSession(kbase);
            List list2 = new ArrayList();
            sess2.setGlobal("list", list2);
            sess2.execute(CommandFactory.newInsertElements(Arrays.asList(data)));
            // session.execute( data );
            count++;
        }
        System.out.println("Iterations in for " + message + " : " + count);
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person)

Aggregations

StatelessKieSession (org.kie.api.runtime.StatelessKieSession)55 Test (org.junit.Test)42 ArrayList (java.util.ArrayList)28 KieBase (org.kie.api.KieBase)20 List (java.util.List)14 Cheese (org.drools.compiler.Cheese)14 KieContainer (org.kie.api.runtime.KieContainer)13 Command (org.kie.api.command.Command)12 KieServices (org.kie.api.KieServices)10 DynamicRulesTest (org.drools.compiler.integrationtests.DynamicRulesTest)9 ExecutionResults (org.kie.api.runtime.ExecutionResults)9 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 KieSession (org.kie.api.runtime.KieSession)7 Message (org.drools.compiler.Message)6 Person (org.drools.compiler.Person)6 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)6 KieModuleModel (org.kie.api.builder.model.KieModuleModel)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 KieBuilder (org.kie.api.builder.KieBuilder)5 KieFileSystem (org.kie.api.builder.KieFileSystem)5