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);
}
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());
}
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);
}
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;
}
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));
}
Aggregations