Lesson 00: Hello World

This is the classic hello world example, and is based on the equivalent example in the CArtAgo for Jason guide, which can be found here.

The Code

The code for this example is given below:

  package astra.cartago.example0;
  
  agent HelloWorld {
      module Cartago cartago;
      module System system;

      rule +!main(list args) {
          cartago.startService();
          cartago.link();
	
          string Name = system.name();
          CARTAGO.println("Hello, world! by " + Name);
      }
  }

This code is equivalent to both the MAS configuration file and the source code outlined in the CArtAgO for Jason example.

The first two lines of the !main goal addition rule set up the cartago infrastructure and link the agent to that infrastructure respectively. The latter two lines implement the actual program (equivalent to hello_world.asl).

To provide an example that is closer to the Jason version, we can separate the program into two sub programs:

  package astra.cartago.example0;
  
  agent Main {
      module Cartago cartago;
      module System system;

      rule +!main(list args) {
          cartago.startService();
          system.createAgent("hello", "astra.cartago.example0.Hello");
      }
  }
  
  package astra.cartago.example0;
  
  agent Hello {
      module Cartago cartago;
      module System system;
      
      initial !hello();
      
      rule +!hello() {
          cartago.link();
          string Name = system.name();
          CARTAGO.println("Hello, world! by " + Name);
      }
  }

The first program create the CArtAgO node and then creates a “Hello” agent. The second program, which is the “Hello” agent program links the agent to the CArtAgo node, gets the name of the agent, and then prints out the hello world message.

This second program can be further refined as follows:

  package astra.cartago.example0;
  
  agent Hello {
      module Cartago cartago;
      module System system;
      
      initial !hello();
      
      rule +!hello() {
          cartago.link();
          CARTAGO.println("Hello, world! by " + system.name());
      }
  }