Example 09 – Java data-binding


Since CArtAgO data model is based on Java object (POJO), a set of internal actions is provided on the agent side to create/manipulate Java objects. In particular:

  • cartago.new_obj(ClassName,ParamList,?ObjRef): instantiate a new object of the specified class, retrieving its reference
  • cartago.invoke_obj{ObjRef,MethodName{(Params)},RetValue}: call a method, possibly getting the return value
  • cartago.invoke_obj{ClassName,MethodName{(Params)},RetValue}: call a static method, possibly getting the return value

It follows a simple example:

MAS example09_java_data_binding {

  environment:
  c4jason.CartagoEnvironment

  agents:
  java_data_binding_tester agentArchClass c4jason.CAgentArch;

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

The agent source code:

!test_java_api.

+!test_java_api
  <- cartago.new_obj("c4jexamples.FlatCountObject",[10],Id);
     cartago.invoke_obj(Id,inc);
     cartago.invoke_obj(Id,getValue,Res);
     println(Res);
     cartago.invoke_obj("java.lang.System",currentTimeMillis,T);
     println(T);
     cartago.invoke_obj("java.lang.Class",
                   forName("c4jexamples.FlatCountObject"),Class);
     println(Class).

Where the class FlatCountObject is defined as follows:

package c4jexamples;

public class FlatCountObject {

  private int count;

  public FlatCountObject(int v){
    count = v;
  }

  public FlatCountObject(){
    count = 0;
  }

  public void inc(){
    count++;
  }

  public void inc(int dv){
    count+=dv;
  }

  public int getValue(){
    return count;
  }

}

Highlights:

  • No sharing: Java objects are not meant to be shared, each agent has its own Java library, managing its own Java objects
  • Object references: Object references are kept track by means of atoms with a specific signature. When used outside the Java related internal actions, they are treated as normal atoms. When used in Java related internal actions, they refer to objects.
  • Null value: Underscore (_) is used to represent the null value.

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>