use of org.drools.core.RuleBaseConfiguration in project opennms by OpenNMS.
the class DroolsNorthbounder method initializeDroolsEngine.
/**
* Initialize drools engine.
*
* @throws Exception the exception
*/
private void initializeDroolsEngine() throws Exception {
KieServices ks = KieServices.Factory.get();
KieFileSystem kFileSystem = ks.newKieFileSystem();
for (String ruleFile : m_engine.getRuleFiles()) {
LOG.debug("Loading rules file: {}", ruleFile);
kFileSystem.write("src/main/resources/" + ruleFile, ks.getResources().newFileSystemResource(new File(ruleFile)));
}
KieBuilder kbuilder = ks.newKieBuilder(kFileSystem);
kbuilder.buildAll();
if (kbuilder.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
LOG.warn("Unable to initialize Drools engine: {}", kbuilder.getResults().getMessages(Level.ERROR));
throw new IllegalStateException("Unable to initialize Drools engine: " + kbuilder.getResults().getMessages(Level.ERROR));
}
KieContainer kContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
AssertBehaviour behaviour = AssertBehaviour.determineAssertBehaviour(m_engine.getAssertBehaviour());
RuleBaseConfiguration ruleBaseConfig = new RuleBaseConfiguration();
ruleBaseConfig.setAssertBehaviour(behaviour);
ruleBaseConfig.setEventProcessingMode(EventProcessingOption.STREAM);
m_kieBase = kContainer.newKieBase(ruleBaseConfig);
m_kieSession = m_kieBase.newKieSession();
m_kieSession.setGlobal("engine", this);
unmarshallStateFromDisk(true);
ApplicationContext ctx = m_context;
if (m_engine.getAppContext() != null) {
ctx = new FileSystemXmlApplicationContext(new String[] { m_engine.getAppContext() }, m_context);
}
for (Global global : m_engine.getGlobals()) {
m_kieSession.setGlobal(global.getName(), global.constructValue(ctx));
}
new Thread(() -> {
Logging.putPrefix(getName());
LOG.debug("Starting task thread for {}", getName());
m_kieSession.fireUntilHalt();
LOG.debug("Stopping task thread for {}", getName());
}, "FireTask-" + getName()).start();
}
use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.
the class MultithreadTest method testJittingShortComparison.
@Test(timeout = 20000)
public void testJittingShortComparison() {
// DROOLS-1633
final String drl = "import " + BeanA.class.getCanonicalName() + "\n;" + "global java.util.List list;" + "rule R when\n" + " $a1: BeanA($sv1 : shortValue)\n" + " $b2: BeanA(shortValue != $sv1)\n" + "then\n" + " list.add(\"FIRED\");\n" + "end";
final List<String> list = Collections.synchronizedList(new ArrayList());
final RuleBaseConfiguration rbc = (RuleBaseConfiguration) KieServices.Factory.get().newKieBaseConfiguration();
rbc.setJittingThreshold(0);
final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(rbc);
final int threadNr = 1000;
final Thread[] threads = new Thread[threadNr];
for (int i = 0; i < threadNr; i++) {
threads[i] = new Thread(new SessionRunner(kbase, list));
}
for (int i = 0; i < threadNr; i++) {
threads[i].start();
}
for (int i = 0; i < threadNr; i++) {
try {
threads[i].join();
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
}
Assertions.assertThat(list).hasSize(0);
}
use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.
the class AbstractKieModule method createKieBase.
public InternalKnowledgeBase createKieBase(KieBaseModelImpl kBaseModel, KieProject kieProject, ResultsImpl messages, KieBaseConfiguration conf) {
Collection<KiePackage> pkgs = getKnowledgePackagesForKieBase(kBaseModel.getName());
if (pkgs == null) {
KnowledgeBuilder kbuilder = kieProject.buildKnowledgePackages(kBaseModel, messages);
if (kbuilder.hasErrors()) {
// Messages already populated by the buildKnowlegePackages
return null;
}
}
// if we get to here, then we know the pkgs is now cached
pkgs = getKnowledgePackagesForKieBase(kBaseModel.getName());
if (kBaseModel.getEventProcessingMode() == EventProcessingOption.CLOUD && (conf == null || conf.getOption(EventProcessingOption.class) == EventProcessingOption.CLOUD)) {
for (KiePackage kpkg : pkgs) {
if (((KnowledgePackageImpl) kpkg).needsStreamMode()) {
throw new RuntimeException("The requested KieBase \"" + kBaseModel.getName() + "\" has been set to run in CLOUD mode but requires features only available in STREAM mode");
}
}
}
ClassLoader cl = kieProject.getClassLoader();
if (conf == null) {
conf = getKnowledgeBaseConfiguration(kBaseModel, cl);
} else if (conf instanceof RuleBaseConfiguration) {
((RuleBaseConfiguration) conf).setClassLoader(cl);
}
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kBaseModel.getName(), conf);
kBase.addPackages(pkgs);
return kBase;
}
use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.
the class PhreakJoinNodeTest method createContext.
public BuildContext createContext() {
RuleBaseConfiguration conf = new RuleBaseConfiguration();
KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
BuildContext buildContext = new BuildContext(rbase);
RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
pkg.getDialectRuntimeRegistry().setDialectData("mvel", new MVELDialectRuntimeData());
pkg.addRule(rule);
buildContext.setRule(rule);
return buildContext;
}
use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.
the class PhreakNotNodeTest method createContext.
public BuildContext createContext() {
RuleBaseConfiguration conf = new RuleBaseConfiguration();
KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
BuildContext buildContext = new BuildContext(rbase);
RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
pkg.getDialectRuntimeRegistry().setDialectData("mvel", new MVELDialectRuntimeData());
pkg.addRule(rule);
buildContext.setRule(rule);
return buildContext;
}
Aggregations