[Swift-commit] r4680 - in trunk/src/org/griphyn/vdl: karajan/lib mapping mapping/file

wozniak at ci.uchicago.edu wozniak at ci.uchicago.edu
Thu Jun 23 17:22:13 CDT 2011


Author: wozniak
Date: 2011-06-23 17:22:13 -0500 (Thu, 23 Jun 2011)
New Revision: 4680

Modified:
   trunk/src/org/griphyn/vdl/karajan/lib/New.java
   trunk/src/org/griphyn/vdl/karajan/lib/PartialCloseDataset.java
   trunk/src/org/griphyn/vdl/mapping/AbstractDataNode.java
   trunk/src/org/griphyn/vdl/mapping/ArrayDataNode.java
   trunk/src/org/griphyn/vdl/mapping/DSHandle.java
   trunk/src/org/griphyn/vdl/mapping/MapperFactory.java
   trunk/src/org/griphyn/vdl/mapping/RootArrayDataNode.java
   trunk/src/org/griphyn/vdl/mapping/RootDataNode.java
   trunk/src/org/griphyn/vdl/mapping/file/ArrayFileMapper.java
Log:
Readability and warning fixes


Modified: trunk/src/org/griphyn/vdl/karajan/lib/New.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/New.java	2011-06-23 22:02:18 UTC (rev 4679)
+++ trunk/src/org/griphyn/vdl/karajan/lib/New.java	2011-06-23 22:22:13 UTC (rev 4680)
@@ -41,12 +41,14 @@
 	public Object function(VariableStack stack) throws ExecutionException {
 		String typename = TypeUtil.toString(OA_TYPE.getValue(stack));
 		Object value = OA_VALUE.getValue(stack);
-		Map mapping = (Map) OA_MAPPING.getValue(stack);
+		@SuppressWarnings("unchecked")
+        Map<String,Object> mapping = 
+		    (Map<String,Object>) OA_MAPPING.getValue(stack);
 		String dbgname = TypeUtil.toString(OA_DBGNAME.getValue(stack));
 		String waitfor = (String) OA_WAITFOR.getValue(stack);
 
 		if (mapping == null) {
-			mapping = new HashMap();
+			mapping = new HashMap<String,Object>();
 		}
 
 		if (dbgname != null) {
@@ -55,26 +57,26 @@
 
 		mapping.put("swift#restartid", getThreadPrefix(stack) + ":" + dbgname);
 
-		if(waitfor != null) {
+		if (waitfor != null) {
 			mapping.put("waitfor", waitfor);
 		}
 
 		if (typename == null && value == null) {
 			throw new ExecutionException("You must specify either a type or a value");
 		}
-		if (mapping != null) {
-			String mapper = (String) mapping.get("descriptor");
-			if ("concurrent_mapper".equals(mapper)) {
-				String threadPrefix = getThreadPrefix(stack);
-				ConcurrentMapper.PARAM_THREAD_PREFIX.setValue(mapping, threadPrefix);
-			}
-			mapping.put("#basedir", stack.getExecutionContext().getBasedir());
+	
+		String mapper = (String) mapping.get("descriptor");
+		if ("concurrent_mapper".equals(mapper)) {
+		    String threadPrefix = getThreadPrefix(stack);
+		    ConcurrentMapper.PARAM_THREAD_PREFIX.setValue(mapping, threadPrefix);
 		}
+		mapping.put("#basedir", stack.getExecutionContext().getBasedir());
+		
 		try {
 			Type type;
 			if (typename == null) {
-				throw new ExecutionException("vdl:new requires a type specification for value "
-						+ value);
+				throw new ExecutionException
+				("vdl:new requires a type specification for value: " + value);
 			}
 			else {
 				type = Types.getType(typename);
@@ -91,8 +93,9 @@
 					}
 					else {
 						if (!(value instanceof List)) {
-							throw new ExecutionException(
-									"An array variable can only be initialized with a list of values");
+							throw new ExecutionException
+							("An array variable can only be initialized " +
+							 "with a list of values");
 						}
 						int index = 0;
 						Iterator i = ((List) value).iterator();
@@ -114,14 +117,12 @@
 					closeShallow(stack, handle);
 				}
 
-				if (mapping != null) {
-					handle.init(mapping);
-				}
+				handle.init(mapping);
 			}
 			else if (value instanceof DSHandle) {
 				handle = (DSHandle) value;
 			}
-			else if (type != null) {
+			else {
 				handle = new RootDataNode(type);
 				handle.init(mapping);
 				if (value != null) {
@@ -129,11 +130,7 @@
 					closeShallow(stack, handle);
 				}
 			}
-			else {
-				// TODO when do we hit this case?
-				throw new ExecutionException("vdl:new requires a type specification for value "
-						+ value);
-			}
+			
 			logger.debug("NEW id="+handle.getIdentifier());
 			return handle;
 		}
@@ -141,5 +138,4 @@
 			throw new ExecutionException(e);
 		}
 	}
-
 }

Modified: trunk/src/org/griphyn/vdl/karajan/lib/PartialCloseDataset.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/PartialCloseDataset.java	2011-06-23 22:02:18 UTC (rev 4679)
+++ trunk/src/org/griphyn/vdl/karajan/lib/PartialCloseDataset.java	2011-06-23 22:22:13 UTC (rev 4680)
@@ -15,27 +15,27 @@
 public class PartialCloseDataset extends VDLFunction {
 	public static final Logger logger = Logger.getLogger(CloseDataset.class);
 
-	public static final Arg OA_STATEMENTID = new Arg.Optional("closeID", null);
+	public static final Arg OA_CLOSE_ID = new Arg.Optional("closeID", null);
 
 	static {
-		setArguments(PartialCloseDataset.class, new Arg[] { PA_VAR, OA_STATEMENTID });
+		setArguments(PartialCloseDataset.class, new Arg[] { PA_VAR, OA_CLOSE_ID });
 	}
 
 
 	/** Map from DSHandles (as keys) to lists of what we have seen
 	    already. TODO this may end up growing too much when a program
-	    has lots of objects. consider alternative ways of doing this. */
-	static Map pendingDatasets = new HashMap();
+	    has lots of objects. Consider alternative ways of doing this. */
+	static Map<DSHandle,List<String>> pendingDatasets = 
+	    new HashMap<DSHandle,List<String>>();
 
-
 	public Object function(VariableStack stack) throws ExecutionException {
 		boolean hasUnseenToken = false;
 		DSHandle var = (DSHandle) PA_VAR.getValue(stack);
-		String statementID = (String) OA_STATEMENTID.getValue(stack);
+		String closeID = (String) OA_CLOSE_ID.getValue(stack);
 		if (logger.isDebugEnabled()) {
 			logger.debug("Partially closing " + var +
                                      " hash: " + var.hashCode() +
-                                     " for statement " + statementID);
+                                     " for statement " + closeID);
 		}
 
 		if(var.isClosed()) {
@@ -45,17 +45,17 @@
 
 		synchronized(pendingDatasets) {
 
-			List c = (List) pendingDatasets.get(var);
-			if(c==null) {
-				c=new ArrayList();
-				pendingDatasets.put(var,c);
+			List<String> c = pendingDatasets.get(var);
+			if (c == null) {
+				c = new ArrayList<String>();
+				pendingDatasets.put(var, c);
 			}
 
-			c.add(statementID);
-			logger.debug("Adding token "+statementID+" with hash "+statementID.hashCode());
+			c.add(closeID);
+			logger.debug("Adding token "+closeID+" with hash "+closeID.hashCode());
 
-			String needToWaitFor = (String) var.getParam("waitfor");
-			logger.debug("need to wait for "+needToWaitFor);
+			String needToWaitFor = var.getParam("waitfor");
+			logger.debug("need to wait for " + needToWaitFor);
 			StringTokenizer stok = new StringTokenizer(needToWaitFor, " ");
 			while(stok.hasMoreTokens()) {
 				String s = stok.nextToken();
@@ -65,16 +65,17 @@
 					// then we have a required element that we have not
 					// seen yet, so...
 					hasUnseenToken = true;
-					logger.debug("Container does not contain token "+s);
+					logger.debug("Container does not contain token " + s);
 				} else {
-					logger.debug("Container does contain token "+s);
+					logger.debug("Container does contain token " + s);
 				}
 			}
 		}
 		logger.debug("hasUnseenToken = "+hasUnseenToken);
 		if(!hasUnseenToken) {
 			if(logger.isDebugEnabled()) {
-				logger.debug("All partial closes for " + var + " have happened. Closing fully.");
+				logger.debug("All partial closes for " + var + 
+				             " have happened. Closing fully.");
 			}
 			var.closeDeep();
 			pendingDatasets.remove(var);

Modified: trunk/src/org/griphyn/vdl/mapping/AbstractDataNode.java
===================================================================
--- trunk/src/org/griphyn/vdl/mapping/AbstractDataNode.java	2011-06-23 22:02:18 UTC (rev 4679)
+++ trunk/src/org/griphyn/vdl/mapping/AbstractDataNode.java	2011-06-23 22:22:13 UTC (rev 4680)
@@ -58,8 +58,7 @@
         handles = new HashMap<String,DSHandle>();
     }
 
-    public void init(Map params) {
-
+    public void init(Map<String,Object> params) {
     }
 
     public Type getType() {
@@ -183,11 +182,11 @@
             if (path.size() > 1) {
                 return handle.getField(path.butFirst());
             }
-            else {
-                return handle;
-            }
+            return handle;
         }
         catch (NoSuchFieldException e) {
+            logger.warn("could not find variable: " + field.getName() + 
+                               " " + path);
             throw new InvalidPathException(path, this);
         }
     }
@@ -240,9 +239,7 @@
              */
             throw new RuntimeException("Can't set root data node!");
         }
-        else {
-            ((AbstractDataNode) getParent()).setField(field.getName(), handle);
-        }
+        ((AbstractDataNode) getParent()).setField(field.getName(), handle);
     }
 
     protected void setField(String name, DSHandle handle) {
@@ -258,16 +255,14 @@
             if (closed) {
                 throw new NoSuchFieldException(name);
             }
-            else {
-                handle = createDSHandle(name);
-            }
+            handle = createDSHandle(name);
         }
         return handle;
     }
 
     protected DSHandle getHandle(String name) {
         synchronized (handles) {
-            return (DSHandle) handles.get(name);
+            return handles.get(name);
         }
     }
 
