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 {


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();

linkWindowClosingEventToOp(frame, "closed");


@INTERNAL_OPERATION void ok(ActionEvent ev){

@INTERNAL_OPERATION void closed(WindowEvent ev){

@INTERNAL_OPERATION void updateText(ActionEvent ev){

@OPERATION void setValue(int value){

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

class MyFrame extends JFrame {    

private JButton okButton;
private JTextField text;

public MyFrame(){
setTitle("Simple GUI ");
JPanel panel = new JPanel();
okButton = new JButton("ok");
text = new JTextField(10);

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

public void setText(String s){


  • 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:


<-  makeArtifact("gui","c4jexamples.MySimpleGUI",[],Id);

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

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

<-  .my_name(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>