[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Thu Oct 30 02:33:24 UTC 2008


gordonp
Wed Oct 29 22:33:24 EDT 2008
Update of /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services
In directory dev.open-bio.org:/tmp/cvs-serv10500/src/main/ca/ucalgary/seahawk/services

Modified Files:
	MobyClient.java TextClient.java 
Log Message:
Updates related to SAWSDL wrapping Web Service implementation
moby-live/Java/src/main/ca/ucalgary/seahawk/services MobyClient.java,1.20,1.21 TextClient.java,1.5,1.6
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/MobyClient.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/MobyClient.java	2008/01/14 23:00:44	1.20
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/MobyClient.java	2008/10/30 02:33:24	1.21
@@ -93,12 +93,8 @@
      */
     public MobyClient(Registry reg) throws MobyException{
 	registry = reg;
-        if(reg != null && reg.getEndpoint() != null){
-            c = new CentralCachedCallsImpl(reg.getEndpoint());
-        }
-        else{
-            c = new CentralCachedCallsImpl();
-        }
+        c = CentralImpl.getDefaultCentral(registry);
+
 	xpathMap = new HashMap<XPathExpression,MobyComplexBuilder>();
 	urlRegexMap = new HashMap<Pattern,MobyComplexBuilder>();
 	regexMap = new HashMap<Pattern,MobyComplexBuilder>();
@@ -328,6 +324,8 @@
 	    }
 
 	}  // for each ruleset
+
+	// TODO: add warning if no rules at all were added (the file may not be a MOB rules file!)
     }
 
     public boolean isPrefixRule(Element e){
@@ -634,6 +632,57 @@
     }
 
     /**
+     * Create a MOBY data instance from a DOM by applying the rules of the given MobyClient.
+     * The creation of a single object or collection is done according to the dataTemplate provided.
+     *
+     * @throws MobyServiceException NOTE: these are just warning level exceptions about collection-simple casting that you can add to the service response, or ignore if you like
+     */
+    public MobyDataInstance getMobyObject(Node n, MobyPrimaryData dataTemplate)
+	throws Exception, MobyServiceException{
+	Vector<MobyDataObject> results = new Vector<MobyDataObject>();
+
+	for(MobyDataObject resultPart: getMobyObjects(n)){ //TODO: , dataTemplate.getDataType())){
+	    results.add(resultPart);
+	}
+
+	if(results.isEmpty()){
+	    throw new Exception("No MOB rules could be applied to the input data");
+	}
+	else if(results.size() > 1){
+	    if(dataTemplate instanceof MobyPrimaryDataSimple){
+		// In this case, eliminate any higher-up objects that
+		// have the exact same contents as the same members who are their
+		// subtypes, as these are superfluous.
+
+		System.err.println("Multiple Moby objects were found in the text data, " +
+				   "but the request was to return a simple. " +
+				   "Only the first value in the collection has been returned.");
+		for(MobyDataObject result: results){
+		    System.err.println("Found object: " + result.getDataType().getName()); 
+		}
+		// TODO: should we instead return the one deepest in the hierarchy (or with the most members)?
+		return results.elementAt(0);
+	    }
+	    else{
+		MobyDataObjectSet resultSet = new MobyDataObjectSet(dataTemplate.getName());
+		resultSet.addAll(results);
+		return resultSet;
+	    }
+	}
+	// One result
+	else{
+	    if(dataTemplate instanceof MobyPrimaryDataSimple){
+		return results.elementAt(0);
+	    }
+	    else{  // Collection of 1
+		MobyDataObjectSet resultSet = new MobyDataObjectSet(dataTemplate.getName());
+		resultSet.add(results.elementAt(0));
+		return resultSet;
+	    }
+	}
+    }
+
+    /**
      * Create a MOBY data instance from a map of name->bytes[] by applying the rules of the given MobyClient.
      * The creation of a single object or collection is done according to the dataTemplate provided.
      *
@@ -643,7 +692,6 @@
 	throws Exception, MobyServiceException{
 	Vector<MobyDataObject> results = new Vector<MobyDataObject>();
 
-	// System.err.println("Data has " + resultParts.size()  + " parts");
 	// The output parameter *potentially* has multiple parts 
 	// (e.g. multiple .png files from EMBOSS's "banana" program)
 	// hence the nested for loops (part iteration, followed by created-object iteration)

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/TextClient.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/TextClient.java	2007/08/01 18:14:33	1.5
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/services/TextClient.java	2008/10/30 02:33:24	1.6
@@ -30,7 +30,8 @@
     private TransformerFactory transFactory;
     private DocumentBuilder docBuilder;
 
-    private Map<String, Vector<String>> typeTemplates;
+    private Map<String, Vector<String>> typeTemplates;  //mobytype -> Vector(xsltrule1, xsltrule2,...)
+    private Map<String, Vector<MobyNamespace>> typeNsRestrictions;  //mobytype -> Vector(mobynamespace1, ...)
     private Map<String, String> templateMode;
 
     public TextClient() throws Exception{
@@ -45,10 +46,14 @@
 
 	moby2textConverters = new Vector<Transformer>();
 	typeTemplates = new HashMap<String, Vector<String>>();
+	typeNsRestrictions = new HashMap<String, Vector<MobyNamespace>>();
 	templateMode = new HashMap<String, String>();
     }
 
-    public synchronized void addMappingsFromURL(URL xsltURL) throws Exception{
+    /**
+     * @return the data format the xslt creates (the last template mode attribute in the file) 
+     */
+    public synchronized String addMappingsFromURL(URL xsltURL) throws Exception{
         StreamSource stylesheet = new StreamSource(xsltURL.toString());
 	// Prepend to list, so later rules can override ones specified earlier
 	moby2textConverters.insertElementAt(transFactory.newTransformer(stylesheet), 0);
@@ -64,8 +69,10 @@
 				+ xsltURL + " (empty or malformed document?)");
         }
 
