This example is a simple variation of the previous one, to show action failure.
As in the previous case, two agents create, use and observe a shared artifact, in this case a bounded counter:
MAS example01b_useobs { environment: c4jason.CartagoEnvironment agents: user2 agentArchClass c4jason.CAgentArch #1; observer agentArchClass c4jason.CAgentArch #1; classpath: "../../../lib/cartago.jar";"../../../lib/c4jason.jar"; }
The counter used by the agents has the following code:
public class BoundedCounter extends Artifact { private int max; void init(int max){ defineObsProperty("count",0); this.max = max; } @OPERATION void inc(){ ObsProperty prop = getObsProperty("count"); if (prop.intValue() < max){ prop.updateValue(prop.intValue()+1); signal("tick"); } else { failed("inc failed","inc_failed","max_value_reached",max); } } }
Highlights:
inc
action fails if the count already achieved the maximum value, specified as a parameter of init
. To specify the failure of an operation the failed
primitive is provided:
failed(String failureMsg)
failed(String failureMsg, String descr, Object... args)
descr(Object...)
describing the failure.Then, the user2
agent creates a bounded counter with 50 as bound and tries to increment it 100 times: as soon as the maximum value is reached, the action inc
fails and a repairing plan is executed:
!create_and_use. +!create_and_use : true <- !setupTool(Id); !use(Id). +!use(Counter) <- for (.range(I,1,100)){ inc [artifact_id(Counter)]; }. -!use(Counter) [error_msg(Msg),inc_failed("max_value_reached",Value)] <- println(Msg); println("last value is ",Value). +!setupTool(C): true <- makeArtifact("c0","c4jexamples.BoundedCounter",[50],C).
Highlights:
failed
on the artifact side are included in annotations in the repairing plan. In particular:
error_msg(Msg)
contains the failure message.inc_failed(...)
in the example.