Search in sources :

Example 61 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class MessageImplTests method testMessageFromInvalidDSL.

@Test
public // See DROOLS-193 (KnowledgeBuilderResult does not always contain a Resource)
void testMessageFromInvalidDSL() throws Exception {
    // Some suitably duff DSL
    String dsl = "bananna\n";
    // Some suitably valid DRL
    String drl = "import org.drools.compiler.Person;\n" + "rule R1\n" + "when\n" + "There is a Person\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/dsl.dsl", dsl).write("src/main/resources/drl.dslr", drl);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    Results results = kieBuilder.getResults();
    assertEquals(3, results.getMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) IncrementalResults(org.kie.internal.builder.IncrementalResults) KieServices(org.kie.api.KieServices) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 62 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class Misc2Test method testEvalInSubnetwork.

@Test
public void testEvalInSubnetwork() {
    // DROOLS-460
    String str = "global java.util.List list;\n" + "\n" + "declare StatusEvent\n" + "@role(event)\n" + "timestamp : int\n" + "end\n" + "\n" + "rule R when\n" + "$i : Integer()\n" + "eval(true)\n" + "exists(\n" + "Integer(intValue > $i.intValue)\n" + "and eval(true)\n" + ")\n" + "then\n" + "list.add($i.intValue());\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
    ks.newKieBuilder(kfs).buildAll();
    KieSession ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    ksession.insert(0);
    ksession.fireAllRules();
    ksession.insert(1);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(0, (int) list.get(0));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 63 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class CommonTestMethodBase method createJar.

public static byte[] createJar(KieServices ks, String kmoduleContent, Predicate<String> classFilter, ReleaseId releaseId, Resource... resources) {
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).writeKModuleXML(kmoduleContent);
    for (int i = 0; i < resources.length; i++) {
        if (resources[i] != null) {
            kfs.write(resources[i]);
        }
    }
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    ((InternalKieBuilder) kieBuilder).buildAll(classFilter);
    Results results = kieBuilder.getResults();
    if (results.hasMessages(Message.Level.ERROR)) {
        throw new IllegalStateException(results.getMessages(Message.Level.ERROR).toString());
    }
    InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    byte[] jar = kieModule.getBytes();
    return jar;
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) Results(org.kie.api.builder.Results) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 64 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class KnowledgeBuilderTest method testMalformedDrl.

@Test
public void testMalformedDrl() throws Exception {
    // DROOLS-928
    byte[] content = new byte[] { 0x04, 0x44, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00 };
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", new String(content));
    Results results = ks.newKieBuilder(kfs).buildAll().getResults();
    assertTrue(results.getMessages().size() > 0);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KieServices(org.kie.api.KieServices) Test(org.junit.Test)

Example 65 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class AbductionTest method testBindNonAbductiveQueryError.

@Test
public void testBindNonAbductiveQueryError() {
    String droolsSource = "package org.drools.abductive.test; \n" + "" + "" + "query foo() \n" + "end \n" + "rule R1 " + "when " + "   $x : foo( ) " + "then " + "end \n" + "";
    // ///////////////////////////////////
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ks.getResources().newByteArrayResource(droolsSource.getBytes()).setSourcePath("drl1.drl").setResourceType(ResourceType.DRL));
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    kieBuilder.buildAll();
    Results res = kieBuilder.getResults();
    assertTrue(res.hasMessages(Message.Level.ERROR));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Results(org.kie.api.builder.Results) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) 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