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

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Tue Jul 27 20:11:31 CDT 2010


Author: jonmon
Date: 2010-07-27 20:11:31 -0500 (Tue, 27 Jul 2010)
New Revision: 3478

Modified:
   trunk/libexec/vdl-lib.xml
   trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
   trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Misc.java
Log:
o trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
o trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Misc.java
o trunk/libexec/vdl-lib.xml
  -- Added format function. Takes in a float and formats to desired precision which it returns as a string.
  -- Added pad function. Pads arbitrary number of zeros to the front of an int and return a string.
  -- Added length fucntion. Returns the number of elements of a closed list.
  -- toint and tofloat are now polymorphic. They will try to convert anything to an int or float respectively.
     If they can't they return an ExecutionException.



Modified: trunk/libexec/vdl-lib.xml
===================================================================
--- trunk/libexec/vdl-lib.xml	2010-07-27 19:21:35 UTC (rev 3477)
+++ trunk/libexec/vdl-lib.xml	2010-07-28 01:11:31 UTC (rev 3478)
@@ -13,6 +13,8 @@
 	<export name="regexp"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
 	<export name="toint"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
 	<export name="tofloat"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
+	<export name="format"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
+	<export name="pad"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
 	<export name="tostring"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
 	<export name="trace"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
 	<export name="tracef"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Tracef"/></export>
@@ -20,6 +22,7 @@
 	<export name="fileName"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.FileName"/></export>
 	<export name="fileNames"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.FileNames"/></export>
 	<export name="dirname"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
+	<export name="length"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
 	<export name="arg">
 		<import file="sys.xml"/>
 		<if>

Modified: trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java	2010-07-27 19:21:35 UTC (rev 3477)
+++ trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java	2010-07-28 01:11:31 UTC (rev 3478)
@@ -194,6 +194,13 @@
 		FormalArgumentSignature dirnameOut1 = new FormalArgumentSignature("string");
 		dirname.addOutputArg(dirnameOut1);
 		functionsMap.put(dirname.getName(), dirname);
+		
+		ProcedureSignature length = new ProcedureSignature("length");
+		FormalArgumentSignature lengthIn1 = new FormalArgumentSignature(true);
+		length.addInputArg(lengthIn1);
+		FormalArgumentSignature lengthOut1 = new FormalArgumentSignature("int");
+		length.addOutputArg(lengthOut1);
+		functionsMap.put(length.getName(), length);
                 
 		ProcedureSignature regexp = new ProcedureSignature("regexp");
 		FormalArgumentSignature regexpIn1 = new FormalArgumentSignature("string");
@@ -240,19 +247,37 @@
 		functionsMap.put(strsplit.getName(), strsplit);
 		
 		ProcedureSignature toint = new ProcedureSignature("toint");
-		FormalArgumentSignature tointIn1 = new FormalArgumentSignature("string");
+		FormalArgumentSignature tointIn1 = new FormalArgumentSignature(true);
 		toint.addInputArg(tointIn1);
 		FormalArgumentSignature toOut1 = new FormalArgumentSignature("int");
 		toint.addOutputArg(toOut1);
 		functionsMap.put(toint.getName(), toint);
 
 		ProcedureSignature tofloat = new ProcedureSignature("tofloat");
-		FormalArgumentSignature tofloatIn1 = new FormalArgumentSignature("string");
+		FormalArgumentSignature tofloatIn1 = new FormalArgumentSignature(true);
 		tofloat.addInputArg(tofloatIn1);
 		FormalArgumentSignature tofloatOut1 = new FormalArgumentSignature("float");
 		tofloat.addOutputArg(tofloatOut1);
 		functionsMap.put(tofloat.getName(), tofloat);
 		
+	    ProcedureSignature format = new ProcedureSignature("format");
+	    FormalArgumentSignature formatIn1 = new FormalArgumentSignature("float");
+	    FormalArgumentSignature formatIn2 = new FormalArgumentSignature("int");
+	    format.addInputArg(formatIn1);
+	    format.addInputArg(formatIn2);
+	    FormalArgumentSignature formatOut = new FormalArgumentSignature("string");
+	    format.addOutputArg(formatOut);
+	    functionsMap.put(format.getName(), format);
+	    
+	    ProcedureSignature pad = new ProcedureSignature("pad");
+        FormalArgumentSignature padIn1 = new FormalArgumentSignature("int");
+        FormalArgumentSignature padIn2 = new FormalArgumentSignature("int");
+        pad.addInputArg(padIn1);
+        pad.addInputArg(padIn2);
+        FormalArgumentSignature padOut = new FormalArgumentSignature("string");
+        pad.addOutputArg(padOut);
+        functionsMap.put(pad.getName(), pad);
+		
 		ProcedureSignature tostring = new ProcedureSignature("tostring");
 		FormalArgumentSignature tostringIn1 = new FormalArgumentSignature(true);
 		tostring.addInputArg(tostringIn1);

