Example 07a – Programming GUI as Artifacts


An important example of artifacts encapsulating I/O functionalities is given by GUI artifacts, i.e. artifacts functioning as GUI components, enabling the interaction between human users and agents. Such artifacts allow to use Swing to define the structure of a GUI; then, they allow for defining – on the one side – operations corresponding to user actions on the GUI, so handling specific GUI events. Such operations generates signals or change some observable events to trigger agents observing the GUI; on the other side, they provide operations that can be possibly used by agents to change the GUI.

In the following example, a gui_tester agent creates and uses a GUI artifact to interact with the user.

MAS example07a_gui {

environment:
c4jason.CartagoEnvironment

agents:
gui_tester agentArchClass c4jason.CAgentArch;

classpath: "../../../lib/cartago.jar";"../../../lib/c4jason.jar";
}

To make it easier GUI artifact development, a cartago.tools.GUIArtifact base artifact is provided among the CArtAgO utility tools. The implementation of cartago.tools.GUIArtifact – which can be checked in CArtAgO source code – exploits await and blocking commands. The base artifact provides basic functionalities to link GUI events to the artifact operations.

In the following, MySimpleGUI GUI artifact creates a simple GUI with a text field and a button. Some GUI events – pressing the button, key stroke in the text field, closing the window – are linked to some artifact’s internal operations,
which in turn generate observable events to agents.

public class MySimpleGUI extends GUIArtifact {

private MyFrame frame;

public void setup() {
frame = new MyFrame();

linkActionEventToOp(frame.okButton,"ok");
linkKeyStrokeToOp(frame.text,"ENTER","updateText");
linkWindowClosingEventToOp(frame, "closed");

defineObsProperty("value",getValue());
frame.setVisible(true);
}

@INTERNAL_OPERATION void ok(ActionEvent ev){
signal("ok");
}

@INTERNAL_OPERATION void closed(WindowEvent ev){
signal("closed");
}

@INTERNAL_OPERATION void updateText(ActionEvent ev){
getObsProperty("value").updateValue(getValue());
}

@OPERATION void setValue(int value){
frame.setText(""+value);
getObsProperty("value").updateValue(getValue());
}

private int getValue(){
return Integer.parseInt(frame.getText());
}

class MyFrame extends JFrame {    

private JButton okButton;
private JTextField text;

public MyFrame(){
setTitle("Simple GUI ");
setSize(200,100);
JPanel panel = new JPanel();
setContentPane(panel);
okButton = new JButton("ok");
okButton.setSize(80,50);
text = new JTextField(10);
text.setText("0");
text.setEditable(true);
panel.add(text);
panel.add(okButton);
}

public String getText(){
return text.getText();
}

public void setText(String s){
text.setText(s);
}
}
}

Highlights:

  • Designing GUI artifacts: – a GUI artifact is defined by extending GUIArtifact, wrapping the definition and creation of the structure of the GUI – using the Swing API – and then linking/mapping Swing events into artifact’s internal operations by using linkXXXtoYYY primitives.

The agent creates an instance of the artifact and reacts to user actions on the GUI:

!test_gui.

+!test_gui
<-  makeArtifact("gui","c4jexamples.MySimpleGUI",[],Id);
focus(Id).

+value(V)
<- println("Value updated: ",V).

+ok : value(V)
<-  setValue(V+1).

+closed
<-  .my_name(Me);
.kill_agent(Me).

In particular, the agent reacts to the pressing of the button by setting a new value in the GUI; it prints a message on the console as soon as a new value is observed; it shutdown as soon as the window is closed.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>