[MOBY-l] Re: a question

Benjamin Good bmg at sfu.ca
Fri Feb 4 07:28:47 UTC 2005


Hi Paul,

  I've installed your updated class but am getting another class cast 
error.  I'm absolutely sure I'm running the updated version, I can see 
where you changed the Vector.toArray method.

The casting error has moved to here in the example

Vector response = (Vector) responses.next();

It seems that this should be casting to a MobyDataInstance array no?  
Changing this to an array made the example I'm running work.

Below is the current working example.  I haven't tried it yet, but it 
would be great to complete it by taking the response and using it for 
querying for and executing another round of services.

cheers

-Ben

import org.biomoby.client.CentralImpl;
import org.biomoby.client.MobyRequest;
import org.biomoby.shared.*;

import java.util.Iterator;
import java.util.Vector;

/**
* @author benjamgo
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestRequest {

    public static void main(String[] args) {
        try{
            Central worker = new CentralImpl ();
            worker.setDebug(true);
//make template for finding services
            MobyService templateService = new MobyService("dummy");
            MobyDataType type = new MobyDataType("Object");
                 MobyDataSimpleInstance input = new 
MobyDataSimpleInstance ("");
            input.setDataType (type);
            input.addNamespace (new MobyNamespace ("NCBI_gi"));
            input.setId("111076");
            input.setXmlMode(MobyDataSimpleInstance.CENTRAL_XML_MODE);
            templateService.addInput(input);
            templateService.setName("MOBYSHoundGetGenBankff");
//        Find services that match this template service
            System.out.println("finding services matching template: 
"+templateService);
            MobyService[] validServices = 
worker.findService(templateService);
          //        Make sure we have a service to run for this input
                if(validServices == null || validServices.length == 0){
                    System.err.println("Could not find any valid 
services");
                }
                else{
                    System.out.println("found some services");
                }
                
input.setXmlMode(MobyDataSimpleInstance.SERVICE_XML_MODE);
                MobyDataSimpleInstance[] moby_in = new 
MobyDataSimpleInstance[1];
                moby_in[0] = input;

                MobyRequest mr = new MobyRequest(worker);

                mr.setDebugMode(false);
                mr.setService(validServices[0]);
                mr.setInput(moby_in);
                java.util.Vector output = mr.invokeService();
                String xresponse = mr.getResponseXML();
                String c = "";
                Iterator responses = output.iterator();
                // The service invocation may have had many requests, 
there is a response
                // object for each request
                while(responses.hasNext()){

                	System.out.println("Casting...");
                    //bmg Vector response = (Vector) responses.next();
                	MobyDataInstance[] response = (MobyDataInstance[]) 
responses.next();
                	//bmg Iterator resultObjects = response.iterator();

                    // The response for a request may have many objects
                    System.out.println("The actual response 
contained...");
                    //bmg while(resultObjects.hasNext()){
                    for(int i=0;i<response.length;i++){
                        //bmg MobyDataInstance resultObject = 
(MobyDataInstance) resultObjects.next();
                    	MobyDataInstance resultObject = response[i];
                        // The response objects may either be simple 
data, or data sets
                        if(resultObject instanceof 
MobyDataSimpleInstance){
                            System.out.println("Plain simple instance is 
"+resultObject.toXML());
                        }

                        else if(resultObject instanceof 
MobyDataSetInstance){
                            System.out.println("Found a collection:");
                            MobyPrimaryDataSimple[] simples = 
((MobyDataSetInstance) resultObject).getElements();
                            for(int j = 0; j < simples.length; j++){
                                System.out.println("  Member instance is 
\n"+simples[j].toXML());
                            }
                        }
                    }
                }
            }catch(Exception e){
                System.out.println("Problem "+e);
            }
    }
}




More information about the moby-l mailing list