use of org.jbpm.process.instance.impl.Action in project jbpm by kiegroup.
the class VariablePersistenceStrategyTest method getKnowledgeBaseForExtendingInterfaceVariablePersistence.
private KieBase getKnowledgeBaseForExtendingInterfaceVariablePersistence(String processId, final String variableText) {
RuleFlowProcess process = new RuleFlowProcess();
process.setId(processId);
List<Variable> variables = new ArrayList<Variable>();
Variable variable = new Variable();
variable.setName("x");
ObjectDataType extendingSerializableDataType = new ObjectDataType();
extendingSerializableDataType.setClassName(MyVariableExtendingSerializable.class.getName());
variable.setType(extendingSerializableDataType);
variables.add(variable);
process.getVariableScope().setVariables(variables);
StartNode startNode = new StartNode();
startNode.setName("Start");
startNode.setId(1);
WorkItemNode workItemNode = new WorkItemNode();
workItemNode.setName("workItemNode");
workItemNode.setId(2);
Work work = new WorkImpl();
work.setName("MyWork");
workItemNode.setWork(work);
ActionNode actionNode = new ActionNode();
actionNode.setName("Print");
DroolsAction action = new DroolsConsequenceAction("java", null);
action.setMetaData("Action", new Action() {
public void execute(ProcessContext context) throws Exception {
Assert.assertEquals(variableText, ((MyVariableExtendingSerializable) context.getVariable("x")).getText());
;
}
});
actionNode.setAction(action);
actionNode.setId(3);
EndNode endNode = new EndNode();
endNode.setName("EndNode");
endNode.setId(4);
connect(startNode, workItemNode);
connect(workItemNode, actionNode);
connect(actionNode, endNode);
process.addNode(startNode);
process.addNode(workItemNode);
process.addNode(actionNode);
process.addNode(endNode);
KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
((KnowledgeBaseImpl) kbase).addProcess(process);
return kbase;
}
use of org.jbpm.process.instance.impl.Action in project jbpm by kiegroup.
the class DefaultExceptionScopeInstance method handleException.
public void handleException(ExceptionHandler handler, String exception, Object params) {
if (handler instanceof ActionExceptionHandler) {
ActionExceptionHandler exceptionHandler = (ActionExceptionHandler) handler;
Action action = (Action) exceptionHandler.getAction().getMetaData("Action");
try {
ProcessInstance processInstance = getProcessInstance();
ProcessContext processContext = new ProcessContext(processInstance.getKnowledgeRuntime());
ContextInstanceContainer contextInstanceContainer = getContextInstanceContainer();
if (contextInstanceContainer instanceof NodeInstance) {
processContext.setNodeInstance((NodeInstance) contextInstanceContainer);
} else {
processContext.setProcessInstance(processInstance);
}
String faultVariable = exceptionHandler.getFaultVariable();
if (faultVariable != null) {
processContext.setVariable(faultVariable, params);
}
action.execute(processContext);
} catch (Exception e) {
throw new RuntimeException("unable to execute Action", e);
}
} else {
throw new IllegalArgumentException("Unknown exception handler " + handler);
}
}
use of org.jbpm.process.instance.impl.Action in project jbpm by kiegroup.
the class ProcessEventSupportTest method testProcessEventListenerWithEvent.
@Test
public void testProcessEventListenerWithEvent() throws Exception {
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
// create a simple package with one process to test the events
final InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.test");
RuleFlowProcess process = new RuleFlowProcess();
process.setId("org.drools.core.process.event");
process.setName("Event Process");
StartNode startNode = new StartNode();
startNode.setName("Start");
startNode.setId(1);
process.addNode(startNode);
ActionNode actionNode = new ActionNode();
actionNode.setName("Print");
DroolsAction action = new DroolsConsequenceAction("java", null);
action.setMetaData("Action", new Action() {
public void execute(ProcessContext context) throws Exception {
logger.info("Executed action");
}
});
actionNode.setAction(action);
actionNode.setId(2);
process.addNode(actionNode);
new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
EventNode eventNode = new EventNode();
eventNode.setName("Event");
eventNode.setId(3);
List<EventFilter> filters = new ArrayList<EventFilter>();
EventTypeFilter filter = new EventTypeFilter();
filter.setType("signal");
filters.add(filter);
eventNode.setEventFilters(filters);
process.addNode(eventNode);
new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, eventNode, Node.CONNECTION_DEFAULT_TYPE);
EndNode endNode = new EndNode();
endNode.setName("End");
endNode.setId(4);
process.addNode(endNode);
new ConnectionImpl(eventNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
pkg.addProcess(process);
List<KiePackage> pkgs = new ArrayList<KiePackage>();
pkgs.add(pkg);
kbase.addPackages(pkgs);
KieSession session = kbase.newKieSession();
final List<ProcessEvent> processEventList = new ArrayList<ProcessEvent>();
final ProcessEventListener processEventListener = new ProcessEventListener() {
public void afterNodeLeft(ProcessNodeLeftEvent event) {
processEventList.add(event);
}
public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
processEventList.add(event);
}
public void afterProcessCompleted(ProcessCompletedEvent event) {
processEventList.add(event);
}
public void afterProcessStarted(ProcessStartedEvent event) {
processEventList.add(event);
}
public void beforeNodeLeft(ProcessNodeLeftEvent event) {
processEventList.add(event);
}
public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
processEventList.add(event);
}
public void beforeProcessCompleted(ProcessCompletedEvent event) {
processEventList.add(event);
}
public void beforeProcessStarted(ProcessStartedEvent event) {
processEventList.add(event);
}
public void beforeVariableChanged(ProcessVariableChangedEvent event) {
processEventList.add(event);
}
public void afterVariableChanged(ProcessVariableChangedEvent event) {
processEventList.add(event);
}
};
session.addEventListener(processEventListener);
// execute the process
ProcessInstance pi = session.startProcess("org.drools.core.process.event");
pi.signalEvent("signal", null);
assertEquals(20, processEventList.size());
assertEquals("org.drools.core.process.event", ((ProcessStartedEvent) processEventList.get(0)).getProcessInstance().getProcessId());
assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(1)).getNodeInstance().getNodeName());
assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(2)).getNodeInstance().getNodeName());
assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(3)).getNodeInstance().getNodeName());
assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(4)).getNodeInstance().getNodeName());
assertEquals("Event", ((ProcessNodeTriggeredEvent) processEventList.get(5)).getNodeInstance().getNodeName());
assertEquals("Event", ((ProcessNodeTriggeredEvent) processEventList.get(6)).getNodeInstance().getNodeName());
assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(7)).getNodeInstance().getNodeName());
assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(8)).getNodeInstance().getNodeName());
assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(9)).getNodeInstance().getNodeName());
assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(10)).getNodeInstance().getNodeName());
assertEquals("org.drools.core.process.event", ((ProcessStartedEvent) processEventList.get(11)).getProcessInstance().getProcessId());
assertEquals("Event", ((ProcessNodeLeftEvent) processEventList.get(12)).getNodeInstance().getNodeName());
assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(13)).getNodeInstance().getNodeName());
assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(14)).getNodeInstance().getNodeName());
assertEquals("org.drools.core.process.event", ((ProcessCompletedEvent) processEventList.get(15)).getProcessInstance().getProcessId());
assertEquals("org.drools.core.process.event", ((ProcessCompletedEvent) processEventList.get(16)).getProcessInstance().getProcessId());
assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(17)).getNodeInstance().getNodeName());
assertEquals("Event", ((ProcessNodeLeftEvent) processEventList.get(19)).getNodeInstance().getNodeName());
assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(18)).getNodeInstance().getNodeName());
}
use of org.jbpm.process.instance.impl.Action in project jbpm by kiegroup.
the class ProcessEventSupportTest method testProcessEventListenerWithStartEvent.
@Test
public void testProcessEventListenerWithStartEvent() throws Exception {
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
// create a simple package with one process to test the events
final InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.test");
RuleFlowProcess process = new RuleFlowProcess();
process.setId("org.drools.core.process.event");
process.setName("Event Process");
StartNode startNode = new StartNode();
startNode.setName("Start");
startNode.setId(1);
EventTrigger trigger = new EventTrigger();
EventTypeFilter eventFilter = new EventTypeFilter();
eventFilter.setType("signal");
trigger.addEventFilter(eventFilter);
startNode.addTrigger(trigger);
process.addNode(startNode);
ActionNode actionNode = new ActionNode();
actionNode.setName("Print");
DroolsAction action = new DroolsConsequenceAction("java", null);
action.setMetaData("Action", new Action() {
public void execute(ProcessContext context) throws Exception {
logger.info("Executed action");
}
});
actionNode.setAction(action);
actionNode.setId(2);
process.addNode(actionNode);
new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
EndNode endNode = new EndNode();
endNode.setName("End");
endNode.setId(3);
process.addNode(endNode);
new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
pkg.addProcess(process);
List<KiePackage> pkgs = new ArrayList<KiePackage>();
pkgs.add(pkg);
kbase.addPackages(pkgs);
KieSession session = kbase.newKieSession();
final List<ProcessEvent> processEventList = new ArrayList<ProcessEvent>();
final ProcessEventListener processEventListener = new ProcessEventListener() {
public void afterNodeLeft(ProcessNodeLeftEvent event) {
processEventList.add(event);
}
public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
processEventList.add(event);
}
public void afterProcessCompleted(ProcessCompletedEvent event) {
processEventList.add(event);
}
public void afterProcessStarted(ProcessStartedEvent event) {
processEventList.add(event);
}
public void beforeNodeLeft(ProcessNodeLeftEvent event) {
processEventList.add(event);
}
public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
processEventList.add(event);
}
public void beforeProcessCompleted(ProcessCompletedEvent event) {
processEventList.add(event);
}
public void beforeProcessStarted(ProcessStartedEvent event) {
processEventList.add(event);
}
public void beforeVariableChanged(ProcessVariableChangedEvent event) {
processEventList.add(event);
}
public void afterVariableChanged(ProcessVariableChangedEvent event) {
processEventList.add(event);
}
};
session.addEventListener(processEventListener);
// execute the process
// session.startProcess("org.drools.core.process.event");
session.signalEvent("signal", null);
assertEquals(16, processEventList.size());
assertEquals("org.drools.core.process.event", ((ProcessStartedEvent) processEventList.get(0)).getProcessInstance().getProcessId());
assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(1)).getNodeInstance().getNodeName());
assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(2)).getNodeInstance().getNodeName());
assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(3)).getNodeInstance().getNodeName());
assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(4)).getNodeInstance().getNodeName());
assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(5)).getNodeInstance().getNodeName());
assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(6)).getNodeInstance().getNodeName());
assertEquals("org.drools.core.process.event", ((ProcessCompletedEvent) processEventList.get(7)).getProcessInstance().getProcessId());
assertEquals("org.drools.core.process.event", ((ProcessCompletedEvent) processEventList.get(8)).getProcessInstance().getProcessId());
assertEquals("End", ((ProcessNodeLeftEvent) processEventList.get(9)).getNodeInstance().getNodeName());
assertEquals("End", ((ProcessNodeTriggeredEvent) processEventList.get(10)).getNodeInstance().getNodeName());
assertEquals("Print", ((ProcessNodeLeftEvent) processEventList.get(11)).getNodeInstance().getNodeName());
assertEquals("Print", ((ProcessNodeTriggeredEvent) processEventList.get(12)).getNodeInstance().getNodeName());
assertEquals("Start", ((ProcessNodeLeftEvent) processEventList.get(13)).getNodeInstance().getNodeName());
assertEquals("Start", ((ProcessNodeTriggeredEvent) processEventList.get(14)).getNodeInstance().getNodeName());
assertEquals("org.drools.core.process.event", ((ProcessStartedEvent) processEventList.get(15)).getProcessInstance().getProcessId());
}
use of org.jbpm.process.instance.impl.Action in project jbpm by kiegroup.
the class CompensationTest method createBoundaryEventCompensationHandler.
private void createBoundaryEventCompensationHandler(org.jbpm.workflow.core.NodeContainer nodeContainer, Node attachedToNode, final List<String> eventList, final String id) throws Exception {
NodeCreator<BoundaryEventNode> boundaryNodeCreator = new NodeCreator<BoundaryEventNode>(nodeContainer, BoundaryEventNode.class);
BoundaryEventNode boundaryNode = boundaryNodeCreator.createNode("boundary" + id);
String attachedTo = (String) attachedToNode.getMetaData().get("UniqueId");
boundaryNode.setMetaData("AttachedTo", attachedTo);
boundaryNode.setAttachedToNodeId(attachedTo);
EventTypeFilter eventFilter = new NonAcceptingEventTypeFilter();
eventFilter.setType("Compensation");
List<EventFilter> eventFilters = new ArrayList<EventFilter>();
boundaryNode.setEventFilters(eventFilters);
eventFilters.add(eventFilter);
addCompensationScope(boundaryNode, nodeContainer, attachedTo);
NodeCreator<ActionNode> actionNodeCreator = new NodeCreator<ActionNode>(nodeContainer, ActionNode.class);
ActionNode actionNode = actionNodeCreator.createNode("handlerAction" + id);
actionNode.setMetaData("isForCompensation", true);
actionNode.setName("Execute");
DroolsAction action = new DroolsConsequenceAction("java", null);
action.setMetaData("Action", new Action() {
public void execute(ProcessContext context) throws Exception {
eventList.add("action" + id);
}
});
actionNode.setAction(action);
connect(boundaryNode, actionNode);
}
Aggregations