Sample4_RemoteServiceFunctions.java
Created with JBuilder
import mads.core.*;
import mads.servicefunctions.*;

/**
 * <p><H1>Remote service functions</H1></p>
 *
 * <p><B>Sample project no. 4 </B><BR\> </p>
 * This is the last tutorial concerning the usage of service functions. Here the user will see
 * the most important aspects regarding service functions and MADS in general. By contacting a
 * service providing agent residing on the agent server, a service function will be provided.
 * Usually, one or more parameters need to be provided as well, but there are cases when none
 * is needed. The remote agent will check to see if there are missing or invalid parameters. If
 * all is OK, the agent will process the parameters, do some computations and return the result
 * in the form of a service function. If there is some error, an error message will be returned
 * and the simulation will be terminated.<BR\>
 * The user should notice the following things:<BR\>
 * <UL>
 *    <LI> Service functions can be obtained from service providing agents which run on the server
 *    <LI> A service function request must contain the name of the agent and, usually, one or more
 *         parameters
 *    <LI> The name, functionality and parameter list can be found using the MADS Browser Utility
 *         web interface
 *    <LI> All the parameters are themselves service functions
 *    <LI> Error messages will be returned if the number of parameters is wrong or if the values
 *         of the parameters are not valid
 *
 * </UL>
 * <p>This simulation will request two service functions from two service agents. In the first case,
 *    no parameters are required. In the second case, four parameters must be provided.
 * </p>
 */

public class Sample4_RemoteServiceFunctions extends ASimulationAgent
{

  public void doWork(){
    /**
     * We will first obtain a service function that does not require any parameters. In this case,
     * the only thing we need to know is the name of the service agent.
     * For this sample, the 2000-2050 gasoline price prediction for the US market will be
     * requested. The name of the agent that provides it is "sags.gasoline.AGasoline_Hi". This
     * name can be found by using the MADS Browser Utility web interface.
     */

    // Obtaining and plotting the service function
    ServiceFunction sfGassolinePriceHi = findRemoteServiceFunction("sags.gasoline.AGasoline_Hi");
    sfGassolinePriceHi.plot("Price ($)", "US Gasoline Price, High price scenario");

    /**
     * We will now continue and request a service function that needs parameters. All the parameters
     * given must themselves be service functions, so instead of constants, constant service functions
     * have to be used and so on.
     * For this sample, the lifetime (20 years) electricity cost for a steam boiler will be computed.
     * For this computations, some further data must be given to the service providing agent in the
     * form of parameters. These parameters can be found using the MADS Browser Utility web interface.
     * The meaning of each parameter and what type of values are expected
     * (e.g. only positive, between 0 and 1) is also specified. In our case, there are 4 parameters:
     * 1. electricity price > 0
     * 2. electricity load profile > 0
     * 3. discount rate > 0
     * 4. operation time > 0
     */

    // Defining the parameters
    ServiceFunction sfElectricityPrice = new ConstantServiceFunction(10);
    ServiceFunction sfElectricityLoadProfile = new ConstantServiceFunction(100);
    ServiceFunction sfDiscountRate = new ConstantServiceFunction(0.05);
    ServiceFunction sfOperationTime = new ConstantServiceFunction(3650);

    // Requesting and plotting the service function
    // Note that all parameters must be included in the "new ServiceFunction[]{}" syntax

    ServiceFunction sfBoilerElectricityPrice = findRemoteServiceFunction(
                               "sags.Boilers.SteamBoiler.ASteamBoiler_ElectricityCost",
                               new ServiceFunction[]{sfElectricityPrice, sfElectricityLoadProfile, sfDiscountRate, sfOperationTime});
    sfBoilerElectricityPrice.plot("Price ($)", "Steam boiler electricity price");
    }

}



Sample4_RemoteServiceFunctions.java
Created with JBuilder