/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jbpm.pvm.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.jbpm.env.session.Message;
import org.jbpm.pvm.Condition;
import org.jbpm.pvm.Node;
import org.jbpm.pvm.Transition;
public class TakeTransitionOp implements AtomicOperation {
private static Logger log = Logger.getLogger(TakeTransitionOp.class.getName());
public boolean isAsync(ExecutionImpl execution) {
return execution.getNode().isLeaveAsync()
|| execution.getTransition().isTakeAsync();
}
public void perform(ExecutionImpl execution) {
TransitionImpl transition = execution.getTransition();
if (execution.getName()!=null) {
log.fine(execution.toString()+" takes "+transition);
} else {
log.fine("taking "+transition);
}
List<NodeImpl> leftNodes = getNodesLeft(execution.getNode(), transition.getDestination());
for (Node leftNode : leftNodes) {
execution.fire(Node.EVENT_NODE_LEAVE, leftNode);
}
execution.node = null;
execution.fire(Transition.EVENT_TRANSITION_TAKE, transition);
boolean wait = false;
Condition waitCondition = transition.getWaitCondition();
if (waitCondition!=null) {
wait = waitCondition.evaluate(execution);
}
if (!wait) {
execution.performAtomicOperation(ExecutionImpl.PROCEED_TO_DESTINATION);
}
}
List<NodeImpl> getNodesLeft(NodeImpl source, NodeImpl destination) {
List<NodeImpl> nodesLeft = new ArrayList<NodeImpl>();
if (source.equals(destination)) {
nodesLeft.add(source);
} else {
List<ObservableElementImpl> destinationChain = destination.getParentChain();
if (!destinationChain.contains(source)) {
NodeImpl sourceNode = source;
while ( (sourceNode!=null)
&& (!destinationChain.contains(sourceNode))
) {
nodesLeft.add(sourceNode);
sourceNode = sourceNode.getParentNode();
}
}
}
return nodesLeft;
}
public String toString() {
return "take-transition";
}
public Message createAsyncMessage(ExecutionImpl execution) {
return new TakeTransitionMessage(execution);
}
}