Search in sources :

Example 21 with KnowledgeBuilderError

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

the class KieBuilderSetImpl method buildChanges.

private IncrementalResults buildChanges(Collection<String> filesToBuild) {
    Map<String, Collection<KnowledgeBuilderError>> currentErrors = new HashMap<String, Collection<KnowledgeBuilderError>>();
    InternalKieModule kieModule = (InternalKieModule) kieBuilder.getKieModuleIgnoringErrors();
    for (KieBaseModel kBaseModel : kieModule.getKieModuleModel().getKieBaseModels().values()) {
        KnowledgeBuilder kBuilder = kieModule.getKnowledgeBuilderForKieBase(kBaseModel.getName());
        if (kBuilder == null) {
            continue;
        }
        CompositeKnowledgeBuilder ckbuilder = kBuilder.batch();
        KnowledgeBuilderImpl.ResourceRemovalResult removalResult = new KnowledgeBuilderImpl.ResourceRemovalResult();
        KnowledgeBuilderImpl pkgBuilder = ((KnowledgeBuilderImpl) kBuilder);
        Set<String> wrongResources = resourcesWithErrors.get(kBaseModel.getName());
        for (String resourceName : wrongResources) {
            removalResult.add(pkgBuilder.removeObjectsGeneratedFromResource(new DummyResource(resourceName)));
            removalResult.mergeModified(addResource(ckbuilder, kBaseModel, kieModule, resourceName));
        }
        for (String file : filesToBuild) {
            if (wrongResources.contains(file)) {
                removalResult.mergeModified(true);
            } else {
                // remove the objects generated by the old Resource
                removalResult.add(pkgBuilder.removeObjectsGeneratedFromResource(new DummyResource(file)));
                // add the modified Resource
                removalResult.mergeModified(addResource(ckbuilder, kBaseModel, kieModule, file));
            }
        }
        if (removalResult.isModified()) {
            if (!removalResult.getRemovedTypes().isEmpty()) {
                ProjectClassLoader projectClassLoader = (ProjectClassLoader) ((KnowledgeBuilderImpl) kBuilder).getRootClassLoader();
                projectClassLoader.reinitTypes();
                for (String removedType : removalResult.getRemovedTypes()) {
                    projectClassLoader.undefineClass(removedType);
                }
            }
            ckbuilder.build();
            resourcesWithErrors.put(kBaseModel.getName(), findResourcesWithErrors(kBuilder));
            if (kBuilder.hasErrors()) {
                currentErrors.put(kBaseModel.getName(), kBuilder.getErrors());
                kBuilder.undo();
            } else {
                KieServices.Factory.get().getRepository().addKieModule(kieModule);
                kieBuilder.updateKieModuleMetaInfo();
            }
        }
    }
    IncrementalResultsImpl results = getIncrementalResults(currentErrors);
    previousErrors = currentErrors;
    return results;
}
Also used : ProjectClassLoader(org.drools.core.common.ProjectClassLoader) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) HashMap(java.util.HashMap) KieBaseModel(org.kie.api.builder.model.KieBaseModel) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Collection(java.util.Collection)

Example 22 with KnowledgeBuilderError

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

the class SpreadsheetCompilerUnitTest method readKnowledgeBase.

private KieBase readKnowledgeBase(String resource) throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    DecisionTableConfiguration config = KnowledgeBuilderFactory.newDecisionTableConfiguration();
    config.setInputType(DecisionTableInputType.XLS);
    kbuilder.add(ResourceFactory.newClassPathResource(resource, getClass()), ResourceType.DTABLE, config);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println(error);
        }
        throw new IllegalArgumentException("Could not parse knowledge.");
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return kbase;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 23 with KnowledgeBuilderError

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

the class MemoryLeakTest method createKnowledgeBase.

private KieBase createKnowledgeBase() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("memory/BPMN2-RuleTaskWithInsertProcessInstance.bpmn2"), ResourceType.BPMN2);
    kbuilder.add(ResourceFactory.newClassPathResource("memory/ProcessInstanceRule.drl"), ResourceType.DRL);
    if (!kbuilder.getErrors().isEmpty()) {
        Iterator<KnowledgeBuilderError> errIter = kbuilder.getErrors().iterator();
        while (errIter.hasNext()) {
            KnowledgeBuilderError err = errIter.next();
            StringBuilder lines = new StringBuilder("");
            if (err.getLines().length > 0) {
                lines.append(err.getLines()[0]);
                for (int i = 1; i < err.getLines().length; ++i) {
                    lines.append(", " + err.getLines()[i]);
                }
            }
            logger.warn(err.getMessage() + " (" + lines.toString() + ")");
        }
        throw new IllegalArgumentException("Errors while parsing knowledge base");
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return kbase;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 24 with KnowledgeBuilderError

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

the class PersistentStatefulSessionTest method testPersistenceVariables.

@Test
public void testPersistenceVariables() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("VariablesProcess.rf"), ResourceType.DRF);
    for (KnowledgeBuilderError error : kbuilder.getErrors()) {
        logger.debug(error.toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    long id = ksession.getIdentifier();
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("name", "John Doe");
    ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess", parameters);
    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    assertEquals("John Doe", workItem.getParameter("name"));
    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNotNull(processInstance);
    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    assertEquals("John Doe", workItem.getParameter("text"));
    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNotNull(processInstance);
    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNull(workItem);
    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNull(processInstance);
}
Also used : TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) HashMap(java.util.HashMap) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) WorkItem(org.kie.api.runtime.process.WorkItem) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 25 with KnowledgeBuilderError

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

the class HumanTaskResolver method createKBuilder.

private KnowledgeBuilder createKBuilder(String resource, ResourceType resourceType) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource(resource), resourceType);
    if (kbuilder.hasErrors()) {
        int errors = kbuilder.getErrors().size();
        if (errors > 0) {
            System.out.println("Found " + errors + " errors");
            for (KnowledgeBuilderError error : kbuilder.getErrors()) {
                System.out.println(error.getMessage());
            }
        }
        throw new IllegalArgumentException("Application process definition has errors, see log for more details");
    }
    return kbuilder;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder)

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