+	String mode = null; // returned in the end
+
 	// Lines look something like below
-	// <xsl:template match="moby:GenericSequence | GenericSequence" name="Collection-GenericSequence#1" mode="seq">
+	// <xsl:template match="moby:GenericSequence | GenericSequence" name="Collection-GenericSequence.1" mode="seq">
 	NodeList templates = xsltDOMRoot.getElementsByTagNameNS(XSLT_NS, "template");
 	for(int i = 0; i < templates.getLength(); i++){
 	    Element template = (Element) templates.item(i);
@@ -73,17 +80,21 @@
 		continue;
 	    }
 	    String templateName = template.getAttribute("name");
-	    templateMode.put(templateName, template.getAttribute("mode"));
+	    String m = template.getAttribute("mode");
+	    if(m != null && m.trim().length() > 0){
+		mode = template.getAttribute("mode");
+	    }
+	    templateMode.put(templateName, m);
 
 	    //System.err.println("Processing template " + templateName + ", mode " + template.getAttribute("mode"));
 
 	    // Keep track of the list of templates using the given MOBY object type
 	    String[] templateNameParts = templateName.split("\\.");
-	    if(templateNameParts.length == 1){
-		System.err.println("Ignoring template without \"DataType#1\" format for its name attribute (" + 
-				   templateName);
-		continue;
-	    }
+	    // if(templateNameParts.length == 1){
+// 		System.err.println("Ignoring template without \"DataType.1\" format for its name attribute (" + 
+// 				   templateName);
+// 		continue;
+// 	    }
 
 	    if(templateNameParts[0].startsWith("Collection-")){
 		if(templateNameParts[0].length() == 11){
@@ -94,20 +105,41 @@
 	    }
 
 	    // See if the type exists in the ontology
+	    // TODO: add support for registry spec via LSID in mode (how to deal with colons?)
 	    MobyDataType type = MobyDataType.getDataType(templateNameParts[0], SeahawkOptions.getRegistry());
+	    MobyNamespace ns = null;  //no ns restriction by default
 	    if(type == null){
-		System.err.println("Ignoring template whose name attribute (" + templateName + 
-				   ") uses a non-existent datatype (" + templateNameParts[0] + ")");
-		continue;
+		// See if there is a namespace restriction e.g. Object-EC
+		if(templateNameParts[0].contains("-") && !templateNameParts[0].endsWith("-")){
+		    // Assumes no dashes in the namespace label itself
+		    String namespace = templateNameParts[0].substring(templateNameParts[0].lastIndexOf("-")+1);
+		    //System.err.println("Found a dash in "+templateNameParts[0] + ", assuming ns is " + namespace);
+		    ns = MobyNamespace.getNamespace(namespace, SeahawkOptions.getRegistry());
+		    // If it can be parsed as a real namespace, treat the stuff before the "-NS" as the data type
+		    if(ns != null){
+			templateNameParts[0] = templateNameParts[0].substring(0, templateNameParts[0].lastIndexOf("-"));
+			type = MobyDataType.getDataType(templateNameParts[0], SeahawkOptions.getRegistry());
+		    }
+		    //System.err.println("Resolved ns is " + type);
+		}
+		if(type == null){
+		    System.err.println("Ignoring template whose name attribute (" + templateName + 
+				       ") uses a non-existent datatype (" + templateNameParts[0] + ")");		    
+		    continue;
+		}
 	    }
 
 	    if(!typeTemplates.containsKey(templateNameParts[0])){
 		typeTemplates.put(templateNameParts[0], new Vector<String>());
+		typeNsRestrictions.put(templateNameParts[0], new Vector<MobyNamespace>());
 	    }
 	    // Later templates have higher priority than earlier declarations
 	    typeTemplates.get(templateNameParts[0]).insertElementAt(templateName, 0);
-
+	    typeNsRestrictions.get(templateNameParts[0]).insertElementAt(ns, 0);	    
 	}
