use of org.kie.api.builder.KieFileSystem in project graylog2-server by Graylog2.
the class DroolsEngine method createKJar.
private byte[] createKJar(KieServices ks, ReleaseId releaseId, String pom, String... drls) throws RulesCompilationException {
KieFileSystem kfs = ks.newKieFileSystem();
if (pom != null) {
kfs.write("pom.xml", pom);
} else {
kfs.generateAndWritePomXML(releaseId);
}
for (int i = 0; i < drls.length; i++) {
if (drls[i] != null) {
kfs.write("src/main/resources/r" + i + ".drl", drls[i]);
}
}
for (URI builtinRuleUrl : builtinRuleUrls) {
final String rulesFileName = FilenameUtils.getName(builtinRuleUrl.getPath());
final String path = "src/main/resources/" + rulesFileName;
final URL url;
try {
url = builtinRuleUrl.toURL();
} catch (MalformedURLException e) {
throw new RulesCompilationException(Collections.<org.kie.api.builder.Message>emptyList());
}
final Resource resource = ResourceFactory.newUrlResource(url).setSourcePath(path).setResourceType(ResourceType.DRL);
kfs.write(resource);
}
final KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
if (kb.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
throw new RulesCompilationException(kb.getResults().getMessages());
}
final InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
return kieModule.getBytes();
}
use of org.kie.api.builder.KieFileSystem 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.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class WindowTest method initialization.
@Before
public void initialization() {
KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
kfs.write("src/main/resources/kbase1/window_test.drl", drl);
KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
kbuilder.buildAll();
List<Message> res = kbuilder.getResults().getMessages(Level.ERROR);
assertEquals(res.toString(), 0, res.size());
KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = KieServices.Factory.get().newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieBase(kbconf);
KieSessionConfiguration ksconfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconfig.setOption(ClockTypeOption.get("pseudo"));
ksession = kbase.newKieSession(ksconfig, null);
clock = ksession.getSessionClock();
}
use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class IncrementalCompilationTest method buildKieModule.
private KieModule buildKieModule(String version, String declares, String rules) {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
ReleaseId rid = ks.newReleaseId("org.drools", "kiemodulemodel-example", version);
kfs.generateAndWritePomXML(rid);
KieModuleModel kModuleModel = ks.newKieModuleModel();
kModuleModel.newKieBaseModel("kiemodulemodel").addPackage("kiemodulemodel").addInclude("kiedeclare");
kModuleModel.newKieBaseModel("kiedeclare").addPackage("kiedeclare");
kfs.writeKModuleXML(kModuleModel.toXML());
kfs.write("src/main/resources/kiedeclare/declares.drl", declares);
kfs.write("src/main/resources/kiemodulemodel/rules.drl", rules);
KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
return kb.getKieModule();
}
use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationWithAmbiguousRedeclares.
@Test
public void testIncrementalCompilationWithAmbiguousRedeclares() {
String drl1 = "package domestic; " + "import foreign.*; " + "declare foreign.Score " + " id : String " + "end ";
String drl2 = "\n" + "package domestic; " + "import foreign.*; " + "declare foreign.Score " + " id : String " + "end\n" + "declare Score " + " value : double " + "end " + "";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
kfs.generateAndWritePomXML(id);
kfs.write(ks.getResources().newReaderResource(new StringReader(drl1)).setResourceType(ResourceType.DRL).setSourcePath("drl1.drl"));
kieBuilder.buildAll();
KieContainer kc = ks.newKieContainer(id);
KieSession ksession = kc.newKieSession();
ksession.fireAllRules();
kfs.write(ks.getResources().newReaderResource(new StringReader(drl2)).setResourceType(ResourceType.DRL).setSourcePath("drl2.drl"));
IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
System.out.println(results.getAddedMessages());
assertEquals(0, results.getAddedMessages().size());
Results updateResults = kc.updateToVersion(id);
assertEquals(0, updateResults.getMessages().size());
}
Aggregations