Search in sources :

Example 6 with Message

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

the class TypeDeclarationTest method testMultipleAnnotationDeclarations.

@Test
public void testMultipleAnnotationDeclarations() {
    String str1 = "";
    str1 += "package org.kie1 " + "" + "declare Foo \n" + "    name : String " + "    age : int " + "end ";
    String str2 = "";
    str2 += "package org.kie2 " + "" + "declare org.kie1.Foo " + "    @role(event) " + "end ";
    String str3 = "";
    str3 += "package org.kie3 " + "" + "declare org.kie1.Foo " + "    @propertyReactive " + "end ";
    String str4 = "" + "package org.kie4; " + "import org.kie1.Foo; " + "" + "rule Check " + "when " + " $f : Foo( name == 'bar' ) " + "then " + " modify( $f ) { setAge( 99 ); } " + "end ";
    KieHelper helper = new KieHelper();
    helper.addContent(str1, ResourceType.DRL);
    helper.addContent(str2, ResourceType.DRL);
    helper.addContent(str3, ResourceType.DRL);
    helper.addContent(str4, ResourceType.DRL);
    List<Message> msg = helper.verify().getMessages(Message.Level.ERROR);
    System.out.println(msg);
    assertEquals(0, msg.size());
    KieBase kieBase = helper.build();
    FactType type = kieBase.getFactType("org.kie1", "Foo");
    assertEquals(2, type.getFields().size());
    Object foo = null;
    try {
        foo = type.newInstance();
        type.set(foo, "name", "bar");
        assertEquals("bar", type.get(foo, "name"));
    } catch (InstantiationException e) {
        fail(e.getMessage());
    } catch (IllegalAccessException e) {
        fail(e.getMessage());
    }
    KieSession session = kieBase.newKieSession();
    FactHandle handle = session.insert(foo);
    int n = session.fireAllRules(5);
    assertTrue(handle instanceof EventFactHandle);
    assertEquals(1, n);
    assertEquals(99, type.get(foo, "age"));
}
Also used : Message(org.kie.api.builder.Message) FactHandle(org.kie.api.runtime.rule.FactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) KieHelper(org.kie.internal.utils.KieHelper) FactType(org.kie.api.definition.type.FactType) KieBase(org.kie.api.KieBase) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 7 with Message

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

the class IncrementalCompilationTest method testIncrementalCompilationDuplicateXLSResources.

@Test
public void testIncrementalCompilationDuplicateXLSResources() throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    InputStream in1 = null;
    InputStream in2 = null;
    try {
        // Add XLS decision table
        in1 = this.getClass().getResourceAsStream("incrementalBuild.dtable.xls");
        kfs.write("src/main/resources/incrementalBuild1.dtable.xls", KieServices.Factory.get().getResources().newInputStreamResource(in1));
        // Expect no errors
        KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
        assertEquals(0, kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).size());
        // Add the same XLS decision table again as a different resource
        in2 = this.getClass().getResourceAsStream("incrementalBuild.dtable.xls");
        kfs.write("src/main/resources/incrementalBuild2.dtable.xls", KieServices.Factory.get().getResources().newInputStreamResource(in2));
        IncrementalResults addResults = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/incrementalBuild2.dtable.xls").build();
        // Expect duplicate rule errors
        assertEquals(1, addResults.getAddedMessages().size());
        assertEquals(0, addResults.getRemovedMessages().size());
        // Check errors on a full build
        List<Message> messages = ks.newKieBuilder(kfs).buildAll().getResults().getMessages();
        assertFalse(messages.isEmpty());
    } finally {
        if (in1 != null) {
            in1.close();
        }
        if (in2 != null) {
            in2.close();
        }
    }
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) InputStream(java.io.InputStream) KieServices(org.kie.api.KieServices) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) Test(org.junit.Test)

Example 8 with Message

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

the class BaseModelTest method createKieBuilder.

protected KieBuilder createKieBuilder(KieServices ks, KieModuleModel model, ReleaseId releaseId, boolean failIfBuildError, KieFile... stringRules) {
    ks.getRepository().removeKieModule(releaseId);
    KieFileSystem kfs = ks.newKieFileSystem();
    if (model != null) {
        kfs.writeKModuleXML(model.toXML());
    }
    kfs.writePomXML(KJARUtils.getPom(releaseId));
    for (int i = 0; i < stringRules.length; i++) {
        kfs.write(stringRules[i].path, stringRules[i].content);
    }
    KieBuilder kieBuilder;
    if (testRunType == RUN_TYPE.FLOW_DSL) {
        kieBuilder = ks.newKieBuilder(kfs).buildAll(ExecutableModelFlowProject.class);
    } else if (testRunType == RUN_TYPE.PATTERN_DSL) {
        kieBuilder = ks.newKieBuilder(kfs).buildAll(ExecutableModelProject.class);
    } else {
        kieBuilder = ks.newKieBuilder(kfs).buildAll();
    }
    if (failIfBuildError) {
        List<Message> messages = kieBuilder.getResults().getMessages();
        if (!messages.isEmpty()) {
            fail(messages.toString());
        }
    }
    return kieBuilder;
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) KieBuilder(org.kie.api.builder.KieBuilder)

