[MOBY-guts] biomoby commit

senger@ebi.ac.uk senger at pub.open-bio.org
Mon Oct 18 14:35:06 UTC 2004


senger
Mon Oct 18 10:35:06 EDT 2004
Update of /home/repository/moby/moby-live/Java/src/Clients
In directory pub.open-bio.org:/tmp/cvs-serv2405/src/Clients

Modified Files:
	MobyGraphs.java 
Log Message:
searching data paths and more, see docs/ChangeLog

moby-live/Java/src/Clients MobyGraphs.java,1.4,1.5
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/Clients/MobyGraphs.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- /home/repository/moby/moby-live/Java/src/Clients/MobyGraphs.java	2004/04/01 16:41:03	1.4
+++ /home/repository/moby/moby-live/Java/src/Clients/MobyGraphs.java	2004/10/18 14:35:05	1.5
@@ -1,6 +1,5 @@
 // MobyGraphs.java
 //    A command-line client creating graphs of the Moby objects.
-//    !!! Work in progress... !!!
 //
 //    senger at ebi.ac.uk
 //    October 2003
@@ -17,6 +16,10 @@
 import java.io.*;
 
 /**
+ * This is a command-line creating graphs of the Moby objects, and
+ * generating workflow definitions for Taverna. Start with -help for
+ * details. <p>
+ *
  * @author <A HREF="mailto:senger at ebi.ac.uk">Martin Senger</A>
  * @version $Id$
  */
@@ -57,8 +60,10 @@
 	    verbose = !cmd.hasOption ("-q");
 
 	    // where is a Moby registry
