# MadKit FAQ

## Frequently Asked Questions, Tips and Tricks

The MadKit team

This FAQ may not be up to date, please get the last FAQ file at www.madkit.org

## Content

1. General
   1. Could I use MadKit to create a distributed application?
   2. Could I use MadKit to make Agent Based Simulation software?
2. Installation and running MadKit for the first time
   1. I could not install MadKit at all. Nothing happens when I try to to use the installer.
   2. I had a bug when installing MadKit on Linux.
   3. When I run MadKit and I click on some applications, MadKit hangs...
3. Desktop
   1. How could I build my own toolbar and launch my agents from it?
   2. I made a new script file and I don't see it in the MadKit explorator?
4. Agents development
   1. I did a 'requestRole' (or 'createGroup'..) in the constructor of the agent but it sends me error messages...
   2. When I send a message to another agent, it seems to take seconds to be taken into account by the receiver
5. Communications with distant kernels
   1. I would like to make remote agents communicate. Do I have to use Java sockets or RMI?
   2. I could send messages between agents in the same virtual machine, but nothing happens when I connected two distant kernels.
6. GUI
   1. I added a GUI to an agent as a JPanel. Could I change it dynamically. Can MadKit agents change their GUI on the fly?
   2. I would like to see the GUI of several agents in the same window. How could I do it?
7. Developping agents under MadKit
   1. I tried to run the tutorial, but I had errors when I put the jars into the autoload directory and I started the Desktop..
   2. Do I have to reload MadKit each time I compile my Java agents to use them?
8. Adapting and extending MadKit
   1. How could I start MadKit without the Desktop, with only one or two agents?
   2. I would like to embed MadKit into my application written in Java
   3. Can MadKit use agents written in C or C++

## Answers

### General

#### Could I use MadKit to create a distributed application

Yes, MadKit is an agent oriented middleware with which you may build distributed applications. One of the most interesting feature of MadKit is that distribution is done transparently. You do not have to take care of which agent is remote or not, Madkit, is handling all the necessary communications and connections to make agents situated on different platforms work together transparently. All you have to care is 1) Use the message passing technique of communication between agents, 2) Run MadKit on the two remote kernels (let us call the computers `toto.org` and `riri.net`), and open a `Communicator` agent on both of them. On one of them (`toto.org` for instance) you type the name of the other one (`riri.net` then) and the two will be connected. This means that the organization of `toto.org` will be seen by `riri.net` (and vice-versa) except for local groups and some communities, and that agents of both platforms may communicate. Test a demo program (for instance `ping-pong` or `travel`) with some agents on one kernel and some agents on the other one... See the User Guide and the description of the Communicator agent to get some details about it. You have to be aware that MadKit works on a 'peer to peer' basis, i.e. there is no "server" and "client". Every kernel is both a server and a client...

#### Could I use MadKit to build agent based simulations?

Yes MadKit has been widely used for agent based simulations. {{to be detailed}}. See the synchronous engine to get information about the basic tools and techniques of agent based simulation (scheduling and observation essentially), and see the Turtle kits manuals (TurtleKit and Turtle packs), a more "high level" development context for building simple reactive agents acting in a two dimension environment. Warbot is also a simulation context for testing coordination schemes, which is based on the synchronous engine.

## Installing and running MadKit for the first time

#### I have not been able to install MadKit at all. Nothing happens when I try to to use the installer

Do you have a Java virtual machine installed. MadKit uses a Java 1.3. It uses only the JRE (run time support) for its own purpose, but if you want to develop your agents in Java, you should have a JDK 1.3 (or higher, we use JDK 1.4 for our own purposes) installed...

#### I had a bug when installing MadKit to Linux, the install did not work

We have been aware of a very strange bug which may appear on some Linux distribution. We use the InstallAnywhere program for installing MadKit. But this Java based program does not use the command Java, and tries to guess what Java to run. Unfortunately some Linux distribution comes with a very old Java virtual machine (called Kaffe) and InstallAnywhere may use it by mistake. Thus, if the install does not work, check your Java installation. Be sure that you have at least a jdk 1.3 (type`java -version` to know the version of your JVM) installed and remove all obsolete JVM (and remove Kaffe!).

#### When I run MadKit and I click on some applications, MadKit hangs...

MadKit may hangs the first time you use it when you click on a Python agent. The first time Python is used, it creates a `cachedir` directory in the install directory of MadKit. This may takes between 10 s to 30 s dependings on your computer. But this will happen only once. Afterwards, you will be able to use Python agents quite smoothly and quickly.

### Desktop

#### How could I build my own toolbar and launch my agents from it?

This is quite simple. You just have to modify the `desktop.ini` file. See the User Guide, configuring the Desktop.

#### I made a new script file and I don't see it in the MadKit explorator?

For the moment you have to quit MadKit and start it back. The construction of the whole tree is done at launch time. This is a bug which will be solved very soon. We are really sorry about it...

### Agents development

#### I did a 'requestRole' (or 'createGroup'..) in the constructor of the agent but it sends me error messages...

You may only use the agent stuff (all the primitives concerning groups and roles) of an agent AFTER its launch time. Thus, the best way to put all these things is in its `activate()` method not in its constructor. When executing its constructor, and agent is still an object and not an agent yet.

#### When I send a message to another agent, it seems to take a lot of time to be taken into account by the receiver

