Search in sources :

Example 6 with KnowledgeBuilderError

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

the class DynamicEvalTest method loadPackages.

public void loadPackages(Resource res, ResourceType type) {
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(res, type);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println(error);
        }
        throw new IllegalArgumentException("Could not parse knowledge.");
    }
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors)

Example 7 with KnowledgeBuilderError

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

the class VerifierKnowledgeBaseBuilder method newVerifierKnowledgeBase.

public KieBase newVerifierKnowledgeBase(VerifierConfiguration configuration) {
    InternalKnowledgeBase verifierKnowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    KnowledgeBuilderConfiguration kbuilderConfiguration = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    kbuilderConfiguration.setProperty("drools.dialect.java.compiler", "JANINO");
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbuilderConfiguration);
    if (configuration.getVerifyingResources() != null) {
        for (Resource resource : configuration.getVerifyingResources().keySet()) {
            kbuilder.add(resource, configuration.getVerifyingResources().get(resource));
        }
    }
    if (kbuilder.hasErrors()) {
        Iterator<KnowledgeBuilderError> errors = kbuilder.getErrors().iterator();
        while (errors.hasNext()) {
            this.errors.add(new VerifierError("Error compiling verifier rules: " + errors.next().getMessage()));
        }
    }
    verifierKnowledgeBase.addPackages(kbuilder.getKnowledgePackages());
    return verifierKnowledgeBase;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) VerifierError(org.drools.verifier.VerifierError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Resource(org.kie.api.io.Resource) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 8 with KnowledgeBuilderError

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

the class VariablePersistenceStrategyTest method createKnowledgeBase.

private KieBase createKnowledgeBase(String flowFile) {
    KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    conf.setProperty("drools.dialect.java.compiler", "JANINO");
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(conf);
    kbuilder.add(new ClassPathResource(flowFile), ResourceType.DRF);
    if (kbuilder.hasErrors()) {
        StringBuilder errorMessage = new StringBuilder();
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            errorMessage.append(error.getMessage());
            errorMessage.append(System.getProperty("line.separator"));
        }
        fail(errorMessage.toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return kbase;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 9 with KnowledgeBuilderError

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

the class SimpleRuntimeEnvironment method addAsset.

/**
 * Adds given asset to knowledge builder to produce KieBase
 * @param resource asset to be added
 * @param type type of the asset
 */
public void addAsset(Resource resource, ResourceType type) {
    /**
     * The code below (CSV/XLS) was added because of timelines related to switchyard/fuse.
     *
     * However, it is an ugly hack: As soon as is possible, the code below should be removed or refactored.
     * - an "addAsset(Resource, ResourceType, ResourceConfiguration)" method should be added to this implementation
     * - or the kbuilder code should be refactored so that there are two ResourceTypes: CSV and XLS
     *
     * (refactoring the kbuilder code is probably a better idea.)
     */
    boolean replaced = false;
    if (resource.getSourcePath() != null) {
        String path = resource.getSourcePath();
        String typeStr = null;
        if (path.toLowerCase().endsWith(".csv")) {
            typeStr = DecisionTableInputType.CSV.toString();
        } else if (path.toLowerCase().endsWith(".xls")) {
            typeStr = DecisionTableInputType.XLS.toString();
        }
        if (typeStr != null) {
            String worksheetName = null;
            boolean replaceConfig = true;
            ResourceConfiguration config = resource.getConfiguration();
            if (config != null && config instanceof DecisionTableConfiguration) {
                DecisionTableInputType realType = DecisionTableInputType.valueOf(typeStr);
                if (((DecisionTableConfiguration) config).getInputType().equals(realType)) {
                    replaceConfig = false;
                } else {
                    worksheetName = ((DecisionTableConfiguration) config).getWorksheetName();
                }
            }
            if (replaceConfig) {
                Properties prop = new Properties();
                prop.setProperty(ResourceTypeImpl.KIE_RESOURCE_CONF_CLASS, DecisionTableConfigurationImpl.class.getName());
                prop.setProperty(DecisionTableConfigurationImpl.DROOLS_DT_TYPE, typeStr);
                if (worksheetName != null) {
                    prop.setProperty(DecisionTableConfigurationImpl.DROOLS_DT_WORKSHEET, worksheetName);
                }
                ResourceConfiguration conf = ResourceTypeImpl.fromProperties(prop);
                this.kbuilder.add(resource, type, conf);
                replaced = true;
            }
        }
    }
    if (!replaced) {
        this.kbuilder.add(resource, type);
    }
    if (this.kbuilder.hasErrors()) {
        StringBuffer errorMessage = new StringBuffer();
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            errorMessage.append(error.getMessage() + ",");
        }
        this.kbuilder.undo();
        throw new IllegalArgumentException("Cannot add asset: " + errorMessage.toString());
    }
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) DecisionTableInputType(org.kie.internal.builder.DecisionTableInputType) Properties(java.util.Properties) DecisionTableConfigurationImpl(org.drools.core.builder.conf.impl.DecisionTableConfigurationImpl) ResourceConfiguration(org.kie.api.io.ResourceConfiguration)

Example 10 with KnowledgeBuilderError

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

the class StandaloneBPMNProcessTest method testCallActivity.

@Test
public void testCallActivity() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("BPMN2-CallActivity.bpmn2"), ResourceType.BPMN2);
    kbuilder.add(ResourceFactory.newClassPathResource("BPMN2-CallActivitySubProcess.bpmn2"), ResourceType.BPMN2);
    if (!kbuilder.getErrors().isEmpty()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            logger.error("{}", error);
        }
        throw new IllegalArgumentException("Errors while parsing knowledge base");
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createKnowledgeSession(kbase);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", "oldValue");
    ProcessInstance processInstance = ksession.startProcess("ParentProcess", params);
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    assertThat(((WorkflowProcessInstance) processInstance).getVariable("y")).isEqualTo("new value");
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) HashMap(java.util.HashMap) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) 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