You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by la...@apache.org on 2011/10/23 21:28:25 UTC

svn commit: r1187954 - in /incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya: concurrent/ graph/ graph/impl/ graph/ws/ interpretor/ invoker/ provenance/ util/

Author: lahiru
Date: Sun Oct 23 19:28:24 2011
New Revision: 1187954

URL: http://svn.apache.org/viewvc?rev=1187954&view=rev
Log:
implementing input registering feature at https://issues.apache.org/jira/browse/AIRAVATA-160.

Added:
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/concurrent/
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/concurrent/PredicatedExecutable.java
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/concurrent/PredicatedTaskRunner.java
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/ForEachExecutableNode.java
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/interpretor/ProvenanceFileWrite.java
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/provenance/
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/provenance/ProvenanceReader.java
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/provenance/ProvenanceWrite.java
Modified:
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/Node.java
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/impl/NodeImpl.java
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/ws/WSNode.java
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/invoker/GenericInvoker.java
    incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/util/XBayaUtil.java

Added: incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/concurrent/PredicatedExecutable.java
URL: http://svn.apache.org/viewvc/incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/concurrent/PredicatedExecutable.java?rev=1187954&view=auto
==============================================================================
--- incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/concurrent/PredicatedExecutable.java (added)
+++ incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/concurrent/PredicatedExecutable.java Sun Oct 23 19:28:24 2011
@@ -0,0 +1,33 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+package org.apache.airavata.xbaya.concurrent;
+
+/**
+ * Intended for jobs that are runnable; Way of determining
+ * whether a given job is ready to be executed
+ *
+ * @author Chathura Herath
+ */
+public interface PredicatedExecutable extends Runnable{
+
+	public boolean isReady();
+
+}

Added: incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/concurrent/PredicatedTaskRunner.java
URL: http://svn.apache.org/viewvc/incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/concurrent/PredicatedTaskRunner.java?rev=1187954&view=auto
==============================================================================
--- incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/concurrent/PredicatedTaskRunner.java (added)
+++ incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/concurrent/PredicatedTaskRunner.java Sun Oct 23 19:28:24 2011
@@ -0,0 +1,159 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+package org.apache.airavata.xbaya.concurrent;
+
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * Used to run jobs that need to be started after a predicate is
+ *
+ * @author Chathura Herath
+ */
+public class PredicatedTaskRunner {
+
+	protected volatile ConcurrentLinkedQueue<PredicatedExecutable> jobQueue = new ConcurrentLinkedQueue<PredicatedExecutable>();
+
+	protected ExecutorService threadPool;
+
+	protected volatile boolean stop = false;
+
+	public PredicatedTaskRunner(int numberOfThreads) {
+		this.threadPool = Executors.newFixedThreadPool(numberOfThreads);
+		addIdleTask();
+		startCheckThread();
+
+	}
+
+	private void addIdleTask() {
+		PredicatedExecutable sleepTask = new PredicatedExecutable() {
+
+			@Override
+			public void run() {
+				synchronized (jobQueue) {
+					if (jobQueue.size() == 1) {
+						try {
+							jobQueue.wait();
+						} catch (InterruptedException e) {
+							Thread.currentThread().interrupt();
+						}
+					} else if (allTasksAreWaiting(jobQueue)) {
+						try {
+							Thread.sleep(50);
+						} catch (InterruptedException e) {
+							Thread.currentThread().interrupt();
+						}
+					}
+				}
+
+			}
+
+			private boolean allTasksAreWaiting(
+					ConcurrentLinkedQueue<PredicatedExecutable> jobQueue) {
+				for (PredicatedExecutable predicatedExecutable : jobQueue) {
+					if (predicatedExecutable.isReady()) {
+						return false;
+					}
+				}
+				return true;
+
+			}
+
+			@Override
+			public boolean isReady() {
+				// TODO Auto-generated method stub
+				return true;
+			}
+		};
+
+		this.jobQueue.add(sleepTask);
+	}
+
+	private void startCheckThread() {
+		new Thread(new Runnable() {
+
+			@Override
+			public void run() {
+
+				while (!stop) {
+					try {
+
+
+
+
+
+						synchronized (jobQueue) {
+							while(jobQueue.size() == 0 || allTasksAreWaiting(jobQueue)){
+								jobQueue.wait(50);
+							}
+						}
+
+						PredicatedExecutable job = jobQueue.remove();
+						if (job.isReady()) {
+							// remove from front and execute and you are done
+							threadPool.execute(job);
+						} else {
+							// add to end if not ready to run
+							jobQueue.add(job);
+						}
+
+
+
+
+
+
+					} catch (Throwable e) {
+						// we go on no matter what
+						e.printStackTrace();
+					}
+				}
+
+			}
+		}).start();
+	}
+
+	private  boolean allTasksAreWaiting(
+			ConcurrentLinkedQueue<PredicatedExecutable> jobQueue) {
+		for (PredicatedExecutable predicatedExecutable : jobQueue) {
+			if (predicatedExecutable.isReady()) {
+				return false;
+			}
+		}
+		return true;
+
+	}
+
+	public void scedule(PredicatedExecutable job) {
+
+		synchronized (jobQueue) {
+			this.jobQueue.add(job);
+			this.jobQueue.notifyAll();
+		}
+
+	}
+
+	public void shutDown() {
+		this.threadPool.shutdown();
+		this.stop = true;
+	}
+
+}