-	    Central worker = new CentralImpl (cmd.getParam ("-e"),
-					      cmd.getParam ("-uri"));
+	    CentralAll worker =
+		new CentralDigestCachedImpl (cmd.getParam ("-e"),
+					     cmd.getParam ("-uri"),
+					     cmd.getParam ("-cachedir"));
 	    String param;
 
 	    if (cmd.hasOption ("-debug")) {
@@ -68,26 +73,18 @@
 	    // collect properties customizing graphs
 	    Properties props = new Properties();
 	    add (props, cmd, Graphviz.PROP_RANKDIR);
-
+	    addBool (props, cmd, Taverna.PROP_RAWINPUT);
+	    addBool (props, cmd, Taverna.PROP_RAWOUTPUT);
+	    if (cmd.hasOption ("-raw")) {
+		props.put (Taverna.PROP_RAWINPUT, "true");
+		props.put (Taverna.PROP_RAWOUTPUT, "true");
+	    }
 
 	    //
 	    // read all data types and their relationships
 	    //
-	    MobyDataType[] dataTypes;
-	    Vector v = new Vector();
-	    if (cmd.hasOption ("-d") || cmd.hasOption ("-s")) {
-		decorationLn ("Asking for all data type names...");
-		Map types = worker.getDataTypeNames();
-		for (Iterator it = types.entrySet().iterator(); it.hasNext(); ) {
-		    Map.Entry entry = (Map.Entry)it.next();
-		    String name = (String)entry.getKey();
-		    decorationLn ("Processing " + name + "...");
-		    v.addElement (worker.getDataType (name));
-		}
-		decorationLn ("");
-	    }
-	    dataTypes = new MobyDataType [v.size()];
-	    v.copyInto (dataTypes);
+	    decorationLn ("Retrieving data types...");
+	    MobyDataType[] dataTypes = worker.getDataTypes();
 
 	    //
 	    // create a graph with data types
@@ -109,23 +106,8 @@
 	    // create a graph with service types
 	    //
 	    if (cmd.hasOption ("-t")) {
-
-		// get all services types
-		v.clear();
-		decorationLn ("Asking for all service types...");
-		Map types = worker.getServiceTypes();
-		for (Iterator it = types.entrySet().iterator(); it.hasNext(); ) {
-		    Map.Entry entry = (Map.Entry)it.next();
-		    String typeName = (String)entry.getKey();
-		    decorationLn ("Processing service type " + typeName + "...");
-		    MobyServiceType serviceType = new MobyServiceType (typeName);
-		    serviceType.setDescription ((String)entry.getKey());
-		    serviceType.setParentNames (worker.getServiceTypeRelationships (typeName, false));
-		    v.addElement (serviceType);
-		}
-		decorationLn ("");
-		MobyServiceType[] serviceTypes = new MobyServiceType [v.size()];
-		v.copyInto (serviceTypes);
+		decorationLn ("Retrieving service types...");
+		MobyServiceType[] serviceTypes = worker.getFullServiceTypes();
 		decorationLn ("Creating a graph of the service types...");
 		String graph = Graphviz.createServiceTypesGraph (serviceTypes, props);
 		param = cmd.getParam ("-ft");
@@ -141,25 +123,8 @@
 	    // create a graph with services
 	    //
 	    if (cmd.hasOption ("-s")) {
-
-		// read all services
-		MobyService[] services;
-		v.clear();
-		decorationLn ("Asking for all service names...");
-		Map names = worker.getServiceNames();
-		for (Iterator it = names.entrySet().iterator(); it.hasNext(); ) {
-		    Map.Entry entry = (Map.Entry)it.next();
-		    String name = (String)entry.getKey();
-		    decorationLn ("Processing service " + name + "...");
-		    MobyService[] servs = worker.findService (new MobyService (name));
-		    for (int i = 0; i < servs.length; i++)
-			v.addElement (servs[i]);
-		}
-		decorationLn ("");
-		services = new MobyService [v.size()];
-		v.copyInto (services);
-
-		// create a set of graph edges
+		decorationLn ("Retrieving services...");
+		MobyService[] services = worker.getServices();
 		decorationLn ("Creating a graph of the services...");
 		ServicesEdge[] edges = ServiceConnections.build (dataTypes, services);
 
@@ -193,29 +158,141 @@
 		    }
 		}
 
+		ServicesEdge[] allPaths = null;
+		ServicesEdge[][] separatePaths = null;
+		String[] pathEnds = null;
+		if (cmd.hasParam ("-datapath")) {
+		    pathEnds = cmd.getParam ("-datapath", 2);
+		    if (pathEnds[0] == null || pathEnds[1] == null) {
+			System.err.println ("Missing value for parameter '-datapath'. It should be followed by two data type names.");
+			System.exit (1);
+		    }
+		    MobyPrimaryDataSimple sourceData = createSimpleData (pathEnds[0]);
+		    MobyPrimaryDataSimple targetData = createSimpleData (pathEnds[1]);
+
+		    DataServiceEdge[] startingEdges = ServiceConnections.findStartingEdges (sourceData, dataTypes, services);
+		    DataServiceEdge[] endingEdges = ServiceConnections.findEndingEdges (targetData, dataTypes, services);
+
+		    // this creates *all* pathes, but some of them have cycles and inside branches
+		    separatePaths = FilterServices.dataPaths (startingEdges, edges, endingEdges);
+		    if (separatePaths.length == 0) {
+			System.err.println ("No connection found between '" +
+					    pathEnds[0] + "' and '" + pathEnds[1] + "'");
+			System.exit(1);
+		    }
+		    allPaths = FilterServices.joinPaths (separatePaths);
+
+		    // this separate paths to straight paths (no cycles, no branches)
+		    separatePaths = FilterServices.straightDataPaths (startingEdges, allPaths, endingEdges);
+		}
+
 		// create a graph (in whatever format)
-		String graph;
-		if (cmd.hasOption ("-rdf"))
-		    graph = RDF.createServicesGraph (edges, props);
-		else
-		    graph = Graphviz.createServicesGraph (edges, props);
+		if (cmd.hasParam ("-datapath")) {
+		    boolean generateScufl = ( cmd.hasOption ("-scufl") || cmd.hasOption ("-onlyscufl") );
+		    boolean generateGraph = (! cmd.hasOption ("-onlyscufl") );
+		    int pageSize = UUtils.toInt (cmd.getParam ("-join"));
+ 		    String[] graphs = null;
+ 		    String[] pathNames = null;
+		    String[] scufls = null;
+		    if ( (cmd.hasOption ("-separate") || cmd.hasParam ("-join") || generateScufl) ) {
+			if (separatePaths.length > 0) {
+			    pathNames = new String [separatePaths.length];
+			    for (int i = 0; i < separatePaths.length; i++)
+				pathNames[i] = Printf.format ("Path_%.2d", "" + (i+1));
+			    if (generateGraph) {
+				if (pageSize > 1) {
+				    int pageBeginPos = 0;
+				    int graphIndex = 0;
+				    int numberOfGraphs = (separatePaths.length - 1) / pageSize + 1;
+				    graphs = new String [numberOfGraphs];
+				    for (int i = 0; i < separatePaths.length; i++) {
+					// have we reached an end of a page?
+					if ( (i+1) % pageSize == 0 || (i+1) == separatePaths.length ) {
+					    // yes => create a graph containing only paths from 'pageBeginPos' to 'i'
+					    graphs [graphIndex++] =
+						Graphviz.createServicesGraph (separatePaths, pageBeginPos, i, pathNames, props);
+					    pageBeginPos = i+1;
+					}
+				    }
+				} else {
+				    graphs = new String [separatePaths.length];
+				    for (int i = 0; i < separatePaths.length; i++) {
+					graphs[i] = Graphviz.createServicesGraph (separatePaths[i], props);
+				    }
+				}
+			    }
+			    if (generateScufl) {
+				scufls = new String [separatePaths.length];
+				for (int i = 0; i < separatePaths.length; i++) {
+				    scufls[i] = Taverna.buildWorkflow (separatePaths[i], worker.getRegistryEndpoint(),
+								       props);
+				}
+			    }
+			} else {
+			    System.err.println ("No straight path exitsts between '" +
+						pathEnds[0] + "' and '" + pathEnds[1] + "'. Only cyclic paths found.");
+			    System.exit(1);
+			}
+
+		    } else {
+			graphs = new String [1];
+			graphs[0] = Graphviz.createServicesGraph (allPaths, props);
+		    }
 
-		// output the graph
-		param = cmd.getParam ("-fs");
-		if (param == null)
-		    param = cmd.getParam ("-f");
-		if (param == null)
-		    System.out.println (graph);
-		else
-		    createFile (param, graph);
-	    }
+		    String fn = cmd.getParam ("-fs");
+		    if (fn == null)
+			fn = cmd.getParam ("-f");
+
+		    // output the graph[s]
+		    if (graphs != null) {
+			boolean usePathNames = (pathNames != null && graphs.length == pathNames.length);
+			for (int i = 0; i < graphs.length; i++) {
+			    if (fn == null)
+				System.out.println (graphs[i]);
+			    else {
+				if (graphs.length == 1)
+				    createFile (fn, graphs[i]);
+				else
+				    createFile (modifyFileName (fn,
+								(usePathNames ?
+								 pathNames[i] :
+								 Printf.format ("Graph_%.2d", "" + (i+1))
+								 )
+								),
+						graphs[i]);
+			    }
+			}
+		    }
 
+		    // output scufl definitions
+		    if (scufls != null) {
+			for (int i = 0; i < scufls.length; i++) {
+			    if (fn == null)
+				System.out.println (scufls[i]);
+			    else
+				createFile (replaceExtension (modifyFileName (fn, pathNames [i]),
+							      "xml"),
+					    scufls[i]);
+			}
+		    }
 
-	} catch (NoSuccessException e) {
-	    System.err.println ("===ERROR===");
-	    System.err.println (e.getMessage());
-	    System.err.println (e.getCulprit());
-	    System.err.println ("===========");
+		} else {
+		    String graph;
+		    if (cmd.hasOption ("-rdf"))
+			graph = RDF.createServicesGraph (edges, props);
+		    else
+			graph = Graphviz.createServicesGraph (edges, props);
+
+		    // output the graph
+		    param = cmd.getParam ("-fs");
+		    if (param == null)
+			param = cmd.getParam ("-f");
+		    if (param == null)
+			System.out.println (graph);
+		    else
+			createFile (param, graph);
+		}
+	    }
 
 	} catch (Exception e) {
 	    System.err.println ("===ERROR===");
@@ -224,6 +301,21 @@
 	}
     }
 
