[Swift-commit] r5420 - branches/release-0.93/src/org/griphyn/vdl/karajan/lib

hategan at ci.uchicago.edu hategan at ci.uchicago.edu
Wed Dec 14 16:04:16 CST 2011


Author: hategan
Date: 2011-12-14 16:04:16 -0600 (Wed, 14 Dec 2011)
New Revision: 5420

Modified:
   branches/release-0.93/src/org/griphyn/vdl/karajan/lib/CreateArray.java
   branches/release-0.93/src/org/griphyn/vdl/karajan/lib/SetFieldValue.java
   branches/release-0.93/src/org/griphyn/vdl/karajan/lib/VDLFunction.java
Log:
fixed mapper issues in createarray

Modified: branches/release-0.93/src/org/griphyn/vdl/karajan/lib/CreateArray.java
===================================================================
--- branches/release-0.93/src/org/griphyn/vdl/karajan/lib/CreateArray.java	2011-12-14 22:03:11 UTC (rev 5419)
+++ branches/release-0.93/src/org/griphyn/vdl/karajan/lib/CreateArray.java	2011-12-14 22:04:16 UTC (rev 5420)
@@ -1,16 +1,19 @@
 package org.griphyn.vdl.karajan.lib;
 
-import java.util.Iterator;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 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.globus.cog.karajan.workflow.futures.FutureFault;
 import org.griphyn.vdl.mapping.DSHandle;
 import org.griphyn.vdl.mapping.Path;
 import org.griphyn.vdl.mapping.RootArrayDataNode;
+import org.griphyn.vdl.type.Field;
 import org.griphyn.vdl.type.Type;
 
 public class CreateArray extends VDLFunction {
@@ -23,7 +26,8 @@
 		setArguments(CreateArray.class, new Arg[] { PA_VALUE });
 	}
 
-	public Object function(VariableStack stack) throws ExecutionException {
+	@SuppressWarnings("unchecked")
+    public Object function(VariableStack stack) throws ExecutionException {
 		Object value = PA_VALUE.getValue(stack);
 		try {
 
@@ -32,56 +36,105 @@
 					"An array variable can only be initialized with a list of values");
 			}
 
-			Type type = null;
+			Type type = checkTypes((List<?>) value);
+			
+			DSHandle handle = new RootArrayDataNode(type.arrayType());
+			if (hasMappableFields(type)) {
+			    setMapper(handle);
+			}
 
-			Iterator i = ((List) value).iterator();
-			while (i.hasNext()) {
-				Object o = i.next();
-				if (o instanceof DSHandle) {
-					DSHandle d = (DSHandle)o;
-					Type thisType = d.getType();
-					if(type == null) {
-						// this first element
-						type = thisType;
-					} else {
-						// other elements, when we have a type to expect
-						if(!(type.equals(thisType))) {
-							throw new RuntimeException(
-								"Expecting all array elements to have SwiftScript type "+type+" but found an element with type "+thisType);
-						}
-					}
-				}
-				else {
-					throw new RuntimeException("An array variable can only be initialized by a list of DSHandle values.");
-				}
-
+			if (logger.isInfoEnabled()) {
+			    logger.info("CREATEARRAY START array=" + handle.getIdentifier());
 			}
 
-			DSHandle handle = new RootArrayDataNode(type.arrayType());
-
-			logger.info("CREATEARRAY START array="+handle.getIdentifier());
-
 			int index = 0;
-			i = ((List) value).iterator();
-			while (i.hasNext()) {
+			for (Object o : (List<?>) value) {
 				// TODO check type consistency of elements with
 				// the type of the array
-				DSHandle n = (DSHandle) i.next();
+				DSHandle n = (DSHandle) o;
 				// we know this DSHandle cast will work because we checked
 				// it in the previous scan of the array contents
 				Path p = Path.EMPTY_PATH.addLast(String.valueOf(index), true);
+				
+				DSHandle dst = handle.getField(p);
 
-				handle.getField(p).set(n);
-				logger.info("CREATEARRAY MEMBER array="+handle.getIdentifier()+" index="+index+" member="+n.getIdentifier());
+				SetFieldValue.deepCopy(dst, n, stack, 1);
+				
+				if (logger.isInfoEnabled()) {
+				    logger.info("CREATEARRAY MEMBER array=" + handle.getIdentifier() 
+				        + " index=" + index + " member=" + n.getIdentifier());
+				}
 				index++;
 			}
+			
 			handle.closeShallow();
-			logger.info("CREATEARRAY COMPLETED array="+handle.getIdentifier());
+			
+			if (logger.isInfoEnabled()) {
+			    logger.info("CREATEARRAY COMPLETED array=" + handle.getIdentifier());
+			}
 
 			return handle;
 		}
+		catch (FutureFault e) {
+		    throw e;
+		}
 		catch (Exception e) {
 			throw new ExecutionException(e);
 		}
 	}
+
+    private void setMapper(DSHandle handle) {
+        // slap a concurrent mapper on this
+        Map<String, Object> params = new HashMap<String, Object>();
+        params.put("descriptor", "concurrent_mapper");
+        params.put("dbgname", "arrayexpr");
+        handle.init(params);
+    }
+
+    private boolean hasMappableFields(Type type) {
+        if (type.isPrimitive()) {
+            return false;
+        }
+        else if (!type.isComposite()) {
+            return true;
+        }
+        else if (type.isArray()) {
+            return hasMappableFields(type.itemType());
+        }
+        else {
+            // struct
+            for (Field f : type.getFields()) {
+                if (hasMappableFields(f.getType())) {
+                    return true;
+                }
+            }
+            return false;
+        }
+    }
+
+    private Type checkTypes(List<?> value) {
+        Type type = null;
+        
+        for (Object o : value) {            
+            if (o instanceof DSHandle) {
+                DSHandle d = (DSHandle)o;
+                Type thisType = d.getType();
+                if(type == null) {
+                    // this first element
+                    type = thisType;
+                } else {
+                    // other elements, when we have a type to expect
+                    if(!(type.equals(thisType))) {
+                        throw new RuntimeException(
+                            "Expecting all array elements to have SwiftScript type " + 
+                            type + " but found an element with type "+thisType);
+                    }
+                }
+            }
+            else {
+                throw new RuntimeException("An array variable can only be initialized by a list of DSHandle values.");
+            }
+        }
+        return type;
+    }
 }

