Accessing Tasks

Once a Task can convert external information and populate a Task, the task needs the information. In general, the information must be in XML format. Specifically, the information must support XPath queries in order to access the data.

Usually, supporting XPath queries requires the data to be XML. However, when properly populated, an ordered list can also support a limited (but sufficient) subset of such queries.

Topics

 

XPath

[ TOP ]

While the full specification of XPath allows for traversing an XML tree, the conversion process can be performed if a particular item, or sequence of items, can be accessed. Typically, a query specifies the children elements to descend a tree. For example, given the following XML data:

<?xml version="1.0"?>
<ExternalData>
   <Parent attribute="value">
      <Child1>1</Child1>
      <Child2>2</Child2>
      <Child3>
         <Data>item3.1</Data>
         <Data>item3.2</Data>
         <Data>item3.3</Data>
      </Child3>
   </Parent>
</ExternalData>

Given the following functions that extract data from an XML tree:

String   getValue( String xpathQuery );
String[] getValues( String xpathQuery );

The data can then be retrieved with the following XPath queries:

int child1 = Integer.parseInt( 
		getValue( "/ExternalData/Parent/Child1" ) );
int child2 = Integer.parseInt( 
		getValue( "/ExternalData/Parent/Child2" ) );
String[] child3 = getValues( "/ExternalData/Parent/Child3/Data/*" );
which results in:
child1 = 1
child2 = 2
child3 = { "item3.1", "item3.2", "item3.3" }


 

<FORM>

[ TOP ]

HTML supports transfering data from the client by using forms. Forms contain various <INPUT> fields of different types. The ones demonstrated below are the hidden, text, and submit types.

The web client will transmit the data in a stream of name/value pairs. An important note is that HTTP sends the data in the same order as the INPUTs appear on the page. This can be important for reconstructing the data.

For example, let's say a FORM page has three input fields, one hidden, one text, and one submit.

<FORM ACTION="http://localhost:8181/ExternalRequest" METHOD="post">
<INPUT TYPE="hidden" NAME="taskName"
       VALUE="jicos.examples.external.fibonacci.Fibonacci"></INPUT>
Number: <INPUT TYPE="text" SIZE="5"
               NAME="/ExternalRequest/Fibonacci/n"></INPUT>
<INPUT TYPE="submit">
(In HTML, it isn't necessary to close the <INPUT> tag, but when also dealing with XML, it is a good habit to get into.)

The above form creates a label "Number", followed by a text field five characters wide for the number, and a button that will send the data to the URL specified in the ACTION of the form. Assume that the user used 20 as the number. The following data would be sent to the CollectorHttp:

taskName=jicos.examples.external.fibonacci.Fibonacci&/ExternalRequest/Fibonacci/n=20

 

Submitting the Task and Getting the Result

[ TOP ]

Before submitting the information to JICOS, the last selection to make is whether to wait for a response, or poll for an answer. For short running jobs (such as getting the Fibonacci sequence for n=10) it is better to wait for an answer. Longer running jobs (such as solving the Traveling Salesman Problem for 500 cities) might take a day or two, and losing the result because of a network partition would be disappointing.

At the bottom of the form is a pull-down menu ( ) that will allow the user to wait for the response ("immediate" mode) or poll the web server ("delayed" mode) for the answer. If immediate mode is selected, then the collector will not send back a result, causing the browser to wait until the answer is computed.

In delayed mode, the collector will immediately return a web page containing a URL where the answer will eventually be found. The page returned will periodically (every 60 seconds) check to see of the answer is available. Another approach would be to bookmark the URL and try back. A future enhancement will be to have the Collector send a mail message once the answer is available.

 

Example Code: Fibonacci.java

[ TOP ]

The Fibonacci sequence is used as sort of a "Hello World" program in JICOS, since it clearly demonstrates the decomposition of a Task, and the composition of an answer. The Fibonacci sequence is defined recursively as follows:

F(n) = F(n-1) + F(n-2), for all integers > 1
    where F(0) = 1, and
          F(1) = 0
thus, F(5) =
= F(4)+F(3)
= F(3)+F(2) + F(2)+F(1)
= F(2)+F(1)+F(1)+F(0) + F(1)+F(0)+1
= F(1)+F(0)+1+1+1 + 1+1+1
= 1+1+1+1+1 + 1+1+1
= 8

 F(6) = F(5)+F(4) =  8 +  5 = 13
 F(7) = F(6)+F(5) = 13 +  8 = 21
 F(8) = F(7)+F(6) = 21 + 13 = 34
 F(9) = F(8)+F(7) = 34 + 21 = 55
F(10) = F(9)+F(8) = 34 + 55 = 89
...

Fibonacci.java is an example of how to extend this basic Task to support external access. Some details to note:

  1. The fibonacci task does not require an Input or Shared Object, and as such these methods return null.
  2. The result Object is an Integer.
  3. The toHtmlString() method allows for a null XmlDocument. This allows the web browser to request (an HTTP GET, instead of a PUT or POST) a blank form from the ColectorHttp.