[Swift-commit] r3647 - in trunk: libexec src/org/griphyn/vdl/engine src/org/griphyn/vdl/karajan/lib/swiftscript tests/functions
noreply at svn.ci.uchicago.edu
noreply at svn.ci.uchicago.edu
Mon Sep 27 12:56:09 CDT 2010
Author: wozniak
Date: 2010-09-27 12:56:09 -0500 (Mon, 27 Sep 2010)
New Revision: 3647
Added:
trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Sprintf.java
trunk/tests/functions/200-sprintf.swift
trunk/tests/functions/README.txt
Modified:
trunk/libexec/vdl-lib.xml
trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Tracef.java
Log:
New @sprintf() and test
Modified: trunk/libexec/vdl-lib.xml
===================================================================
--- trunk/libexec/vdl-lib.xml 2010-09-27 16:32:09 UTC (rev 3646)
+++ trunk/libexec/vdl-lib.xml 2010-09-27 17:56:09 UTC (rev 3647)
@@ -15,6 +15,7 @@
<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="sprintf"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Sprintf"/></export>
<export name="tostring"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
<export name="assert"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Assert"/></export>
<export name="trace"><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 2010-09-27 16:32:09 UTC (rev 3646)
+++ trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java 2010-09-27 17:56:09 UTC (rev 3647)
@@ -224,6 +224,12 @@
strcat.addOutputArg(strcatOut1);
functionsMap.put(strcat.getName(), strcat);
+ ProcedureSignature sprintf = new ProcedureSignature("sprintf");
+ sprintf.setAnyNumOfInputArgs();
+ FormalArgumentSignature sprintfOut1 = new FormalArgumentSignature("string");
+ sprintf.addOutputArg(sprintfOut1);
+ functionsMap.put(sprintf.getName(), sprintf);
+
ProcedureSignature strcut = new ProcedureSignature("strcut");
FormalArgumentSignature strcutIn1 = new FormalArgumentSignature("string");
strcut.addInputArg(strcutIn1);
Added: trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Sprintf.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Sprintf.java (rev 0)
+++ trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Sprintf.java 2010-09-27 17:56:09 UTC (rev 3647)
@@ -0,0 +1,245 @@
+package org.griphyn.vdl.karajan.lib.swiftscript;
+
+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.SwiftArg;
+import org.griphyn.vdl.karajan.lib.VDLFunction;
+import org.griphyn.vdl.mapping.ArrayDataNode;
+import org.griphyn.vdl.mapping.DSHandle;
+import org.griphyn.vdl.mapping.Path;
+import org.griphyn.vdl.mapping.RootDataNode;
+import org.griphyn.vdl.type.Types;
+
+/**
+ Formatted string generation. <br>
+ Example: sprintf("\t%s\n", "hello"); <br>
+ Differences from trace():
+ 1) respects \t, \n and \\;
+ 2) allows for typechecked format specifiers;
+ 3) allows for consumption of variables without display (%k)
+ Format specifiers: <br>
+ %%: % sign. <br>
+ %M: Filename output: waits for close
+ %p: Not typechecked, output as in trace(). <br>
+ %f: Typechecked float output. <br>
+ %i: Typechecked int output. <br>
+ %s: Typechecked string output. <br>
+ %k: Variable sKipped, no output. <br>
+ %q: Array output
+ */
+public class Sprintf extends VDLFunction {
+
+ private static final Logger logger =
+ Logger.getLogger(Sprintf.class);
+
+ static {
+ setArguments(Sprintf.class, new Arg[] { Arg.VARGS });
+ }
+
+ @Override
+ protected Object function(VariableStack stack)
+ throws ExecutionException {
+ DSHandle[] args = SwiftArg.VARGS.asDSHandleArray(stack);
+
+ for (int i = 0; i < args.length; i++) {
+ DSHandle handle = args[i];
+ VDLFunction.waitFor(stack, handle);
+ }
+ String msg = format(args);
+ logger.debug("generated: " + msg);
+
+ DSHandle result = new RootDataNode(Types.STRING);
+ result.setValue(msg);
+ return result;
+ }
+
+ private String format(DSHandle[] args)
+ throws ExecutionException {
+ if (! (args[0].getType() == Types.STRING))
+ throw new ExecutionException
+ ("First argument to sprintf() must be a string!");
+
+ String spec = args[0].toString();
+ DSHandle[] vars = copyArray(args, 1, args.length-1);
+
+ StringBuilder output = new StringBuilder();
+ format(spec, vars, output);
+
+ return output.toString();
+ }
+
+ public static DSHandle[] copyArray(DSHandle[] src,
+ int offset, int length)
+ {
+ DSHandle[] result = new DSHandle[length];
+
+ for (int i = 0; i < length; i++)
+ result[i] = src[i+offset];
+
+ return result;
+ }
+
+ /**
+ This method can be targeted as a helper function
+ (by @sprintf(), etc.)
+ */
+ public static void format(String spec, DSHandle[] vars,
+ StringBuilder output)
+ throws ExecutionException
+ {
+ int i = 0;
+ int arg = 0;
+ while (i < spec.length()) {
+ char c = spec.charAt(i);
+ if (c == '%') {
+ char d = spec.charAt(++i);
+ arg = append(d, arg, vars, output);
+ }
+ else if (c == '\\') {
+ char d = spec.charAt(++i);
+ escape(d, output);
+ }
+ else {
+ output.append(c);
+ }
+ i++;
+ }
+ }
+
+ private static int append(char c, int arg, DSHandle[] vars,
+ StringBuilder output)
+ throws ExecutionException {
+ if (c == '%') {
+ output.append('%');
+ return arg;
+ }
+ if (arg >= vars.length) {
+ throw new ExecutionException
+ ("tracef(): too many specifiers!");
+ }
+ if (c == 'M') {
+ append_M(vars[arg], output);
+ }
+ else if (c == 'f') {
+ append_f(vars[arg], output);
+ }
+ else if (c == 'i') {
+ append_i(vars[arg], output);
+ }
+ else if (c == 'p') {
+ output.append(vars[arg].toString());
+ }
+ else if (c == 's') {
+ append_s(vars[arg], output);
+ }
+ else if (c == 'q') {
+ append_q(vars[arg], output);
+ }
+ else if (c == 'k') {
+ ;
+ }
+ else {
+ throw new ExecutionException
+ ("tracef(): Unknown format: %" + c);
+ }
+ return arg+1;
+ }
+
+ private static void append_M(DSHandle arg, StringBuilder output)
+ throws ExecutionException {
+ try {
+ synchronized (arg.getRoot()) {
+ String[] names = VDLFunction.filename(arg);
+ if (names.length > 1)
+ output.append(names);
+ else
+ output.append(names[0]);
+ }
+ }
+ catch (Exception e) {
+ throw new ExecutionException
+ ("tracef(%M): Could not lookup: " + arg);
+ }
+ }
+
+ private static void append_f(DSHandle arg, StringBuilder output)
+ throws ExecutionException {
+ if (arg.getType() == Types.FLOAT) {
+ output.append(arg).toString();
+ }
+ else {
+ throw new ExecutionException
+ ("tracef(): %f requires a float!");
+ }
+ }
+
+ private static void append_i(DSHandle arg, StringBuilder output)
+ throws ExecutionException {
+ if (arg.getType() == Types.INT) {
+ output.append(arg).toString();
+ }
+ else {
+ throw new ExecutionException
+ ("tracef(): %i requires an int!");
+ }
+ }
+
+ private static void append_q(DSHandle arg, StringBuilder output)
+ throws ExecutionException {
+ if (arg instanceof ArrayDataNode) {
+ ArrayDataNode node = (ArrayDataNode) arg;
+ output.append("[");
+ try {
+ int size = node.size();
+ for (int i = 0; i < size; i++) {
+ String entry = ""+i;
+ DSHandle handle =
+ node.getField(Path.parse(entry));
+ output.append(handle);
+ if (i < size-1)
+ output.append(",");
+ }
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ throw new ExecutionException
+ ("trace(%q): Could not get children of: " + arg);
+ }
+ output.append("]");
+ }
+ else {
+ throw new ExecutionException
+ ("tracef(): %q requires an array!");
+ }
+ }
+
+ private static void append_s(DSHandle arg, StringBuilder output)
+ throws ExecutionException {
+ if (arg.getType() == Types.STRING) {
+ output.append(arg).toString();
+ }
+ else {
+ throw new ExecutionException
+ ("tracef(): %s requires a string!");
+ }
+ }
+
+ private static void escape(char c, StringBuilder output)
+ throws ExecutionException {
+ if (c == '\\') {
+ output.append('\\');
+ }
+ else if (c == 'n') {
+ output.append('\n');
+ }
+ else if (c == 't') {
+ output.append('\t');
+ }
+ else {
+ throw new ExecutionException
+ ("tracef(): unknown backslash escape sequence!");
+ }
+ }
+}
Modified: trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Tracef.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Tracef.java 2010-09-27 16:32:09 UTC (rev 3646)
+++ trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Tracef.java 2010-09-27 17:56:09 UTC (rev 3647)
@@ -6,28 +6,17 @@
import org.globus.cog.karajan.workflow.ExecutionException;
import org.griphyn.vdl.karajan.lib.SwiftArg;
import org.griphyn.vdl.karajan.lib.VDLFunction;
-import org.griphyn.vdl.mapping.ArrayDataNode;
import org.griphyn.vdl.mapping.DSHandle;
-import org.griphyn.vdl.mapping.Path;
-import org.griphyn.vdl.type.Types;
/**
Formatted trace output. <br>
Example: tracef("\t%s\n", "hello"); <br>
Differences from trace():
1) respects \t, \n and \\;
- 2) allows for typechecked format specifiers;
+ 2) allows for typechecked format specifiers
+ (cf. {@link Sprintf});
3) allows for consumption of variables without display (%k);
4) does not impose any formatting (commas, etc.). <br><br>
- Format specifiers: <br>
- %%: % sign. <br>
- %M: Filename output: waits for close
- %p: Not typechecked, output as in trace(). <br>
- %f: Typechecked float output. <br>
- %i: Typechecked int output. <br>
- %s: Typechecked string output. <br>
- %k: Variable sKipped, no output. <br>
- %q: Array output
*/
public class Tracef extends VDLFunction {
@@ -47,175 +36,15 @@
DSHandle handle = args[i];
VDLFunction.waitFor(stack, handle);
}
- String msg = format(args);
+
+ String spec = args[0].toString();
+ DSHandle[] vars = Sprintf.copyArray(args, 1, args.length-1);
+
+ StringBuilder output = new StringBuilder();
+ Sprintf.format(spec, vars, output);
+ String msg = output.toString();
logger.info(msg);
System.out.print(msg);
return null;
}
-
- private String format(DSHandle[] args)
- throws ExecutionException {
- if (! (args[0].getType() == Types.STRING))
- throw new ExecutionException
- ("First argument to tracef() must be a string!");
-
- String spec = args[0].toString();
- StringBuffer output = new StringBuffer();
- int i = 0;
- int arg = 1;
- while (i < spec.length()) {
- char c = spec.charAt(i);
- if (c == '%') {
- char d = spec.charAt(++i);
- arg = append(d, arg, args, output);
- }
- else if (c == '\\') {
- char d = spec.charAt(++i);
- escape(d, output);
- }
- else {
- output.append(c);
- }
- i++;
- }
- String result = output.toString();
- return result;
- }
-
- private int append(char c, int arg, DSHandle[] args,
- StringBuffer output)
- throws ExecutionException {
- if (c == '%') {
- output.append('%');
- return arg;
- }
- if (arg >= args.length) {
- throw new ExecutionException
- ("tracef(): too many specifiers!");
- }
- if (c == 'M') {
- append_M(args[arg], output);
- }
- else if (c == 'f') {
- append_f(args[arg], output);
- }
- else if (c == 'i') {
- append_i(args[arg], output);
- }
- else if (c == 'p') {
- output.append(args[arg].toString());
- }
- else if (c == 's') {
- append_s(args[arg], output);
- }
- else if (c == 'q') {
- append_q(args[arg], output);
- }
- else if (c == 'k') {
- ;
- }
- else {
- throw new ExecutionException
- ("tracef(): Unknown format: %" + c);
- }
- return arg+1;
- }
-
- private void append_M(DSHandle arg, StringBuffer output)
- throws ExecutionException {
- try {
- synchronized (arg.getRoot()) {
- String[] names = VDLFunction.filename(arg);
- if (names.length > 1)
- output.append(names);
- else
- output.append(names[0]);
- }
- }
- catch (Exception e) {
- throw new ExecutionException
- ("tracef(%M): Could not lookup: " + arg);
- }
- }
-
- private void append_f(DSHandle arg, StringBuffer output)
- throws ExecutionException {
- if (arg.getType() == Types.FLOAT) {
- output.append(arg).toString();
- }
- else {
- throw new ExecutionException
- ("tracef(): %f requires a float!");
- }
- }
-
- private void append_i(DSHandle arg, StringBuffer output)
- throws ExecutionException {
- if (arg.getType() == Types.INT) {
- output.append(arg).toString();
- }
- else {
- throw new ExecutionException
- ("tracef(): %i requires an int!");
- }
- }
-
- private void append_q(DSHandle arg, StringBuffer output)
- throws ExecutionException {
- if (arg instanceof ArrayDataNode) {
- ArrayDataNode node = (ArrayDataNode) arg;
- output.append("[");
- try {
- int size = node.size();
- for (int i = 0; i < size; i++) {
- String entry = ""+i;
- DSHandle handle =
- node.getField(Path.parse(entry));
- output.append(handle);
- if (i < size-1)
- output.append(",");
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- throw new ExecutionException
- ("trace(%q): Could not get children of: " + arg);
- }
- output.append("]");
- }
- else {
- throw new ExecutionException
- ("tracef(): %q requires an array!");
- }
- }
-
- private void append_s(DSHandle arg, StringBuffer output)
- throws ExecutionException {
- if (arg.getType() == Types.STRING) {
- output.append(arg).toString();
- }
- else {
- throw new ExecutionException
- ("tracef(): %s requires a string!");
- }
- }
-
- private void escape(char c, StringBuffer output)
- throws ExecutionException {
- if (c == '\\') {
- output.append('\\');
- }
- else if (c == 'n') {
- output.append('\n');
- }
- else if (c == 't') {
- output.append('\t');
- }
- else {
- throw new ExecutionException
- ("tracef(): unknown backslash escape sequence!");
- }
- }
-
-
}
Added: trunk/tests/functions/200-sprintf.swift
===================================================================
--- trunk/tests/functions/200-sprintf.swift (rev 0)
+++ trunk/tests/functions/200-sprintf.swift 2010-09-27 17:56:09 UTC (rev 3647)
@@ -0,0 +1,7 @@
+
+string d = "-";
+string b = "bye";
+
+string f = @sprintf("hi%s%s", d, b);
+
+assert(f == "hi-bye");
Added: trunk/tests/functions/README.txt
===================================================================
--- trunk/tests/functions/README.txt (rev 0)
+++ trunk/tests/functions/README.txt 2010-09-27 17:56:09 UTC (rev 3647)
@@ -0,0 +1,6 @@
+
+Tests for Swift's functions library
+
+100 series: Fundamentals
+200 series: String processing
+400 series: I/O
More information about the Swift-commit
mailing list