This example shows the basics about artifact creation and use, including observation. Two agents create, use and observe a shared artifact.

MAS example01_useobs {

  environment:
  c4jason.CartagoEnvironment

  agents:
  user user agentArchClass c4jason.CAgentArch #1;
  observer observer agentArchClass c4jason.CAgentArch #1;

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

The user agent creates a c0 artifact of type c4jexamples.Counter and then uses it twice, executing the inc action (operation) two times:

!create_and_use.

+!create_and_use : true
  <- !setupTool(Id);
     inc;
     inc [artifact_id(Id)].

+!setupTool(C): true
  <- makeArtifact("c0","c4jexamples.Counter",[],C).

Hightlights:

The Counter artifact is characterised by a single inc operation and a count observable property, updated by the operation. The operation also generates a tick signal.

package c4jexamples;

import cartago.*;

public class Counter extends Artifact {

  void init(){
    defineObsProperty("count",0);
  }

  @OPERATION void inc(){
    ObsProperty prop = getObsProperty("count");
    prop.updateValue(prop.intValue()+1);
    signal("tick");
  }
}

Highlights

Finally, an observer agent observes the counter and prints on standard output a message each time it perceives a change in count observable property or a tick signal:

!observe.

+!observe : true
  <- ?myTool(C);  // discover the tool
     focus(C).

+count(V)
  <- println("observed new value: ",V).

+tick [artifact_name(Id,"c0")]
  <- println("perceived a tick").

+?myTool(CounterId): true
  <- lookupArtifact("c0",CounterId).

-?myTool(CounterId): true
  <- .wait(10);
     ?myTool(CounterId).

Highlights