Go to the first, previous, next, last section, table of contents.


A full-scale example

This section demonstrates the capabilities of the LaSt engine in a full scale example; from the structured data, via all the engine inputs, to the final HTML result. The example has been chosen to be simple, yet instructive, and a more complex set of examples is distributed along with the LaSt engine executable.

The engine inputs

The LaSt engine processing always start with reading the program input, with the format previously described in section LaSt engine inputs. In the following example the engine input controlling it all is assumed to be the one given below.

<last>
<process file="documents/simple.rw" package="rules/simple.last" />
<flags output="full"/>
<choices file="documents/simple.choices" />
</last>

The LaSt engine will, as a result of this input request, read the RadioWeb document documents/toplist.rw, the LaSt rule package rules/toplists.last, and the choices in documents/toplist.choices. Each one of these documents will be described in the following sections.

The RadioWeb document

In order to create an output page, the LaSt engine must be supplied with a RadioWeb document, much like a template containing holes to fill. The "holes" in this document, however, are references to data objects that will be interpreted by the WSDL interface.

<html>
<head>
  <title>A RadioWeb Toplist</title>
</head>
<body bgcolor="#ffffff">

<h1>A RadioWeb Toplist</h1>

<h2>Top five every week</h2>

<rw id="top_five_list" query="toplist"></rw>

<hr>

<em>This is a timeless document</em>

</body>
</html>

In the current version of the LaSt engine, the WSDL interface will interpret the special attributes `id' and `query' for each object. A WSDL data tree will be created for each object, as a result of calling the WOF interpreter with the given query. In this case there will only be one such tree, as there is only one object in the RadioWeb document. This result tree is further described in section The WSDL data.

The WSDL data

The LaSt engine operates on structured data (as described in the section "A note on input and output" in [Ct99]), and this data is supplied by the WSDL interface. The interface is really describing an abstract data type, leaving the actual data representation free. In the example below a fictive XML syntax has been invented for displaying the structured data.

<toplist>
  <topsong>
    <position>1</position>
    <song>
      <name>One</name>
      <artist url="http://www.island.co.uk/u2/">
        <name>U2</name>
      </artist>
      <album>
        <name>Achtung Baby</name>
        <company>Island Records</company>
      </album>
    </song>
  </topsong>
  <topsong>
    <position>2</position>
    <song>
      <name>Across the border</name>
      <artist url="http://www.sonymusic.com/artists/BruceSpringsteen/">
        <name>Bruce Springsteen</name>
      </artist>
      <album>
        <name>The ghost of Tom Joad</name>
        <company>Columbia Records</company>
      </album>
    </song>
  </topsong>
  <topsong>
    <position>3</position>
    <song>
      <name>Most of the time</name>
      <artist url="http://www.bobdylan.com/">
        <name>Bob Dylan</name>
      </artist>
      <album>
        <name>Oh Mercy</name>
        <company>Columbia Records</company>
      </album>
    </song>
  </topsong>
  <topsong>
    <position>4</position>
    <song>
      <name>Parklife</name>
      <artist url="http://www.blur.co.uk">
        <name>Blur</name>
      </artist>
      <album>
        <name>Parklife</name>
        <company>EMI / Parlophone</company>
      </album>
    </song>
  </topsong>
  <topsong>
    <position>5</position>
    <song>
      <name>TwentyOne</name>
      <artist url="http://www.cranberries.com">
        <name>The Cranberries</name>
      </artist>
      <album>
        <name>No need to argue</name>
        <company>Island Records</company>
      </album>
    </song>
  </topsong>
</toplist>

The data above represents a top-five list with artists and songs. More data is present in the structure than what will later be used, and through delaying such data the WSDL interface could be made more efficient. It should also be noted in the example above that the data contain attributes, although such attributes normally should not be used for storing data. The reason for using attributes in this example is to be able to demonstrate how attributes are be accessed in the LaSt rules.

The LaSt package

The actual processing of the structured data in the LaSt engine, is performed with LaSt rules, present in one or more LaSt packages. In this example one single package is used, containing two LaSt rules, as can be seen below.

<last:package>

<last:import prolog="simple.pl" />

<last:rule name="song-table" auto-apply="true" match="toplist" 
           call-succeeds="max_node_depth(Output,3)">
  <last:template>
    <table cellpadding="3" cellspacing="0" border="2">
    <last:for-each select="topsong">
      <tr bgcolor=_bgcolor>
        <td><font color=_fgcolor>
          <b><last:value-of select="position"/></b>
        </font></td>
        <td><font color=_fgcolor>
          <b><last:value-of select="song/name"/></b>
        </font></td>
        <td><font color=_fgcolor>
          <b><last:value-of select="artist/name"/></b>
        </font></td>
        <td><font color=_fgcolor>
          <b><last:value-of select="album/name"/></b>
        </font></td>
      </tr>
    </last:for-each>
    </table>
  </last:template>

  <last:avoid call-fails="color_contrast(_bgcolor,_fgcolor)" />