@@ -326,9 +321,7 @@
         if (field.getType().isArray()) {
             return handles;
         }
-        else {
-            return value;
-        }
+        return value;
     }
 
     public Map<String, DSHandle> getArrayValue() {

Modified: trunk/src/org/griphyn/vdl/mapping/ArrayDataNode.java
===================================================================
--- trunk/src/org/griphyn/vdl/mapping/ArrayDataNode.java	2011-06-23 22:02:18 UTC (rev 4679)
+++ trunk/src/org/griphyn/vdl/mapping/ArrayDataNode.java	2011-06-23 22:22:13 UTC (rev 4680)
@@ -19,7 +19,7 @@
 		if (!isClosed()) {
 			throw new HandleOpenException(this);
 		}
-		Map handles = getHandles();
+		Map<String,DSHandle> handles = getHandles();
 		synchronized (handles) {
 			Iterator i = handles.entrySet().iterator();
 			while (i.hasNext()) {

Modified: trunk/src/org/griphyn/vdl/mapping/DSHandle.java
===================================================================
--- trunk/src/org/griphyn/vdl/mapping/DSHandle.java	2011-06-23 22:02:18 UTC (rev 4679)
+++ trunk/src/org/griphyn/vdl/mapping/DSHandle.java	2011-06-23 22:22:13 UTC (rev 4680)
@@ -30,7 +30,7 @@
      */
     public Type getType();
 
-    public void init(Map params);
+    public void init(Map<String,Object> params);
 
     public DSHandle getRoot();
 

Modified: trunk/src/org/griphyn/vdl/mapping/MapperFactory.java
===================================================================
--- trunk/src/org/griphyn/vdl/mapping/MapperFactory.java	2011-06-23 22:02:18 UTC (rev 4679)
+++ trunk/src/org/griphyn/vdl/mapping/MapperFactory.java	2011-06-23 22:22:13 UTC (rev 4680)
@@ -20,7 +20,8 @@
 import org.griphyn.vdl.mapping.file.StructuredRegularExpressionMapper;
 
 public class MapperFactory {
-	private static Map mappers = new HashMap();
+	private static Map<String,Class<Mapper>> mappers = 
+	    new HashMap<String,Class<Mapper>>();
 
 	static {
 
@@ -42,13 +43,15 @@
 		registerMapper("ext", ExternalMapper.class);
 	}
 
-	public synchronized static Mapper getMapper(String type, Map params) throws InvalidMapperException {
-		Class cls = (Class) mappers.get(type);
+	public synchronized static Mapper getMapper(String type, 
+	                                            Map<String,Object> params) 
+	throws InvalidMapperException {
+		Class<Mapper> cls = mappers.get(type);
 		if (cls == null) {
 			throw new InvalidMapperException("no such mapper: "+type);
 		}
 		try {
-			Mapper mapper = (Mapper) cls.newInstance();
+			Mapper mapper = cls.newInstance();
 			mapper.setParams(params);
 			return mapper;
 		}
@@ -57,12 +60,14 @@
 		}
 	}
 
-	public static void registerMapper(String type, String cls) throws ClassNotFoundException {
-		registerMapper(type, MapperFactory.class.getClassLoader().loadClass(cls));
+    public static void registerMapper(String type, String cls) 
+	throws ClassNotFoundException {
+	    registerMapper(type, MapperFactory.class.getClassLoader().loadClass(cls));
 	}
 
-	public synchronized static void registerMapper(String type, Class cls) {
-		mappers.put(type, cls);
+	@SuppressWarnings("unchecked")
+    public synchronized static void registerMapper(String type, 
+	                                               Class<?> cls) {
+		mappers.put(type, (Class<Mapper>) cls);
 	}
-
 }

Modified: trunk/src/org/griphyn/vdl/mapping/RootArrayDataNode.java
===================================================================
--- trunk/src/org/griphyn/vdl/mapping/RootArrayDataNode.java	2011-06-23 22:02:18 UTC (rev 4679)
+++ trunk/src/org/griphyn/vdl/mapping/RootArrayDataNode.java	2011-06-23 22:22:13 UTC (rev 4680)
@@ -3,15 +3,18 @@
 import java.util.Iterator;
 import java.util.Map;
 
+import org.apache.log4j.Logger;
 import org.griphyn.vdl.karajan.VDL2FutureException;
 import org.griphyn.vdl.type.Field;
 import org.griphyn.vdl.type.Type;
 
 public class RootArrayDataNode extends ArrayDataNode implements DSHandleListener {
 
+    Logger logger = Logger.getLogger(RootArrayDataNode.class);
+    
 	private boolean initialized = false;
 	private Mapper mapper;
-	private Map params;
+	private Map<String, Object> params;
 	private DSHandle waitingMapperParam;
 
 	/**
@@ -22,7 +25,7 @@
 		getField().setType(type);
 	}
 
-	public void init(Map params) {
+	public void init(Map<String, Object> params) {
 		this.params = params;
 		if (this.params == null) {
 			initialized();
@@ -33,6 +36,7 @@
 	}
 
 	private synchronized void innerInit() {
+	    logger.debug("innerInit: " + this);
 		Iterator i = params.entrySet().iterator();
 		while(i.hasNext()) {
 			Map.Entry entry = (Map.Entry) i.next();
@@ -98,10 +102,8 @@
 		if (initialized) {
 			return mapper;
 		}
-		else {
-		    assert(waitingMapperParam != null);
-		    throw new VDL2FutureException(waitingMapperParam);
-		}
+        assert(waitingMapperParam != null);
+        throw new VDL2FutureException(waitingMapperParam);
 	}
 
 	public boolean isArray() {

Modified: trunk/src/org/griphyn/vdl/mapping/RootDataNode.java
===================================================================
--- trunk/src/org/griphyn/vdl/mapping/RootDataNode.java	2011-06-23 22:02:18 UTC (rev 4679)
+++ trunk/src/org/griphyn/vdl/mapping/RootDataNode.java	2011-06-23 22:22:13 UTC (rev 4680)
@@ -6,21 +6,25 @@
 import java.util.Iterator;
 import java.util.Map;
 
+import org.apache.log4j.Logger;
 import org.griphyn.vdl.karajan.VDL2FutureException;
 import org.griphyn.vdl.type.Field;
 import org.griphyn.vdl.type.Type;
 
 public class RootDataNode extends AbstractDataNode implements DSHandleListener {
 
+    static Logger logger = Logger.getLogger(RootDataNode.class); 
+    
 	private boolean initialized=false;
 	private Mapper mapper;
-	private Map params;
+	private Map<String, Object> params;
 	private DSHandle waitingMapperParam;
 
 	public static DSHandle newNode(Type type, Object value) {
 		DSHandle handle = new RootDataNode(type);
 		handle.setValue(value);
 		handle.closeShallow();
+		logger.debug("newNode");
 		return handle;
 	}
 
@@ -29,7 +33,7 @@
 		getField().setType(type);
 	}
 
-	public void init(Map params) {
+	public void init(Map<String,Object> params) {
 		this.params = params;
 		if(this.params == null) { 
 			initialized();
@@ -41,12 +45,11 @@
 	/** must have this.params set to the appropriate parameters before
 	    being called. */
 	private synchronized void innerInit() {
-		Iterator i = params.entrySet().iterator();
-		while(i.hasNext()) {
-			Map.Entry entry = (Map.Entry) i.next();
-			Object v = entry.getValue();
+	    logger.debug("innerInit: " + this);
+	    for (Object v : params.values()) {
 			if(v instanceof DSHandle && !((DSHandle) v).isClosed()) {
-				waitingMapperParam = (DSHandle) v;
+			    logger.debug("addListener: " + this + " " + v);
+			    waitingMapperParam = (DSHandle) v;
                 waitingMapperParam.addListener(this);
 				return;
 			}
@@ -67,7 +70,8 @@
 			checkInputs();
 		}
 		catch (InvalidMapperException e) {
-			throw new RuntimeException("InvalidMapperException caught in mapper initialization", e);
+			throw new RuntimeException
+			("InvalidMapperException caught in mapper initialization", e);
 		}
 		if (isClosed()) {
 		    notifyListeners();
@@ -123,19 +127,21 @@
 		    }
 			// Static mappers are (array) mappers which know the size of
 			// an array statically. A good example is the fixed array mapper
-			Iterator i = mapper.existing().iterator();
-			while (i.hasNext()) {
-				Path p = (Path) i.next();
+		    logger.debug("mapper: " + mapper);
+			for (Path p : mapper.existing()) {
 				try {
-					// Try to get the path in order to check that the path is valid - we'll get an exception if not
+					// Try to get the path in order to check that the 
+				    // path is valid - we'll get an exception if not
 					root.getField(p);
 					if (logger.isInfoEnabled()) {
 						logger.info("Found mapped data " + root + "." + p);
 					}
 				}
 				catch (InvalidPathException e) {
-					throw new IllegalStateException("mapper.existing() returned a path " + p
-							+ " that it cannot subsequently map");
+					throw new IllegalStateException
+					("mapper.existing() returned a path " + 
+					" that it cannot subsequently map: " + 
+					" root: " + root + " path: " + p);
 				}
 			}
 			if (root.isArray()) {
@@ -207,16 +213,12 @@
 		return null;
 	}
 
-
-
 	public Mapper getMapper() {
 		if (initialized) {
 			return mapper;
-		} 
-		else {
-			assert (waitingMapperParam != null);
-            throw new VDL2FutureException(waitingMapperParam);
 		}
+        assert (waitingMapperParam != null);
+        throw new VDL2FutureException(waitingMapperParam);
 	}
 
 	public boolean isArray() {

Modified: trunk/src/org/griphyn/vdl/mapping/file/ArrayFileMapper.java
===================================================================
--- trunk/src/org/griphyn/vdl/mapping/file/ArrayFileMapper.java	2011-06-23 22:02:18 UTC (rev 4679)
+++ trunk/src/org/griphyn/vdl/mapping/file/ArrayFileMapper.java	2011-06-23 22:22:13 UTC (rev 4680)
@@ -7,6 +7,7 @@
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.log4j.Logger;
 import org.griphyn.vdl.mapping.AbsFile;
 import org.griphyn.vdl.mapping.AbstractMapper;
 import org.griphyn.vdl.mapping.DSHandle;
@@ -16,6 +17,8 @@
 import org.griphyn.vdl.mapping.PhysicalFormat;
 
 public class ArrayFileMapper extends AbstractMapper {
+    static Logger logger = Logger.getLogger(ArrayFileMapper.class);
+    
 	public static final MappingParam PARAM_FILES = new MappingParam("files");
 
 	public Collection<Path> existing() {
@@ -24,9 +27,9 @@
 		if (dn == null) {
 		    throw new RuntimeException("Missing 'files' mapper attribute");
 		}
-		Map m = dn.getArrayValue();
-		Set s = m.keySet();
-		Iterator i = s.iterator();
+		Map<String,DSHandle> m = dn.getArrayValue();
+		Set<String> s = m.keySet();
+		Iterator<String> i = s.iterator();
 		while(i.hasNext()) {
 			String nextKey = i.next().toString();
 			l.add(Path.EMPTY_PATH.addLast(nextKey,true));
@@ -41,25 +44,28 @@
 		if (!path.isArrayIndex(0)) {
 			throw new IllegalArgumentException("First element of path "+path.toString()+" must be an array index");
 		}
-		else {
-			// we could typecheck more elegantly here to make sure that
-			// we really do have an array of strings as parameter.
-			DSHandle dn = (DSHandle) PARAM_FILES.getRawValue(this);
-			assert(dn.isClosed());
-
-			DSHandle srcNode = null;
-			try {
-				srcNode = dn.getField(path);
-			} catch(InvalidPathException e) {
-				logger.error("Invalid path exception "+e+" for path "+path,e);
-				return null;
-			}
-			String returnValue = srcNode.getValue().toString();
-			return new AbsFile(returnValue);
-		}
+        // we could typecheck more elegantly here to make sure that
+        // we really do have an array of strings as parameter.
+        DSHandle dn = (DSHandle) PARAM_FILES.getRawValue(this);
+        assert(dn.isClosed());
+        logger.debug("dn: " + dn);
+        
+        DSHandle srcNode = null;
+        try {
+        	srcNode = dn.getField(path);
+        } catch(InvalidPathException e) {
+        	logger.error("Invalid path exception "+e+" for path "+path,e);
+        	return null;
+        }
+        String returnValue = srcNode.getValue().toString();
+        return new AbsFile(returnValue);
 	}
 
 	public boolean isStatic() {
 		return true;
 	}
+	
+	public String toString() { 
+	    return "ArrayFileMapper";
+	}
 }




More information about the Swift-commit mailing list