Search in sources :

Example 1 with ReaderResource

use of org.drools.core.io.impl.ReaderResource in project drools by kiegroup.

the class KnowledgeBuilderImpl method addPackageFromDrl.

/**
 * Load a rule package from DRL source using the supplied DSL configuration.
 *
 * @param source The source of the rules.
 * @param dsl    The source of the domain specific language configuration.
 * @throws DroolsParserException
 * @throws IOException
 */
public void addPackageFromDrl(final Reader source, final Reader dsl) throws DroolsParserException, IOException {
    this.resource = new ReaderResource(source, ResourceType.DSLR);
    final DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    final PackageDescr pkg = parser.parse(source, dsl);
    this.results.addAll(parser.getErrors());
    if (!parser.hasErrors()) {
        addPackage(pkg);
    }
    this.resource = null;
}
Also used : DrlParser(org.drools.compiler.compiler.DrlParser) ReaderResource(org.drools.core.io.impl.ReaderResource) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 2 with ReaderResource

use of org.drools.core.io.impl.ReaderResource in project kie-wb-common by kiegroup.

the class BpmnFileIndexer method fillIndexBuilder.

/* (non-Javadoc)
     * @see org.kie.workbench.common.services.refactoring.backend.server.indexing.AbstractFileIndexer#fillIndexBuilder(org.uberfire.java.nio.file.Path)
     */
@Override
protected DefaultIndexBuilder fillIndexBuilder(Path path) throws Exception {
    final KieModule module = moduleService.resolveModule(Paths.convert(path));
    if (module == null) {
        logger.error("Unable to index " + path.toUri().toString() + ": module could not be resolved.");
        return null;
    }
    // responsible for basic index info: module name, branch, etc
    final DefaultIndexBuilder builder = getIndexBuilder(path, module);
    String bpmnStr = ioService.readAllString(path);
    ClassLoader moduleClassLoader = getModuleClassLoader(module);
    try {
        List<BpmnProcessDataEventListener> processDataList = buildProcessDefinition(bpmnStr, moduleClassLoader);
        if (processDataList != null) {
            for (BpmnProcessDataEventListener processData : processDataList) {
                addReferencedResourcesToIndexBuilder(builder, processData);
                builder.setPackageName(processData.getProcess().getPackageName());
            }
        }
    } catch (Exception e) {
        // log and ignore
        logger.info("Indexing hampered because BPMN2 compilation failed [" + path.toString() + "]: " + e.getMessage());
    }
    /**
     * IMPORTANT: sometimes the build of the BPMN2 might fail for minor reasons, including things like
     * a bad script in a script task
     *
     * When this happens, we (re)parse the process definition, but do not completely "build" it
     * (as in, what org.jbpm.compiler.ProcessBuilderImpl.buildProcess(Process, Resource) does).
     *
     * It *would* be more efficient to basically copy/paste the
     * jbpm-flow-builder org.jbpm.compiler.ProcessBuilderImpl.addProcessFromXml(Resource) logic here,
     * so that we do something like:
     *
     * 1. Use the XmlProcessReader to create a process
     * 2. *try* to build the rest of the process (and fail safely if not)
     * 3. do XmlProcessReader.getProcessBuildData().onBuildComplete(process)
     *    to complete collecting the information
     *
     * But... that's a high maintenance cost for this piece of software
     *
     * So until we can refactor the ProcessBuilderImpl logic (using functional logic for conditional handling?)
     * to be used here, let's keep it simple (as in, parsing the BPMN2 a second time when the build fails..)
     */
    // parse process definitions
    XmlProcessReader processReader = new XmlProcessReader(modules, moduleClassLoader);
    List<Process> processes = Collections.emptyList();
    try {
        processes = processReader.read(new StringReader(bpmnStr));
    } catch (Exception e) {
        logger.info("Unable to index because BPMN2 parsing failed [" + path.toString() + "]: " + e.getMessage());
    }
    // complete process definition processing
    if (processes != null) {
        for (Process process : processes) {
            Resource resource = new ReaderResource(new StringReader(bpmnStr));
            ProcessValidationError[] errors;
            ProcessValidator validator = ProcessValidatorRegistry.getInstance().getValidator(process, resource);
            errors = validator.validateProcess(process);
            if (errors.length > 0) {
                logger.error("Trying to finish indexing process '" + process.getId() + "/" + process.getName() + "' despite " + errors.length + " validation errors.");
            }
            processReader.getProcessBuildData().onBuildComplete(process);
            BpmnProcessDataEventListener helper = (BpmnProcessDataEventListener) process.getMetaData().get(BpmnProcessDataEventListener.NAME);
            addReferencedResourcesToIndexBuilder(builder, helper);
        }
    } else {
        logger.warn("No process was found in file: " + path.toUri());
    }
    return builder;
}
Also used : XmlProcessReader(org.jbpm.compiler.xml.XmlProcessReader) ReaderResource(org.drools.core.io.impl.ReaderResource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Resource(org.kie.api.io.Resource) Process(org.kie.api.definition.process.Process) DefaultIndexBuilder(org.kie.workbench.common.services.refactoring.backend.server.indexing.DefaultIndexBuilder) ReaderResource(org.drools.core.io.impl.ReaderResource) ProcessValidationError(org.jbpm.process.core.validation.ProcessValidationError) StringReader(java.io.StringReader) KieModule(org.kie.workbench.common.services.shared.project.KieModule) ProcessValidator(org.jbpm.process.core.validation.ProcessValidator)

Example 3 with ReaderResource

use of org.drools.core.io.impl.ReaderResource in project jbpm by kiegroup.

the class ProcessActionTest method testActionVariableJava.

@Test
public void testActionVariableJava() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <imports>\n" + "      <import name=\"org.jbpm.integrationtests.TestVariable\" />\n" + "    </imports>\n" + "    <globals>\n" + "      <global identifier=\"list\" type=\"java.util.List\" />\n" + "    </globals>\n" + "    <variables>\n" + "      <variable name=\"person\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.ObjectDataType\" className=\"org.jbpm.integrationtests.TestVariable\" />\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <actionNode id=\"2\" name=\"MyActionNode\" >\n" + "      <action type=\"expression\" dialect=\"java\" >System.out.println(\"Triggered\");\n" + "list.add(person.getName());\n" + "</action>\n" + "    </actionNode>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "\n" + "</process>");
    kbuilder.add(new ReaderResource(source), ResourceType.DRF);
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    TestVariable person = new TestVariable("John Doe");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("person", person);
    ProcessInstance processInstance = ksession.startProcess("org.drools.actions", params);
    assertEquals(1, list.size());
    assertEquals("John Doe", list.get(0));
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) ReaderResource(org.drools.core.io.impl.ReaderResource) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 4 with ReaderResource

