
package mTripleApl.micro;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import mTripleApl.*;
import java.util.*;

public class HelloWorld
    extends MIDlet
    implements CommandListener {

  Form form;

  public void startApp() {

    // create micro from and display
    this.form = new Form("My First 3APL-M Application");
    this.form.setCommandListener(this);
    this.form.addCommand(new Command("Exit", Command.EXIT, 1));
    TextField textField = new TextField("", "", 30, TextField.ANY);
    this.form.append(textField);
    Display.getDisplay(this).setCurrent(this.form);

    // create tApl agent
    Agent ag = new Agent("micro");
    ag.setTrace(3);

    // load knowledge
    ag.addActuator("Print(X)", new TextFieldAct(textField));
    ag.addPlanRule("print <- TRUE | Print('hello world')");
    ag.addGoal("print");

    // deliberate
    ag.deliberate();
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
    notifyDestroyed();
  }

  public void commandAction(Command command, Displayable displayable) {
    if (command.getCommandType() == Command.EXIT) {
      destroyApp(true);
    }
  }
}

/**
 * ACTUATOR TO PRINT INTO A FIELD
 */
class TextFieldAct
    implements ActuatorInterface {
  TextField field;

  // create: associate to field
  public TextFieldAct(TextField field) {
    this.field = field;
  }

  // called from Deliberation engine
  public boolean actuator(String[] data) {
    if (data.length > 0) {
      this.field.setString(data[0]);
    }
    return true;
  }
}

