Search in sources :

Example 71 with KieBuilder

use of org.kie.api.builder.KieBuilder 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)

Example 72 with KieBuilder

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

the class KieContainerTest method createKieModule.

/**
 * Helper method creating simple KieModule with given ReleaseId.
 */
private void createKieModule(final ReleaseId releaseId) {
    final KieModuleModel kmodule = kieServices.newKieModuleModel();
    kmodule.newKieBaseModel("defaultKBase").setDefault(true).newKieSessionModel(SESSION_NAME);
    final KieFileSystem kfs = kieServices.newKieFileSystem();
    kfs.generateAndWritePomXML(releaseId);
    kfs.writeKModuleXML(kmodule.toXML());
    kfs.write("src/main/resources/defaultKBase/test.drl", kieServices.getResources().newByteArrayResource(DRL.getBytes()));
    final KieBuilder kbuilder = kieServices.newKieBuilder(kfs);
    kbuilder.buildAll();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBuilder(org.kie.api.builder.KieBuilder)

Example 73 with KieBuilder

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

the class NonStringCompareTest method testNonQuotedStringComapre.

@Test
public void testNonQuotedStringComapre() {
    final KieBuilder kbuilder = build("someString");
    Assertions.assertThat(kbuilder.getResults().getMessages(Level.ERROR).size()).isEqualTo(1);
}
Also used : KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 74 with KieBuilder

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

the class NonStringCompareTest method testScenario.

private void testScenario(final String factFieldValueForDrl, final String factFieldValueForTest) throws IllegalAccessException, InstantiationException {
    final KieBuilder kbuilder = build(factFieldValueForDrl);
    Assertions.assertThat(kbuilder.getResults().getMessages(Level.ERROR)).isEmpty();
    final KieBase kbase = KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBaseTestConfiguration, kbuilder);
    final KieSession ksession = kbase.newKieSession();
    try {
        final FactType type = kbase.getFactType(TestConstants.PACKAGE_REGRESSION, "Fact");
        final Object fact = type.newInstance();
        type.set(fact, "field", factFieldValueForTest);
        ksession.insert(fact);
        final int count = ksession.fireAllRules();
        Assertions.assertThat(count).isEqualTo(1);
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) FactType(org.kie.api.definition.type.FactType)

Example 75 with KieBuilder

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

the class DeclarationWithOrTest method testBindingWithOrInLHS.

/**
 * Verifies that the rule with binding and OR in LHS compiles and works as expected.
 */
@Test
public void testBindingWithOrInLHS() {
    final Resource resource = KieServices.Factory.get().getResources().newReaderResource(new StringReader(DRL));
    resource.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
    final KieBuilder kbuilder = KieUtil.getKieBuilderFromResources(kieBaseTestConfiguration, true, resource);
    final KieSession ksession = KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBaseTestConfiguration, kbuilder).newKieSession();
    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    try {
        ksession.insert("y");
        ksession.fireAllRules();
    } finally {
        ksession.dispose();
    }
    Assertions.assertThat(list).as("Unexpected element in result global").containsExactly(FACT);
}
Also used : Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Aggregations

KieBuilder (org.kie.api.builder.KieBuilder)211 KieFileSystem (org.kie.api.builder.KieFileSystem)179 KieServices (org.kie.api.KieServices)145 Test (org.junit.Test)134 KieSession (org.kie.api.runtime.KieSession)59 KieModuleModel (org.kie.api.builder.model.KieModuleModel)57 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)54 KieContainer (org.kie.api.runtime.KieContainer)48 IncrementalResults (org.kie.internal.builder.IncrementalResults)35 ReleaseId (org.kie.api.builder.ReleaseId)34 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)33 Results (org.kie.api.builder.Results)32 Message (org.kie.api.builder.Message)30 KieBase (org.kie.api.KieBase)29 ArrayList (java.util.ArrayList)22 KieBaseModel (org.kie.api.builder.model.KieBaseModel)22 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)21 Resource (org.kie.api.io.Resource)19 FactType (org.kie.api.definition.type.FactType)16 KieModule (org.kie.api.builder.KieModule)12