Search in sources :

Example 36 with StatelessKieSession

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

the class AddRemoveRulesTest method testFireAfterRemoveRule.

@Test
public void testFireAfterRemoveRule() {
    // DROOLS-893
    final String rule1Name = "rule1";
    final String rule2Name = "rule2";
    final String rule1 = "rule " + rule1Name + " \n" + "when \n" + " Map(  this['type'] == 'Goods'  )" + " and " + " Map(  this['type'] == 'Cinema'  )" + "then \n" + " System.out.println('test in rule1'); \n" + "end";
    final String rule2 = "rule " + rule2Name + " \n" + "when \n" + " Map(  this['type'] == 'Goods'  )" + " and " + " Map(  this['type'] == 'Cinema'  )" + "then \n" + " System.out.println('test in rule2'); \n" + "end";
    final Map<String, Object> fact = new HashMap<String, Object>();
    fact.put("type", "Cinema");
    final StatelessKieSession session = base.newStatelessKieSession();
    this.addRuleToEngine(rule1);
    session.execute(fact);
    this.addRuleToEngine(rule2);
    session.execute(fact);
    this.deleteRule(rule1Name);
    session.execute(fact);
}
Also used : HashMap(java.util.HashMap) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 37 with StatelessKieSession

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

the class AddRemoveRulesTest method testRemoveHasSameConElement.

@Test
public void testRemoveHasSameConElement() {
    // DROOLS-891
    final String packageName = "test";
    final String rule1 = "package " + packageName + ";" + "import java.util.Map; \n" + "rule 'rule1' \n" + "when \n" + " Map(this['type'] == 'Goods' && this['brand'] == 'a') \n" + " Map(this['type'] == 'Goods' && this['category'] == 'b') \n" + "then \n" + "System.out.println('test rule 1'); \n" + "end";
    final KnowledgeBuilder kbuilder = TestUtil.createKnowledgeBuilder(null, rule1);
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    kbase.removeKiePackage(packageName);
    final StatelessKieSession session = kbase.newStatelessKieSession();
    session.execute(new HashMap());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) HashMap(java.util.HashMap) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 38 with StatelessKieSession

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

the class KieContainerImpl method dispose.

public void dispose() {
    kBases.values().forEach(kb -> ((InternalKnowledgeBase) kb).setKieContainer(null));
    Set<DroolsManagementAgent.CBSKey> cbskeys = new HashSet<DroolsManagementAgent.CBSKey>();
    if (isMBeanOptionEnabled()) {
        for (Entry<String, KieSession> kv : kSessions.entrySet()) {
            cbskeys.add(new DroolsManagementAgent.CBSKey(containerId, ((InternalKnowledgeBase) kv.getValue().getKieBase()).getId(), kv.getKey()));
        }
        for (Entry<String, StatelessKieSession> kv : statelessKSessions.entrySet()) {
            cbskeys.add(new DroolsManagementAgent.CBSKey(containerId, ((InternalKnowledgeBase) kv.getValue().getKieBase()).getId(), kv.getKey()));
        }
    }
    for (KieSession kieSession : kSessions.values()) {
        kieSession.dispose();
    }
    kSessions.clear();
    statelessKSessions.clear();
    if (isMBeanOptionEnabled()) {
        for (CBSKey c : cbskeys) {
            DroolsManagementAgent.getInstance().unregisterKnowledgeSessionBean(c);
        }
        for (KieBase kb : kBases.values()) {
            DroolsManagementAgent.getInstance().unregisterKnowledgeBase((InternalKnowledgeBase) kb);
        }
        DroolsManagementAgent.getInstance().unregisterMBeansFromOwner(this);
    }
    ((InternalKieServices) KieServices.Factory.get()).clearRefToContainerId(this.containerId, this);
}
Also used : DroolsManagementAgent(org.drools.core.management.DroolsManagementAgent) CBSKey(org.drools.core.management.DroolsManagementAgent.CBSKey) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) CBSKey(org.drools.core.management.DroolsManagementAgent.CBSKey) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) HashSet(java.util.HashSet)

