Search in sources :

Example 1 with StatelessKieSession

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

the class IncrementalCompilationTest method testUpdateVersionWithKSessionLogger.

@Test
public void testUpdateVersionWithKSessionLogger() {
    // DROOLS-790
    String drl1 = "import java.util.List\n" + "import java.util.ArrayList\n" + "\n" + "rule \"Test1\"\n" + "\n" + "when\n" + "   $a : Integer()\n" + "then\n" + "   insert(new ArrayList());\n" + "end\n";
    String drl2 = "rule \"Test2\"\n" + "when\n" + "   $b : List()\n" + " then\n" + "   $b.isEmpty();\n" + "end";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    StatelessKieSession statelessKieSession = kc.newStatelessKieSession();
    KieRuntimeLogger kieRuntimeLogger = ks.getLoggers().newConsoleLogger(statelessKieSession);
    List<Command> cmds = new ArrayList<Command>();
    cmds.add(CommandFactory.newInsertElements(new ArrayList()));
    FireAllRulesCommand fireAllRulesCommand = (FireAllRulesCommand) CommandFactory.newFireAllRules();
    cmds.add(fireAllRulesCommand);
    cmds.add(CommandFactory.newGetObjects("returnedObjects"));
    BatchExecutionCommand batchExecutionCommand = CommandFactory.newBatchExecution(cmds);
    statelessKieSession.execute(batchExecutionCommand);
    kieRuntimeLogger.close();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl1 + drl2);
    kc.updateToVersion(km.getReleaseId());
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) Command(org.kie.api.command.Command) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 2 with StatelessKieSession

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

the class StatelessStressTest method testLotsOfStateless.

@Test
@Ignore
public void testLotsOfStateless() throws Exception {
    final KieBase kbase = loadKnowledgeBase("thread_class_test.drl");
    int numThreads = 100;
    Thread[] ts = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
        Runnable run = new Runnable() {

            public void run() {
                long start = 0;
                long previous = 0;
                while (true) {
                    start = System.currentTimeMillis();
                    StatelessKieSession sess = kbase.newStatelessKieSession();
                    try {
                        sess = SerializationHelper.serializeObject(sess);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    Person p = new Person();
                    p.setName("Michael");
                    Address add1 = new Address();
                    add1.setStreet("High");
                    Address add2 = new Address();
                    add2.setStreet("Low");
                    List l = new ArrayList();
                    l.add(add1);
                    l.add(add2);
                    p.setAddresses(l);
                    sess.execute(p);
                    long current = System.currentTimeMillis() - start;
                    if (previous != 0) {
                        float f = current / previous;
                        if (f > 1) {
                            System.err.println("SLOWDOWN");
                        }
                    }
                    previous = current;
                }
            }
        };
        Thread t = new Thread(run);
        t.start();
        ts[i] = t;
    }
    for (int i = 0; i < ts.length; i++) {
        ts[i].join();
    }
}
Also used : Address(org.drools.compiler.Address) ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with StatelessKieSession

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

the class SequentialTest method testBasicOperation.

@Test
public void testBasicOperation() throws Exception {
    KieBase kbase = loadKnowledgeBase(kconf, "simpleSequential.drl");
    StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Person p1 = new Person("p1", "stilton");
    final Person p2 = new Person("p2", "cheddar");
    final Person p3 = new Person("p3", "stilton");
    final Cheese stilton = new Cheese("stilton", 15);
    final Cheese cheddar = new Cheese("cheddar", 15);
    ksession.execute(CommandFactory.newInsertElements(Arrays.asList(new Object[] { p1, stilton, p2, cheddar, p3 })));
    assertEquals(3, list.size());
}
Also used : 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) DynamicRulesTest(org.drools.compiler.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 4 with StatelessKieSession

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

the class SequentialTest method testSequentialWithNoLoop.

@Test(timeout = 10000L)
public void testSequentialWithNoLoop() throws Exception {
    // BZ-1228098
    String str = "package org.drools.compiler.test\n" + "import \n" + Message.class.getCanonicalName() + ";" + "rule R1 no-loop when\n" + "    $s : String( )" + "    $m : Message( )\n" + "then\n" + "    modify($m) { setMessage($s) };\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/r0.drl", str);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    Results results = kieBuilder.getResults();
    if (results.hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
        throw new RuntimeException(results.getMessages().toString());
    }
    KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    KieBaseConfiguration kieBaseConf = ks.newKieBaseConfiguration();
    kieBaseConf.setOption(SequentialOption.YES);
    StatelessKieSession sequentialKsession = kieContainer.newKieBase(kieBaseConf).newStatelessKieSession();
    List result = (List) sequentialKsession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message())));
    assertEquals(2, result.size());
    StatelessKieSession ksession = kieContainer.getKieBase().newStatelessKieSession();
    result = (List) ksession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message())));
    assertEquals(2, result.size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) Results(org.kie.api.builder.Results) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieServices(org.kie.api.KieServices) ArrayList(java.util.ArrayList) List(java.util.List) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer) DynamicRulesTest(org.drools.compiler.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 5 with StatelessKieSession

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

the class StatelessSessionTest method getSession2.

private StatelessKieSession getSession2(final Resource resource) throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(resource, ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        System.out.println(kbuilder.getErrors());
    }
    assertFalse(kbuilder.hasErrors());
    final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    final StatelessKieSession session = kbase.newStatelessKieSession();
    session.setGlobal("list", this.list);
    session.setGlobal("cheesery", this.cheesery);
    return session;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

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