[Swift-commit] r3257 - 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
Mon Mar 1 14:22:24 CST 2010


Author: wozniak
Date: 2010-03-01 14:22:24 -0600 (Mon, 01 Mar 2010)
New Revision: 3257

Added:
   trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Java.java
Modified:
   trunk/libexec/vdl-lib.xml
   trunk/src/org/griphyn/vdl/engine/Karajan.java
   trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
Log:
New @java().


Modified: trunk/libexec/vdl-lib.xml
===================================================================
--- trunk/libexec/vdl-lib.xml	2010-03-01 20:17:03 UTC (rev 3256)
+++ trunk/libexec/vdl-lib.xml	2010-03-01 20:22:24 UTC (rev 3257)
@@ -13,6 +13,7 @@
 	<export name="toint"><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.Misc"/></export>
+	<export name="java"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Java"/></export>
 	<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="arg">

Modified: trunk/src/org/griphyn/vdl/engine/Karajan.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/Karajan.java	2010-03-01 20:17:03 UTC (rev 3256)
+++ trunk/src/org/griphyn/vdl/engine/Karajan.java	2010-03-01 20:22:24 UTC (rev 3257)
@@ -430,7 +430,8 @@
 			StringTemplate assignST = template("assign");
 			StringTemplate varST = expressionToKarajan(assign.getAbstractExpressionArray(0),scope);
 			StringTemplate valueST = expressionToKarajan(assign.getAbstractExpressionArray(1),scope);
-			if (!datatype(varST).equals(datatype(valueST)))
+			if (! (datatype(varST).equals(datatype(valueST)) ||
+			       datatype(valueST).equals("java")))
 				throw new CompilationException("You cannot assign value of type " + datatype(valueST) +
 						" to a variable of type " + datatype(varST));
 			assignST.setAttribute("var", varST);
@@ -1395,7 +1396,13 @@
 	}
 	
 	String datatype(StringTemplate st) {
-		return st.getAttribute("datatype").toString();
+	    String result = null;
+	    try { 
+	        result = st.getAttribute("datatype").toString();
+	    }
+	    catch (Exception e) { 
+	        throw new RuntimeException("Not typed properly: " + st); 
+	    }
+	    return result; 
 	}
-
 }

Modified: trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java	2010-03-01 20:17:03 UTC (rev 3256)
+++ trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java	2010-03-01 20:22:24 UTC (rev 3257)
@@ -139,10 +139,10 @@
 		proceduresMap.put("trace", trace);
 
 		ProcedureSignature tracef = new ProcedureSignature("tracef");
-		trace.setAnyNumOfInputArgs();
-		trace.setInvocationMode(INVOCATION_INTERNAL);
-		proceduresMap.put("tracef", trace);
-			
+		tracef.setAnyNumOfInputArgs();
+		tracef.setInvocationMode(INVOCATION_INTERNAL);
+		proceduresMap.put("tracef", tracef);
+		
 		ProcedureSignature writeData = new ProcedureSignature("writeData");
 		FormalArgumentSignature wdInputArg = new FormalArgumentSignature(true);
 		writeData.addInputArg(wdInputArg);
@@ -230,6 +230,12 @@
 		toint.addOutputArg(toOut1);
 		functionsMap.put(toint.getName(), toint);
 		
+		ProcedureSignature java = new ProcedureSignature("java");
+		java.setAnyNumOfInputArgs();
+		FormalArgumentSignature output = new FormalArgumentSignature("java");
+		java.addOutputArg(output);
+		functionsMap.put(java.getName(), java);
+		
 		return functionsMap;
 	}
 	

