Java Code Examples for org.kie.api.io.Resource#setSourcePath()

The following examples show how to use org.kie.api.io.Resource#setSourcePath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemoryFileSystem.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public void setFileContents(MemoryFile file, Resource resource) throws IOException {
    if ( !existsFolder( (MemoryFolder) file.getFolder() ) ) {
        createFolder( (MemoryFolder) file.getFolder() );
    }

    String fileName = file.getPath().toPortableString();
    if (modifiedFilesSinceLastMark != null) {
        byte[] contents = resourceToBytes( resource );
        byte[] oldContent = resourceToBytes( fileContents.get( fileName ) );
        if (oldContent == null || !Arrays.equals(oldContent, contents)) {
            modifiedFilesSinceLastMark.add(fileName);
        }
    }
    fileContents.put( fileName, (InternalResource) resource );
    resource.setSourcePath( file.getPath().toPortableString() );
    folders.get( file.getFolder().getPath().toPortableString() ).add( file );
}
 
Example 2
Source File: BigRuleSetCodegenTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private Collection<Resource> generateResourcesToBeCompiled(int numberOfResources, int rulesPerResource) {
    Collection<Resource> resources = new ArrayList<>();
    for (int i = 0; i < numberOfResources; i++) {
        Resource resource = new ByteArrayResource( generateRules( "org.kie.kogito.codegen.test" + i, rulesPerResource ).getBytes() );
        resource.setResourceType( ResourceType.DRL );
        resource.setSourcePath( "org/kie/kogito/codegen/test" + i + "/rules.drl" );
        resources.add(resource);
    }
    return resources;
}
 
Example 3
Source File: ProcessFactoryTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessFactory() throws Exception {
    RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.process");
    factory
            // header
            .name("My process")
            .packageName("org.jbpm")
            // nodes
            .startNode(1)
                .name("Start")
                .done()
            .actionNode(2)
                .name("Action")
                .action("java",
                        "System.out.println(\"Action\");")
                .done()
            .endNode(3)
                .name("End")
                .done()
            // connections
            .connection(1, 2)
            .connection(2, 3);
    RuleFlowProcess process = factory.validate().getProcess();
    Resource res = ResourceFactory.newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes());
    res.setSourcePath("/tmp/processFactory.bpmn2"); // source path or target path must be set to be added into kbase
    KieBase kbase = createKnowledgeBaseFromResources(res);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ksession.startProcess("org.jbpm.process");
    ksession.dispose();
}
 
Example 4
Source File: ProcessFactoryTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
@Timeout(10)
public void testActionNodeIsDroolsAction() throws Exception {
    RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.process");
    factory
            .name("ActionNodeActionProcess")
            .version("1")
            .startNode(1)
                .name("Start")
                .done()
            .endNode(3)
                .name("End")
                .done()
            .actionNode(2)
                .name("printTextActionNode")
                .action("java",
                        "System.out.println(\"test print\");",
                        true)
                .done()
            .connection(1, 2)
            .connection(2, 3);
    RuleFlowProcess process = factory.validate().getProcess();

    assertNotNull(process);

    Resource res = ResourceFactory.newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes());
    res.setSourcePath("/tmp/processFactory.bpmn2");
    KieBase kbase = createKnowledgeBaseFromResources(res);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ProcessInstance pi = ksession.startProcess("org.jbpm.process");

    assertNotNull(pi);

    assertEquals(ProcessInstance.STATE_COMPLETED,
                 pi.getState());

    ksession.dispose();
}
 
