# Programming Agents by Their Social Relationships: A Commitment-Based Approach
## 5 2 COMM: An Infrastructure for Programming Social Relationships
### Programming JADE Agents with 2 COMM  
Broadly speaking, a JADE agent can be seen as a set of behaviors that can be executed when needed. In this sense, a behavior (or a set of behaviors) represents a strategy to achieve one of the agent's goals. In order to use 2 COMM, a JADE agent has to perceive the 2 COMM workspace. Observing the workspace, however, does not require the agent to proactively sense it because agents can register to the commitment artifacts of interest and then will be notified by these artifacts whenever a social event occurs. Agent programmers, thus, have to implement the behaviors either by adopting the _Entice_ schema and then taking the initiative of creating a commitment or by adopting the _Cooperate_ schema and then reacting to the social events that are relevant to their agents; that is, the social events the agent at hand is expected to handle. The _Entice_ schema is implemented by a behavior that, inside its _action()_ method, includes the execution of a create() operation on some commitment artifacts. The effect of such an operation is to bring the commitment state from null to active (specifically, conditional). Of course, this change of state is automatically notified by 2 COMM to all the agents that have focused on that commitment artifact. As concerns the _Cooperate_ schema, the agent programmer implements a behavior reacting to each event of interest by exploiting the _handleEvent()_ method that allows the programmer to add one (or more) behavior(s) to the agent depending on the event intercepted from 2 COMM, and on which of the two sub-cases of the _Cooperate_ schema is needed. For instance, if the social event is the detachment of a commitment involving the agent as debtor, _handleEvent()_ activates a behavior that will satisfy the very same commitment. The behavior body could encompass operations upon the environment (which, in turn, will make commitments evolve) or could also involve the creation of additional commitments. Conversely, if the social event is the creation of a commitment involving the agent as creditor, _handleEvent()_ triggers a behavior designed to detach such a commitment, being the agent interested in the consequent condition. Summing up, by means of _handleEvent()_, the occurrence of a social event relevant for the agent activates a corresponding behavior, properly implemented by the agent programmer. The following pseudo-code outlines the implementation of a JADE agent that follows the _Entice_ and _Cooperate_ schema. ```
1 publicclassMyBehaviorextendsSomeJadeBehaviorimplementsCommitmentObserver{
2 publicvoidaction(){
3 ArtifactIdart=Role.createArtifact(myCommitmentArtifactName,
4 MyCommitmentArtifactCl.class);
5 myRole=(SomeRole)(Role.enact(MyArtifact.ROLE,NAME,art,
6 new)JadeBehaviorPlayer(this,myAgent.getAID())));
7 myRole.startObseving(this);
8//Enticeschema:addbehavior(s)tocreatecommitmentCl
9//ECA:ONgoal_to_achieveIFlocal_conditionTHENcreate(C1)
10 myAgent.addBehavior(newMyEnticeBehaviorForCommitmentCl());
11//Enticeschema:addbehavior(s)tocreatecommitmentC2
12...
13]
14 publicvoidhandleEvent(SocialEvente,Object...args){
15 if(e.getElementChanged().getEIType()==SocialStateElementType.COMMIMENT){
16 Commitment c = (Commitment) e.getElementChanged(); //Cooperate schema as debtor of c //ECA:ONdetacted(c)IFlocal_conditionDO consequent. if(c.getDebtor().equals(myRole)&&c.getConsequent().equals(...)&&c.getLifeCycleStatus()==DETACHED){ //addbehavior(s)tostafyc myAgent.addBehavior(newMySatisfyBehaviour()); } //Cooperate schemaasceditorofc //ECA:ONcreate(c)\conditional(c)IFconsequentinterestingDO antecedent. \modelskip elseif(c.getCreditor().equals(myRole)&&c.getAntecedent().equals(...)&&c.getLifeCycleStatus()==CONDITIONAL){ //addbehavior(s)todenc myAgent.addBehavior(newMyDetachBehaviour()); } } }
```  
Summing up, the basic schema for implementing a JADE agent using social commitments requires two steps.