Package edu.tufts.hrilab.gui
Class GuiAdapter
java.lang.Object
edu.tufts.hrilab.gui.GuiAdapter
- Direct Known Subclasses:
ActionProgrammerAdapter,BeliefAdapter,ChatAdapter,GoalManagerAdapter,GoalViewerAdapter,MapAdapter,TradeServiceAdapter
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 agroupsargument (there is no default constructor). The constructor implementation must also bepublicto 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 theHandlerto 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:
- Construction of the object, which is done reflectively.
- Registration of TRADE services. This happens if and only if the
implementing subclass'
providesTradeServices()returnstrue. - 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 appropriatepathmapping. - 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 thehandleMessage()callback in theGuiAdapterimplementation. 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(); // ... } }
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedGuiAdapter(Collection<String> groups) Enforce constructing with agroupsargument in subclasses. -
Method Summary
Modifier and TypeMethodDescriptionstatic <A extends GuiAdapter>
AcreateAndRegisterInstance(Class<A> clazz, Collection<String> groups) Construct an adapter by class and register its TRADE services.getPath()Get this adapter's URL path.protected abstract StringEnforce implementation of path root getter forgetPath().protected voidhandleMessage(com.google.gson.JsonObject message) Handle a message from the client.protected voidinit()Initialization after construction and after TRADE services are registered.protected abstract booleanForce implementing classes to declare whether they provide TRADE services.protected final voidsendMessage(com.google.gson.JsonObject message) Send a message to the client.
-
Field Details
-
groups
Set of groups the associated DIARC component belongs to -
log
protected org.slf4j.Logger logLogger instance for subclasses.
-
-
Constructor Details
-
GuiAdapter
Enforce constructing with agroupsargument 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
Enforce implementation of path root getter forgetPath(). This is usually based on the component's/adapter's name; e.g., the belief GUI's path root isbelief.- Returns:
- the path root as a
String.
-
getPath
Get this adapter's URL path.- Returns:
- a string representation of the url path.
-