Next: , Previous: , Up: Programs   [Contents][Index]


3.9.6 Action Definitions

An action definition consists of a sequence of rules of the form

Head :: Commit ~> Actions

where Head is a simple compound term, Commit is a simple conjunction, and Actions is a an action sequence.

Both the :: Commit and ~> Actions parts of the rule are optional. The heads of each rule of an action have the same functor and arity.

The semantics of action definitions is the nearly the same as for relation definitions. The first difference is that at least one of the elements of the Actions sequence is an action which typically has a side effect such as writing, reading, sending a message or updating the database. The second difference is that backtracking is only local to the rule and so has equivalent semantics to the Prolog rule

Head :- Commit, !, Actions

Also, for each action definition, a catchall clause is added to the end of the defined clauses which throws an exception. This means that, if a call to an action does not match any of the defined clauses then an exception will be thrown as opposed to failure occurring (as would happen in a relation definition). This is also true for builtin actions. All builtin actions are also deterministic (i.e. no choice points are created by calling a builtin action. A consequence of this is that user defined actions also have this property. That is all actions are deterministic and all actions throw an exception rather than failing if no matches are found.

As examples, the definitions of the actions ask_query and handle_response are given below.

act ask_query(atom,  ??term, list(??term), handle)
ask_query(QId,Ans,QList,Ag) ~>
    ask(QId,Ans,QList) to Ag; Reply from Ag; 
    handle_response(QId,Ag,Reply,Ans)

act handle_response(atom,handle, ??term, ??term)
handle_response(QId,_,tell(QId,ans(Ans)),Ans) :: true ~> {}
handle_response(QId,Ag,tell(QId,Reply),_) ~> 
     writeLine(['Agent ',Ag,' responded ',Reply,' to query ',QId]); fail

Next: , Previous: , Up: Programs   [Contents][Index]