Class GuiAdapter

java.lang.Object
edu.tufts.hrilab.gui.GuiAdapter
Direct Known Subclasses:
ActionProgrammerAdapter, BeliefAdapter, ChatAdapter, GoalManagerAdapter, GoalViewerAdapter, MapAdapter, TradeServiceAdapter

public abstract class GuiAdapter extends Object
An abstract class which defines required behavior for GUI adapters. These are the stipulations on GUI adapters:
  • They must construct with knowledge of their DIARC groups (which may be the empty set). This is enforced by requiring a super() constructor call with a groups argument (there is no default constructor). The constructor implementation must also be public to allow for reflective usage.
  • They must declare whether or not they provide TRADE services by implementing providesTradeServices().
  • They must provide their path root by implementing getPathRoot(). (This is an identifer used by the Handler to distribute messages. It is recommended to use a short one- or two-word descriptor in camelCase, e.g. goalViewer).

Like DiarcComponents, each GUI adapter implementation is given an SLF4J Logger instance called log.

During creation of a GuiAdapter instance, the following events take place:

  1. Construction of the object, which is done reflectively.
  2. Registration of TRADE services. This happens if and only if the implementing subclass' providesTradeServices() returns true.
  3. The init() function is called. Any startup code that interacts with TRADE services should be placed here.

Basic usage guidelines:

  • Use the sendMessage() method to send information to the frontend (client). It will automatically add the appropriate path mapping.
  • Use the handleMessage() method to receive information from the frontend (client).
  • To send one-time information on startup of the client component, the React component should include a useEffect() that sends a unique startup message. Then, this message should be handled in the handleMessage() callback in the GuiAdapter implementation. An example:
         
         // In the React component
         useEffect(() => {
             sendMessage(JSON.stringify({
                 path: path,
                 method: "startup"
             }));
         }, [path, sendMessage]);
         
         
         // In the GuiAdapter
         @Override
         protected void handleMessage(JSONObject message) {
             String method = message.getString("method");
             switch(method) {
                 case "startup" -> sendStartupMessage();
                 case "foo" -> foo();
                 case "bar" -> bar();
                 // ...
             }
         }
         
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected SortedSet<String>
    Set of groups the associated DIARC component belongs to
    protected org.slf4j.Logger
    Logger instance for subclasses.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Enforce constructing with a groups argument in subclasses.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <A extends GuiAdapter>
    A
    Construct an adapter by class and register its TRADE services.
    Get this adapter's URL path.
    protected abstract String
    Enforce implementation of path root getter for getPath().
    protected void
    handleMessage(com.google.gson.JsonObject message)
    Handle a message from the client.
    protected void
    Initialization after construction and after TRADE services are registered.
    protected abstract boolean
    Force implementing classes to declare whether they provide TRADE services.
    protected final void
    sendMessage(com.google.gson.JsonObject message)
    Send a message to the client.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • groups

      protected SortedSet<String> groups
      Set of groups the associated DIARC component belongs to
    • log

      protected org.slf4j.Logger log
      Logger instance for subclasses.
  • Constructor Details

    • GuiAdapter

      protected GuiAdapter(Collection<String> groups)
      Enforce constructing with a groups argument in subclasses. This ensures that implementing subclasses are initialized with information about their groups.

      Implementing classes must make this public so that reflective construction works.

      Parameters:
      groups - the groups that the associated DIARC component belongs to.
  • Method Details

    • createAndRegisterInstance

      public static <A extends GuiAdapter> A createAndRegisterInstance(Class<A> clazz, Collection<String> groups) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ai.thinkingrobots.trade.TRADEException
      Construct an adapter by class and register its TRADE services.
      Parameters:
      clazz - the class of adapter to instantiate.
      groups - the TRADE groups to assign to the adapter.
      Returns:
      the adapter.
      Throws:
      NoSuchMethodException - if the constructor does not exist.
      InvocationTargetException - if the constructor throws an exception.
      InstantiationException - if the class is abstract.
      IllegalAccessException - if the constructor is inaccessible.
      ai.thinkingrobots.trade.TRADEException - if the registration with TRADE fails.
    • init

      protected void init()
      Initialization after construction and after TRADE services are registered.
    • handleMessage

      protected void handleMessage(com.google.gson.JsonObject message)
      Handle a message from the client.
      Parameters:
      message - a JsonObject representing the message.
    • sendMessage

      protected final void sendMessage(com.google.gson.JsonObject message) throws ai.thinkingrobots.trade.TRADEException
      Send a message to the client.
      Parameters:
      message - a JsonObject representing the message.
      Throws:
      ai.thinkingrobots.trade.TRADEException
    • providesTradeServices

      protected abstract boolean providesTradeServices()
      Force implementing classes to declare whether they provide TRADE services. This is to decide whether or not to register any new instances of that class with TRADE.
      Returns:
      true iff the adapter provides TRADE services.
    • getPathRoot

      protected abstract String getPathRoot()
      Enforce implementation of path root getter for getPath(). This is usually based on the component's/adapter's name; e.g., the belief GUI's path root is belief.
      Returns:
      the path root as a String.
    • getPath

      public String getPath()
      Get this adapter's URL path.
      Returns:
      a string representation of the url path.