Search in sources :

Example 11 with ClassPathResource

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

the class AgendaRuleFlowGroupsTest method createSession.

private CommandBasedStatefulKnowledgeSession createSession(long id, String... rules) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    for (String rule : rules) {
        kbuilder.add(new ClassPathResource(rule), ResourceType.DRL);
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    kbase.addPackages(kbuilder.getKnowledgePackages());
    Environment env = createEnvironment(context);
    if (locking) {
        env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
    }
    if (id == -1) {
        return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    } else {
        return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) DroolsPersistenceUtil.createEnvironment(org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment) Environment(org.kie.api.runtime.Environment) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 12 with ClassPathResource

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

the class RuleFlowGroupRollbackTest method createSession.

private CommandBasedStatefulKnowledgeSession createSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("ruleflowgroup_rollback.drl"), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    kbase.addPackages(kbuilder.getKnowledgePackages());
    Environment env = createEnvironment(context);
    if (locking) {
        env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
    }
    return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) DroolsPersistenceUtil.createEnvironment(org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment) Environment(org.kie.api.runtime.Environment) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 13 with ClassPathResource

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

the class DroolsDeclaredTypeSequenceFlowExpressionTest method testDeclaredTypesInSequenceFlowDroolsExpression.

@Test
public void testDeclaredTypesInSequenceFlowDroolsExpression() {
    // DROOLS-1327
    String drl = "package org.drools.test;" + "declare TestFact \n" + "value: Integer\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();
    Resource drlResource = new InputStreamResource(new ByteArrayInputStream(drl.getBytes(StandardCharsets.UTF_8)));
    Resource bpmn2Resource = new ClassPathResource("BPMN2-DroolsDeclaredTypeSequenceFlowExpressionTest.bpmn2");
    ckbuilder.add(drlResource, ResourceType.DRL);
    ckbuilder.add(bpmn2Resource, ResourceType.BPMN2);
    ckbuilder.build();
    // Assert that we don't have any issues in the build.
    if (kbuilder.hasErrors()) {
        KnowledgeBuilderErrors kbErrors = kbuilder.getErrors();
        for (KnowledgeBuilderError nextError : kbErrors) {
            fail(nextError.getMessage());
        }
    }
    if (kbuilder.hasResults(ResultSeverity.WARNING)) {
        KnowledgeBuilderResults kbResults = kbuilder.getResults(ResultSeverity.WARNING);
        for (KnowledgeBuilderResult nextResult : kbResults) {
            fail(nextResult.getMessage());
        }
    }
}
Also used : KnowledgeBuilderResults(org.kie.internal.builder.KnowledgeBuilderResults) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Resource(org.kie.api.io.Resource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InputStreamResource(org.drools.core.io.impl.InputStreamResource) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult) Test(org.junit.Test)

Example 14 with ClassPathResource

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

the class PersistentStatefulSessionTest method testPersistenceWorkItems2.

@Test
public void testPersistenceWorkItems2() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("WorkItemsProcess.rf"), ResourceType.DRF);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    long id = ksession.getIdentifier();
    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();
    ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
    ksession.insert("TestString");
    logger.debug("Started process instance {}", processInstance.getId());
    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    ut.commit();
    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);
    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());
    assertEquals(1, ksession.getObjects().size());
    for (Object o : ksession.getObjects()) {
        logger.debug(o.toString());
    }
    assertNull(processInstance);
}
Also used : UserTransaction(javax.transaction.UserTransaction) TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkItem(org.kie.api.runtime.process.WorkItem) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InitialContext(javax.naming.InitialContext) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 15 with ClassPathResource

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

the class PersistentStatefulSessionTest method testPersistenceSubProcess.

@Test
public void testPersistenceSubProcess() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("SuperProcess.rf"), ResourceType.DRF);
    kbuilder.add(new ClassPathResource("SubProcess.rf"), ResourceType.DRF);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    long id = ksession.getIdentifier();
    ProcessInstance processInstance = ksession.startProcess("com.sample.SuperProcess");
    logger.debug("Started process instance {}", processInstance.getId());
    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    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);
    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("Process did not complete.", processInstance);
}
Also used : TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkItem(org.kie.api.runtime.process.WorkItem) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Aggregations

ClassPathResource (org.drools.core.io.impl.ClassPathResource)88 BpmnProcess (org.kie.kogito.process.bpmn2.BpmnProcess)45 Test (org.junit.jupiter.api.Test)38 BpmnVariables (org.kie.kogito.process.bpmn2.BpmnVariables)33 Test (org.junit.Test)24 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)21 HashMap (java.util.HashMap)16 ProcessMetaData (org.jbpm.compiler.canonical.ProcessMetaData)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)12 VerifierBuilder (org.drools.verifier.builder.VerifierBuilder)11 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)11 VerifierReport (org.drools.verifier.data.VerifierReport)10 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)10 WorkItem (org.kie.kogito.process.WorkItem)10 StaticIdentityProvider (org.kie.kogito.services.identity.StaticIdentityProvider)9 ArrayList (java.util.ArrayList)6 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)6 KieSession (org.kie.api.runtime.KieSession)6 SecurityPolicy (org.kie.kogito.auth.SecurityPolicy)6