Search in sources :

Example 1 with SubWorkflowNode

use of org.apache.airavata.workflow.model.graph.subworkflow.SubWorkflowNode 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");
            }
        }
    }
}
Also used : TException(org.apache.thrift.TException) DynamicNode(org.apache.airavata.workflow.model.graph.dynamic.DynamicNode) Node(org.apache.airavata.workflow.model.graph.Node) SubWorkflowNode(org.apache.airavata.workflow.model.graph.subworkflow.SubWorkflowNode) WSNode(org.apache.airavata.workflow.model.graph.ws.WSNode) DataPort(org.apache.airavata.workflow.model.graph.DataPort) WSNode(org.apache.airavata.workflow.model.graph.ws.WSNode) SubWorkflowNode(org.apache.airavata.workflow.model.graph.subworkflow.SubWorkflowNode) NodeImpl(org.apache.airavata.workflow.model.graph.impl.NodeImpl) WorkflowException(org.apache.airavata.workflow.model.exceptions.WorkflowException) Workflow(org.apache.airavata.workflow.model.wf.Workflow) RegistryException(org.apache.airavata.registry.cpi.RegistryException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 2 with SubWorkflowNode

use of org.apache.airavata.workflow.model.graph.subworkflow.SubWorkflowNode in project airavata by apache.

the class SubWorkflowComponent method createNode.

/**
 * @see org.apache.airavata.workflow.model.component.ws.WSComponent#createNode(org.apache.airavata.workflow.model.graph.Graph)
 */
@Override
public SubWorkflowNode createNode(Graph graph) {
    SubWorkflowNode node = new SubWorkflowNode(graph);
    node.setWorkflow(workflow.clone());
    // Copy some infomation from the component
    node.setName(getName());
    node.setComponent(this);
    // node.setWSDLQName(this.wsdlQName);
    // Creates a unique ID for the node. This has to be after setName().
    node.createID();
    // Creat ports
    createPorts(node);
    return node;
}
Also used : SubWorkflowNode(org.apache.airavata.workflow.model.graph.subworkflow.SubWorkflowNode)

Example 3 with SubWorkflowNode

use of org.apache.airavata.workflow.model.graph.subworkflow.SubWorkflowNode in project airavata by apache.

the class WorkflowInterpreter method handleSubWorkComponent.

private void handleSubWorkComponent(Node node) throws WorkflowException {
    // TODO we will not support this in 0.13
    notifyViaInteractor(WorkflowExecutionMessage.OPEN_SUBWORKFLOW, node);
    // setting the inputs
    Workflow subWorkflow = ((SubWorkflowNode) node).getWorkflow();
    ArrayList<Node> subWorkflowInputNodes = getInputNodes(subWorkflow);
    List<DataPort> inputPorts = node.getInputPorts();
    for (DataPort port : inputPorts) {
        Object inputVal = InterpreterUtil.findInputFromPort(port, this.invokerMap);
        if (null == inputVal) {
            throw new WorkFlowInterpreterException("Unable to find inputs for the subworkflow node node:" + node.getID());
        }
        for (Iterator<Node> iterator = subWorkflowInputNodes.iterator(); iterator.hasNext(); ) {
            InputNode inputNode = (InputNode) iterator.next();
            if (inputNode.getName().equals(port.getName())) {
                inputNode.setDefaultValue(inputVal);
            }
        }
    }
    for (Iterator<Node> iterator = subWorkflowInputNodes.iterator(); iterator.hasNext(); ) {
        InputNode inputNode = (InputNode) iterator.next();
        if (inputNode.getDefaultValue() == null) {
            throw new WorkFlowInterpreterException("Input not set for  :" + inputNode.getID());
        }
    }
    try {
        WorkflowInterpreter subworkflowInterpreter = (WorkflowInterpreter) getInputViaInteractor(WorkflowExecutionMessage.INPUT_WORKFLOWINTERPRETER_FOR_WORKFLOW, subWorkflow);
        subworkflowInterpreter.scheduleDynamically();
    } catch (Exception e) {
        throw new WorkflowException(e);
    }
}
Also used : DynamicNode(org.apache.airavata.workflow.model.graph.dynamic.DynamicNode) Node(org.apache.airavata.workflow.model.graph.Node) SubWorkflowNode(org.apache.airavata.workflow.model.graph.subworkflow.SubWorkflowNode) WSNode(org.apache.airavata.workflow.model.graph.ws.WSNode) WorkflowException(org.apache.airavata.workflow.model.exceptions.WorkflowException) Workflow(org.apache.airavata.workflow.model.wf.Workflow) XPathExpressionException(javax.xml.xpath.XPathExpressionException) WorkflowRuntimeException(org.apache.airavata.workflow.model.exceptions.WorkflowRuntimeException) RegistryException(org.apache.airavata.registry.cpi.RegistryException) AiravataException(org.apache.airavata.common.exception.AiravataException) TException(org.apache.thrift.TException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) WorkflowException(org.apache.airavata.workflow.model.exceptions.WorkflowException) DataPort(org.apache.airavata.workflow.model.graph.DataPort) SubWorkflowNode(org.apache.airavata.workflow.model.graph.subworkflow.SubWorkflowNode)

Aggregations

SubWorkflowNode (org.apache.airavata.workflow.model.graph.subworkflow.SubWorkflowNode)3 RegistryException (org.apache.airavata.registry.cpi.RegistryException)2 WorkflowException (org.apache.airavata.workflow.model.exceptions.WorkflowException)2 DataPort (org.apache.airavata.workflow.model.graph.DataPort)2 Node (org.apache.airavata.workflow.model.graph.Node)2 DynamicNode (org.apache.airavata.workflow.model.graph.dynamic.DynamicNode)2 WSNode (org.apache.airavata.workflow.model.graph.ws.WSNode)2 Workflow (org.apache.airavata.workflow.model.wf.Workflow)2 TException (org.apache.thrift.TException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 AiravataException (org.apache.airavata.common.exception.AiravataException)1 ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)1 WorkflowRuntimeException (org.apache.airavata.workflow.model.exceptions.WorkflowRuntimeException)1 NodeImpl (org.apache.airavata.workflow.model.graph.impl.NodeImpl)1