Search in sources :

Example 26 with KnowledgeBuilderError

use of org.kie.internal.builder.KnowledgeBuilderError in project drools by kiegroup.

the class TimerAndCalendarTest method buildKnowledgePackage.

private Collection<KiePackage> buildKnowledgePackage(Resource resource, ResourceType resourceType) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(resource, resourceType);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors != null && errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println("Error: " + error.getMessage());
        }
        Assert.fail("KnowledgeBase did not build");
    }
    Collection<KiePackage> packages = kbuilder.getKnowledgePackages();
    return packages;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) KiePackage(org.kie.api.definition.KiePackage)

Example 27 with KnowledgeBuilderError

use of org.kie.internal.builder.KnowledgeBuilderError in project drools by kiegroup.

the class MapPersistenceTest method createPersistentSessionWithRules.

@Test
public void createPersistentSessionWithRules() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    String rule = "package org.drools.persistence.map.impl\n";
    rule += "import org.drools.persistence.map.impl.Buddy;\n";
    rule += "rule \"echo2\" \n";
    rule += "dialect \"mvel\"\n";
    rule += "when\n";
    rule += "    $m : Buddy()\n";
    rule += "then\n";
    rule += "    System.out.println(\"buddy inserted\")";
    rule += "end\n";
    kbuilder.add(new ByteArrayResource(rule.getBytes()), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors != null && errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            logger.warn("Error: " + error.getMessage());
        }
        Assert.fail("KnowledgeBase did not build");
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createSession(kbase);
    FactHandle buddyFactHandle = ksession.insert(new Buddy());
    ksession.fireAllRules();
    Assert.assertEquals(1, ksession.getObjects().size());
    ksession = disposeAndReloadSession(ksession, kbase);
    Assert.assertNotNull(ksession);
    Assert.assertEquals(1, ksession.getObjects().size());
    Assert.assertNull("An object can't be retrieved with a FactHandle from a disposed session", ksession.getObject(buddyFactHandle));
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 28 with KnowledgeBuilderError

use of org.kie.internal.builder.KnowledgeBuilderError in project drools by kiegroup.

the class CommonTestMethodBase method genSession.

public KieSession genSession(String[] sources, int numerrors) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    for (String source : sources) kbuilder.add(ResourceFactory.newClassPathResource(source, getClass()), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (kbuilder.getErrors().size() > 0) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.err.println(error);
        }
    }
    assertEquals(numerrors, errors.size());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return createKnowledgeSession(kbase);
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 29 with KnowledgeBuilderError

use of org.kie.internal.builder.KnowledgeBuilderError in project kie-wb-common by kiegroup.

the class BpmnFileIndexer method buildProcessDefinition.

