Search in sources :

Example 11 with KnowledgeBuilderError

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

the class DroolsDeclaredTypeSequenceFlowExpressionTest method testDeclaredTypesInSequenceFlowDroolsExpression.

@Test
public void testDeclaredTypesInSequenceFlowDroolsExpression() {
    // DROOLS-1327
    String drl = "package org.drools.test;" + "declare TestFact \n" + "value: Integer\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();
    Resource drlResource = new InputStreamResource(new ByteArrayInputStream(drl.getBytes(StandardCharsets.UTF_8)));
    Resource bpmn2Resource = new ClassPathResource("BPMN2-DroolsDeclaredTypeSequenceFlowExpressionTest.bpmn2");
    ckbuilder.add(drlResource, ResourceType.DRL);
    ckbuilder.add(bpmn2Resource, ResourceType.BPMN2);
    ckbuilder.build();
    // Assert that we don't have any issues in the build.
    if (kbuilder.hasErrors()) {
        KnowledgeBuilderErrors kbErrors = kbuilder.getErrors();
        for (KnowledgeBuilderError nextError : kbErrors) {
            fail(nextError.getMessage());
        }
    }
    if (kbuilder.hasResults(ResultSeverity.WARNING)) {
        KnowledgeBuilderResults kbResults = kbuilder.getResults(ResultSeverity.WARNING);
        for (KnowledgeBuilderResult nextResult : kbResults) {
            fail(nextResult.getMessage());
        }
    }
}
Also used : KnowledgeBuilderResults(org.kie.internal.builder.KnowledgeBuilderResults) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Resource(org.kie.api.io.Resource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InputStreamResource(org.drools.core.io.impl.InputStreamResource) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult) Test(org.junit.Test)

Example 12 with KnowledgeBuilderError

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

the class BPMN2DataServiceImpl method buildProcessDefinition.

@Override
public ProcessDefinition buildProcessDefinition(String deploymentId, String bpmn2Content, KieContainer kieContainer, boolean cache) throws IllegalArgumentException {
    if (StringUtils.isEmpty(bpmn2Content)) {
        return null;
    }
    KnowledgeBuilder kbuilder = null;
    if (kieContainer != null && kieContainer.getClassLoader() != null) {
        KnowledgeBuilderConfigurationImpl pconf = new KnowledgeBuilderConfigurationImpl(kieContainer.getClassLoader());
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(pconf);
    } else {
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    }
    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 null;
    }
    KiePackage pckg = kbuilder.getKnowledgePackages().iterator().next();
    Process process = pckg.getProcesses().iterator().next();
    ProcessDescriptor helper = (ProcessDescriptor) process.getMetaData().get("ProcessDescriptor");
    ProcessAssetDesc definition = fillProcessDefinition(helper, kieContainer);
    // cache the data if requested
    if (cache) {
        validateNonEmptyDeploymentIdAndProcessId(deploymentId, "no proc id");
        Map<String, ProcessDescriptor> definitions = null;
        synchronized (definitionCache) {
            Map<String, ProcessDescriptor> newDef = new ConcurrentHashMap<String, ProcessDescriptor>();
            definitions = definitionCache.putIfAbsent(deploymentId, newDef);
            if (definitions == null) {
                definitions = newDef;
            }
            definitions.put(process.getId(), helper);
        }
    }
    return definition;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ProcessAssetDesc(org.jbpm.kie.services.impl.model.ProcessAssetDesc) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) KiePackage(org.kie.api.definition.KiePackage) Process(org.kie.api.definition.process.Process) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 13 with KnowledgeBuilderError

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

the class ProcessSplitTest method testSplitWithProcessInstanceConstraint2.

