Sample10_ServiceAgents.java
Created with JBuilder
import mads.core.*;
import java.util.*;

/**
 * <p><H1>Service Agents</H1></p>
 *
 * <p><B>Sample project no. 10 </B><BR\> </p>
 * As shown in the previous samples, the central idea in MADS is to encapsulate most of
 * the data and computations in remote agents called Service Agents. On request by the
 * main simulation (using the <I>findRemoteServiceFunction</I> function), such service
 * agents will return the requested data or computation result in the form of a service
 * function. This sample illustrates how a very simple service agent can be created.<BR\>
 * <UL>
 *    <LI> All simulations are based on a common agent type, called AServiceAgent.
 *         This fact is specified by writing "extends AServiceAgent" after the name of the simulation class.<BR\>
 *    <LI> To create a service agent, one must at least include the mads.core.* and java.util.*
 *         packages
 *    <LI> All the necesarry data is written as the return value of some specially defined
 *         function. If these functions are not implemented, the program will not compile.
 *
 * </UL>
 * <p>This sample will show how one can write a simple agent. The focus is not on the
 * functionality of the agent, but on the syntax. </p>
 */

public class Sample10_ServiceAgents extends AServiceAgent{

  /**
   * A very important aspect of the simulation design process is data management. As
   * real-world simulations deal with lots of data, hence lots of agents, one needs an
   * efficient management system to find which agents contain which data, how is this
   * data represented, where was it taken from and so on. In MADS, this information
   * can be found using the MADS Agent Browser Interface which, based on some search
   * criteria, queries all the available agents. In order to expose all relevant
   * information, each agent has to specify its name, functionality, parameters and so on.
   * This is done by re-writing a few specially designed functions, as shown below.
   */

  // Here we specify the service provided by the agent. This is different form the agent
  // name, which is actually the name of the class. One can specify this name by writing
  // it as the String returned by this function.
  public String getServiceName(){
   return "My sample agent";
 }

  // In the same way, we set the description. This a more thorough description of the
  // service which may include info on the internal operation, measurement units and so on.
  public String getDescription(){
    return "Sample agent that doesn't do anything useful";
  }

  // Here we specify the time range of the agent. If the time range is not relevant, one
  // should write "Any time range". Otherwise, one should write the years between which
  // the prediction is valid. Note that this time range just for informative purposes.
  public String getTimeRange(){
    return "2000 to 2050";
  }

  // If the agent requires parameters, one should write here the name and meaning of each one.
  // If no parameters are required, one should put nothing between the brackets. Note that
  // here we return not just a simple string but a vector of strings, hence the
  // "new String[]{...}" syntax.
  public String[] getParameters(){
    return new String[]{};
 }

 // Here one must specify references to the sources from where the data or the model
 // implemented by the agent were taken
 public String getReference(){
   return "Own work";
 }

  // If parameters were requested, it is a good practice to do validation. For example,
  // if one parameter would represent a price, it should be verified that the service
  // function received as parameter holds only positive values. If some error is detected,
  // one will signal it by returning the error message. If all is OK, one should return
  // an empty string.
  // For this case, there were no parameters, hence no validation, so we return the empty
  // string.
  public String validateParameters(ArrayList parameterServiceFunctionsVector){
    return "";
  }

  // Finally, this is the place where actual computation takes place. Having access to
  // the parameters sent by the simulation (from parameterServiceFunctionsVector array)
  // one performs the necessary computations and stores the result in the
  // myServiceFunction service function.
  // Here, we will just return some value, say 100, valid from 2000 to 2050.
  public void setServiceFunctionValues(ArrayList parameterServiceFunctionsVector, ServiceFunction myServiceFunction)
  {
    myServiceFunction.setBase(2000);
    myServiceFunction.setBase(50);

    for (int t = myServiceFunction.getBase(); t <= myServiceFunction.getBase() + myServiceFunction.getRange(); t++)
    {
      myServiceFunction.setValue(t, 100);
    }
  }

}


Sample10_ServiceAgents.java
Created with JBuilder