Search in sources :

Example 1 with KieFileSystem

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();
}
Also used : MalformedURLException(java.net.MalformedURLException) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.graylog2.plugin.Message) Resource(org.kie.api.io.Resource) URI(java.net.URI) URL(java.net.URL) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 2 with KieFileSystem

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();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) AssertBehaviour(org.drools.core.RuleBaseConfiguration.AssertBehaviour) KieBuilder(org.kie.api.builder.KieBuilder) File(java.io.File) KieContainer(org.kie.api.runtime.KieContainer)

Example 3 with KieFileSystem

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();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) KieBase(org.kie.api.KieBase) KieBuilder(org.kie.api.builder.KieBuilder) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Before(org.junit.Before)

Example 4 with KieFileSystem

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();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder)

Example 5 with KieFileSystem

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());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) StringReader(java.io.StringReader) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

KieFileSystem (org.kie.api.builder.KieFileSystem)297 KieServices (org.kie.api.KieServices)241 Test (org.junit.Test)211 KieBuilder (org.kie.api.builder.KieBuilder)179 KieSession (org.kie.api.runtime.KieSession)101 KieModuleModel (org.kie.api.builder.model.KieModuleModel)79 KieContainer (org.kie.api.runtime.KieContainer)74 ReleaseId (org.kie.api.builder.ReleaseId)64 Results (org.kie.api.builder.Results)60 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)53 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)39 KieBase (org.kie.api.KieBase)38 IncrementalResults (org.kie.internal.builder.IncrementalResults)36 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)32 KieBaseModel (org.kie.api.builder.model.KieBaseModel)31 ArrayList (java.util.ArrayList)30 Message (org.kie.api.builder.Message)26 KieModule (org.kie.api.builder.KieModule)22 Message (org.drools.compiler.Message)20 Resource (org.kie.api.io.Resource)18