@Test
public void testSplitWithProcessInstanceConstraint2() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Reader source = new StringReader("<process xmlns=\"http://drools.org/drools-5.0/process\"" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"" + "         type=\"RuleFlow\" name=\"ruleflow\" id=\"org.jbpm.process-split\" package-name=\"org.jbpm\" >" + "" + "  <header>" + "    <imports>" + "      <import name=\"org.jbpm.integrationtests.test.Person\" />" + "      <import name=\"org.kie.api.runtime.process.WorkflowProcessInstance\" />" + "    </imports>" + "    <globals>" + "      <global identifier=\"list\" type=\"java.util.List\" />" + "    </globals>" + "    <variables>\n" + "      <variable name=\"name\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "      </variable>\n" + "    </variables>\n" + "  </header>" + "" + "  <nodes>" + "    <actionNode id=\"2\" name=\"Action\" >" + "        <action type=\"expression\" dialect=\"mvel\" >insert(kcontext.getProcessInstance());</action>" + "    </actionNode>" + "    <split id=\"4\" name=\"Split\" type=\"2\" >" + "      <constraints>" + "        <constraint toNodeId=\"8\" toType=\"DROOLS_DEFAULT\" priority=\"2\" type=\"rule\" dialect=\"mvel\" >eval(true)</constraint>" + "        <constraint toNodeId=\"6\" toType=\"DROOLS_DEFAULT\" name=\"constraint\" priority=\"1\" type=\"rule\" dialect=\"mvel\" >processInstance: WorkflowProcessInstance()" + "Person( name == ( processInstance.getVariable(\"name\") ) )</constraint>" + "      </constraints>" + "    </split>" + "    <end id=\"8\" name=\"End\" />" + "    <actionNode id=\"6\" name=\"Action\" >" + "        <action type=\"expression\" dialect=\"mvel\" >list.add(kcontext.getProcessInstance().getId());</action>" + "    </actionNode>" + "    <start id=\"1\" name=\"Start\" />" + "    <end id=\"3\" name=\"End\" />" + "  </nodes>" + "  <connections>" + "    <connection from=\"1\" to=\"2\" />" + "    <connection from=\"2\" to=\"4\" />" + "    <connection from=\"4\" to=\"8\" />" + "    <connection from=\"4\" to=\"6\" />" + "    <connection from=\"6\" to=\"3\" />" + "  </connections>" + "" + "</process>");
    kbuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRF);
    for (KnowledgeBuilderError error : kbuilder.getErrors()) {
        logger.error(error.toString());
    }
    Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kpkgs);
    KieSession ksession = kbase.newKieSession();
    List<Long> list = new ArrayList<Long>();
    ksession.setGlobal("list", list);
    Person john = new Person("John Doe", 20);
    Person jane = new Person("Jane Doe", 20);
    Person julie = new Person("Julie Doe", 20);
    ksession.insert(john);
    ksession.insert(jane);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("name", john.getName());
    ProcessInstance processInstance1 = ksession.startProcess("org.jbpm.process-split", params);
    params = new HashMap<String, Object>();
    params.put("name", jane.getName());
    ProcessInstance processInstance2 = ksession.startProcess("org.jbpm.process-split", params);
    params = new HashMap<String, Object>();
    params.put("name", julie.getName());
    ProcessInstance processInstance3 = ksession.startProcess("org.jbpm.process-split", params);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance1.getState());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance2.getState());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance3.getState());
    assertEquals(2, list.size());
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Person(org.jbpm.integrationtests.test.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 14 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 15 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)25 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)21 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)11 KnowledgeBuilderErrors (org.kie.internal.builder.KnowledgeBuilderErrors)10 Test (org.junit.Test)9 HashMap (java.util.HashMap)4 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)4 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)3 ClassPathResource (org.drools.core.io.impl.ClassPathResource)3 KiePackage (org.kie.api.definition.KiePackage)3 Resource (org.kie.api.io.Resource)3 KieSession (org.kie.api.runtime.KieSession)3 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)2 Process (org.kie.api.definition.process.Process)2 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)2 CompositeKnowledgeBuilder (org.kie.internal.builder.CompositeKnowledgeBuilder)2 KnowledgeBuilderConfiguration (org.kie.internal.builder.KnowledgeBuilderConfiguration)2 KnowledgeBuilderResult (org.kie.internal.builder.KnowledgeBuilderResult)2