Modified: trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Misc.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Misc.java	2010-07-27 19:21:35 UTC (rev 3477)
+++ trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Misc.java	2010-07-28 01:11:31 UTC (rev 3478)
@@ -1,6 +1,8 @@
 package org.griphyn.vdl.karajan.lib.swiftscript;
 
 import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
 
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -26,6 +28,8 @@
 import org.griphyn.vdl.mapping.AbsFile;
 import org.globus.cog.karajan.workflow.futures.FutureNotYetAvailable;
 
+import org.griphyn.vdl.mapping.ArrayDataNode;
+
 public class Misc extends FunctionsCollection {
 
 	private static final Logger logger = Logger.getLogger(Misc.class);
@@ -43,8 +47,11 @@
 		setArguments("swiftscript_regexp", new Arg[] { PA_INPUT, PA_PATTERN, PA_TRANSFORM });
 		setArguments("swiftscript_toint", new Arg[] { PA_INPUT });
 		setArguments("swiftscript_tofloat", new Arg[] { PA_INPUT });
+		setArguments("swiftscript_format", new Arg[] { PA_INPUT, PA_TRANSFORM });
+		setArguments("swiftscript_pad", new Arg[] { PA_INPUT, PA_TRANSFORM });
 		setArguments("swiftscript_tostring", new Arg[] { PA_INPUT });
 		setArguments("swiftscript_dirname", new Arg[] { Arg.VARGS });
+		setArguments("swiftscript_length", new Arg[] { Arg.VARGS });
 	}
 
 	private static final Logger traceLogger = Logger.getLogger("org.globus.swift.trace");
@@ -206,8 +213,17 @@
 			inputString = inputString.substring(0, i);
 		}
 		DSHandle handle = new RootDataNode(Types.INT);
-		handle.setValue(new Double(inputString));
+		
+		try
+		{
+		    handle.setValue(new Double(inputString));
+		}
+		catch(NumberFormatException e)
+		{
+		    throw new ExecutionException(stack, "Could not convert value \""+inputString+"\" to type int");
+		}
 		handle.closeShallow();
+		
 		int provid=VDLFunction.nextProvenanceID();
 		VDLFunction.logProvenanceResult(provid, handle, "toint");
 		VDLFunction.logProvenanceParameter(provid, PA_INPUT.getRawValue(stack), "string");
@@ -218,7 +234,15 @@
 			InvalidPathException {
 		String inputString = TypeUtil.toString(PA_INPUT.getValue(stack));
 		DSHandle handle = new RootDataNode(Types.FLOAT);
-		handle.setValue(new Double(inputString));
+		
+		try
+		{
+		    handle.setValue(new Double(inputString));
+		}
+		catch(NumberFormatException e)
+		{
+		    throw new ExecutionException(stack, "Could not convert value \""+inputString+"\" to type float");
+		}
 		handle.closeShallow();
 		int provid=VDLFunction.nextProvenanceID();
 		VDLFunction.logProvenanceResult(provid, handle, "tofloat");
@@ -226,6 +250,50 @@
 		return handle;
 	}
 	