Added: trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Java.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Java.java	                        (rev 0)
+++ trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Java.java	2010-03-01 20:22:24 UTC (rev 3257)
@@ -0,0 +1,147 @@
+package org.griphyn.vdl.karajan.lib.swiftscript;
+
+import java.lang.reflect.Method;
+
+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.DSHandle;
+import org.griphyn.vdl.mapping.HandleOpenException;
+import org.griphyn.vdl.mapping.InvalidPathException;
+import org.griphyn.vdl.mapping.RootDataNode;
+import org.griphyn.vdl.type.NoSuchTypeException;
+import org.griphyn.vdl.type.Type;
+import org.griphyn.vdl.type.Types;
+
+public class Java extends VDLFunction {
+
+    static {
+        setArguments(Java.class, new Arg[] { Arg.VARGS });
+    }
+    
+    protected Object function(VariableStack stack) throws ExecutionException,
+            HandleOpenException {
+        DSHandle[] args = SwiftArg.VARGS.asDSHandleArray(stack);
+     
+        for (int i = 0; i < args.length; i++) {
+            DSHandle handle = args[i];
+            VDLFunction.waitFor(stack, handle);
+        }
+        
+        Method method = getMethod(args); 
+        Object[] p = convertInputs(method, args);
+        Type type = returnType(method);
+        Object value = invoke(method, p);
+        DSHandle result = swiftResult(type, value);
+        
+        return result;
+    }
+ 
+    /** 
+       Given the user args, locate the Java Method.
+    */
+    Method getMethod(DSHandle[] args) {
+        Method result = null;
+        Class clazz;
+        
+        String lib = "unset";
+        String name = "unset";
+        
+        if (args.length < 2)
+            throw new RuntimeException
+            ("@java() requires at least two arguments");
+        
+        try {
+            lib = args[0].toString();
+            name = args[1].toString();
+            clazz = Class.forName(lib); 
+            Method[] methods = clazz.getMethods();
+            result = null;
+            for (Method m : methods) { 
+                if (m.getName().equals(name)) { 
+                    result = m;
+                    break;
+                }
+            }
+        } 
+        catch (Exception e) { 
+            e.printStackTrace(); 
+            throw new RuntimeException
+            ("@java(): Error attempting to use: " + args[0]); 
+        }
+        
+        if (result == null) 
+            throw new RuntimeException
+            ("No method: " + name + " in " + lib); 
+        return result;
+    }
+    
+    /** 
+       Convert the user args to a Java Object array. 
+    */
+    Object[] convertInputs(Method method, DSHandle[] args) {
+        Object[] result = new Object[args.length-2];
+        Object a = null;
+        try {
+            for (int i = 2; i < args.length; i++) {
+                Type   t = args[i].getType();
+                Object v = args[i].getValue(); 
+                if (t.equals(Types.FLOAT))
+                    a = (Double) v;
+                else if (t.equals(Types.INT))
+                    a = (Integer) v;
+                else if (t.equals(Types.BOOLEAN))
+                    a = (Boolean) v;
+                else if (t.equals(Types.STRING))
+                    a = (String) v;
+                result[i-2] = a;   
+            }
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException
+            ("Error converting input arguments: \n" + 
+                " to: " + method.getDeclaringClass() + 
+                "." + method + " \n argument: " + a);
+        }
+        return result;
+    }
+    
+    Type returnType(Method method) {
+        Type result = null;
+        
+        Class rt = method.getReturnType(); 
+        if (rt.equals(Double.TYPE))
+            result = Types.FLOAT; 
+        else if (rt.equals(Integer.TYPE))
+            result = Types.INT;
+        else if (rt.equals(Boolean.TYPE))
+            result = Types.BOOLEAN;
+        else if (rt.equals(String.class))
+            result = Types.STRING;
+        return result;
+    }
+    
+    Object invoke(Method method, Object[] p) { 
+        Object result = null; 
+        try { 
+            result = method.invoke(null, p);
+        }
+        catch (Exception e) { 
+            e.printStackTrace(); 
+            throw new RuntimeException
+            ("Error attempting to invoke: " + method.getDeclaringClass() + 
+                "." + method); 
+        }
+        return result;
+    }
+    
+    DSHandle swiftResult(Type type, Object value) { 
+        DSHandle result = new RootDataNode(type);
+        result.setValue(value);
+        result.closeShallow();
+        return result;
+    }
+}




More information about the Swift-commit mailing list