workingMemory.startProcess("org.drools.state");
// should be in state A
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
assertEquals(1, nodeInstances.size());
StateNodeInstance stateInstance = (StateNodeInstance) nodeInstances.iterator().next();
assertEquals("StateA", stateInstance.getNodeName());
// signal "toB" so we move to state B
processInstance.signalEvent("signal", "toB");
nodeInstances = processInstance.getNodeInstances();
assertEquals(1, nodeInstances.size());
stateInstance = (StateNodeInstance) nodeInstances.iterator().next();
assertEquals("StateB", stateInstance.getNodeName());
// if no constraint specified for a connection,
// we default to the name of the target node
// signal "StateA", so we move back to state A
processInstance.signalEvent("signal", "StateA");
nodeInstances = processInstance.getNodeInstances();
assertEquals(1, nodeInstances.size());
stateInstance = (StateNodeInstance) nodeInstances.iterator().next();
assertEquals("StateA", stateInstance.getNodeName());
// signal "toC" so we move to state C
processInstance.signalEvent("signal", "toC");
nodeInstances = processInstance.getNodeInstances();
assertEquals(1, nodeInstances.size());
stateInstance = (StateNodeInstance) nodeInstances.iterator().next();
assertEquals("StateC", stateInstance.getNodeName());
// signal something completely wrong, this should simply be ignored
processInstance.signalEvent("signal", "Invalid");
nodeInstances = processInstance.getNodeInstances();
assertEquals(1, nodeInstances.size());
stateInstance = (StateNodeInstance) nodeInstances.iterator().next();
assertEquals("StateC", stateInstance.getNodeName());
// signal "End", so we move to the end
processInstance.signalEvent("signal", "End");
nodeInstances = processInstance.getNodeInstances();
assertEquals(0, nodeInstances.size());
assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());