Added: incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/ForEachExecutableNode.java
URL: http://svn.apache.org/viewvc/incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/ForEachExecutableNode.java?rev=1187954&view=auto
==============================================================================
--- incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/ForEachExecutableNode.java (added)
+++ incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/ForEachExecutableNode.java Sun Oct 23 19:28:24 2011
@@ -0,0 +1,24 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+package org.apache.airavata.xbaya.graph;
+
+public interface ForEachExecutableNode extends Node {
+}

Modified: incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/Node.java
URL: http://svn.apache.org/viewvc/incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/Node.java?rev=1187954&r1=1187953&r2=1187954&view=diff
==============================================================================
--- incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/Node.java (original)
+++ incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/Node.java Sun Oct 23 19:28:24 2011
@@ -218,4 +218,9 @@ public interface Node extends GraphPiece
      * @return
      */
     public boolean getRequireJoin();
+
+    /**
+	 * @return
+	 */
+	public boolean isFinished();
 }
\ No newline at end of file

Modified: incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/impl/NodeImpl.java
URL: http://svn.apache.org/viewvc/incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/impl/NodeImpl.java?rev=1187954&r1=1187953&r2=1187954&view=diff
==============================================================================
--- incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/impl/NodeImpl.java (original)
+++ incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/impl/NodeImpl.java Sun Oct 23 19:28:24 2011
@@ -40,6 +40,7 @@ import org.apache.airavata.xbaya.graph.G
 import org.apache.airavata.xbaya.graph.Node;
 import org.apache.airavata.xbaya.graph.Port;
 import org.apache.airavata.xbaya.graph.Port.Kind;
+import org.apache.airavata.xbaya.monitor.gui.MonitorEventHandler;
 import org.apache.airavata.xbaya.streaming.StreamTableModel;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -725,4 +726,7 @@ public abstract class NodeImpl implement
     public boolean getRequireJoin() {
         return this.requireJoin;
     }
+    public boolean isFinished(){
+		return this.getGUI().getBodyColor() == MonitorEventHandler.NodeState.FINISHED.color;
+	}
 }
\ No newline at end of file

Modified: incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/ws/WSNode.java
URL: http://svn.apache.org/viewvc/incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/ws/WSNode.java?rev=1187954&r1=1187953&r2=1187954&view=diff
==============================================================================
--- incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/ws/WSNode.java (original)
+++ incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/graph/ws/WSNode.java Sun Oct 23 19:28:24 2011
@@ -26,10 +26,7 @@ import javax.xml.namespace.QName;
 import org.apache.airavata.xbaya.component.ComponentException;
 import org.apache.airavata.xbaya.component.ws.WSComponent;
 import org.apache.airavata.xbaya.component.ws.WSComponentFactory;
-import org.apache.airavata.xbaya.graph.Edge;
-import org.apache.airavata.xbaya.graph.Graph;
-import org.apache.airavata.xbaya.graph.GraphException;
-import org.apache.airavata.xbaya.graph.GraphSchema;
+import org.apache.airavata.xbaya.graph.*;
 import org.apache.airavata.xbaya.graph.gui.NodeGUI;
 import org.apache.airavata.xbaya.graph.impl.NodeImpl;
 import org.apache.airavata.xbaya.graph.util.GraphUtil;
