# Programming Agents by Their Social Relationships: A Commitment-Based Approach
## 5 2 COMM: An Infrastructure for Programming Social Relationships
### Modeling the Environment
In the same way, in order to satisfy \(c_{3}\), \(pln1\) has to detach \(c_{4}\). Only if every agent properly handles the social events related to its commitments, thereby following the two proposed schemas, the interaction will succeed, and the customer will receive the goods for which it has paid. #### 6.2.2 JaCaMo Agents

As shown in Section 5.2, the _Entice_ and _Cooperate_ schemas can be implemented in a Jason agent by defining a set of plans that, respectively, create commitments and react to social events. The following piece of code shows an excerpt of the \(trk1\) agent, implemented in Jason:

```
1!start. 2//Enticeschema:///BCA:ONneed_moneyIFcan_deliver_from_A_to_B
3//DOcreate(c_2:C(trk1,seller,pay(50,trk1)))
4+!start
5:focused(_,c2,C2 Artld)&
6:-enact(\"debtorC2\")[artifact_id(C2 Artld)];
7 createC2. 8//Cooperateschemaasdebtor:
9//ECA:ONdetached(c_{2})IFtrueDOdeliver_goods_at_B
10+cc(Mydl,_\"(\"(&goods,A)ANDpay(50,trk1))\",\"at(pkg,B)\",\"DETACHED\")
11:focused(_,log,LogArtld)&
12<-move(\"goods\",\"B\")[artifact_id(LogArtld)]. ```

The first plan (Line 5) is triggered as soon the execution starts. This plan implements the entice schema for \(c_{2}\). After enacting the debtor role in the artifact, the agent creates the commitment representing its offer. The second plan (Line 11) is an example of the _Cooperate_ schema in the debtor case; it is in fact triggered when commitment \(c_{2}\) is detached. The body of the plan, just like the analogous behavior in JADE, contains the operations to be performed over the environment to satisfy the detached commitment. In this case, the only operation to be performed is to move the goods to \(B\). As an example of the _Cooperate_ schema in the creditor case, let us consider the _customer_ agent that accepts an offer made by _seller_ by detaching commitment \(c_{1}\):

```
1//Cooperateschemaasceditor://ECA:ONconditional(\(c_{1}\))IFhuregoodsat_DDOpayforgoods
3+cc(_,Mydl,\"pay500(s)\",\"at(goods,D)\",\"CONDITIONAL\")
4:enactment_id(Mydl,c1,C1 ArtId)
5<-pay(500,\"seller\")[artifact_id(C1 ArtId)]
```

Of course, whenever an agent is involved in a commitment, either as debtor or creditor, the programmer has to pay attention in implementing at least the plans (or behaviors) that comply with the two proposed schemas. For instance, \(seller\) is involved in many commitments (i.e., \(c_{1}\), \(c_{2}\), and \(c_{3}\)), and hence, the programmer has to implement the plans that handle the progression of each of these commitments, by applying the _Entice_ or _Cooperate_ schema as necessary. ### A More Complex Scenario

An interaction devised in terms of social relationships explicitly created among the different actors enables a form of flexibility that neither message-based protocols, nor interaction components can obtain. These two approaches are basically prescriptive: they describe a specific sequence of steps through which an interaction can correctly evolve. In real-world cases, however, there exist alternative paths that could bring the same result. Prescriptive approaches either ignore these alternatives or encode them by making the resulting system potentially very complex or rigid. Interactions defined in terms of sets of social commitments that can be created and manipulated by agents do not impose any strict ordering of the messages (or actions). It follows that agents generally have greater flexibility in deciding how to operate. As an illustration, let us consider a scenario in which the _customer_ agent is not happy with the offer made by the _seller_ with the creation of \(c_{1}\). More in detail, let us assume that the customer is willing to pay at most 450 Euros for the goods and that it would like to pay only after the delivery. It could, then, make a counteroffer to \(seller\) through the creation of an additional commitment. Additionally, _customer_ could decide to release \(c_{1}\) (despite not being obliged to do so), to make clear that the original offer made by \(seller\) is unacceptable for it.