Modified: branches/release-0.93/src/org/griphyn/vdl/karajan/lib/SetFieldValue.java
===================================================================
--- branches/release-0.93/src/org/griphyn/vdl/karajan/lib/SetFieldValue.java	2011-12-14 22:03:11 UTC (rev 5419)
+++ branches/release-0.93/src/org/griphyn/vdl/karajan/lib/SetFieldValue.java	2011-12-14 22:04:16 UTC (rev 5420)
@@ -17,7 +17,10 @@
 import org.griphyn.vdl.mapping.AbstractDataNode;
 import org.griphyn.vdl.mapping.DSHandle;
 import org.griphyn.vdl.mapping.InvalidPathException;
+import org.griphyn.vdl.mapping.Mapper;
 import org.griphyn.vdl.mapping.Path;
+import org.griphyn.vdl.type.Field;
+import org.griphyn.vdl.type.Type;
 
 public class SetFieldValue extends VDLFunction {
 	public static final Logger logger = Logger.getLogger(SetFieldValue.class);
@@ -99,83 +102,114 @@
 	
     /** make dest look like source - if its a simple value, copy that
 	    and if its an array then recursively copy */
-	void deepCopy(DSHandle dest, DSHandle source, VariableStack stack, int level) throws ExecutionException {
+	public static void deepCopy(DSHandle dest, DSHandle source, VariableStack stack, int level) throws ExecutionException {
 	    ((AbstractDataNode) source).waitFor();
 		if (source.getType().isPrimitive()) {
 			dest.setValue(source.getValue());
 		}
 		else if (source.getType().isArray()) {
-			PairIterator it;
-			if (stack.isDefined("it" + level)) {
-			    it = (PairIterator) stack.getVar("it" + level);
-			}
-			else {
-			    it = new PairIterator(source.getArrayValue());
-			    stack.setVar("it" + level, it);
-			}
-			while (it.hasNext()) {
-				Pair pair = (Pair) it.next();
-				Object lhs = pair.get(0);
-				DSHandle rhs = (DSHandle) pair.get(1);
-				Path memberPath;
-				if (lhs instanceof Double) {
-				    memberPath = Path.EMPTY_PATH.addLast(String.valueOf(((Double) lhs).intValue()), true);
-				}
-				else {
-				    memberPath = Path.EMPTY_PATH.addLast(String.valueOf(lhs), true);
-				}
-				DSHandle field;
-				try {
-					field = dest.getField(memberPath);
-				}
-				catch (InvalidPathException ipe) {
-					throw new ExecutionException("Could not get destination field",ipe);
-				}
-				deepCopy(field, rhs, stack, level + 1);
-			}
-			stack.currentFrame().deleteVar("it" + level);
-			dest.closeShallow();
-		} 
-		else if (!source.getType().isComposite()) {
-		    Path dpath = dest.getPathFromRoot();
-		    if (dest.getMapper().canBeRemapped(dpath)) {
-		        if (logger.isDebugEnabled()) {
-		            logger.debug("Remapping " + dest + " to " + source);
-		        }
-		        dest.getMapper().remap(dpath, source.getMapper().map(source.getPathFromRoot()));
-		        dest.closeShallow();
-		    }
-		    else {
-		        if (stack.currentFrame().isDefined("fc")) {
-		            FileCopier fc = (FileCopier) stack.currentFrame().getVar("fc");
-		            if (!fc.isClosed()) {
-		                throw new FutureNotYetAvailable(fc);
-		            }
-		            else {
-		                if (fc.getException() != null) {
-		                    throw new ExecutionException("Failed to copy " + source + " to " + dest, fc.getException());
-		                }
-		            }
-		            dest.closeShallow();
-		        }
-		        else {
-		            FileCopier fc = new FileCopier(source.getMapper().map(source.getPathFromRoot()), 
-		                dest.getMapper().map(dpath));
-		            stack.setVar("fc", fc);
-		            try {
-		                fc.start();
-		            }
-		            catch (Exception e) {
-		                throw new ExecutionException("Failed to start file copy", e);
-		            }
-		            throw new FutureNotYetAvailable(fc);
-		        }
-		    }
+		    copyArray(dest, source, stack, level);
 		}
+		else if (source.getType().isComposite()) {
+		    copyStructure(dest, source, stack, level);
+		}
 		else {
-		    // TODO implement this
-            //throw new RuntimeException("Deep non-array structure copying not implemented, when trying to copy "+source);
+		    copyNonComposite(dest, source, stack, level);
 		}
 	}
 
+    private static void copyStructure(DSHandle dest, DSHandle source,
+            VariableStack stack, int level) throws ExecutionException {
+        Type type = dest.getType();
+        for (String fname : type.getFieldNames()) {
+            Path fpath = Path.EMPTY_PATH.addFirst(fname);
+            try {
+                DSHandle dstf = dest.getField(fpath);
+                try {
+                    DSHandle srcf = source.getField(fpath);
+                    deepCopy(dstf, srcf, stack, level + 1);
+                }
+                catch (InvalidPathException e) {
+                    // do nothing. It's an unused field in the source.
+                }
+            }
+            catch (InvalidPathException e) {
+                throw new ExecutionException("Internal type inconsistency detected. " + 
+                    dest + " claims not to have a " + fname + " field");
+            }
+        }
+    }
+
+    private static void copyNonComposite(DSHandle dest, DSHandle source,
+            VariableStack stack, int level) throws ExecutionException {
+        Path dpath = dest.getPathFromRoot();
+        Mapper dmapper = dest.getRoot().getMapper();
+        if (dmapper.canBeRemapped(dpath)) {
+            if (logger.isDebugEnabled()) {
+                logger.debug("Remapping " + dest + " to " + source);
+            }
+            dmapper.remap(dpath, source.getMapper().map(source.getPathFromRoot()));
+            dest.closeShallow();
+        }
+        else {
+            if (stack.currentFrame().isDefined("fc")) {
+                FileCopier fc = (FileCopier) stack.currentFrame().getVar("fc");
+                if (!fc.isClosed()) {
+                    throw new FutureNotYetAvailable(fc);
+                }
+                else {
+                    if (fc.getException() != null) {
+                        throw new ExecutionException("Failed to copy " + source + " to " + dest, fc.getException());
+                    }
+                }
+                dest.closeShallow();
+            }
+            else {
+                FileCopier fc = new FileCopier(source.getMapper().map(source.getPathFromRoot()), 
+                    dest.getMapper().map(dpath));
+                stack.setVar("fc", fc);
+                try {
+                    fc.start();
+                }
+                catch (Exception e) {
+                    throw new ExecutionException("Failed to start file copy", e);
+                }
+                throw new FutureNotYetAvailable(fc);
+            }
+        }
+    }
+
+    private static void copyArray(DSHandle dest, DSHandle source,
+            VariableStack stack, int level) throws ExecutionException {
+        PairIterator it;
+        if (stack.isDefined("it" + level)) {
+            it = (PairIterator) stack.getVar("it" + level);
+        }
+        else {
+            it = new PairIterator(source.getArrayValue());
+            stack.setVar("it" + level, it);
+        }
+        while (it.hasNext()) {
+            Pair pair = (Pair) it.next();
+            Object lhs = pair.get(0);
+            DSHandle rhs = (DSHandle) pair.get(1);
+            Path memberPath;
+            if (lhs instanceof Double) {
+                memberPath = Path.EMPTY_PATH.addLast(String.valueOf(((Double) lhs).intValue()), true);
+            }
+            else {
+                memberPath = Path.EMPTY_PATH.addLast(String.valueOf(lhs), true);
+            }
+            DSHandle field;
+            try {
+                field = dest.getField(memberPath);
+            }
+            catch (InvalidPathException ipe) {
+                throw new ExecutionException("Could not get destination field",ipe);
+            }
+            deepCopy(field, rhs, stack, level + 1);
+        }
+        stack.currentFrame().deleteVar("it" + level);
+        dest.closeShallow();
+    }
 }

Modified: branches/release-0.93/src/org/griphyn/vdl/karajan/lib/VDLFunction.java
===================================================================
--- branches/release-0.93/src/org/griphyn/vdl/karajan/lib/VDLFunction.java	2011-12-14 22:03:11 UTC (rev 5419)
+++ branches/release-0.93/src/org/griphyn/vdl/karajan/lib/VDLFunction.java	2011-12-14 22:04:16 UTC (rev 5420)
@@ -201,6 +201,11 @@
         synchronized (var.getRoot()) {
             mapper = var.getMapper();
         }
+        
+        if (mapper == null) {
+            throw new ExecutionException(var.getType() + " is not a mapped type");
+        }
+        
 		List<String> l = new ArrayList<String>();
 		try {
 			Collection<Path> fp = var.getFringePaths();




More information about the Swift-commit mailing list