@@ -37,7 +34,7 @@ import org.apache.airavata.xbaya.graph.w
 import org.apache.airavata.xbaya.gui.ErrorMessages;
 import org.xmlpull.infoset.XmlElement;
 
-public class WSNode extends NodeImpl {
+public class WSNode extends NodeImpl implements ForEachExecutableNode{
 
     protected String wsdlID;
 

Added: incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/interpretor/ProvenanceFileWrite.java
URL: http://svn.apache.org/viewvc/incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/interpretor/ProvenanceFileWrite.java?rev=1187954&view=auto
==============================================================================
--- incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/interpretor/ProvenanceFileWrite.java (added)
+++ incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/interpretor/ProvenanceFileWrite.java Sun Oct 23 19:28:24 2011
@@ -0,0 +1,52 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+package org.apache.airavata.xbaya.interpretor;
+
+import org.apache.airavata.common.utils.XMLUtil;
+import org.xmlpull.infoset.XmlElement;
+
+import java.io.File;
+import java.io.IOException;
+
+public class ProvenanceFileWrite {
+
+
+	private String file;
+	private String folder;
+
+	public ProvenanceFileWrite(String folder, String file){
+		this.file = file;
+		this.folder = folder;
+	}
+
+	/**
+	 * @param elem
+	 */
+	public void write(XmlElement elem) {
+
+		try {
+			System.out.println(xsul5.XmlConstants.BUILDER.serializeToString(elem));
+			XMLUtil.saveXML(elem, new File(new File(this.folder), this.file));
+		} catch (IOException e) {
+			//failing to write
+		}
+	}
+}

Modified: incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/invoker/GenericInvoker.java
URL: http://svn.apache.org/viewvc/incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/invoker/GenericInvoker.java?rev=1187954&r1=1187953&r2=1187954&view=diff
==============================================================================
--- incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/invoker/GenericInvoker.java (original)
+++ incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/invoker/GenericInvoker.java Sun Oct 23 19:28:24 2011
@@ -156,16 +156,13 @@ public class GenericInvoker implements I
     }
 
     /**
-     * Constructs a GenericInvoker.
-     * 
-     * @param portTypeQName2
-     * @param wsdlDefinitions5ToWsdlDefintions3
-     * @param id
-     * @param string
-     * @param object
-     * @param object2
-     * @param object3
-     * @param notifier2
+     *
+     * @param portTypeQName
+     * @param wsdl
+     * @param nodeID
+     * @param messageBoxURL
+     * @param gfacURL
+     * @param notifier
      */
     public GenericInvoker(QName portTypeQName, WsdlDefinitions wsdl, String nodeID, String messageBoxURL,
             String gfacURL, WorkflowNotifiable notifier) {

Added: incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/provenance/ProvenanceReader.java
URL: http://svn.apache.org/viewvc/incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/provenance/ProvenanceReader.java?rev=1187954&view=auto
==============================================================================
--- incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/provenance/ProvenanceReader.java (added)
+++ incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/provenance/ProvenanceReader.java Sun Oct 23 19:28:24 2011
@@ -0,0 +1,97 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+package org.apache.airavata.xbaya.provenance;
+
+import org.apache.airavata.common.utils.Pair;
+import org.apache.airavata.common.utils.XMLUtil;
+import org.xmlpull.infoset.XmlElement;
+
+import java.io.File;
+import java.io.FileFilter;
+
+public class ProvenanceReader {
+
+    public String DEFAULT_LIBRARY_FOLDER_NAME = "provenance";
+
+	public ProvenanceReader() {
+
+	}
+
+	public Object read(final String nodeName, Pair<String, String>[] inputs)
+			throws Exception {
+
+		File directory = new File(DEFAULT_LIBRARY_FOLDER_NAME);
+		if (!directory.isDirectory()) {
+			return null;
+		}
+		File[] componentMatchFiles = directory.listFiles(new FileFilter() {
+
+			@Override
+			public boolean accept(File pathname) {
+				if (pathname.isDirectory()) {
+					return false;
+				} else {
+					String fileName = pathname.getName();
+					return -1 != fileName.indexOf(nodeName);
+				}
+			}
+		});
+
+		for (File file : componentMatchFiles) {
+			if (!file.isDirectory()) {
+				XmlElement xml = XMLUtil.loadXML(file);
+				XmlElement root = xml;
+				XmlElement wsnode;
+
+				XmlElement foreach;
+				if (null != (wsnode = root.element("wsnode"))) {
+					XmlElement inputElems = root.element("inputs");
+					Iterable inputValElems = inputElems.children();
+					for (Object object : inputValElems) {
+						if (object instanceof XmlElement) {
+							XmlElement inputElem = (XmlElement)object;
+							for (Pair<String, String> pair : inputs) {
+								String inputName = pair.getLeft();
+								//issue "x".equals("CreateCoordinatePortType_createCoordinate_in_0")
+								if(inputName.equals(inputElem.getName())){
+									//found the input now check whether values are the same
+									if(XMLUtil.isEqual(inputElem, XMLUtil.stringToXmlElement(pair.getRight()))){
+										//match found return output
+										XmlElement output = root.element("output");
+										return output.children().iterator().next();
+
+									}
+								}
+
+							}
+						}
+					}
+
+				} else if (null != (foreach = root.element("foreach"))) {
+
+				}
+
+			}
+		}
+
+		return null;
+	}
+}

Added: incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/provenance/ProvenanceWrite.java
URL: http://svn.apache.org/viewvc/incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/provenance/ProvenanceWrite.java?rev=1187954&view=auto
==============================================================================
--- incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/provenance/ProvenanceWrite.java (added)
+++ incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/provenance/ProvenanceWrite.java Sun Oct 23 19:28:24 2011
@@ -0,0 +1,198 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+package org.apache.airavata.xbaya.provenance;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.airavata.common.utils.XMLUtil;
+import org.apache.airavata.xbaya.XBayaException;
+import org.apache.airavata.xbaya.concurrent.PredicatedExecutable;
+import org.apache.airavata.xbaya.graph.DataPort;
+import org.apache.airavata.xbaya.graph.ForEachExecutableNode;
+import org.apache.airavata.xbaya.graph.Node;
+import org.apache.airavata.xbaya.graph.system.EndForEachNode;
+import org.apache.airavata.xbaya.graph.system.ForEachNode;
+import org.apache.airavata.xbaya.graph.system.InputNode;
+import org.apache.airavata.xbaya.graph.ws.WSNode;
+import org.apache.airavata.xbaya.interpretor.ProvenanceFileWrite;
+import org.apache.airavata.xbaya.invoker.Invoker;
+import org.apache.airavata.xbaya.util.XBayaUtil;
+import org.xmlpull.infoset.XmlElement;
+
+import xsul5.XmlConstants;
+
+/**
+ * @author Chathura Herath
+ */
+public final class ProvenanceWrite implements PredicatedExecutable {
+
+	private static final String PROVENANCE_DIR = "provenance";
+
+	private Node node;
+
+	private String workflowName;
+
+	private Map<Node, Invoker> invokerMap;
+
+	public ProvenanceWrite(Node node, String workflowName,
+			Map<Node, Invoker> invokerMap) {
+		this.node = node;
+		this.workflowName = workflowName;
+		this.invokerMap = invokerMap;
+	}
+
+	public void run() {
+
+		try {
+			saveNodeOutputs(node, invokerMap, workflowName);
+		} catch (XBayaException e) {
+			// do nothing its a failure but go on
+			e.printStackTrace();
+		}
+
+	}
+
+	public boolean isReady() {
+		return this.node.isFinished() && invokerMap.get(node) != null;
+	}
+
+	private void saveNodeOutputs(Node node,
+			Map<Node, Invoker> invokerMap, String workflowName)
+			throws XBayaException {
+
+		if (null != node && !(node instanceof InputNode)) {
+			XmlElement elem = XmlConstants.BUILDER.newFragment("previousdat");
+			if (node instanceof WSNode) {
+				String nodeID = node.getComponent().getName();
+				XmlElement nodeElement = elem.newElement("wsnode");
+				elem.addChild(nodeElement);
+				nodeElement.addChild(nodeID);
+				XmlElement inputs = elem.newElement("inputs");
+				elem.addChild(inputs);
+
+				List<DataPort> portsToBeSaved = node.getInputPorts();
+				for (DataPort savePort : portsToBeSaved) {
+
+					String portID = savePort.getName();
+					XmlElement portElem = inputs.newElement(portID);
+					inputs.addChild(portElem);
+					Object portInput = XBayaUtil.findInputFromPort(
+                            savePort, invokerMap);
+					if (portInput instanceof org.xmlpull.v1.builder.XmlElement) {
+						portInput = XMLUtil
+								.xmlElement3ToXmlElement5((org.xmlpull.v1.builder.XmlElement) portInput);
+					}
+					portElem.addChild(portInput);
+
+				}
+
+			} else if (node instanceof EndForEachNode) {
+				// here we save the inputs for the entire foreach block
+				Node middleNode = node.getInputPort(0).getFromNode();
+				String nodeID = middleNode.getComponent().getName();
+				XmlElement nodeElement = elem.newElement("foreach");
+				elem.addChild(nodeElement);
+				nodeElement.addChild(nodeID);
+				XmlElement inputs = elem.newElement("inputs");
+				elem.addChild(inputs);
+				XmlConstants.BUILDER.serializeToString(elem);
+				if (middleNode instanceof ForEachExecutableNode) {
+					List<DataPort> portsToBeSaved = middleNode.getInputPorts();
+					for (DataPort savePort : portsToBeSaved) {
+						// we will save all the inputs
+						// these are static inputs and
+						// input to the foreach node
+
+						if (savePort.getFromNode() instanceof ForEachNode) {
+							// this is the foreach node rest are simple
+							// inputs
+							Object value = XBayaUtil
+									.getInputsForForEachNode(
+											(ForEachNode) savePort
+													.getFromNode(),
+											new LinkedList<String>(),
+											invokerMap);
+							if (value instanceof org.xmlpull.v1.builder.XmlElement) {
+								value = XMLUtil
+										.xmlElement3ToXmlElement5((org.xmlpull.v1.builder.XmlElement) value);
+							}
+
+							XmlElement portElement = inputs.newElement(savePort
+									.getName());
+							inputs.addChild(portElement);
+							portElement.addChild(value);
+						} else {
+							String portID = savePort.getName();
+							XmlElement portElem = inputs.newElement(portID);
+							inputs.addChild(portElem);
+							Object portInput = XBayaUtil
+									.findInputFromPort(savePort, invokerMap);
+							if (portInput instanceof org.xmlpull.v1.builder.XmlElement) {
+								portInput = XMLUtil
+										.xmlElement3ToXmlElement5((org.xmlpull.v1.builder.XmlElement) portInput);
+							}
+
+							portElem.addChild(portInput);
+						}
+
+					}
+
+				} else {
+					// error but we will let it pass because it will be
+					// caught at higher level
+				}
+
+			}
+
+			// deal with the outputs
+
+			XmlElement outputs = elem.newElement("outputs");
+			elem.addChild(outputs);
+
+			List<DataPort> outputPorts = node.getOutputPorts();
+			for (DataPort outputPort : outputPorts) {
+				String outputName = outputPort.getName();
+
+				XmlElement outputParamElement = outputs.newElement(outputName);
+				outputs.addChild(outputParamElement);
+				Object ouputParamValue = invokerMap.get(node).getOutput(
+						outputName);
+
+				if (ouputParamValue instanceof org.xmlpull.v1.builder.XmlElement) {
+					ouputParamValue = XMLUtil
+							.xmlElement3ToXmlElement5((org.xmlpull.v1.builder.XmlElement) ouputParamValue);
+				}
+
+				if (ouputParamValue != null) {
+					outputParamElement.addChild(ouputParamValue);
+				} else {
+					outputParamElement.addChild("null");
+				}
+			}
+
+			new ProvenanceFileWrite(PROVENANCE_DIR, workflowName + node.getID()
+					+ System.currentTimeMillis() + ".xml").write(elem);
+
+		}
+	}
+}

Modified: incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/util/XBayaUtil.java
URL: http://svn.apache.org/viewvc/incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/util/XBayaUtil.java?rev=1187954&r1=1187953&r2=1187954&view=diff
==============================================================================
--- incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/util/XBayaUtil.java (original)
+++ incubator/airavata/trunk/modules/xbaya-gui/src/main/java/org/apache/airavata/xbaya/util/XBayaUtil.java Sun Oct 23 19:28:24 2011
@@ -26,18 +26,29 @@ import java.net.SocketTimeoutException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.util.*;
 
-import org.apache.airavata.xbaya.XBayaConfiguration;
-import org.apache.airavata.xbaya.XBayaEngine;
+import org.apache.airavata.xbaya.*;
 import org.apache.airavata.xbaya.component.gui.JCRRegistryWindow;
+import org.apache.airavata.xbaya.graph.DataPort;
+import org.apache.airavata.xbaya.graph.Node;
+import org.apache.airavata.xbaya.graph.amazon.InstanceNode;
+import org.apache.airavata.xbaya.graph.system.*;
+import org.apache.airavata.xbaya.interpretor.SystemComponentInvoker;
+import org.apache.airavata.xbaya.interpretor.WorkFlowInterpreterException;
+import org.apache.airavata.xbaya.invoker.GenericInvoker;
+import org.apache.airavata.xbaya.invoker.Invoker;
+import org.apache.airavata.xbaya.invoker.WorkflowInvokerWrapperForGFacInvoker;
 import org.apache.airavata.xbaya.lead.LeadContextHeaderHelper;
 import org.apache.airavata.xbaya.monitor.MonitorConfiguration;
 import org.apache.airavata.xbaya.wf.Workflow;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import org.xmlpull.infoset.XmlElement;
 import xsul.lead.LeadContextHeader;
 import xsul.lead.LeadResourceMapping;
+import xsul5.XmlConstants;
 
 public class XBayaUtil {
 
@@ -122,5 +133,170 @@ public class XBayaUtil {
         }
         return engine.getConfiguration().getJcrComponentRegistry() != null;
     }
+    public static Object getInputsForForEachNode(final ForEachNode forEachNode,
+			final LinkedList<String> listOfValues, Map<Node, Invoker> invokerMap) throws XBayaException {
+		Node forEachInputNode = forEachNode.getInputPort(0).getFromNode();
+		// if input node for for-each is WSNode
+		Object returnValForProvenance = null;
+		if (forEachInputNode instanceof InputNode) {
+			for (DataPort dataPort : forEachNode.getInputPorts()) {
+				returnValForProvenance = XBayaUtil
+						.findInputFromPort(dataPort, invokerMap);
+				if (null == returnValForProvenance) {
+					throw new WorkFlowInterpreterException(
+							"Unable to find input for the node:"
+									+ forEachNode.getID());
+				}
+				String[] vals = returnValForProvenance.toString().split(",");
+				listOfValues.addAll(Arrays.asList(vals));
+			}
+		} else {
+			Invoker workflowInvoker = invokerMap
+					.get(forEachInputNode);
+			if (workflowInvoker != null) {
+				if (workflowInvoker instanceof GenericInvoker) {
+
+					returnValForProvenance = ((GenericInvoker) workflowInvoker)
+							.getOutputs();
+					String message = returnValForProvenance.toString();
+
+					XmlElement msgElmt = XmlConstants.BUILDER
+							.parseFragmentFromString(message);
+					Iterator children = msgElmt.children().iterator();
+					while (children.hasNext()) {
+						Object object = children.next();
+						// foreachWSNode.getInputPort(0).getType()
+						if (object instanceof XmlElement) {
+							listOfValues.add(XmlConstants.BUILDER
+									.serializeToString(object));
+							// TODO fix for simple type - Done
+						}
+					}
+				} else if (workflowInvoker instanceof WorkflowInvokerWrapperForGFacInvoker) {
+					String outputName = forEachInputNode.getOutputPort(0)
+							.getName();
+					returnValForProvenance = workflowInvoker
+							.getOutput(outputName);
+					org.xmlpull.v1.builder.XmlElement msgElmt = (org.xmlpull.v1.builder.XmlElement) returnValForProvenance;
+					Iterator children = msgElmt.children();
+					while (children.hasNext()) {
+						Object object = children.next();
+						if (object instanceof org.xmlpull.v1.builder.XmlElement) {
+							org.xmlpull.v1.builder.XmlElement child = (org.xmlpull.v1.builder.XmlElement) object;
+							Iterator valItr = child.children();
+							if (valItr.hasNext()) {
+								Object object2 = valItr.next();
+								if (object2 instanceof String) {
+									listOfValues.add(object2.toString());
+								}
+							}
+						}
+					}
+				} else if (workflowInvoker instanceof SystemComponentInvoker) {
+					String outputName = forEachInputNode.getOutputPort(0)
+							.getName();
+					returnValForProvenance = workflowInvoker
+							.getOutput(outputName);
+					XmlElement msgElmt = XmlConstants.BUILDER
+							.parseFragmentFromString("<temp>"
+									+ returnValForProvenance + "</temp>");
+					Iterator valItr = msgElmt.children().iterator();
+					while (valItr.hasNext()) {
+						Object object2 = valItr.next();
+						if (object2 instanceof XmlElement) {
+							listOfValues.add(((XmlElement) object2).children()
+									.iterator().next().toString());
+						}
+					}
+				}
+			} else {
+				throw new WorkFlowInterpreterException(
+						"Did not find inputs from WS to foreach");
+			}
+		}
+		return returnValForProvenance;
+	}
+
+    /**
+     *
+     * @param inputPort
+     * @param invokerMap
+     * @return
+     * @throws XBayaException
+     */
+	public static Object findInputFromPort(DataPort inputPort, Map<Node, Invoker>  invokerMap) throws XBayaException {
+		Object outputVal = null;
+		Node fromNode = inputPort.getFromNode();
+		if (fromNode instanceof InputNode) {
+			outputVal = ((InputNode) fromNode).getDefaultValue();
+		} else if (fromNode instanceof ConstantNode) {
+			outputVal = ((ConstantNode) fromNode).getValue();
+		} else if (fromNode instanceof EndifNode) {
+			Invoker fromInvoker = invokerMap.get(fromNode);
+			outputVal = fromInvoker.getOutput(inputPort.getFromPort().getID());
+		} else if (fromNode instanceof InstanceNode) {
+			return ((InstanceNode) fromNode).getOutputInstanceId();
+		} else if (fromNode instanceof EndForEachNode) {
+			outputVal = "";
+			Invoker workflowInvoker = invokerMap.get(fromNode);
+			String outputName = fromNode.getOutputPort(0).getName();
+			XmlElement msgElmt = XmlConstants.BUILDER
+					.parseFragmentFromString("<temp>"
+							+ workflowInvoker.getOutput(outputName) + "</temp>");
+			Iterator valItr = msgElmt.children().iterator();
+			while (valItr.hasNext()) {
+				Object object2 = valItr.next();
+				if (object2 instanceof XmlElement) {
+					outputVal = outputVal
+							+ ","
+							+ ((XmlElement) object2).children().iterator()
+									.next().toString();
+				}
+			}
+			outputVal = ((String) outputVal).substring(1,
+					((String) outputVal).length());
+		} else {
+			Invoker fromInvoker = invokerMap.get(fromNode);
+			try {
+				if (fromInvoker != null)
+					outputVal = fromInvoker.getOutput(inputPort.getFromPort()
+							.getName());
+
+			} catch (Exception e) {
+				// if the value is still null look it up from the inputport name
+				// because the value is set to the input port name at some point
+				// there is no harm in doing this
+				if (null == outputVal) {
+					outputVal = fromInvoker.getOutput(inputPort.getName());
+				}
+			}
+
+		}
+		return outputVal;
+
+	}
+
+	/**
+	 * @param node
+	 * @return
+	 */
+	public static Node findEndForEachFor(ForEachNode node) {
+
+		Collection<Node> toNodes = node.getOutputPort(0).getToNodes();
+		if(toNodes.size() != 1){
+			throw new XBayaRuntimeException("ForEach output does not contain single out-edge");
+		}
+		Node middleNode = toNodes.iterator().next();
+		List<DataPort> outputPorts = middleNode.getOutputPorts();
+		for (DataPort dataPort : outputPorts) {
+			if(dataPort.getToNodes().size() == 1){
+				Node possibleEndForEachNode = dataPort.getToNodes().get(0);
+				if(possibleEndForEachNode instanceof EndForEachNode){
+					return possibleEndForEachNode;
+				}
+			}
+		}
+		throw new XBayaRuntimeException("EndForEachNode not found");
+	}
 
 }
\ No newline at end of file