Example 39 with StatelessKieSession

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

the class KieContainerImpl method newStatelessKieSession.

public StatelessKieSession newStatelessKieSession(String kSessionName, KieSessionConfiguration conf) {
    KieSessionModelImpl kSessionModel = kSessionName != null ? (KieSessionModelImpl) getKieSessionModel(kSessionName) : (KieSessionModelImpl) findKieSessionModel(true);
    if (kSessionModel == null) {
        log.error("Unknown KieSession name: " + kSessionName);
        return null;
    }
    if (kSessionModel.getType() == KieSessionModel.KieSessionType.STATEFUL) {
        throw new RuntimeException("Trying to create a stateless KieSession from a stateful KieSessionModel: " + kSessionModel.getName());
    }
    KieBase kBase = getKieBase(kSessionModel.getKieBaseModel().getName());
    if (kBase == null) {
        log.error("Unknown KieBase name: " + kSessionModel.getKieBaseModel().getName());
        return null;
    }
    StatelessKieSession statelessKieSession = kBase.newStatelessKieSession(conf != null ? conf : getKieSessionConfiguration(kSessionModel));
    if (isJndiAvailable()) {
        wireSessionComponents(kSessionModel, statelessKieSession);
    }
    registerLoggers(kSessionModel, statelessKieSession);
    ((StatelessKnowledgeSessionImpl) statelessKieSession).initMBeans(containerId, ((InternalKnowledgeBase) kBase).getId(), kSessionModel.getName());
    statelessKSessions.put(kSessionModel.getName(), statelessKieSession);
    return statelessKieSession;
}
Also used : KieSessionModelImpl(org.drools.compiler.kproject.models.KieSessionModelImpl) StatelessKnowledgeSessionImpl(org.drools.core.impl.StatelessKnowledgeSessionImpl) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession)

Example 40 with StatelessKieSession

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

the class TraitTest method traitsInBatchExecution.

// BZ #748752
@Test(timeout = 10000)
public void traitsInBatchExecution() {
    String str = "package org.jboss.qa.brms.traits\n" + "import org.drools.compiler.Person;\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "" + "global java.util.List list;" + "" + "declare Person \n" + "  @Traitable \n" + "end \n" + "" + "declare trait Student\n" + "  school : String\n" + "end\n" + "\n" + "rule \"create student\" \n" + "  when\n" + "    $student : Person( age < 26 )\n" + "  then\n" + "    Student s = don( $student, Student.class );\n" + "    s.setSchool(\"Masaryk University\");\n" + "end\n" + "\n" + "rule \"print student\"\n" + "  when\n" + "    student : Person( this isA Student )\n" + "  then" + "    list.add( 1 );\n" + "    System.out.println(\"Person is a student: \" + student);\n" + "end\n" + "\n" + "rule \"print school\"\n" + "  when\n" + "    Student( $school : school )\n" + "  then\n" + "    list.add( 2 );\n" + "    System.out.println(\"Student is attending \" + $school);\n" + "end";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        throw new RuntimeException(kbuilder.getErrors().toString());
    }
    List list = new ArrayList();
    KieBase kbase = kbuilder.newKieBase();
    TraitFactory.setMode(mode, kbase);
    StatelessKieSession ksession = kbase.newStatelessKieSession();
    ksession.setGlobal("list", list);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    Person student = new Person("student", 18);
    commands.add(CommandFactory.newInsert(student));
    System.out.println("Starting execution...");
    commands.add(CommandFactory.newFireAllRules());
    ksession.execute(CommandFactory.newBatchExecution(commands));
    System.out.println("Finished...");
    assertEquals(2, list.size());
    assertTrue(list.contains(1));
    assertTrue(list.contains(2));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) List(java.util.List) ArrayList(java.util.ArrayList) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Person(org.drools.compiler.Person) Test(org.junit.Test)

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