[Swift-commit] r2994 - in trunk: . docs libexec src/org/griphyn/vdl/engine src/org/griphyn/vdl/karajan/lib/swiftscript tests/language-behaviour

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Wed Jul 1 10:11:01 CDT 2009


Author: benc
Date: 2009-07-01 10:11:01 -0500 (Wed, 01 Jul 2009)
New Revision: 2994

Added:
   trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/WriteData.java
   trunk/tests/language-behaviour/writeDataPrimitive.out.expected
   trunk/tests/language-behaviour/writeDataPrimitive.swift
   trunk/tests/language-behaviour/writeDataStringArray.swift
   trunk/tests/language-behaviour/writeDataStringArray2.out.expected
   trunk/tests/language-behaviour/writeDataStringArray2.swift
   trunk/tests/language-behaviour/writeDataStruct.out.expected
   trunk/tests/language-behaviour/writeDataStruct.swift
   trunk/tests/language-behaviour/writeDataStructArray2.out.expected
   trunk/tests/language-behaviour/writeDataStructArray2.swift
Modified:
   trunk/CHANGES.txt
   trunk/docs/userguide.xml
   trunk/libexec/vdl-lib.xml
   trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
Log:
New procedure writeData to write data structures to a file.

This should be the inverse of readData: readData(writeData(X)) == X
and, modulo formatting and assuming data is in the correct format,
writeData(readData(X)) == X

The initial use for this is to write out long lists of filenames into a disk
file, rather than passing them on the command line.

Modified: trunk/CHANGES.txt
===================================================================
--- trunk/CHANGES.txt	2009-06-30 12:40:25 UTC (rev 2993)
+++ trunk/CHANGES.txt	2009-07-01 15:11:01 UTC (rev 2994)
@@ -1,3 +1,7 @@
+(07/01/09)
+*** New writeData procedure which writes data structures to files in the same
+    format as used by readData.
+
 (06/30/09)
 *** New parameter -condor-g for swift-osg-ress-site-catalog which causes
     sites files to be generate which will use a local condor-g installation

Modified: trunk/docs/userguide.xml
===================================================================
--- trunk/docs/userguide.xml	2009-06-30 12:40:25 UTC (rev 2993)
+++ trunk/docs/userguide.xml	2009-07-01 15:11:01 UTC (rev 2994)
@@ -2189,6 +2189,12 @@
 			</para>
 		</section>
 
+		<section id="procedure.writedata"><title>writeData</title>
+			<para>
+<literal>writeData</literal> will write out data structures in the format
+described for <literal>readData</literal>
+			</para>
+		</section>
 	</section>
 
 	<section id="engineconfiguration">	

Modified: trunk/libexec/vdl-lib.xml
===================================================================
--- trunk/libexec/vdl-lib.xml	2009-06-30 12:40:25 UTC (rev 2993)
+++ trunk/libexec/vdl-lib.xml	2009-07-01 15:11:01 UTC (rev 2994)
@@ -5,6 +5,7 @@
 	<export name="extractint"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.ExtractInt"/></export>
 	<export name="readdata"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.ReadData"/></export>
 	<export name="readdata2"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.ReadData2"/></export>
+	<export name="writedata"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.WriteData"/></export>
 	<export name="strcat"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
 	<export name="strcut"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
 	<export name="strsplit"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>

Modified: trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java	2009-06-30 12:40:25 UTC (rev 2993)
+++ trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java	2009-07-01 15:11:01 UTC (rev 2994)
@@ -137,7 +137,15 @@
 		trace.setAnyNumOfInputArgs();
 		trace.setInvocationMode(INVOCATION_INTERNAL);
 		proceduresMap.put("trace", trace);
-		
+			
+		ProcedureSignature writeData = new ProcedureSignature("writeData");
+		FormalArgumentSignature wdInputArg = new FormalArgumentSignature(true);
+		writeData.addInputArg(wdInputArg);
+		FormalArgumentSignature wdOutputArg = new FormalArgumentSignature(true);
+		writeData.addOutputArg(wdOutputArg);		
+		writeData.setInvocationMode(INVOCATION_INTERNAL);
+		proceduresMap.put("writeData", writeData);
+			
 		return proceduresMap;
 	}
 	