+    // 'name' has one of the following formats:
+    //     dataTypeName
+    //     namespace/dataTypeName
+    static MobyPrimaryDataSimple createSimpleData (String name) {
+	MobyPrimaryDataSimple data = new MobyPrimaryDataSimple ("dummy_name_for " + name);
+	int pos = name.indexOf ("/");
+	if (pos > -1 && pos != name.length() - 1) {
+	    data.addNamespace (new MobyNamespace (name.substring (0, pos)));
+	    data.setDataType (new MobyDataType (name.substring (pos + 1)));
+	} else {
+	    data.setDataType (new MobyDataType (name));
+	}
+	return data;
+    }
+
     /*************************************************************************
      * Create 'filename' from 'content'.
      *************************************************************************/
@@ -233,6 +325,29 @@
 	    (new BufferedWriter (new FileWriter (filename)));
 	fileout.print (content);
 	fileout.close();
+	decorationLn ("File created: " + filename);
+    }
+
+    /*************************************************************************
+     * Add an additional part ('addon') into a file name...
+     *************************************************************************/
+    static String modifyFileName (String filename, String addon) {
+	int pos = filename.lastIndexOf (".");
+	if (pos > -1)
+	    return filename.substring (0, pos+1) + addon + filename.substring (pos);
+	else
+	    return filename + "." + addon;
+    }
+
+    /*************************************************************************
+     * Replace (or add) extension in 'filename' by 'newExtension'.
+     *************************************************************************/
+    static String replaceExtension (String filename, String newExtension) {
+	int pos = filename.lastIndexOf (".");
+	if (pos > -1)
+	    return filename.substring (0, pos+1) + newExtension;
+	else
+	    return filename + "." + newExtension;
     }
 
     /*************************************************************************
@@ -248,6 +363,17 @@
     }
 
     /*************************************************************************
+     * For collecting boolean properties, and using property names
+     * with or without starting dash.
+     *************************************************************************/
+    static void addBool (Properties props, BaseCmdLine cmd, String propertyName) {
+	if ( cmd.hasOption ("-" + propertyName) ||
+	     cmd.hasOption (propertyName) ) {
+	    props.put (propertyName, "true");
+	}
+    }
+
+    /*************************************************************************
      * Print 'msg' but only if in verbose mode
      *************************************************************************/
     static void decoration (String msg) {




More information about the MOBY-guts mailing list