Example 9 with Message

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

the class BuildFromKJarTest method createKieModule.

private InternalKieModule createKieModule(KieServices ks, ReleaseId releaseId) {
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(getDefaultKieModuleModel(ks).toXML());
    kfs.writePomXML(KJARUtils.getPom(releaseId));
    String javaSrc = Person.class.getCanonicalName().replace('.', '/') + ".java";
    Resource javaResource = ks.getResources().newFileSystemResource("src/test/java/" + javaSrc);
    kfs.write("src/main/java/" + javaSrc, javaResource);
    kfs.write("src/main/resources/rule.drl", getRule());
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    List<Message> messages = ((KieBuilderImpl) kieBuilder).buildAll(ExecutableModelFlowProject.class).getResults().getMessages();
    if (!messages.isEmpty()) {
        fail(messages.toString());
    }
    return (InternalKieModule) kieBuilder.getKieModule();
}
Also used : KieBuilderImpl(org.drools.compiler.kie.builder.impl.KieBuilderImpl) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) Resource(org.kie.api.io.Resource) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 10 with Message

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

the class KieUtil method getKieBuilderFromFileSystemWithResources.

private static KieBuilder getKieBuilderFromFileSystemWithResources(final KieBaseTestConfiguration kieBaseTestConfiguration, final KieFileSystem kfs, final boolean failIfBuildError, final Resource... resources) {
    for (Resource res : resources) {
        kfs.write(res);
    }
    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
    final Class<? extends KieBuilder.ProjectType> projectClass;
    switch(kieBaseTestConfiguration.runType()) {
        case STANDARD_FROM_DRL:
            projectClass = DrlProject.class;
            break;
        case FLOW_DSL:
            projectClass = ExecutableModelFlowProject.class;
            break;
        case PATTERN_DSL:
            projectClass = ExecutableModelProject.class;
            break;
        default:
            throw new RuntimeException("Unknown type: " + kieBaseTestConfiguration.runType());
    }
    kbuilder.buildAll(projectClass);
    // Messages from KieBuilder with increasing severity
    List<Message> msgs = kbuilder.getResults().getMessages(Message.Level.INFO);
    if (msgs.size() > 0) {
        LOGGER.info("KieBuilder information: {}", msgs.toString());
    }
    msgs = kbuilder.getResults().getMessages(Message.Level.WARNING);
    if (msgs.size() > 0) {
        LOGGER.warn("KieBuilder warnings: {}", msgs.toString());
    }
    msgs = kbuilder.getResults().getMessages(Message.Level.ERROR);
    if (msgs.size() > 0) {
        LOGGER.error("KieBuilder errors: {}", msgs.toString());
    }
    if (failIfBuildError) {
        Assertions.assertThat(msgs).as(msgs.toString()).isEmpty();
    }
    return kbuilder;
}
Also used : Message(org.kie.api.builder.Message) Resource(org.kie.api.io.Resource) KieBuilder(org.kie.api.builder.KieBuilder)

Aggregations

Message (org.kie.api.builder.Message)33 KieBuilder (org.kie.api.builder.KieBuilder)30 KieFileSystem (org.kie.api.builder.KieFileSystem)26 Test (org.junit.Test)21 KieServices (org.kie.api.KieServices)19 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)10 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)7 KieBase (org.kie.api.KieBase)6 ReleaseId (org.kie.api.builder.ReleaseId)5 KieModule (org.kie.api.builder.KieModule)4 KieModuleModel (org.kie.api.builder.model.KieModuleModel)4 IncrementalResults (org.kie.internal.builder.IncrementalResults)4 Before (org.junit.Before)3 Resource (org.kie.api.io.Resource)3 KieSession (org.kie.api.runtime.KieSession)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)1 MavenRepository (org.appformer.maven.integration.MavenRepository)1 MavenRepository.getMavenRepository (org.appformer.maven.integration.MavenRepository.getMavenRepository)1