Added: trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/WriteData.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/WriteData.java	                        (rev 0)
+++ trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/WriteData.java	2009-07-01 15:11:01 UTC (rev 2994)
@@ -0,0 +1,179 @@
+package org.griphyn.vdl.karajan.lib.swiftscript;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Iterator;
+
+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.AbstractDataNode;
+import org.griphyn.vdl.mapping.DSHandle;
+import org.griphyn.vdl.mapping.HandleOpenException;
+import org.griphyn.vdl.mapping.InvalidPathException;
+import org.griphyn.vdl.mapping.Path;
+import org.griphyn.vdl.mapping.PhysicalFormat;
+import org.griphyn.vdl.type.Type;
+import org.griphyn.vdl.type.Types;
+
+
+public class WriteData extends VDLFunction {
+	public static final Logger logger = Logger.getLogger(WriteData.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(WriteData.class, new Arg[] { DEST, SRC });
+	}
+
+	protected Object function(VariableStack stack) throws ExecutionException, HandleOpenException {
+		// dest needs to be mapped to a file, or a string
+		DSHandle dest = (DSHandle) DEST.getValue(stack);
+
+		// src can be any of several forms of value
+		DSHandle src = (DSHandle) SRC.getValue(stack);
+
+		waitFor(stack, src);
+
+		if (dest.getType().equals(Types.STRING)) {
+			writeData((String)dest.getValue(), src);
+		}
+		else {
+			PhysicalFormat pf = dest.getMapper().map(Path.EMPTY_PATH);
+			if (pf instanceof AbsFile) {
+				AbsFile af = (AbsFile) pf;
+				if (!af.getProtocol().equalsIgnoreCase("file")) {
+					throw new ExecutionException("writeData only supports local files");
+				}
+				writeData(af.getPath(), src);
+			}
+			else {
+				throw new ExecutionException("writeData only supports writing to files");
+			}
+			dest.closeDeep();
+		}
+		return null;
+	}
+
+	private void writeData(String path, DSHandle src) throws ExecutionException {
+		File f = new File(path);
+		try {
+			BufferedWriter br = new BufferedWriter(new FileWriter(f));
+			try {
+				if (src.getType().isArray()) {
+					// each line is an item
+					writeArray(br, src);
+				}
+				else if (src.getType().isPrimitive()) {
+					writePrimitive(br, src);
+				}
+				else {
+					// struct
+					writeStructHeader(src.getType(), br);
+					writeStruct(br, src);
+				}
+			}
+			finally {
+				try {
+					br.close();
+				}
+				catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+		}
+		catch (IOException e) {
+			throw new ExecutionException(e);
+		}
+	}
+
+	private void writePrimitive(BufferedWriter br, DSHandle src) throws IOException,
+			ExecutionException {
+		br.write(src.getValue().toString());
+	}
+
+	private void writeArray(BufferedWriter br, DSHandle src) throws IOException, ExecutionException {
+		if (src.getType().itemType().isPrimitive()) {
+			writePrimitiveArray(br, src);
+		}
+		else {
+			writeStructArray(br, src);
+		}
+	}
+
+	private void writePrimitiveArray(BufferedWriter br, DSHandle src) throws IOException,
+			ExecutionException {
+		Map m = ((AbstractDataNode) src).getArrayValue();
+		Map c = new TreeMap(new ArrayIndexComparator());
+		c.putAll(m);
+		Iterator i = c.values().iterator();
+		while(i.hasNext()) {
+			br.write(((DSHandle)i.next()).toString());
+			br.newLine();
+		}
+	}
+
+	private void writeStructArray(BufferedWriter br, DSHandle src) throws IOException,
+			ExecutionException {
+		writeStructHeader(src.getType().itemType(), br);
+		Map m = ((AbstractDataNode) src).getArrayValue();
+		Map c = new TreeMap(new ArrayIndexComparator());
+		c.putAll(m);
+		Iterator i = c.values().iterator();
+		while(i.hasNext()) {
+			writeStruct(br, (DSHandle)i.next());
+		}
+	}
+
+
+	private void writeStructHeader(Type type, BufferedWriter br) throws ExecutionException,
+			IOException {
+		List l = type.getFieldNames();
+		Iterator i = l.iterator();
+		while(i.hasNext()) {
+			br.write(i.next().toString());
+			br.write(" ");
+		}
+		br.newLine();
+	}
+
+	private void writeStruct(BufferedWriter br, DSHandle struct) throws IOException, ExecutionException {
+		List l = struct.getType().getFieldNames();
+		Iterator i = l.iterator();
+		try {
+			while(i.hasNext()) {
+				DSHandle child = struct.getField(Path.EMPTY_PATH.addLast((String)i.next()));
+				br.write(child.toString());
+				br.write(" ");
+			}
+			br.newLine();
+		} catch(InvalidPathException e) {
+			throw new ExecutionException("Unexpectedly invalid path", e);
+		}
+	}
+
+	class ArrayIndexComparator implements Comparator {
+		public int compare(Object o1, Object o2) {
+			int i1 = Integer.parseInt((String)o1);
+			int i2 = Integer.parseInt((String)o2);
+			if(i1 < i2) return -1;
+			if(i1 > i2) return 1;
+			return 0;
+		}
+	}
+}

Added: trunk/tests/language-behaviour/writeDataPrimitive.out.expected
===================================================================
--- trunk/tests/language-behaviour/writeDataPrimitive.out.expected	                        (rev 0)
+++ trunk/tests/language-behaviour/writeDataPrimitive.out.expected	2009-07-01 15:11:01 UTC (rev 2994)
@@ -0,0 +1 @@
+foo
\ No newline at end of file

Added: trunk/tests/language-behaviour/writeDataPrimitive.swift
===================================================================
--- trunk/tests/language-behaviour/writeDataPrimitive.swift	                        (rev 0)
+++ trunk/tests/language-behaviour/writeDataPrimitive.swift	2009-07-01 15:11:01 UTC (rev 2994)
@@ -0,0 +1,8 @@
+type file;
+
+string s = "foo";
+
+file f <"writeDataPrimitive.out">;
+
+f=writeData(s);
+

Added: trunk/tests/language-behaviour/writeDataStringArray.swift
===================================================================
--- trunk/tests/language-behaviour/writeDataStringArray.swift	                        (rev 0)
+++ trunk/tests/language-behaviour/writeDataStringArray.swift	2009-07-01 15:11:01 UTC (rev 2994)
@@ -0,0 +1,8 @@
+type file;
+
+string s[] = ["foo", "bar"];
+
+file f <"writeDataStringArray.out">;
+
+f=writeData(s);
+

Added: trunk/tests/language-behaviour/writeDataStringArray2.out.expected
===================================================================
--- trunk/tests/language-behaviour/writeDataStringArray2.out.expected	                        (rev 0)
+++ trunk/tests/language-behaviour/writeDataStringArray2.out.expected	2009-07-01 15:11:01 UTC (rev 2994)
@@ -0,0 +1,5 @@
+foo
+bar
+baz
+qux
+frrrr

Added: trunk/tests/language-behaviour/writeDataStringArray2.swift
===================================================================
--- trunk/tests/language-behaviour/writeDataStringArray2.swift	                        (rev 0)
+++ trunk/tests/language-behaviour/writeDataStringArray2.swift	2009-07-01 15:11:01 UTC (rev 2994)
@@ -0,0 +1,14 @@
+type file;
+
+string s[];
+
+file f <"writeDataStringArray2.out">;
+
+f=writeData(s);
+
+s[2] = "baz";
+s[3] = "qux";
+s[0] = "foo";
+s[1] = "bar";
+s[4] = "frrrr";
+

Added: trunk/tests/language-behaviour/writeDataStruct.out.expected
===================================================================
--- trunk/tests/language-behaviour/writeDataStruct.out.expected	                        (rev 0)
+++ trunk/tests/language-behaviour/writeDataStruct.out.expected	2009-07-01 15:11:01 UTC (rev 2994)
@@ -0,0 +1,2 @@
+r c l 
+Baz BAZ baz 

Added: trunk/tests/language-behaviour/writeDataStruct.swift
===================================================================
--- trunk/tests/language-behaviour/writeDataStruct.swift	                        (rev 0)
+++ trunk/tests/language-behaviour/writeDataStruct.swift	2009-07-01 15:11:01 UTC (rev 2994)
@@ -0,0 +1,14 @@
+type file;
+
+type S { string l; string c; string r; }
+
+S s;
+
+file f <"writeDataStruct.out">;
+
+f=writeData(s);
+
+s.l = "baz";
+s.c = "BAZ";
+s.r = "Baz";
+

Added: trunk/tests/language-behaviour/writeDataStructArray2.out.expected
===================================================================
--- trunk/tests/language-behaviour/writeDataStructArray2.out.expected	                        (rev 0)
+++ trunk/tests/language-behaviour/writeDataStructArray2.out.expected	2009-07-01 15:11:01 UTC (rev 2994)
@@ -0,0 +1,6 @@
+r c l 
+Foo FOO foo 
+Bar BAR bar 
+Baz BAZ baz 
+Qux QUX qux 
+Frrrr FRRRR frrrr 

Added: trunk/tests/language-behaviour/writeDataStructArray2.swift
===================================================================
--- trunk/tests/language-behaviour/writeDataStructArray2.swift	                        (rev 0)
+++ trunk/tests/language-behaviour/writeDataStructArray2.swift	2009-07-01 15:11:01 UTC (rev 2994)
@@ -0,0 +1,26 @@
+type file;
+
+type S { string l; string c; string r; }
+
+S s[];
+
+file f <"writeDataStructArray2.out">;
+
+f=writeData(s);
+
+s[2].l = "baz";
+s[2].c = "BAZ";
+s[2].r = "Baz";
+s[3].l = "qux";
+s[3].c = "QUX";
+s[3].r = "Qux";
+s[0].l = "foo";
+s[0].c = "FOO";
+s[0].r = "Foo";
+s[1].l = "bar";
+s[1].c = "BAR";
+s[1].r = "Bar";
+s[4].l = "frrrr";
+s[4].c = "FRRRR";
+s[4].r = "Frrrr";
+




More information about the Swift-commit mailing list