+	/*
+	 * Takes in a float and formats to desired precision and returns a string
+	 */
+	public DSHandle swiftscript_format(VariableStack stack) throws ExecutionException, NoSuchTypeException,
+    InvalidPathException {
+	    String inputString = TypeUtil.toString(PA_INPUT.getValue(stack));
+	    String inputFormat = TypeUtil.toString(PA_TRANSFORM.getValue(stack));
+	    DSHandle handle = new RootDataNode(Types.STRING);
+	    
+	    String output = String.format("%."+inputFormat+"f", Double.parseDouble(inputString));
+	    handle.setValue(output);
+	    handle.closeShallow();
+	    
+	    int provid=VDLFunction.nextProvenanceID();
+	    VDLFunction.logProvenanceResult(provid, handle, "format");
+	    VDLFunction.logProvenanceParameter(provid, PA_INPUT.getRawValue(stack), "float");
+	    VDLFunction.logProvenanceParameter(provid, PA_TRANSFORM.getRawValue(stack), "float");
+	    return handle;
+	}
+	
+	/*
+	 * Takes in an int and pads zeros to the left and returns a string
+	 */
+	public DSHandle swiftscript_pad(VariableStack stack) throws ExecutionException, NoSuchTypeException,
+	        InvalidPathException {
+	    String inputString = TypeUtil.toString(PA_INPUT.getValue(stack));
+	    String inputFormat = TypeUtil.toString(PA_TRANSFORM.getValue(stack));
+	    DSHandle handle = new RootDataNode(Types.STRING);  
+	    
+	    int num_length = inputString.length();
+	    int zeros_to_pad = Integer.parseInt(inputFormat);
+	    zeros_to_pad += num_length;
+	    
+	    String output = String.format("%0"+zeros_to_pad+"d", Integer.parseInt(inputString));
+	    handle.setValue(output);
+	    handle.closeShallow();
+	        
+	    int provid=VDLFunction.nextProvenanceID();
+	    VDLFunction.logProvenanceResult(provid, handle, "pad");
+	    VDLFunction.logProvenanceParameter(provid, PA_INPUT.getRawValue(stack), "int");
+	    VDLFunction.logProvenanceParameter(provid, PA_TRANSFORM.getRawValue(stack), "int");
+	    return handle;
+	}
+	
 	public DSHandle swiftscript_tostring(VariableStack stack)
                 throws ExecutionException, NoSuchTypeException,
                 InvalidPathException {
@@ -236,26 +304,51 @@
                 return handle;
 	}
 
-        public DSHandle swiftscript_dirname(VariableStack stack) 
-                throws ExecutionException, NoSuchTypeException, InvalidPathException {
-                DSHandle handle;
-                try
-                {
-                        DSHandle[] args = SwiftArg.VARGS.asDSHandleArray(stack);
-                        DSHandle arg = args[0];
-                        String[] input = VDLFunction.filename(arg);
-                        String name = input[0]; 
-                        String result = new AbsFile(name).getDir();
-                        handle = new RootDataNode(Types.STRING);
-                        handle.setValue(result);
-                        handle.closeShallow();
-                }
-                catch (HandleOpenException e) {
-                        throw new FutureNotYetAvailable
-                                (VDLFunction.addFutureListener(stack, e.getSource()));
-                }
-                return handle;
-        }
+	public DSHandle swiftscript_dirname(VariableStack stack) 
+	throws ExecutionException, NoSuchTypeException, InvalidPathException {
+	    DSHandle handle;
+	    try
+	    {
+	        DSHandle[] args = SwiftArg.VARGS.asDSHandleArray(stack);
+	        DSHandle arg = args[0];
+	        String[] input = VDLFunction.filename(arg);
+	        String name = input[0]; 
+	        String result = new AbsFile(name).getDir();
+	        handle = new RootDataNode(Types.STRING);
+	        handle.setValue(result);
+	        handle.closeShallow();
+	    }
+	    catch (HandleOpenException e) {
+	        throw new FutureNotYetAvailable
+	        (VDLFunction.addFutureListener(stack, e.getSource()));
+	    }
+	    return handle;
+	}
+	
+	/*
+	 * This is copied from swiftscript_dirname. Both the functions could be changed to be more readable.
+	 * Returns length of an array.
+	 * Good for debugging because array needs to be closed before the length is determined
+	 */
+	public DSHandle swiftscript_length(VariableStack stack) 
+	throws ExecutionException, NoSuchTypeException, InvalidPathException {
+	    DSHandle handle;
+	    DSHandle[] args = SwiftArg.VARGS.asDSHandleArray( stack );
+	    DSHandle arg = args[0];
+	    ArrayDataNode adn = (ArrayDataNode)arg;
+	        
+	    if( !( adn.isClosed() ) )
+	    {
+	        throw new FutureNotYetAvailable( VDLFunction.addFutureListener( stack, adn ) );
+	    }
+	        
+	    int result = adn.size();
+	    handle = new RootDataNode(Types.INT);
+	    handle.setValue(result);
+	    handle.closeShallow();
+	    
+	    return handle;
+	}
 }
 
 /*




More information about the Swift-commit mailing list