private List<BpmnProcessDataEventListener> buildProcessDefinition(String bpmn2Content, ClassLoader moduleClassLoader) throws IllegalArgumentException {
    if (StringUtils.isEmpty(bpmn2Content)) {
        return Collections.<BpmnProcessDataEventListener>emptyList();
    }
    // Set class loader
    KnowledgeBuilder kbuilder = null;
    if (moduleClassLoader != null) {
        KnowledgeBuilderConfigurationImpl pconf = new KnowledgeBuilderConfigurationImpl(moduleClassLoader);
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(pconf);
    } else {
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    }
    // Build
    kbuilder.add(new ByteArrayResource(bpmn2Content.getBytes()), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            logger.error("Error: {}", error.getMessage());
        }
        logger.debug("Process Cannot be Parsed! \n {} \n", bpmn2Content);
        return Collections.<BpmnProcessDataEventListener>emptyList();
    }
    // Retrieve ProcessInfoHolder
    List<BpmnProcessDataEventListener> processDataList = new ArrayList<>();
    kbuilder.getKnowledgePackages().forEach(pkg -> pkg.getProcesses().forEach(p -> {
        BpmnProcessDataEventListener processData = (BpmnProcessDataEventListener) p.getMetaData().get(BpmnProcessDataEventListener.NAME);
        processDataList.add(processData);
    }));
    return processDataList;
}
Also used : KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) XmlProcessReader(org.jbpm.compiler.xml.XmlProcessReader) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) ModuleClassLoaderHelper(org.kie.workbench.common.services.backend.project.ModuleClassLoaderHelper) DefaultIndexBuilder(org.kie.workbench.common.services.refactoring.backend.server.indexing.DefaultIndexBuilder) Path(org.uberfire.java.nio.file.Path) ReaderResource(org.drools.core.io.impl.ReaderResource) LoggerFactory(org.slf4j.LoggerFactory) ResourceType(org.kie.api.io.ResourceType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Package(org.guvnor.common.services.project.model.Package) ProcessValidationError(org.jbpm.process.core.validation.ProcessValidationError) Paths(org.uberfire.backend.server.util.Paths) Inject(javax.inject.Inject) ProcessValidatorRegistry(org.jbpm.process.core.validation.ProcessValidatorRegistry) BPMNSemanticModule(org.jbpm.bpmn2.xml.BPMNSemanticModule) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Module(org.guvnor.common.services.project.model.Module) SemanticModules(org.drools.core.xml.SemanticModules) AbstractFileIndexer(org.kie.workbench.common.services.refactoring.backend.server.indexing.AbstractFileIndexer) Logger(org.slf4j.Logger) Process(org.kie.api.definition.process.Process) KieModule(org.kie.workbench.common.services.shared.project.KieModule) BPMNDISemanticModule(org.jbpm.bpmn2.xml.BPMNDISemanticModule) Resource(org.kie.api.io.Resource) BPMNExtensionsSemanticModule(org.jbpm.bpmn2.xml.BPMNExtensionsSemanticModule) List(java.util.List) StringReader(java.io.StringReader) ApplicationScoped(javax.enterprise.context.ApplicationScoped) BPMNDefinitionSetResourceType(org.kie.workbench.common.stunner.bpmn.resource.BPMNDefinitionSetResourceType) Collections(java.util.Collections) ProcessValidator(org.jbpm.process.core.validation.ProcessValidator) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ArrayList(java.util.ArrayList) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource)

Example 30 with KnowledgeBuilderError

use of org.kie.internal.builder.KnowledgeBuilderError in project drools by kiegroup.

the class JBRULESTest method testJBRULES2872.

@Test
public void testJBRULES2872() {
    final String str = "package org.drools.compiler.test\n" + "import org.drools.compiler.FactA\n" + "rule X\n" + "when\n" + "    FactA( enumVal == TestEnum.ONE || == TestEnum.TWO )\n" + "then\n" + "end\n";
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertTrue(kbuilder.hasErrors());
    final KnowledgeBuilderErrors errors = kbuilder.getErrors();
    assertEquals(1, errors.size());
    final KnowledgeBuilderError error = errors.iterator().next();
    assertEquals(5, error.getLines()[0]);
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) Test(org.junit.Test)

Aggregations

KnowledgeBuilderError (org.kie.internal.builder.KnowledgeBuilderError)39 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)24 KnowledgeBuilderErrors (org.kie.internal.builder.KnowledgeBuilderErrors)14 StringReader (java.io.StringReader)10 ArrayList (java.util.ArrayList)9 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)9 Test (org.junit.Test)9 Reader (java.io.Reader)8 Test (org.junit.jupiter.api.Test)8 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 ReaderResource (org.drools.core.io.impl.ReaderResource)7 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)6 Resource (org.kie.api.io.Resource)6 KogitoProcessRuntime (org.kie.kogito.internal.process.runtime.KogitoProcessRuntime)5 HashMap (java.util.HashMap)4 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)4 ClassPathResource (org.drools.core.io.impl.ClassPathResource)4 KieSession (org.kie.api.runtime.KieSession)4 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)3 Person (org.jbpm.integrationtests.test.Person)3