Scripts

Ken Webb 2010-02-25T02:43:18Z

A Xholon script is a small self-contained object that does one particular task. It is typically pasted into a Xholon tree at runtime. A script may be written in any language supported by the Java Virtual Machine (JVM), but for now only Java and Groovy have been tested.

The following is a simple script that renumbers the nodes of a subtree in a running Xholon application. To run this script, the user would copy the text <Renumber/> into the system clipboard for example from an external text editor or from this wiki page. He or she would then right click a node in the Xholon GUI, and select "Paste Last Child". All nodes in the subtree starting with the selected node would be renumbered.

public class Renumber extends XholonScript {

  private int nextId = 0;

  public void postConfigure() {
    nextId = getParentNode().getId();
    getParentNode().visit(this);
    removeChild();
  }

  public boolean visit(IXholon visitee) {
    visitee.setId(nextId++);
    return true;
  }

}

The entire text of the following inline Groovy script can be pasted as the last child of any node in any Xholon application. It will count and print out the number of nodes in the subtree.

<Tabulator implName="lang:groovy:inline:"><![CDATA[
import org.primordion.xholon.script.XholonScript
import org.primordion.xholon.base.IXholon

class Tabulator extends XholonScript
{
  int count = 0

  public void postConfigure() {
    getParentNode().visit(this)
    println("There are ${count} nodes in the subtree rooted by ${getParentNode()}.")
    removeChild()
  }

  public boolean visit(IXholon visitee) {
    //println("visitor ${this} visitee ${visitee}")
    count++
    return true
  }

}

// the following object will be returned from this script
new Tabulator()
]]></Tabulator>

The following diagram demonstrates the process of running an inline script in a Xholon application. The text of the script is selected (shown highlighted in the diagram), and it is copied to the clipboard (Ctrl-C). The "structure_2" node is right-clicked, and "Paste Last Child" is selected from the menu. The Groovy compiler compiles the script (if not already compiled), and Xholon calls the postConfigure() method of the script. The script uses the visit method (the Visitor pattern) that is built into Xholon, and counts each visitee.

return to main page