Search in sources :

Example 6 with ReaderResource

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

the class ProcessActionTest method testActionVariableMVEL.

@Test
public void testActionVariableMVEL() {
    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=\"mvel\" >System.out.println(\"Triggered\");\n" + "list.add(person.name);\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 7 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 8 with ReaderResource

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

the class ProcessUpgradeTest method testMappingUpgrade.

@Test
public void testMappingUpgrade() throws Exception {
    String rule = "package org.test;\n";
    rule += "import org.jbpm.integrationtests.test.Person\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Rule 1\"\n";
    rule += "  ruleflow-group \"hello\"\n";
    rule += "when\n";
    rule += "    $p : Person( ) \n";
    rule += "then\n";
    rule += "    list.add( $p );\n";
    rule += "end";
    KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder.add(new ReaderResource(new StringReader(rule)), ResourceType.DRL);
    String process = "<?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=\"ruleflow\" id=\"org.test.ruleflow\" package-name=\"org.test\" >\n" + "  <header>\n" + "  </header>\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <workItem id=\"2\" name=\"Hello\" >\n" + "      <work name=\"Human Task\" >\n" + "      </work>\n" + "    </workItem>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\"/>\n" + "    <connection from=\"2\" to=\"3\"/>\n" + "  </connections>\n" + "</process>";
    builder.add(new ReaderResource(new StringReader(process)), ResourceType.DRF);
    // RuleBaseConfiguration config = new RuleBaseConfiguration();
    // config.setRuleBaseUpdateHandler(null);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(builder.getKnowledgePackages());
    KieSession session = kbase.newKieSession();
    TestWorkItemHandler handler = new TestWorkItemHandler();
    session.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    List<String> list = new ArrayList<String>();
    session.setGlobal("list", list);
    Person p = new Person("bobba fet", 32);
    session.insert(p);
    ProcessInstance processInstance = (ProcessInstance) session.startProcess("org.test.ruleflow");
    assertEquals(1, session.getProcessInstances().size());
    String process2 = "<?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=\"ruleflow\" id=\"org.test.ruleflow2\" package-name=\"org.test\" >\n" + "  <header>\n" + "    <globals>\n" + "      <global identifier=\"list\" type=\"java.util.List\" />\n" + "    </globals>\n" + "  </header>\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <workItem id=\"102\" name=\"Hello\" >\n" + "      <work name=\"Human Task\" >\n" + "      </work>\n" + "    </workItem>\n" + "    <actionNode id=\"4\" name=\"Action\" >" + "      <action type=\"expression\" dialect=\"java\">System.out.println();\n" + "list.add(\"Executed\");</action>\n" + "    </actionNode>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"102\"/>\n" + "    <connection from=\"102\" to=\"4\"/>\n" + "    <connection from=\"4\" to=\"3\"/>\n" + "  </connections>\n" + "</process>";
    builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder.add(new ReaderResource(new StringReader(process2)), ResourceType.DRF);
    kbase.addPackages(builder.getKnowledgePackages());
    Map<String, Long> mapping = new HashMap<String, Long>();
    mapping.put("2", 102L);
    WorkflowProcessInstanceUpgrader.upgradeProcessInstance(session, processInstance.getId(), "org.test.ruleflow2", mapping);
    assertEquals("org.test.ruleflow2", processInstance.getProcessId());
    session.getWorkItemManager().completeWorkItem(handler.getWorkItem().getId(), null);
    assertEquals(1, list.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : TestWorkItemHandler(org.jbpm.integrationtests.handler.TestWorkItemHandler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ReaderResource(org.drools.core.io.impl.ReaderResource) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) Person(org.jbpm.integrationtests.test.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 9 with ReaderResource

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

the class KnowledgeBuilderImpl method addPackageFromXml.

/**
 * Load a rule package from XML source.
 *
 * @param reader
 * @throws DroolsParserException
 * @throws IOException
 */
public void addPackageFromXml(final Reader reader) throws DroolsParserException, IOException {
    this.resource = new ReaderResource(reader, ResourceType.XDRL);
    final XmlPackageReader xmlReader = new XmlPackageReader(this.configuration.getSemanticModules());
    xmlReader.getParser().setClassLoader(this.rootClassLoader);
    try {
        xmlReader.read(reader);
    } catch (final SAXException e) {
        throw new DroolsParserException(e.toString(), e.getCause());
    }
    addPackage(xmlReader.getPackageDescr());
    this.resource = null;
}
Also used : ReaderResource(org.drools.core.io.impl.ReaderResource) DroolsParserException(org.drools.drl.parser.DroolsParserException) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) SAXException(org.xml.sax.SAXException)

Example 10 with ReaderResource

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

the class AbstractBpmnFileIndexer method fillIndexBuilder.

/* (non-Javadoc)
     * @see org.kie.workbench.common.services.refactoring.backend.server.indexing.AbstractFileIndexer#fillIndexBuilder(org.uberfire.java.nio.file.Path)
     */
@Override
protected IndexBuilder 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<AbstractBpmnProcessDataEventListener> processDataList = buildProcessDefinition(bpmnStr, moduleClassLoader);
        if (processDataList != null) {
            for (AbstractBpmnProcessDataEventListener 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);
            AbstractBpmnProcessDataEventListener helper = (AbstractBpmnProcessDataEventListener) process.getMetaData().get(getProcessDescriptorName());
            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)

Aggregations

ReaderResource (org.drools.core.io.impl.ReaderResource)14 StringReader (java.io.StringReader)12 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 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)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 XmlProcessReader (org.jbpm.compiler.xml.XmlProcessReader)2