use of org.drools.core.io.impl.ReaderResource in project jbpm by kiegroup.

the class ProcessActionTest method testActionContextJava.

@Test
public void testActionContextJava() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <imports>\n" + "      <import name=\"org.jbpm.integrationtests.test.Message\" />\n" + "    </imports>\n" + "    <globals>\n" + "      <global identifier=\"list\" type=\"java.util.List\" />\n" + "    </globals>\n" + "    <variables>\n" + "      <variable name=\"variable\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        <value>SomeText</value>\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <actionNode id=\"2\" name=\"MyActionNode\" >\n" + "      <action type=\"expression\" dialect=\"java\" >System.out.println(\"Triggered\");\n" + "String myVariable = (String) kcontext.getVariable(\"variable\");\n" + "list.add(myVariable);\n" + "String nodeName = kcontext.getNodeInstance().getNodeName();\n" + "list.add(nodeName);\n" + "insert( new Message() );\n" + "</action>\n" + "    </actionNode>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "\n" + "</process>");
    kbuilder.add(new ReaderResource(source), ResourceType.DRF);
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ProcessInstance processInstance = ksession.startProcess("org.drools.actions");
    assertEquals(2, list.size());
    assertEquals("SomeText", list.get(0));
    assertEquals("MyActionNode", list.get(1));
    Collection<FactHandle> factHandles = ksession.getFactHandles(new ObjectFilter() {

        public boolean accept(Object object) {
            return object instanceof Message;
        }
    });
    assertFalse(factHandles.isEmpty());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : Message(org.jbpm.integrationtests.test.Message) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) ReaderResource(org.drools.core.io.impl.ReaderResource) ObjectFilter(org.kie.api.runtime.ObjectFilter) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 5 with ReaderResource

use of org.drools.core.io.impl.ReaderResource in project jbpm by kiegroup.

the class ProcessActionTest method testActionContextMVEL.

@Test
public void testActionContextMVEL() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <imports>\n" + "      <import name=\"org.jbpm.integrationtests.test.Message\" />\n" + "    </imports>\n" + "    <globals>\n" + "      <global identifier=\"list\" type=\"java.util.List\" />\n" + "    </globals>\n" + "    <variables>\n" + "      <variable name=\"variable\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        <value>SomeText</value>\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <actionNode id=\"2\" name=\"MyActionNode\" >\n" + "      <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Triggered\");\n" + "System.out.println(kcontext.getKnowledgeRuntime());\n" + "String myVariable = (String) kcontext.getVariable(\"variable\");\n" + "list.add(myVariable);\n" + "String nodeName = kcontext.getNodeInstance().getNodeName();\n" + "list.add(nodeName);\n" + "insert( new Message() );\n" + "</action>\n" + "    </actionNode>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "\n" + "</process>");
    kbuilder.add(new ReaderResource(source), ResourceType.DRF);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ProcessInstance processInstance = ksession.startProcess("org.drools.actions");
    assertEquals(2, list.size());
    assertEquals("SomeText", list.get(0));
    assertEquals("MyActionNode", list.get(1));
    Collection<FactHandle> factHandles = ksession.getFactHandles(new ObjectFilter() {

        public boolean accept(Object object) {
            return object instanceof Message;
        }
    });
    assertFalse(factHandles.isEmpty());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : Message(org.jbpm.integrationtests.test.Message) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) ReaderResource(org.drools.core.io.impl.ReaderResource) ObjectFilter(org.kie.api.runtime.ObjectFilter) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

ReaderResource (org.drools.core.io.impl.ReaderResource)13 StringReader (java.io.StringReader)11 ArrayList (java.util.ArrayList)10 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)10 Test (org.junit.Test)10 KieSession (org.kie.api.runtime.KieSession)10 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)10 Reader (java.io.Reader)7 KieBase (org.kie.api.KieBase)7 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)7 HashMap (java.util.HashMap)4 TestWorkItemHandler (org.jbpm.integrationtests.handler.TestWorkItemHandler)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 Message (org.jbpm.integrationtests.test.Message)3 Person (org.jbpm.integrationtests.test.Person)3 ProcessInstance (org.jbpm.process.instance.ProcessInstance)3 ObjectFilter (org.kie.api.runtime.ObjectFilter)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)2 DrlParser (org.drools.compiler.compiler.DrlParser)1