Working with Complex Environments

Here, we focus on how to work with more complex environments that may have many entities of diverse types. Such environments add more complexity because developers typically want to associate different agent types with different entity types.

Once an environment has been loaded, and an agent has been linked to it, you are able to retrieve a list of the entities in the environment. To get a list of the entities that are not linked to an agent, you can used theEIS API terms: freeEntities() orfreeEntities(id). The former term returns a list of free entities registered to the current default environment, and the latter term returns a list of free entities for the environment with the given identifier.

Once you know the names of the entities, you can check their type by using the EIS API terms:getEntityType(entity) and getEntityType(id, entity). As with the previous terms one assumes the default environment and the second allows you to specify the environment.

The above terms allow you to query an EIS environment to find out what entities exist and what their types are. The example below imagines a “soccer based environment” with four types of entity: goalkeeper, defender, midfielder, attacker. As can be seen different agents are created depending on the type of entity.

  package examples;
  
  agent Launcher {
      module EISAPI eis;
      module Prelude P;
      module System S;
      module Console C;
      
      initial !init();
      
      rule +!init() {
          eis.launch("soccerworld", "soccer.jar");
          eis.join("soccerworld");
          
          list l = eis.freeEntities();
          int i = 0;
          while (i < P.size(l)) {
              string entity = P.valueAsString(l, i);
              string type = eis.getEntityType(entity);
              if (type == "goalkeeper") {
                  S.createAgent("p"+i, "examples.Goalkeeper");
              } else if (type == "defender") {
                  S.createAgent("p"+i, "examples.Defender");
              } else if (type == "midfielder") {
                  S.createAgent("p"+i, "examples.Midfielder");
              } else if (type == "attacker") {
                  S.createAgent("p"+i, "examples.Attacker");
              } else {
                  C.println("Unknown type: " + type + " for entity: " + entity);
              }
              i = i + 1;
          }
      }
  }