[Swift-commit] r2298 - in trunk/src/org/griphyn/vdl: engine karajan/lib/swiftscript

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Tue Oct 14 13:32:49 CDT 2008


Author: hategan
Date: 2008-10-14 13:32:48 -0500 (Tue, 14 Oct 2008)
New Revision: 2298

Added:
   trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/ReadData2.java
Modified:
   trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
Log:
added readData2

Modified: trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java	2008-10-14 16:34:56 UTC (rev 2297)
+++ trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java	2008-10-14 18:32:48 UTC (rev 2298)
@@ -104,6 +104,13 @@
 		readData.addOutputArg(rdOutputArg);		
 		proceduresMap.put("readData", readData);
 		
+		ProcedureSignature readData2 = new ProcedureSignature("readData2");
+        FormalArgumentSignature rd2InputArg = new FormalArgumentSignature(true);
+        readData2.addInputArg(rd2InputArg);
+        FormalArgumentSignature rd2OutputArg = new FormalArgumentSignature(true);
+        readData2.addOutputArg(rd2OutputArg);     
+        proceduresMap.put("readData2", readData2);
+		
 		ProcedureSignature print = new ProcedureSignature("print");
 		print.setAnyNumOfInputArgs();
 		proceduresMap.put("print", print);

Added: trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/ReadData2.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/ReadData2.java	                        (rev 0)
+++ trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/ReadData2.java	2008-10-14 18:32:48 UTC (rev 2298)
@@ -0,0 +1,122 @@
+/*
+ * Created on Oct 8, 2007
+ */
+package org.griphyn.vdl.karajan.lib.swiftscript;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+import org.apache.log4j.Logger;
+import org.globus.cog.karajan.arguments.Arg;
+import org.globus.cog.karajan.stack.VariableStack;
+import org.globus.cog.karajan.workflow.ExecutionException;
+import org.griphyn.vdl.karajan.lib.VDLFunction;
+import org.griphyn.vdl.mapping.AbsFile;
+import org.griphyn.vdl.mapping.DSHandle;
+import org.griphyn.vdl.mapping.HandleOpenException;
+import org.griphyn.vdl.mapping.Path;
+import org.griphyn.vdl.mapping.PhysicalFormat;
+import org.griphyn.vdl.type.Types;
+
+public class ReadData2 extends VDLFunction {
+	public static final Logger logger = Logger.getLogger(ReadData2.class);
+
+	public static final Arg DEST = new Arg.Positional("dest");
+	public static final Arg SRC = new Arg.Positional("src");
+	public static boolean warning;
+
+	static {
+		setArguments(ReadData2.class, new Arg[] { DEST, SRC });
+	}
+
+	protected Object function(VariableStack stack) throws ExecutionException, HandleOpenException {
+		DSHandle dest = (DSHandle) DEST.getValue(stack);
+		DSHandle src = (DSHandle) SRC.getValue(stack);
+		waitFor(stack, src);
+		if (src.getType().equals(Types.STRING)) {
+			readData(dest, (String) src.getValue());
+		}
+		else {
+			PhysicalFormat pf = src.getMapper().map(Path.EMPTY_PATH);
+			if (pf instanceof AbsFile) {
+				AbsFile af = (AbsFile) pf;
+				if (!af.getProtocol().equalsIgnoreCase("file")) {
+					throw new ExecutionException("readData only supports local files");
+				}
+				readData(dest, af.getPath());
+			}
+			else {
+				throw new ExecutionException("readData only supports reading from files");
+			}
+		}
+		return null;
+	}
+
+	private void readData(DSHandle dest, String path) throws ExecutionException {
+		File f = new File(path);
+		try {
+			BufferedReader br = new BufferedReader(new FileReader(f));
+			try {
+				readLines(dest, br, path);
+			}
+			finally {
+				try {
+					br.close();
+				}
+				catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+		}
+		catch (Exception e) {
+			throw new ExecutionException(e);
+		}
+	}
+
+	private void readLines(DSHandle dest, BufferedReader br, String path)
+			throws ExecutionException, IOException {
+		int count = 1;
+		String line = br.readLine();
+		while (line != null) {
+			line = line.trim();
+			if (!line.startsWith("#") && !line.equals("")) {
+				try {
+					String[] sp = line.split("=", 2);
+					setValue(dest.getField(Path.parse(sp[0].trim())), sp[1].trim());
+				}
+				catch (Exception e) {
+					throw new ExecutionException(e.getMessage() + " in " + path + ", line " + count
+							+ ": " + line, e);
+				}
+			}
+			line = br.readLine();
+			count++;
+		}
+	}
+
+	private void setValue(DSHandle dest, String s) throws ExecutionException {
+		try {
+			if (dest.getType().equals(Types.INT)) {
+				dest.setValue(new Double(Integer.parseInt(s.trim())));
+			}
+			else if (dest.getType().equals(Types.FLOAT)) {
+				dest.setValue(new Double(s.trim()));
+			}
+			else if (dest.getType().equals(Types.BOOLEAN)) {
+				dest.setValue(new Boolean(s.trim()));
+			}
+			else if (dest.getType().equals(Types.STRING)) {
+				dest.setValue(s);
+			}
+			else {
+				throw new ExecutionException("Don't know how to read type " + dest.getType()
+						+ " for path " + dest.getPathFromRoot());
+			}
+		}
+		catch (NumberFormatException e) {
+			throw new ExecutionException("Could not convert value to number: " + s);
+		}
+	}
+}




More information about the Swift-commit mailing list