Previous topic  Top  Next topic  Print this Topic
 

Accessing Web Service with .NET

 

If you are using Visual Studio, you have to add a Web reference to your project before you can use the OntoBroker Web service:

1.Click on the menu item Project of the main menu and select Add Service Reference....
2.Enter the URL of the WSDL and press the GO button.

The dialog should report "1 service found".

3.Finally enter a name for the reference and press the Add Reference button.

For other languages or Web service frameworks see the documentation in order to understand how to build/generate a Web service client starting with a WSDL.

The following example shows how to access OntoBroker from C#. The samples directory interfaces\webservice contains complete project archives for Visual Basic and C#. You can use these projects directly with Microsoft Visual Studio 2008 or Microsoft Visual Studio Express Editions.

using System;

using OntoBrokerWebserviceSample.OntoBroker;

 

namespace OntoBrokerWebserviceSample

{

  /// <summary>

  /// This is a sample how to use the OntoBroker 6.0 webservice with C#

  /// It is assumed that this webservice has already been added to the project

  /// using Project/Add Service Reference...

  /// </summary>

  class Program

  {

      static void Main(string[] args)

      {

          OntobrokerTypeClient client = new OntobrokerTypeClient();

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

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

         

          // add some facts to the module http://sample.ontoprise.de/ws

          String msg;

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

          Console.Out.WriteLine(msg);

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

          Console.Out.WriteLine(msg);

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

          Console.Out.WriteLine(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.");

          Console.Out.WriteLine(msg);

          // really add the rule

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

          Console.Out.WriteLine(msg);

 

          // query for all adult persons

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

 

          Console.Out.WriteLine("Results returned: " + results.result.Length);

          PrintResults(results);

 

          Console.Out.WriteLine();

          Console.Out.WriteLine("Same query with paging");

          Console.Out.WriteLine();

         

          // 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

          bool finished = false;

          while (!finished) {

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

              PrintResults(pagedResults);

              finished = pagedResults.finished;

          }

 

          Console.Out.WriteLine("Press any key");

          Console.In.Read();

      }

 

      private static void PrintResults(QueryResults results)

      {

          foreach (WsTuple tuple in results.result)

          {

              for (int i = 0; i < results.variableNames.Length; i++)

              {

                  if (i > 0)

                      Console.Out.Write(", ");

                  Console.Out.Write(results.variableNames[i] + " = " + tuple.repr[i]);

              }

              Console.Out.WriteLine();

          }

      }

  }

}