Example 03 – Operations with output parameters


Operations can have output parameters, i.e. parameters whose value is meant to be computed by the operation execution. On the agent side such parameters are managed as action feedbacks. At the API level, output parameters are represented by the class OpWithFeedbackParam<ParamType>, where ParamType must be the specific type of the output parameter.

The class provides then a set method to set the output parameter value. In the following example, an agent creates and uses a Calc artifact, by executing operations with output parameters:

MAS example03_output_param {

  environment:
  c4jason.CartagoEnvironment

  agents:
  calc_user agentArchClass c4jason.CAgentArch;

  classpath: "../../../lib/cartago.jar";"../../../lib/c4jason.jar";
}

The Calc used by the agents has the following code:

public class Calc extends Artifact {

  @OPERATION
  void sum(double a, double b, OpFeedbackParam sum){
    sum.set(a+b);
  }

  @OPERATION
  void sumAndSub(double a, double b, OpFeedbackParam sum,
                                     OpFeedbackParam sub){
    sum.set(a+b);
    sub.set(a-b);
  }
}

The source code of the agent follows:

!use_calc.

+!use_calc
  <- makeArtifact("myCalc","c4jexamples.Calc",[]);
     sum(4,5,Sum);
     println("The sum is ",Sum);
     sumAndSub(0.5, 1.5, NewSum, Sub);
     println("The new sum is ",NewSum," and the sub is ",Sub).

Highlights:

  • On the agent side output parameters are denoted by bound variables, which are bound with operation execution.
  • An operation can have any number of output parameters.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>