The Main Agent

This lesson introduces some information about a default goal that is generated when an ASTRA program is run, and about how to change the default name of the first agent.

The !main(…) goal

Up to now, we have been triggering the initial behaviour of the agent by specifying an initial goal !init(). This is not strictly necessary because ASTRA generates an initial goal automatically. The form of this goal is !main(list args) and it is the ASTRA equivalent of the public static void main(String[] args) { } method.

Given that we can easily declare initial goals, why is this generated goal necessary? It is no coincidence that the format of the goal closely resembles the main(…) method format. In fact, when you run an agent. you are actually running a main method associated with the class that implements the agent program that you are running. That is, if you create an agent example.MyAgent, then the ASTRA compiler creates a Java class with the same name, in the same package that contains the Java representation of the code you wrote. This Java class is then automatically compiled into a .class file. The Java file is created in the “gen” source folder – which you can explore via Eclipse.

When compiling the ASTRA code, the corresponding generated Java code includes a main method for each ASTRA class, allowing you to run any of the ASTRA classes directly. The !main(…) goal is constructed by generating an ASTRA list of any strings that are passed as arguments when starting an ASTRA program. For example, the following command line would create a goal of the form !main([“43”, “22”]):

  java -cp astra-apis.jar;astra-interpreter.jar;mas-acre.jar example.MyAgent 43 22

This can also be done by adapting the Eclipse Run Configurations.

The above initial goal is only generated for the agent that starts the program (in this case the instance of example.MyAgent). Any other agents that are created by this (or other agents) do not receive this event.

NOTE: The above example requires 3 jar files which can be found in the “astra” folder in the Eclipse Workspace

Changing the default agent name

The default name of the agent that starts a program is “main”. This, however can be overrode by setting a system property. Specifically, you can set the “astra.name” property to any string you wish and this will be used in place of “main”. For example ,the following command creates an agent called “jake”:

  java -Dastra.name="jake" -cp astra-apis.jar;astra-interpreter.jar;mas-acre.jar example.MyAgent 43 22

Again, JVM arguments can also be set in in the Eclipse Run Configurations.