Previous topic  Top  Next topic  Print this Topic
 

Accessing Web Service with Java

 

Below you can see how to access the OntoBroker Web service with JAX-WS. If you are using Java 1.6, nothing needs to be installed, as JAX-WS is part of Java Version 6 (Standard Edition). If you are using Java 1.5, you need to install JAX-WS separately. There are also other web service stacks available for Java which work similarly. Java 1.6 allows easy usage of Web services after generation of the client code. This is done by calling the wsimport tool of Java:

wsimport -d <classes> [-s <source>] http://<ontobrokerhost>:<wsport>/services/ontobroker?wsdl

After this call, the generated java classes are written to the <classes> directory, their source code optionally to the directory <source>. If you include these generated classes to your code, you can start working with OntoBroker. The following sample code shows how to perform commands and queries.

The samples directory interfaces\webservice contains a complete project archive which can be used directly with Eclipse 3.5 and later.

/*

* Copyright (c) 2008-2010 ontoprise GmbH.

*/

package sample;

 

import java.util.List;

 

import com.ontoprise.schema.ontobroker.ws._2009._01.PagedQueryResults;

import com.ontoprise.schema.ontobroker.ws._2009._01.QueryResults;

import com.ontoprise.schema.ontobroker.ws._2009._01.WsTuple;

import com.ontoprise.services.OntobrokerSvcService;

import com.ontoprise.services.OntobrokerType;

 

/*

* This is a sample how to use the OntoBroker 6.0 webservice with Java 1.6 using JAX-WS

*/

public class WebserviceClient {

 

   public static void main(String[] args) throws Exception {

       OntobrokerType client = new OntobrokerSvcService().getOntobrokerSvcPort();

     

       String module = "<http://sample.ontoprise.de/ws>";

       String suffix = "@" + module + ".";

 

       String msg;

       msg = client.command("insert Michael[age->15]" + suffix);

       System.out.println(msg);

       msg = client.command("insert John[age->18]" + suffix);

       System.out.println(msg);

       msg = client.command("insert Sandra[age->21]" + suffix);

       System.out.println(msg);

 

       // add a rule to the module http://sample.ontoprise.de/ws

       String rule = "@{adult} ?P:Adult :- ?P[age->?X], ?X >= 18.";

       // remove it first (only done here to allow multiple runs of this sample)

       msg = client.command(":- module " + module + ".\ndelete rule adult.");

       System.out.println(msg);

       // really add the rule

       msg = client.command(":- module " + module + ".\ninsert {" + rule + "}");

       System.out.println(msg);

 

       // query for all adult persons

       QueryResults results = client.query("?- ?P:Adult[age->?Age]@?M.", false);

 

       System.out.println("Results returned: " + results.getResult().size());

       printResults(results);

 

       System.out.println();

       System.out.println("Same query with paging");

       System.out.println();

     

       // same query using paging

       int timeout = 900; // query can be removed after 900 seconds = 15 min, if client dies      

       long queryKey = client.openQuery("?- ?P:Adult[age->?Age]@?M.", false, timeout);

       int maxResults = 1; // maximum number of results to return

       int maxWaitTime = 100; // maximum wait time for results on server side

       boolean finished = false;

       while (!finished) {

           PagedQueryResults pagedResults = client.getNextQueryResults(queryKey, maxResults, maxWaitTime);

           printResults(pagedResults);

           finished = pagedResults.isFinished();

       }

     

       System.out.println("Press any key");

       System.in.read();

   }

 

 

   private static void printResults(QueryResults results) {

       for (WsTuple tuple : results.getResult()) {

           final List<String> variableNames = results.getVariableNames().getVar();

           for (int i = 0; i < variableNames.size(); i++) {

               if (i > 0)

                   System.out.print(", ");

               System.out.print(variableNames.get(i) + " = " + tuple.getRepr().get(i));

           }

           System.out.println();

       }

   }

}