use of org.apache.airavata.workflow.model.graph.DataPort in project airavata by apache.
the class WorkflowInterpreter method handleForEach.
private void handleForEach(Node node) throws WorkflowException {
final ForEachNode forEachNode = (ForEachNode) node;
EndForEachNode endForEachNode = null;
Collection<Node> repeatNodes = node.getOutputPort(0).getToNodes();
// we will support only one for now
if (repeatNodes.size() != 1) {
throw new WorkFlowInterpreterException("Only one node allowed inside foreach");
}
Iterator<Node> iterator = repeatNodes.iterator();
if (iterator.hasNext()) {
Node middleNode = iterator.next();
// output
if ((!(middleNode instanceof WSNode)) && (!(middleNode instanceof SubWorkflowNode))) {
throw new WorkFlowInterpreterException("Encountered Node inside foreach that is not a WSNode" + middleNode);
} else if (middleNode instanceof SubWorkflowNode) {
/* Get the EndforEach Node of the Subworkflow */
Iterator<Node> subWorkflowOut = middleNode.getOutputPort(0).getToNodes().iterator();
while (subWorkflowOut.hasNext()) {
Node node2 = subWorkflowOut.next();
if (node2 instanceof EndForEachNode) {
endForEachNode = (EndForEachNode) node2;
}
}
final LinkedList<String> listOfValues = new LinkedList<String>();
InterpreterUtil.getInputsForForEachNode(forEachNode, listOfValues, this.invokerMap);
final Integer[] inputNumbers = InterpreterUtil.getNumberOfInputsForForEachNode(forEachNode, this.invokerMap);
Workflow workflow1 = ((SubWorkflowNode) middleNode).getWorkflow();
List<NodeImpl> nodes = workflow1.getGraph().getNodes();
List<Node> wsNodes = new ArrayList<Node>();
/* Take the List of WSNodes in the subworkflow */
for (NodeImpl subWorkflowNode : nodes) {
if (subWorkflowNode instanceof WSNode) {
wsNodes.add(subWorkflowNode);
}
}
for (int i = 0; i < wsNodes.size(); i++) {
final WSNode node1 = (WSNode) wsNodes.get(i);
SystemComponentInvoker systemInvoker = null;
List<DataPort> outputPorts1 = node1.getOutputPorts();
List<Node> endForEachNodes = new ArrayList<Node>();
for (DataPort port : outputPorts1) {
Iterator<Node> endForEachNodeItr1 = port.getToNodes().iterator();
while (endForEachNodeItr1.hasNext()) {
Node node2 = endForEachNodeItr1.next();
if (node2 instanceof EndForEachNode) {
endForEachNodes.add(node2);
} else if (node2 instanceof OutputNode) {
// intentionally left noop
} else {
throw new WorkFlowInterpreterException("Found More than one node inside foreach");
}
}
}
final List<Node> finalEndForEachNodes = endForEachNodes;
Iterator<Node> endForEachNodeItr1 = node1.getOutputPort(0).getToNodes().iterator();
while (endForEachNodeItr1.hasNext()) {
Node node2 = endForEachNodeItr1.next();
// Start reading input came for foreach node
int parallelRuns = listOfValues.size() * node1.getOutputPorts().size();
if (listOfValues.size() > 0) {
forEachNode.setState(NodeExecutionState.EXECUTING);
node1.setState(NodeExecutionState.EXECUTING);
List<DataPort> outputPorts = node1.getOutputPorts();
final AtomicInteger counter = new AtomicInteger();
for (Node endFor : endForEachNodes) {
systemInvoker = new SystemComponentInvoker();
this.invokerMap.put(endFor, systemInvoker);
}
final Map<Node, Invoker> finalMap = this.invokerMap;
new Thread() {
@Override
public void run() {
try {
runInThread(listOfValues, forEachNode, node1, finalEndForEachNodes, finalMap, counter, inputNumbers);
} catch (WorkflowException e) {
log.error(e.getLocalizedMessage(), e);
} catch (RegistryException e) {
log.error(e.getMessage(), e);
} catch (TException e) {
log.error(e.getMessage(), e);
}
}
}.start();
while (counter.intValue() < parallelRuns) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
// if (!(node2 instanceof OutputNode)) {
// listOfValues.removeAll(listOfValues);
// String output = (String) systemInvoker.getOutput(node1.getOutputPort(0).getName());
// XmlElement xmlElement = XMLUtil.stringToXmlElement("<result>" + output + "</result>");
// Iterator iterator1 = xmlElement.children().iterator();
// while (iterator1.hasNext()) {
// Object next1 = iterator1.next();
// if (next1 instanceof XmlElement) {
// listOfValues.add((String) ((XmlElement) next1).children().iterator().next());
// }
// }
// }
}
}
}
// we have finished execution so end foreach is finished
// todo this has to be done in a separate thread
endForEachNode.setState(NodeExecutionState.FINISHED);
middleNode.setState(NodeExecutionState.FINISHED);
node.setState(NodeExecutionState.FINISHED);
} else {
// First node after foreach should end with EndForEachNode
List<DataPort> outputPorts1 = middleNode.getOutputPorts();
List<Node> endForEachNodes = new ArrayList<Node>();
for (DataPort port : outputPorts1) {
Iterator<Node> endForEachNodeItr1 = port.getToNodes().iterator();
while (endForEachNodeItr1.hasNext()) {
Node node2 = endForEachNodeItr1.next();
if (node2 instanceof EndForEachNode) {
endForEachNodes.add(node2);
} else if (node2 instanceof OutputNode) {
// intentionally left noop
} else {
throw new WorkFlowInterpreterException("Found More than one node inside foreach");
}
}
}
final List<Node> finalEndForEachNodes = endForEachNodes;
final Node foreachWSNode = middleNode;
final LinkedList<String> listOfValues = new LinkedList<String>();
// Start reading input came for foreach node
InterpreterUtil.getInputsForForEachNode(forEachNode, listOfValues, this.invokerMap);
final Integer[] inputNumbers = InterpreterUtil.getNumberOfInputsForForEachNode(forEachNode, this.invokerMap);
int parallelRuns = createInputValues(listOfValues, inputNumbers).size() * outputPorts1.size();
if (listOfValues.size() > 0) {
forEachNode.setState(NodeExecutionState.EXECUTING);
foreachWSNode.setState(NodeExecutionState.EXECUTING);
List<DataPort> outputPorts = middleNode.getOutputPorts();
final AtomicInteger counter = new AtomicInteger();
for (Node endFor : endForEachNodes) {
final SystemComponentInvoker systemInvoker = new SystemComponentInvoker();
this.invokerMap.put(endFor, systemInvoker);
}
final Map<Node, Invoker> finalInvokerMap = this.invokerMap;
new Thread() {
@Override
public void run() {
try {
runInThread(listOfValues, forEachNode, foreachWSNode, finalEndForEachNodes, finalInvokerMap, counter, inputNumbers);
} catch (WorkflowException e) {
log.error(e.getLocalizedMessage(), e);
} catch (RegistryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
while (counter.intValue() < parallelRuns) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
// we have finished execution so end foreach is finished
// todo this has to be done in a separate thread
middleNode.setState(NodeExecutionState.FINISHED);
for (Node endForEach : endForEachNodes) {
endForEach.setState(NodeExecutionState.FINISHED);
}
} else {
throw new WorkFlowInterpreterException("No array values found for foreach");
}
}
}
}
use of org.apache.airavata.workflow.model.graph.DataPort in project airavata by apache.
the class WorkflowInterpreter method getReadyNodesDynamically.
private ArrayList<Node> getReadyNodesDynamically() {
ArrayList<Node> list = new ArrayList<Node>();
ArrayList<Node> waiting = InterpreterUtil.getWaitingNodesDynamically(this.getGraph());
// ArrayList<Node> finishedNodes = InterpreterUtil.getFinishedNodesDynamically(this.getGraph());
// This is to support repeat the same application in the workflow.
List<String> finishedNodeIds = InterpreterUtil.getFinishedNodesIds(this.getGraph());
for (Node node : waiting) {
Component component = node.getComponent();
if (component instanceof WSComponent || component instanceof DynamicComponent || component instanceof SubWorkflowComponent || component instanceof ForEachComponent || component instanceof EndForEachComponent || component instanceof IfComponent || component instanceof InstanceComponent) {
/*
* Check for control ports from other node
*/
ControlPort control = node.getControlInPort();
boolean controlDone = true;
if (control != null) {
for (EdgeImpl edge : control.getEdges()) {
controlDone = controlDone && (finishedNodeIds.contains(edge.getFromPort().getNode().getID()) || // that makes sense and if anyone found a scenario it should be otherwise pls fix
((ControlPort) edge.getFromPort()).isConditionMet());
}
}
/*
* Check for input ports
*/
List<DataPort> inputPorts = node.getInputPorts();
boolean inputsDone = true;
for (DataPort dataPort : inputPorts) {
inputsDone = inputsDone && finishedNodeIds.contains(dataPort.getFromNode().getID());
}
if (inputsDone && controlDone) {
list.add(node);
}
} else if (component instanceof EndifComponent) {
/*
* EndIfComponent can run if number of input equals to number of
* output that it expects
*/
int expectedOutput = node.getOutputPorts().size();
int actualInput = 0;
List<DataPort> inputPorts = node.getInputPorts();
for (DataPort dataPort : inputPorts) {
if (finishedNodeIds.contains(dataPort.getFromNode().getID()))
actualInput++;
}
if (expectedOutput == actualInput) {
list.add(node);
}
} else if (component instanceof TerminateInstanceComponent) {
/*
* All node connected to controlIn port must be done
*/
ControlPort control = node.getControlInPort();
boolean controlDone = true;
if (control != null) {
for (EdgeImpl edge : control.getEdges()) {
controlDone = controlDone && finishedNodeIds.contains(edge.getFromPort().getFromNode().getID());
}
}
/*
* Check for input ports
*/
List<DataPort> inputPorts = node.getInputPorts();
boolean inputsDone = true;
for (DataPort dataPort : inputPorts) {
inputsDone = inputsDone && finishedNodeIds.contains(dataPort.getFromNode().getID());
}
if (inputsDone && controlDone) {
list.add(node);
}
} else if (InputComponent.NAME.equals(component.getName()) || DifferedInputComponent.NAME.equals(component.getName()) || S3InputComponent.NAME.equals(component.getName()) || OutputComponent.NAME.equals(component.getName()) || MemoComponent.NAME.equals(component.getName()) || component instanceof EndDoWhileComponent) {
// no op
} else if (component instanceof DoWhileComponent) {
ControlPort control = node.getControlInPort();
boolean controlDone = true;
if (control != null) {
for (EdgeImpl edge : control.getEdges()) {
controlDone = controlDone && finishedNodeIds.contains(edge.getFromPort().getFromNode().getID());
}
}
if (controlDone) {
list.add(node);
}
} else {
throw new WorkFlowInterpreterException("Component Not handled :" + component.getName());
}
}
notifyViaInteractor(WorkflowExecutionMessage.HANDLE_DEPENDENT_NODES_DIFFERED_INPUTS, this.getGraph());
return list;
}
use of org.apache.airavata.workflow.model.graph.DataPort in project airavata by apache.
the class WorkflowInterpreter method handleDynamicComponent.
private void handleDynamicComponent(Node node) throws WorkflowException {
DynamicComponent dynamicComponent = (DynamicComponent) node.getComponent();
String className = dynamicComponent.getClassName();
String operationName = dynamicComponent.getOperationName();
URL implJarLocation = dynamicComponent.getImplJarLocation();
DynamicNode dynamicNode = (DynamicNode) node;
LinkedList<Object> inputs = new LinkedList<Object>();
List<DataPort> inputPorts = dynamicNode.getInputPorts();
for (DataPort dataPort : inputPorts) {
Object inputVal = InterpreterUtil.findInputFromPort(dataPort, this.invokerMap);
/*
* Set type after get input value, and override inputValue if output
* type is array
*/
Node fromNode = dataPort.getFromNode();
DataType type = null;
if (fromNode instanceof InputNode) {
type = DataType.STRING;
} else if (fromNode instanceof ConstantNode) {
type = ((ConstantNode) fromNode).getType();
} else if ((dataPort.getFromPort() instanceof WSPort) && BasicTypeMapping.isArrayType(((WSPort) dataPort.getFromPort()).getComponentPort().getElement())) {
Invoker fromInvoker = this.invokerMap.get(fromNode);
// inputVal = BasicTypeMapping.getOutputArray(XmlConstants.BUILDER.parseFragmentFromString(fromInvoker.getOutputs().toString()), dataPort
// .getFromPort().getName(), BasicTypeMapping.getSimpleTypeIndex(((DataPort) dataPort.getFromPort()).getType()));
type = ((DataPort) dataPort.getFromPort()).getType();
} else {
type = ((DataPort) dataPort.getFromPort()).getType();
}
if (null == inputVal) {
throw new WorkFlowInterpreterException("Unable to find inputs for the node:" + node.getID());
}
// inputs.add(BasicTypeMapping.getObjectOfType(type, inputVal));
}
DynamicInvoker dynamicInvoker = new DynamicInvoker(className, implJarLocation, operationName, inputs.toArray());
this.invokerMap.put(node, dynamicInvoker);
dynamicInvoker.setup();
dynamicInvoker.invoke();
node.setState(NodeExecutionState.FINISHED);
}
use of org.apache.airavata.workflow.model.graph.DataPort in project airavata by apache.
the class WorkflowInterpreter method sendOutputsDynamically.
private void sendOutputsDynamically() throws WorkflowException, RegistryException, AiravataException {
ArrayList<Node> outputNodes = getReadyOutputNodesDynamically();
if (outputNodes.size() != 0) {
LinkedList<Object> outputValues = new LinkedList<Object>();
LinkedList<String> outputKeywords = new LinkedList<String>();
for (Node node : outputNodes) {
// Change it to processing state so we will not pic it up in the
// next run
// even if the next run runs before the notification arrives
WorkflowNodeDetails workflowNodeDetails = createWorkflowNodeDetails(node);
// workflowNodeDetails.setNodeInstanceId((String)getExperimentCatalog().add(ChildDataType.WORKFLOW_NODE_DETAIL, workflowNodeDetails, getExperiment().getExperimentID()));
node.setState(NodeExecutionState.EXECUTING);
updateWorkflowNodeStatus(workflowNodeDetails, WorkflowNodeState.EXECUTING);
publishNodeStatusChange(WorkflowNodeState.EXECUTING, node.getID(), experiment.getExperimentID());
// OutputNode node = (OutputNode) outputNode;
List<DataPort> inputPorts = node.getInputPorts();
for (DataPort dataPort : inputPorts) {
Object val = InterpreterUtil.findInputFromPort(dataPort);
if (null == val) {
throw new WorkFlowInterpreterException("Unable to find output for the node:" + node.getID());
}
// input
if (val instanceof org.xmlpull.v1.builder.XmlElement) {
((OutputNode) node).setDescription(XMLUtil.xmlElementToString((org.xmlpull.v1.builder.XmlElement) val));
} else {
((OutputNode) node).setDescription(val.toString());
}
// Saving output Node data in to database
// WorkflowNodeType workflowNodeType = new WorkflowNodeType();
// workflowNodeType.setNodeType(WorkflowNodeType.WorkflowNode.OUTPUTNODE);
// WorkflowInstanceNode workflowInstanceNode = new WorkflowInstanceNode(new WorkflowExecution(config.getTopic(), config.getTopic()), node.getID());
String portname = node.getName();
String portValue = ((OutputNode) node).getDescription();
// this.getConfig().getConfiguration().getAiravataAPI().getProvenanceManager().setWorkflowInstanceNodeOutput(workflowInstanceNode, portname + "=" + portValue);
// this.getConfig().getConfiguration().getAiravataAPI().getProvenanceManager().setWorkflowNodeType(workflowInstanceNode, workflowNodeType);
OutputDataObjectType elem = new OutputDataObjectType();
elem.setName(portname);
elem.setValue(portValue);
workflowNodeDetails.addToNodeOutputs(elem);
getExperimentCatalog().update(ExperimentCatalogModelType.WORKFLOW_NODE_DETAIL, workflowNodeDetails, workflowNodeDetails.getNodeInstanceId());
if (this.config.isActOnProvenance()) {
// TODO do provanence thing
// try {
// if (val instanceof String) {
// this.getConfig().getConfiguration().getAiravataAPI().getProvenanceManager()
// .saveWorkflowExecutionOutput(this.config.getTopic(), node.getName(), val.toString());
// } else if (val instanceof org.xmlpull.v1.builder.XmlElement) {
// this.getConfig()
// .getConfiguration()
// .getAiravataAPI().getProvenanceManager()
// .saveWorkflowExecutionOutput(this.config.getTopic(), node.getName(),
// XMLUtil.xmlElementToString((org.xmlpull.v1.builder.XmlElement) val));
// }
// outputValues.add(val);
// outputKeywords.add(dataPort.getID());
// } catch (AiravataAPIInvocationException e) {
// e.printStackTrace(); // To change body of catch
// // statement use File |
// // Settings | File
// // Templates.
// }
}
}
node.setState(NodeExecutionState.FINISHED);
publishNodeStatusChange(WorkflowNodeState.COMPLETED, node.getID(), experiment.getExperimentID());
updateWorkflowNodeStatus(workflowNodeDetails, WorkflowNodeState.COMPLETED);
notifyViaInteractor(WorkflowExecutionMessage.NODE_STATE_CHANGED, null);
}
}
}
use of org.apache.airavata.workflow.model.graph.DataPort in project airavata by apache.
the class WorkflowHarvester method getRemainderPorts.
/**
* @param pair
* @return
*/
private List<DataPort> getRemainderPorts(Pair<WSNode, DataPort> pair) {
LinkedList<DataPort> ret = new LinkedList<DataPort>();
List<DataPort> inputPorts = pair.getLeft().getInputPorts();
for (DataPort dataPort : inputPorts) {
if (pair.getRight() != dataPort) {
ret.add(dataPort);
}
}
return ret;
}
Aggregations