This is not normal. Check the connection between the two computers, and use a demo program which uses the connection (for instance you connect the two computers using the communicator, and start a 'PingPong' agent on both computers).

If this test succeeds, then you must suppose that your agent is responsible for it. Maybe you use a `while` loop in the activate() or live() methods in which your agent is trapped.

If it fails, check the connection between the computers. Make sure that the version of MadKit in the two computers are not too different (version 2.x and versions 3.x are not compatible, but all versions 3.1bx are compatibles). Try some 'pings' between the two computers. Check if they are connected by a network (sometimes a cable is simply missing... ;-) this happens to everybody..).

### Communications with distant kernels

#### I would like to make remote agents communicate. Do I have to use Java sockets or RMI?

No you do not have to. MadKit is taking care of all the low level communications. You only have to concentrate on your application. If your agents may communicate inside the same kernel (the same MadKit application if you prefer), then they may communicate and interact through a network (local network or Internet). Sockets are handled by MadKit. RMI is good for client/server applications, but much too slow to be used a basis for building peer-to-peer applications. See the answer on distributed applications.

#### I could send messages between agents in the same virtual machine, but nothing happens when I connected two distant kernels.

This may be due to an important point: if your messages have to transport objects, these objects have to be declared as 'serializable'. Just make your objects implements the `java.io.serializable` interface.

Note: be aware that serialization of objects is time consuming and that serialization copy ALL the objects that are referenced by this object. Thus you should take care of not transporting "heavy" objects. Objects having only attributes of primitive types (such as numbers or booleans), or strings, or collections of such things are OK.

### GUI

#### I added a GUI to an agent as a JPanel. Could I change it dynamically. Can MadKit agents change their GUI on the fly?

No you cannot. An agent should get its GUI object permanently. But if your GUI is a JPanel, you may add and remove graphic component to this JPanel dynamically.

#### I would like to see the GUI of several agents in the same window. How could I do it?

### Developping agents under MadKit

#### I tried to run the tutorial, but I had errors when I put the jars into the autoload directory and I started the Desktop..

To compile the tutorial, you have two solutions: the first one consists in using the `compile.bat` (there is no `compile.sh`, but this should be staightforward to make one from the `compile.bat` for any Unix user). This file compiles the .java files, build the .class, makes a jar and copy this jar into the autoload directory.

The second one, which is the best one I think, is to use the Ant program, which is a kind a 'make' program written in Java. Ant is very popular amond Java developper and if you plan to develop all your agents in Java, you should consider Ant. We have placed a build.xml file (the kind of file ant uses) in the all tutorial folders. This file does the same as the compile.bat above, but does in a better way ;-). Open a shell (also called terminal, DOS or command prompt) go to your tutorial directory and type: `ant`. That's all: the java files are compiled, the jar is made and placed in the autoload folder automatically.

#### Do I have to reload MadKit each time I compile my Java agents to use them?

Unfortunately yes! Well, for the moment, If you want to use the Desktop, you have to reload MadKit each time you develop a recompile a Java agent, because Java does not like very much to remove and/or modify classes that have already been loaded. We work on that subject, and we hope that this will be possible in a near future....

### Adapting and extending MadKit

#### How could I start MadKit without the Desktop, with only one or two agents?

Start with console, using the console.bat or console.sh command and define a config.file (a config file is a XML file which contains a set of agents) which will contain the agents you want to launch at startup. See the Console container in the user guide to get more details and look at the `testconfig.cfg` file to get the structure of a configuration file (see also the User Guide, configuring the Desktop).

#### I would like to embed MadKit into my application written in Java

This is quite easy. If you do not use any of the provided agents (for instance only the communicator and your specific agents) you just have to put boot.jar with the jars of your application, and make your classpath application points to these files. You must have some directories (at least `libs/madkit` and `libs/support`) to contains all the jars of MadKit that you want to use (madkit.jar is required, but you may also use `communicator.jar` if you want to connect to other kernels, system.jar if you plan to use some of the system agents, etc.). You may need also to add some support files (ex: xerces.jar) as third party support.

Then, in your applications you must have a line to start and initialize MadKit. In this line you should pass the argument to MadKit and express, that for instance you want to use the agents GUI, and the communicator. In this case, this line should look like:

    		Vector c = new Vector();
    		c.add("--communicator-on");
    		c.add("--graphics");
    		c.add("--config");
    		c.add("myConfigFile.cfg");
           c.insertElementAt("madkit.platform.console.Booter",0);
           madkit.boot.Madkit.main((String[]) c.toArray(new String[0]));

The console booter is the simplest init program to use for launching MadKit. You may create your own by deriving your boot program from `madkit.kernel.AbstractMadkitBooter`. You may use a configuration file (here called myConfig.file) to launch agents while starting your application.

Caution: you do not need to (and should not do it!) add the jar files to your classpath. You only have to make sure that your classpath points to the boot.jar. You may use the config.file to launch agents at start time.

#### Can MadKit use agents written in C or C++

MadKit may be developped in all languages that are compiled into Java (there are a great number of such languages. See the site {{}} to get a list of all these languages. For the moment, MadKit comes with 4 scripting languages which are executed in the Java Virtual Machine: Scheme (Kawa), Jess (rule based language), BeanShell (Java interpreted) and Python (jython). Using the JNI (Java Native Interface) technique, it should be possible to develop agents written in C or C++. It is also possible to embed Java agents in C/C++ applications using the same technique, using JNI as a glue between the two worlds.