</last:rule>

<last:rule name="song-list" auto-apply="true" match="toplist">
  <last:template>
    <ol>
    <last:for-each select="topsong">
      <li>
        <b><last:value-of select="song/name"/></b>
        (from <last:value-of select="album/name"/>)
        -- <b><last:value-of select="artist/name"/></b>
      </li>
    </last:for-each>
    </ol>
  </last:template>
</last:rule>

<last:default name="bgcolor" value="white" />
<last:default name="bgcolor" value="black" />
<last:default name="bgcolor" value="#cccccc" />
<last:default name="bgcolor" value="#ffffcc" />

<last:default name="fgcolor" value="black" />
<last:default name="fgcolor" value="white" />

</last:package>

The two rules present both operate on data of the type `toplist', resulting in two possible layouts for the data presented earlier. Which one is chosen depend on the choices, and if the certain requirements in the rules are met. The package above also imports a Prolog source file, simple.pl, that is shown below.

max_node_depth(Node, Max) :-
        context_depth(Node, Depth),
        Depth =< Max.

The Prolog predicate max_node_depth/2 is used to check the node depth of the position where the results are inserted. As could be seen in the RadioWeb document, the node depth at the insertion point will be 3, as there are always an `html' and a `body' ancestor to any object in the HTML output.

Processing the choices

The selection of layout and style is controlled, or partly controlled, by the choices sent to the LaSt engine. As can be seen below, the first layout is selected, i.e. using the first of the two LaSt rules, and the variable `bgcolor' is set to a fixed value.

<choices>
<layout id="top_five_list" value="1">
<option name="bgcolor" value="#000088"/>
</layout>
</choices>

When processing the first rule, the insertion point depth will be tested, but as noted before, this test will not fail in our example. The result will be a HTML table with one row for each song. There will also be a free variable, the `fgcolor' variable, that will be bound by one of the default rules. The LaSt avoid rule constrain the possible values of this variable, requiring the color contrast to be good.

The engine output

The result of processing the LaSt rules will be returned in a special output structure, as seen in the example below. The generated output also contain all variable bindings, except the ones defined internally within the LaSt rules. In figure 1, an example of how the output could be rendered in a web browser is shown.

<last>
<choices>
<layout id="top_five_list" value="1">
<option name="bgcolor" value="#000088"/>
<option name="fgcolor" value="white"/>
</layout>
</choices>
<output>
<html>
<head>
  <title>A RadioWeb Toplist</title>
</head>
<body bgcolor="#ffffff">

<h1>A RadioWeb Toplist</h1>

<h2>Top five every week</h2>

    <table cellpadding="3" cellspacing="0" border="2">
    
      <tr bgcolor="#000088">
        <td><font color="white">
          <b>1</b>
        </font></td>
        <td><font color="white">
          <b>One</b>
        </font></td>
        <td><font color="white">
          <b>U2</b>
        </font></td>
        <td><font color="white">
          <b>Achtung Baby</b>
        </font></td>
      </tr>
    
      <tr bgcolor="#000088">
        <td><font color="white">
          <b>2</b>
        </font></td>
        <td><font color="white">
          <b>Across the border</b>
        </font></td>
        <td><font color="white">
          <b>Bruce Springsteen</b>
        </font></td>
        <td><font color="white">
          <b>The ghost of Tom Joad</b>
        </font></td>
      </tr>
    
      <tr bgcolor="#000088">
        <td><font color="white">
          <b>3</b>
        </font></td>
        <td><font color="white">
          <b>Most of the time</b>
        </font></td>
        <td><font color="white">
          <b>Bob Dylan</b>
        </font></td>
        <td><font color="white">
          <b>Oh Mercy</b>
        </font></td>
      </tr>
    
      <tr bgcolor="#000088">
        <td><font color="white">
          <b>4</b>
        </font></td>
        <td><font color="white">
          <b>Parklife</b>
        </font></td>
        <td><font color="white">
          <b>Blur</b>
        </font></td>
        <td><font color="white">
          <b>Parklife</b>
        </font></td>
      </tr>
    
      <tr bgcolor="#000088">
        <td><font color="white">
          <b>5</b>
        </font></td>
        <td><font color="white">
          <b>TwentyOne</b>
        </font></td>
        <td><font color="white">
          <b>The Cranberries</b>
        </font></td>
        <td><font color="white">
          <b>No need to argue</b>
        </font></td>
      </tr>
    
    </table>
  

<hr>

<em>This is a timeless document</em>

</body>
</html>
</output>
</last>

Image:autofigoutput.jpg

Figure 1. An example of how the output could be rendered in a web browser.


Go to the first, previous, next, last section, table of contents.