# Programming Agents by Their Social Relationships: A Commitment-Based Approach
## 5 2 COMM: An Infrastructure for Programming Social Relationships
### Modeling the Environment  
Recalling Section 3, the interaction between the different agents in the scenario can be modeled by the following set of commitments:  
\[ \begin{array}{l}\mathbf{c_{1}}:\mathsf{C}(\mathit{seller},\mathit{customer}, \mathit{pay}(500,\mathit{seller}),\mathit{at}(\mathit{goods},D))\\ \mathbf{c_{2}}:\mathsf{C}(\mathit{trk1},\mathit{seller},\mathit{pay}(50,\mathit{ trk1})\wedge\mathit{at}(\mathit{goods},A),\mathit{at}(\mathit{goods},B))\\ \mathbf{c_{3}}:\mathsf{C}(\mathit{pln1},\mathit{seller},\mathit{pay}(200,\mathit{ pln1})\wedge\mathit{at}(\mathit{goods},B),\mathit{at}(\mathit{goods},D))\\ \mathbf{c_{4}}:\mathsf{C}(\mathit{trk3},\mathit{pln1},\mathit{pay}(50,\mathit{ trk3})\wedge\mathit{at}(\mathit{goods},C),\mathit{at}(\mathit{goods},D))\end{array} \]  
For each commitment, a corresponding commitment artifact, embodied in the environment, is defined in 2 COMM. The following piece of code shows an excerpt of the artifact that maintains \(c_{1}\):```
1 publicclassC1 extendsCommitmentArtifact{
2
3 privateRoleIddebtor;
4 privateRoleIdcreditor;
5 privateCommitmentc;
6
7 static{
8 addEnabledRole(\"debtorC1\",DebtorC1.class);
9 addEnabledRole(\"debtorC1\",CreditorC1.class);
10]
11
12//CreditoranddebtorroleenactmentmethodsforJADEandJaCaMo
13@OPERATION
14 protectedvoidenact(StringroleName,IPlayerp,OpFeedbackParam<Role>r){...}
15@OPERATION
16 protectedvoidenact(StringroleName){...}
17
18//Commitmentmanipulationoperations
19@OPERATIONpublicvoidcreateC1(){
20 RoleIdagent=getRoleIdByRoleName(getCurrentOpAgentId().getAgentName());
21 if(!agent.equals(debtor)){
22 failed(\"Onlythedebtorcancreateacommitment\");
23}
24c=newCommitment(debtor,creditor,
25 newFact(\"pay\",\"500\",debtor.toString()),newFact(\"at\",\"pkg\",\"D\"))createCommitment(c);
26}
27@OPERATIONpublicvoidreleaseC1(){
28 RoleIdagent=getRoleIdByRoleName(getCurrentOpAgentId().getAgentName());
29 if(!agent.equals(creditor)){
30 failed(\"Onlythecreditorcanreleaseacommitment\");
31}
32 releaseCommitment(c);
33
34
35@OPERATIONpublicvoidcancelC1(){
36 RoleIdagent=getRoleIdByRoleName(getCurrentOpAgentId().getAgentName());
37 if(!agent.equals(debtor)){
38 failed(\"Onlythedebtorcanancelacommitment\");
39}
40 cancelCommitment(c);
41}
42
43
44
45//Factassertionoperations
46@OPERATIONpublicvoidassertPay(Stringpred,Stringwho){
47 assertFact(newFact(\"pay\",pred,who));
48
49@OPERATIONpublicvoidassertAt(Stringwhat,Stringwhere){
41 assertFact(newFact(\"at\",what,where));
40}
41}
42//Debtoroleclass
43 publicclassDebtorC1 extendsCommitmentRole{
44@RoleActionpublicvoidcreateC1(){
45 try{
46 doAction(this.getArtifactId(),newOp(\"createC1\"));
47}catch(ActionFailedException|CartagoExceptione){...}
48}
49@RoleActionpublicvoidcancelC1(){...}
50...
51}
52//Creditoroleclass
53 publicclassCreditorC1 extendsCommitmentRole{
```
*@RoleActionpublicvoidreleaseC1(){
*try{
*doAction(this.getArtifactId(),newOp(\"releaseC1\"));
*catch(ActionFailedException|CartagoExceptione){...}
*}
*...
*}
*...
*}
*}
*}
```
The commitment artifact includes enact methods for JADE and JaCaMo agents (omitted for simplicity), which allow the agents to adopt the debtor and creditor roles of the commitment. Role enactment enables the execution of the commitment operations made available by the artifact. In particular, the artifact provides two debtor-dedicated operations (see Lines 19 and 35) to create and cancel the commitment and one creditor-dedicated operation to release it (see Line 28). When such operations are executed, the 2 COMM infrastructure takes care of updating the commitment state and the artifact observable state. Similarly, agents observing the artifact are notified about the occurrence of the social event resulting from the execution of the operation (e.g., the commitment creation). As inner classes, at Lines 52 and 63, the artifact provides two commitment roles amounting to the debtor and creditor, through which, upon enactment, the agents can execute the allowed operations on the artifact.