Example 5
Source File: StartEventTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
@Timeout(10)
public void testTimerStartDateISO() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("StartProcess", 1);
    byte[] content = IoUtils.readBytesFromInputStream(this.getClass().getResourceAsStream("/BPMN2-TimerStartDate.bpmn2"));
    String processContent = new String(content, "UTF-8");

    OffsetDateTime plusTwoSeconds = OffsetDateTime.now().plusSeconds(2);

    processContent = processContent.replaceFirst("#\\{date\\}", plusTwoSeconds.toString());
    Resource resource = ResourceFactory.newReaderResource(new StringReader(processContent));
    resource.setSourcePath("/BPMN2-TimerStartDate.bpmn2");
    resource.setTargetPath("/BPMN2-TimerStartDate.bpmn2");
    KieBase kbase = createKnowledgeBaseFromResources(resource);

    ksession = createKnowledgeSession(kbase);
    ksession.addEventListener(countDownListener);
    final List<String> list = new ArrayList<>();
    ksession.addEventListener(new DefaultProcessEventListener() {
        public void beforeProcessStarted(ProcessStartedEvent event) {
            list.add(event.getProcessInstance().getId());
        }
    });
    assertThat(list.size()).isEqualTo(0);
    countDownListener.waitTillCompleted();
    assertThat(list.size()).isEqualTo(1);

}
 
Example 6
Source File: StandaloneBPMNProcessTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testXXEProcessVulnerability() throws Exception {
	Resource processResource = ResourceFactory.newClassPathResource("xxe-protection/BPMN2-XXE-Process.bpmn2");
	
	File dtdFile = new File("src/test/resources/xxe-protection/external.dtd");
    assertThat(dtdFile).exists();
	
	String dtdContent = IoUtils.readFileAsString(dtdFile);
	dtdContent = dtdContent.replaceAll("@@PATH@@", dtdFile.getParentFile().getAbsolutePath());
	
	IoUtils.write(dtdFile, dtdContent.getBytes("UTF-8"));
	
	byte[] data = IoUtils.readBytesFromInputStream(processResource.getInputStream());
	String processAsString = new String(data, "UTF-8");
	// replace place holders with actual paths
	File testFiles = new File("src/test/resources/xxe-protection");

    assertThat(testFiles).exists();
	
	String path = testFiles.getAbsolutePath();
	processAsString = processAsString.replaceAll("@@PATH@@", path);
	
	Resource resource = ResourceFactory.newReaderResource(new StringReader(processAsString));
	resource.setSourcePath(processResource.getSourcePath());
	resource.setTargetPath(processResource.getTargetPath());
	
    KieBase kbase = createKnowledgeBaseFromResources(resource);
    KieSession ksession = createKnowledgeSession(kbase);
    ProcessInstance processInstance = ksession.startProcess("async-examples.bp1");
    
    String var1 = getProcessVarValue(processInstance, "testScript1");
    String var2 = getProcessVarValue(processInstance, "testScript2");

    assertThat(var1).isNull();
    assertThat(var2).isNull();
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_COMPLETED);
}
 
Example 7
Source File: KieHelper.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public KieHelper addResource(Resource resource, ResourceType type) {
    if (resource.getSourcePath() == null && resource.getTargetPath() == null) {
        resource.setSourcePath(generateResourceName(type));
    }
    return addResource(resource);
}
 
Example 8
Source File: ProcessFactoryTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testCompositeNode() throws Exception {
    RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.process");
    factory
            // header
            .name("My process")
            .packageName("org.jbpm")
            // nodes
            .startNode(1)
                .name("Start")
                .done()
            .compositeContextNode(2)
                .name("SubProcess")
                .startNode(1)
                    .name("SubProcess Start")
                    .done()
                .actionNode(2)
                    .name("SubProcess Action")
                    .action("java",
                            "System.out.println(\"SubProcess Action\");")
                    .done()
                .endNode(3)
                    .name("SubProcess End")
                    .terminate(true)
                    .done()
                .connection(1, 2)
                .connection(2, 3)
                .done()
            .endNode(3)
                .name("End")
                .done()
            // connections
            .connection(1, 2)
            .connection(2, 3);
    RuleFlowProcess process = factory.validate().getProcess();

    assertEquals("SubProcess",
                 process.getNode(2).getName());

    Resource res = ResourceFactory.newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes());
    res.setSourcePath("/tmp/processFactory.bpmn2"); // source path or target path must be set to be added into kbase
    KieBase kbase = createKnowledgeBaseFromResources(res);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ProcessInstance pi = ksession.startProcess("org.jbpm.process");

    assertEquals(ProcessInstance.STATE_COMPLETED,
                 pi.getState());

    ksession.dispose();
}
 
