[Swift-commit] r5846 - trunk/src/org/griphyn/vdl/karajan/lib

hategan at ci.uchicago.edu hategan at ci.uchicago.edu
Tue Jul 17 13:27:39 CDT 2012


Author: hategan
Date: 2012-07-17 13:27:38 -0500 (Tue, 17 Jul 2012)
New Revision: 5846

Modified:
   trunk/src/org/griphyn/vdl/karajan/lib/SetFieldValue.java
Log:
SetFieldValue from 0.93 branch; for some reason this didn't get merged properly

Modified: trunk/src/org/griphyn/vdl/karajan/lib/SetFieldValue.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/SetFieldValue.java	2012-07-17 05:22:15 UTC (rev 5845)
+++ trunk/src/org/griphyn/vdl/karajan/lib/SetFieldValue.java	2012-07-17 18:27:38 UTC (rev 5846)
@@ -36,7 +36,9 @@
 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 {
@@ -122,7 +124,7 @@
 	            if (value.getType().isArray()) {
 	                if (logger.isInfoEnabled()) {
 	                    logger.info("Set: " + name + "=" + 
-                                unpackHandles(value.getArrayValue()));
+                                unpackHandles(value, value.getArrayValue()));
 	                }
 	            }
 	            else {
@@ -134,18 +136,20 @@
 	    }
     }
 
-	String unpackHandles(Map<Comparable<?>, DSHandle> handles) { 
+	String unpackHandles(DSHandle handle, Map<Comparable<?>, DSHandle> handles) { 
 	    StringBuilder sb = new StringBuilder();
 	    sb.append("{");
-	    Iterator<Map.Entry<Comparable<?>, DSHandle>> it = 
-	        handles.entrySet().iterator();
-	    while (it.hasNext()) { 
-	        Map.Entry<Comparable<?>, DSHandle> entry = it.next();
-	        sb.append(entry.getKey());
-	        sb.append('=');
-	        sb.append(entry.getValue().getValue());
-	        if (it.hasNext())
-	            sb.append(", ");
+	    synchronized(handle) {
+    	    Iterator<Map.Entry<String,DSHandle>> it = 
+    	        handles.entrySet().iterator();
+    	    while (it.hasNext()) { 
+    	        Map.Entry<String,DSHandle> entry = it.next();
+    	        sb.append(entry.getKey());
+    	        sb.append('=');
+    	        sb.append(entry.getValue().getValue());
+    	        if (it.hasNext())
+    	            sb.append(", ");
+    	    }
 	    }
 	    sb.append("}");
 	    return sb.toString();
@@ -154,98 +158,19 @@
     /** make dest look like source - if its a simple value, copy that
 	    and if its an array then recursively copy */
 	@SuppressWarnings("unchecked")
-    void deepCopy(DSHandle dest, DSHandle source, VariableStack stack, int level) throws ExecutionException {
-	    // can move this to the leaf assignment to pipeline things
+	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();
-				Comparable<?> lhs = (Comparable<?>) pair.get(0);
-				DSHandle rhs = (DSHandle) pair.get(1);
-				Path memberPath = Path.EMPTY_PATH.addLast(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(), 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 {
-		    Type t = source.getType();
-		    Iterator<String> it;
-            if (stack.isDefined("it" + level)) {
-                it = (Iterator<String>) stack.getVar("it" + level);
-            }
-            else {
-                it = t.getFieldNames().iterator();
-                stack.setVar("it" + level, it);
-            }
-		    while (it.hasNext()) {
-		        Path memberPath = Path.EMPTY_PATH.addLast(it.next(), false);
-                try {
-                    deepCopy(dest.getField(memberPath), source.getField(memberPath), stack, level + 1);
-                }
-                catch (InvalidPathException ipe) {
-                    throw new ExecutionException("Could not get destination field", ipe);
-                }
-		    }
-		    
-		    stack.currentFrame().deleteVar("it" + level);
-            dest.closeShallow();
+		    copyNonComposite(dest, source, stack, level);
 		}
 	}
 
@@ -257,4 +182,101 @@
         else {
             return dest + " = " + src;
         }
-    }}
+    }
+
+    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();
+    }
+}
+




More information about the Swift-commit mailing list