Xholon application

Ken Webb 2010-03-27T17:48:08Z

A Xholon application is software in which at least one object implements the IXholon interface and/or extends the Xholon class.

Simple Example 1

By this definition, which assumes the Java implementation of Xholon, the simplest possible Xholon application would be the following.

package org.primordion.user.app.simple;

import org.primordion.xholon.base.Xholon;
import org.primordion.xholon.base.IXholon;

public class One extends Xholon {

  public static void main(String[] args) {
    IXholon one = new One();
    one.println("This is simple Xholon app One.");
  }

}

There is nothing special about this code. It is just a simple Java program that can be executed as is.

Simple Example 2

In the next example, there are three Xholon nodes, arranged in a hierarchy. With this tree structure in place, the parent and two children can interact with each other.

package org.primordion.user.app.simple;

import org.primordion.xholon.base.Xholon;
import org.primordion.xholon.base.IXholon;

class HelloWorld extends Two {}

class Hello extends Two {}

class World extends Two {}

public abstract class Two extends Xholon {
  public void act() {
    for (int i = 0; i < this.depth(); i++) {
      print(' ');
    }
    print(this);
    if (this.hasParentNode()) {print(" hasParentNode("
        + this.getParentNode() + ")");}
    if (this.hasChildNodes()) {print(" hasChildNodes("
        + this.getNumChildren(false) + " " + this.getFirstChild() + ")");}
    if (this.hasNextSibling()) {print(" hasNextSibling("
        + this.getNextSibling() + ")");}
    print("\n");
    super.act();
  }
  
  public String toString() {
    String nodeName = this.getClass().getName();
    return (nodeName.substring(nodeName.lastIndexOf('.') + 1));
  }
  
  public static void main(String[] args) {
    IXholon helloWorld = new HelloWorld();
    IXholon hello = new Hello();
    IXholon world = new World();
    hello.appendChild(helloWorld);
    world.appendChild(helloWorld);
    helloWorld.act();
  }

}

This simple Xholon application will print out the following.

HelloWorld hasChildNodes(2 Hello)
 Hello hasParentNode(HelloWorld) hasNextSibling(World)
 World hasParentNode(HelloWorld)

Simple Example 3

The next simple example was suggested by Ralf Westphal, and is based on his C# implementation. When executed, this Java code prints the word "hello". Obviously this is overkill if all you want to do is print "hello", but it does show some of the basic features of Xholon without using the full Xholon runtime framework and without using any XML. The following Java code is a completely self-contained Xholon application.

package org.primordion.user.app.simple;

import org.primordion.xholon.base.IMessage;
import org.primordion.xholon.base.Message;
import org.primordion.xholon.base.Xholon;
import org.primordion.xholon.base.IXholon;

/**
 * Board
 */
class Board extends ClientService {}

/**
 * Client
 */
class Client extends ClientService {
  
  /**
   * a Xholon port
   */
  private IXholon outPin = null;
  
  /*
   * @see org.primordion.xholon.base.Xholon#act()
   */
  public void act() {
    /**
     * Send an asynchronous message through the outPin port.
     *  101     is an arbitrary message signal identifier
     *  "hello" is the message data
     *  this    is the sender of the message
     */
    outPin.sendMessage(101, "hello", this);
  }

  public IXholon getOutPin() {
    return outPin;
  }

  public void setOutPin(IXholon outPin) {
    this.outPin = outPin;
  }

}

/**
 * Service
 */
class PrintService extends ClientService {
  
  /*
   * @see org.primordion.xholon.base.Xholon#processReceivedMessage(org.primordion.xholon.base.IMessage)
   */
  public void processReceivedMessage(IMessage msg) {
    System.out.println(msg.getData());
  }

}

/**
 * ClientService abstract superclass
 */
public abstract class ClientService extends Xholon {
  
  /*
   * @see org.primordion.xholon.base.Xholon#sendMessage(int, java.lang.Object, org.primordion.xholon.base.IXholon)
   */
  public void sendMessage(int signal, Object data, IXholon sender) {
    /**
     * Normally sendMessage() would put a new Message into the message queue.
     * But the simple ClientService application does not use the Xholon runtime framework.
     * So instead, sendMessage() directly calls processReceivedMessage().
     */
    processReceivedMessage(new Message(signal, data, sender, this));
  }
  
  public static void main(String[] args) {
    // create some objects
    IXholon board = new Board();
    IXholon client = new Client();
    IXholon service = new PrintService();
    // organize the objects hierarchically; client and service are children of board
    client.appendChild(board);
    service.appendChild(board);
    // set up a port connection from client to service
    ((Client) client).setOutPin(service);
    // do something by recursively invoking all objects in the hierarchy
    board.act();
  }

}

return to main page