Example 9
Source File: ProcessFactoryTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
@Timeout(10)
public void testBoundaryTimerTimeCycle() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("BoundaryTimerEvent",
                                                                                                        1);
    String timeCycle = "1s###5s";
    RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.process");
    factory
            // header
            .name("My process")
            .packageName("org.jbpm")
            // nodes
            .startNode(1)
                .name("Start")
                .done()
            .humanTaskNode(2)
                .name("Task")
                .actorId("john")
                .taskName("MyTask")
                .done()
            .endNode(3)
                .name("End1")
                .terminate(false)
                .done()
            .boundaryEventNode(4)
                .name("BoundaryTimerEvent")
                .attachedTo(2)
                .metaData(TIME_CYCLE, timeCycle)
                .metaData(CANCEL_ACTIVITY, false)
                .eventType(EVENT_TYPE_TIMER, timeCycle)
                .done()
            .endNode(5)
                .name("End2")
                .terminate(false)
                .done()
            // connections
            .connection(1, 2)
            .connection(2, 3)
            .connection(4, 5);
    RuleFlowProcess process = factory.validate().getProcess();

    Resource res = ResourceFactory.newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes());
    res.setSourcePath("/tmp/processFactory.bpmn2"); // source path or target path must be set to be added into kbase
    KieBase kbase = createKnowledgeBaseFromResources(res);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler testHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
                                                          testHandler);
    ksession.addEventListener(countDownListener);

    ProcessInstance pi = ksession.startProcess("org.jbpm.process");
    assertProcessInstanceActive(pi);

    countDownListener.waitTillCompleted(); // wait for boundary timer firing

    assertNodeTriggered(pi.getId(),
                        "End2");
    assertProcessInstanceActive(pi); // still active because CancelActivity = false

    ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(),
                                                   null);
    assertProcessInstanceCompleted(pi);

    ksession.dispose();
}
 
Example 10
Source File: ProcessFactoryTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
@Timeout(10)
public void testBoundaryTimerTimeDuration() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("BoundaryTimerEvent",
                                                                                                        1);
    String timeDuration = "1s";
    RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.process");
    factory
            // header
            .name("My process")
            .packageName("org.jbpm")
            // nodes
            .startNode(1)
                .name("Start")
                .done()
            .humanTaskNode(2)
                .name("Task")
                .actorId("john")
                .taskName("MyTask")
                .done()
            .endNode(3)
                .name("End1")
                .terminate(false)
                .done()
            .boundaryEventNode(4)
                .name("BoundaryTimerEvent")
                .attachedTo(2)
                .metaData(TIME_DURATION, timeDuration)
                .metaData(CANCEL_ACTIVITY, false)
                .eventType(EVENT_TYPE_TIMER, timeDuration)
                .done()
            .endNode(5)
                .name("End2")
                .terminate(false)
                .done()
            // connections
            .connection(1, 2)
            .connection(2, 3)
            .connection(4, 5);
    RuleFlowProcess process = factory.validate().getProcess();

    Resource res = ResourceFactory.newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes());
    res.setSourcePath("/tmp/processFactory.bpmn2"); // source path or target path must be set to be added into kbase
    KieBase kbase = createKnowledgeBaseFromResources(res);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler testHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
                                                          testHandler);
    ksession.addEventListener(countDownListener);

    ProcessInstance pi = ksession.startProcess("org.jbpm.process");
    assertProcessInstanceActive(pi);

    countDownListener.waitTillCompleted(); // wait for boundary timer firing

    assertNodeTriggered(pi.getId(),
                        "End2");
    assertProcessInstanceActive(pi); // still active because CancelActivity = false

    ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(),
                                                   null);
    assertProcessInstanceCompleted(pi);

    ksession.dispose();
}