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

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Sun Mar 22 11:39:13 CDT 2009


Author: hategan
Date: 2009-03-22 11:39:12 -0500 (Sun, 22 Mar 2009)
New Revision: 2732

Modified:
   trunk/src/org/griphyn/vdl/karajan/lib/Range.java
   trunk/src/org/griphyn/vdl/karajan/lib/SetFieldValue.java
Log:
array index handling is crappy

Modified: trunk/src/org/griphyn/vdl/karajan/lib/Range.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/Range.java	2009-03-22 14:26:44 UTC (rev 2731)
+++ trunk/src/org/griphyn/vdl/karajan/lib/Range.java	2009-03-22 16:39:12 UTC (rev 2732)
@@ -3,13 +3,24 @@
  */
 package org.griphyn.vdl.karajan.lib;
 
+import java.util.AbstractMap;
+import java.util.AbstractSet;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
 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.mapping.AbstractDataNode;
 import org.griphyn.vdl.mapping.DSHandle;
+import org.griphyn.vdl.mapping.HandleOpenException;
+import org.griphyn.vdl.mapping.InvalidPathException;
 import org.griphyn.vdl.mapping.Path;
 import org.griphyn.vdl.mapping.RootArrayDataNode;
+import org.griphyn.vdl.mapping.RootDataNode;
 import org.griphyn.vdl.type.Type;
 import org.griphyn.vdl.type.Types;
 
@@ -22,27 +33,113 @@
 		setArguments(Range.class, new Arg[] { PA_FROM, PA_TO, OA_STEP });
 	}
 
-	public Object function(VariableStack stack) throws ExecutionException {
+	public Object function(final VariableStack stack) throws ExecutionException {
 		// TODO: deal with expression
-		Type type = PA_FROM.getType(stack);
-		double start = PA_FROM.getDoubleValue(stack);
-		double stop = PA_TO.getDoubleValue(stack);
-		double incr = OA_STEP.getDoubleValue(stack);
+		final Type type = PA_FROM.getType(stack);
+		final double start = PA_FROM.getDoubleValue(stack);
+		final double stop = PA_TO.getDoubleValue(stack);
+		final double incr = OA_STEP.getDoubleValue(stack);
 
 		// only deal with int and float
 		try {
-			AbstractDataNode handle;
+			final AbstractDataNode handle;
 
-			handle = new RootArrayDataNode(type.arrayType());
-			int index = 0;
-			for (double v = start; v <= stop; v += incr, index++) {
-				Path path = Path.EMPTY_PATH.addLast(String.valueOf(index), true);
-				DSHandle field = handle.getField(path);
-				field.setValue(new Double(v));
-				closeShallow(stack, field);
-			}
+			handle = new RootArrayDataNode(type.arrayType()) {
+				final DSHandle h = this;
+				
+				public Collection getFields(Path path)
+						throws InvalidPathException, HandleOpenException {
+					if (path.size() > 1) {
+						throw new InvalidPathException(path, this);
+					}
+					else if (path.equals(Path.EMPTY_PATH)) {
+						return Collections.singletonList(this);
+					}
+					else {
+						int index = Integer.parseInt(path.getFirst());
+						DSHandle value = new RootDataNode(type);
+						value.init(null);
+						value.setValue(new Double(start + incr * index));
+						value.closeShallow();
+						return Collections.singletonList(value);
+					}
+				}
 
-			closeShallow(stack, handle);
+				public Map getArrayValue() {
+					return new AbstractMap() {
+						public Set entrySet() {
+							return new AbstractSet() {
+								public Iterator iterator() {
+									return new Iterator() {
+										private double crt = start;
+										private int index = 0;
+										
+										public boolean hasNext() {
+											return crt <= stop;
+										}
+
+										public Object next() {
+											try {
+												Map.Entry e = new Map.Entry() {
+													private DSHandle value;
+													private int key;
+													
+													{
+														value = new RootDataNode(type);
+														value.init(null);
+														value.setValue(new Double(crt));
+														value.closeShallow();
+														key = index;
+													}
+
+													public Object getKey() {
+														return new Double(key);
+													}
+
+													public Object getValue() {
+														return value;
+													}
+
+													public Object setValue(Object value) {
+														throw new UnsupportedOperationException();
+													}
+												};
+												index++;
+												crt += incr;
+												if (crt > stop) {
+													VDLFunction.closeShallow(stack, h);
+												}
+												return e;
+											}
+											catch (Exception e) {
+												throw new RuntimeException(e);
+											}
+										}
+
+										public void remove() {
+											throw new UnsupportedOperationException();
+										}
+									};
+								}
+
+								public int size() {
+									return (int) ((stop - start) / incr);
+								}								
+							};
+						}
+					};
+				}
+
+				public boolean isClosed() {
+					//the need for this lie arises from:
+					//1. adding fields to a truly closed array throws an exception
+					//2. this is a lazy array not a future array. Consequently, we 
+					//   want the consumer(s) of this array to think that all values
+					//   in the array are available
+					return true;
+				}
+				
+			};
 			return handle;
 		}
 		catch (Exception e) {

Modified: trunk/src/org/griphyn/vdl/karajan/lib/SetFieldValue.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/SetFieldValue.java	2009-03-22 14:26:44 UTC (rev 2731)
+++ trunk/src/org/griphyn/vdl/karajan/lib/SetFieldValue.java	2009-03-22 16:39:12 UTC (rev 2732)
@@ -60,13 +60,20 @@
 	void deepCopy(DSHandle dest, DSHandle source, VariableStack stack) throws ExecutionException {
 		if(source.getType().isPrimitive()) {
 			dest.setValue(source.getValue());
-		} else if(source.getType().isArray()) {
+		}
+		else if(source.getType().isArray()) {
 			PairIterator it = new PairIterator(source.getArrayValue());
 			while(it.hasNext()) {
 				Pair pair = (Pair) it.next();
 				Object lhs = pair.get(0);
 				DSHandle rhs = (DSHandle) pair.get(1);
-				Path memberPath = Path.EMPTY_PATH.addLast(String.valueOf(lhs),true);
+				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);




More information about the Swift-commit mailing list