+
+	//System.err.println("Mode returned by " + xsltURL + " is " + mode);
+	return mode; // return the last mode value in the file (useful when mapping XSLT -> data format)
     }
 
     /**
@@ -127,8 +159,13 @@
 	    type != null;
 	    type = type.getParent()){
 	    if(typeTemplates.containsKey(type.getName())){
-		for(String templateName: typeTemplates.get(type.getName())){
-		    if(textType.equals(templateMode.get(templateName))){
+		// These two vectors should be of the same length!
+		Vector<String> templateNames = typeTemplates.get(type.getName());
+		Vector<MobyNamespace> nsRestrictions = typeNsRestrictions.get(type.getName());
+		for(int i = 0; i < templateNames.size(); i++){
+		    String templateName = templateNames.elementAt(i);
+		    // the template input moby datatype is the same type as we have an instance of
+		    if(textType.equals(templateMode.get(templateName))){  
 			// Input Moby type and output text type match: does set-ness?
 			if(dataTemplate instanceof MobyPrimaryDataSet){
 			    if(templateName.startsWith("Collection-")){
@@ -140,6 +177,15 @@
 				return true;
 			    }
 			}
+			// Is there a namespace restriction on the transformation rule?
+			MobyNamespace ns = nsRestrictions.elementAt(i);
+			if(ns != null){
+			    for(MobyNamespace n: dataTemplate.getNamespaces()){
+				if(ns.equals(n)){
+				    return true;
+				}
+			    }
+			}
 		    }
 		}
 	    }
@@ -154,6 +200,8 @@
      * try representing the data in simpler top level objects until we get a rule 
      * that produces something.
      *
+     * @param mobyData must be an instance of a MobyPrimaryData that will be transformed
+     *
      * @return null if the moby data or target text type is null, null if data is not primary, null if no templates apply, or a string representing the transformation if successful
      */
     public String getText(MobyDataInstance mobyData, String targetTextType) throws Exception{
@@ -163,11 +211,60 @@
 	// Spaces are not allowed in mode names
 	targetTextType = targetTextType.replace(' ', '-');
 
+	if(!(mobyData instanceof MobyPrimaryData)){
+	    throw new IllegalArgumentException("Require a MobyPrimaryData instance for arg 0 but " +
+					       "was passed a "+mobyData.getClass().getName() + " instead.");
+	}
+
 	Vector<MobyDataType> candidateTypes = new Vector<MobyDataType>();
 	for(MobyDataType type = ((MobyPrimaryData) mobyData).getDataType();
 	    type != null;
 	    type = type.getParent()){
 	    if(typeTemplates.containsKey(type.getName())){
+		// These two vectors should be of the same length!
+		Vector<String> templateNames = typeTemplates.get(type.getName());
+		Vector<MobyNamespace> nsRestrictions = typeNsRestrictions.get(type.getName());
+		for(int i = 0; i < templateNames.size(); i++){
+		    String templateName = templateNames.elementAt(i);
+		    // the template input moby datatype is the same type as we have an instance of
+		    if(targetTextType.equals(templateMode.get(templateName))){  
+			// Is there a namespace restriction on the transformation rule?
+			MobyNamespace ns = nsRestrictions.elementAt(i);
+			if(ns != null){
+			    boolean nsMatch = false;
+			    for(MobyNamespace n: ((MobyPrimaryData) mobyData).getNamespaces()){
+				if(ns.equals(n)){
+				    nsMatch = true;
+				    break;
+				}
+			    }
+			    if(!nsMatch){
+				continue; // ns doesn't match, don't bother checking set-ness
+			    }
+			}
+
+			// Input Moby type and output text type match, we'll try the template here
+			if(mobyData instanceof MobyPrimaryDataSet){
+			    if(templateName.startsWith("Collection-")){
+				candidateTypes.add(type);
+				break;
+			    }
+			}
+			else{
+			    if(!templateName.startsWith("Collection-")){
+				candidateTypes.add(type);
+				break;
+			    }
+			}
+		    }
+		}
+	    }
+	}
+
+	for(MobyDataType type = ((MobyPrimaryData) mobyData).getDataType();
+	    type != null;
+	    type = type.getParent()){
+	    if(typeTemplates.containsKey(type.getName())){
 		for(String templateName: typeTemplates.get(type.getName())){
 		    if(targetTextType.equals(templateMode.get(templateName))){
 			// Input Moby type and output text type match, we'll try the template here




More information about the MOBY-guts mailing list