<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>A Methodology to Extend Imperative Languages with AgentSpeak Declarative Constructs</article-title>
      </title-group>
      <contrib-group>
        <aff id="aff0">
          <label>0</label>
          <institution>Corrado Santoro University of Catania - Dept. of Mathematics and Computer Science Viale Andrea Doria</institution>
          ,
          <addr-line>6 - 95125 - Catania</addr-line>
          ,
          <country country="IT">ITALY</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Daniele Marletta Scuola Superiore di Catania Via S. Nullo 5/i - 95123 - Catania</institution>
          ,
          <country country="IT">ITALY</country>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>Loris Fichera University of Catania - Engineering Faculty Viale Andrea Doria</institution>
          ,
          <addr-line>6 - 95125 - Catania</addr-line>
          ,
          <country country="IT">ITALY</country>
        </aff>
        <aff id="aff3">
          <label>3</label>
          <institution>Vincenzo Nicosia Scuola Superiore di Catania - Laboratorio sui Sistemi Complessi Via S. Nullo 5/i - 95123 - Catania</institution>
          ,
          <country country="IT">ITALY</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>-This paper presents a novel technique for the support of declarative constructs of AgentSpeak in a imperative language. Starting from an analysis of the characteristics and semantics of AgentSpeak, as well as the requirements of an AgentSpeak implementation, the paper proposes a framework which, by exploiting object-orientation and operator overloading, is able to enrich an imperative language with declarative constructs suitable to represent and write AgentSpeak programs. A concrete implementation of the proposed framework is then given by presenting PROFETA (Python RObotic Framework for dEsigning sTrAtegies), a tool written in Python which adds to this language the capability to write and execute AgentSpeak plans. A case-study shows how PROFETA can be used to easily write strategies for an autonomous mobile robot, designed by the authors for the “Eurobot 2010” international robotic competition.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>I. INTRODUCTION</p>
      <p>
        Software agents are an abstraction for autonomous entities
which have a certain amount of knowledge, interact with
an environment and play a given role in order to achieve a
goal. They have been used in the last decade as a reference
model for the solution of a variety of problems in different
fields, from automatic reasoning to data mining, from search
in semantic web to simulation of social behaviours. The
adaptability of agent–oriented modelling and the possibility
to represent different problems as agent–based systems have
produced a proliferation of agent models, such as reflex–
based agents, goal–based agents, utility–based agents,
learning agents [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ], [
        <xref ref-type="bibr" rid="ref20">20</xref>
        ], [
        <xref ref-type="bibr" rid="ref14">14</xref>
        ], [
        <xref ref-type="bibr" rid="ref16">16</xref>
        ], each of them with peculiar
characteristics which make it useful for the solution of a
specific class of problems. At the same time many agent–
oriented programming languages (Soar [
        <xref ref-type="bibr" rid="ref21">21</xref>
        ], NetLogo [
        <xref ref-type="bibr" rid="ref17">17</xref>
        ],
3APL [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ], GOAL [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ]) and agent–based platforms (JADE [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ],
MASON [
        <xref ref-type="bibr" rid="ref10">10</xref>
        ], Repast [
        <xref ref-type="bibr" rid="ref18">18</xref>
        ], Swarm [
        <xref ref-type="bibr" rid="ref19">19</xref>
        ]) have been developed
in the last decade, in order to give programmers powerful and
easy–to–use tools to model, develop and maintain agent–based
software.
      </p>
      <p>
        An increasing interest has been recently devoted to
exploit goal–based agent models for the realization of
intelligent autonomous systems [
        <xref ref-type="bibr" rid="ref11">11</xref>
        ], [
        <xref ref-type="bibr" rid="ref15">15</xref>
        ]. One of the proposed
paradigms for goal–oriented agents is the Belief–Desire–
Intention (BDI) [
        <xref ref-type="bibr" rid="ref13">13</xref>
        ], which models the knowledge as a set of
beliefs about environment and internal state of the agent, then
represents as desires the goals to be achieved, which expand
into a set of intentions, i.e. atomic actions to be performed
in order to achieve a desire. There exists a reference abstract
language for BDI agents, which is called AgentSpeak(L) [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ],
and defines a declarative syntax to express desires and
intentions in a way which is much similar to rule-based logic
programming: beliefs are treated as facts while desires and
intentions respectively correspond to conditions and body
of rules. The only existing implementation of AgentSpeak
semantic is Jason [
        <xref ref-type="bibr" rid="ref9">9</xref>
        ], which is an interpreter, written in Java,
for an extended AgentSpeak syntax written. Jason lets the
programmer write its AgentSpeak statements in a separate file
and requires actions and event handlers to be implemented
by Java classes. Unfortunately, all the AgentSpeak program
is completely separated from action implementation, while it
ag ::=
bs ::=
at ::=
ps ::=
p ::=
te ::=
cd ::=
h ::=
l ::=
f ::=
g ::=
u ::=
bs ps
at1, . . . , atn.
      </p>
      <p>P(t1, . . . , tn)
p1, . . . , pn.
te : cd &lt; − h.
+at | − at | + g | − g
true | l1 &amp; . . . &amp; ln
true | f1 ; . . . ; fn
at | not at
A(t1, . . . , tn) | g | u
!at | ?at
+at | − at
would be very useful to preserve a declarative AgentSpeak
model into the same programming environment.</p>
      <p>
        This paper shows that it is possible to embed an
AgentSpeak semantic into any imperative language which allow
operator overloading, and proposes an example of such
embedding in Python. The developed tool is called PROFETA
(for Python RObotic Framework for dEsigning sTrAtegies),
and has been successfully used to implement agent–based
intelligent autonomous systems to drive robots in dynamic
environments [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ].
      </p>
      <p>The paper is organised as follows. Section II provides an
overview of AgentSpeak(L) syntax and semantics. Section III
explains the methodology used to embed AgentSpeak(L)
semantic into imperative languages through operator
overloading. Section IV describes the implementation of PROFETA.
Section V illustrates a case-study of PROFETA in a real
application. Section VI reports conclusions and future works.</p>
      <p>II. AGENTSPEAK: REQUIREMENTS FOR ITS</p>
      <p>IMPLEMENTATION
A. Overview of AgentSpeak</p>
      <p>
        AgentSpeak(L) [
        <xref ref-type="bibr" rid="ref13">13</xref>
        ], [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ] (or simply AgentSpeak) is an
abstract declarative language designed many years ago to
program BDI agents. According to its basic syntax, first
introduced in [
        <xref ref-type="bibr" rid="ref13">13</xref>
        ], [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ] and then extended in [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ], which is
reported in Figure 1, an agent program is made of a set of
beliefs and a set of plans.
      </p>
      <p>Beliefs represent the knowledge of the agent and are
expressed using atomic formulae made of a predicate symbol
P and a set of zero or more ground terms. The same syntax
(predicate with ground terms) is also used to represent goals,
which are the AgentSpeak abstractions for desires. Two types
of goals are provided: (i) achievement goal, expressed as
!at, meaning that the agent wants to make true formula at
(i.e., ensure that at is a known belief); and (ii) test goal,
expressed as ?at, intended to verify whether formula at is
true (i.e., check if at is a known belief).</p>
      <p>Plans are the tasks that the agent has to execute and are
expressed by means of rules in the form te : cd &lt; − h;
here:
• te is the event triggering the rule; it can be the addition
or removal of a belief (+at or −at), or the request to
achieve (or no more achieve) a goal (+!at or −!at);
• cd is the condition1, that is made of a set of beliefs
that must be all true in order to trigger the plan. The
specified beliefs may contain ground terms or variables,
and express the knowledge the agent must have when the
event te fires in order to trigger the plan.
• h is a set of atomic actions which are executed, in
sequence, following plan triggering. They include, once
again, addition or removal of a belief (+at or −at),
request to achieve or no more achieve a (sub-)goal (+!at
or −!at), or the execution of a user-defined action. With
the exception of the last case (user-defined action), all the
other actions, when executed, generate the relevant trigger
event causing the possible selection of another plan.</p>
      <p>
        As the reader can understand, an AgentSpeak program is a
set of rules in the form event-condition-action(s); events may
come from the perception of the agent’s environment (in this
case they are called external events) or be generated by an
action (internal events), such as the achievement of a sub-goal.
On the basis of event type, the way in which it is processed
differs, according to the specific semantics of the language
which is described in [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ], [
        <xref ref-type="bibr" rid="ref9">9</xref>
        ], [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ].
      </p>
      <p>An example of an AgentSpeak program running in a mobile
robot is given in Figure 2. Let us consider that the robot
aims at grabbing two types of objects, balls and cylinders, and
that, to accomplish this task, it has two different mechanical
arms, one for each type of object. A camera, with a proper
artificial vision software, is used to detect the presence of
an object to be picked. Two predicates are used for beliefs:
object_seen, which is asserted by the artificial vision
software when an object of a certain type is detected in
a given (x, y) position, and arm_deployed, representing
which one of the two arms is currently in the deployed
configuration (and thus able to pick the object of the same
kind). According to the program in Figure 2, when the camera
detects an object, the proper belief is asserted causing the
first plan to be triggered and executed; this plan contains the
achievement of three goals: first the proper arm has to be
deployed, then the position of the detected object has to be
reached so that finally the object can be picked. In achieving
the first goal (prepare_arm), we check if an arm not able
to pick the recognised object is currently deployed and, if
this is the case, we retract the current arm and deploy the
other one. We suppose that such retract/deploy actions, during
their execution, change the belief set by properly updating the
arm_deploy belief2. The second goal aims at reaching the
target position and, as the source code reports, triggers the
actions needed to orientate the robot towards the target point
and then to go to it. The last goal performs object picking by
activating the corresponding mechanical arm.</p>
      <p>1AgentSpeak uses the term “context”, but we prefer “condition” because it
is more appropriate for the real meaning of the cd part of the plan.
2This is in accordance with AgentSpeak model.
+object_seen(Type, X, Y) : true
&lt;+!prepare_arm(Type);
+!reach_object(X, Y);
+!pick_object(Type).
+!prepare_arm(ball) :
arm_deployed(cylinder)
&lt;retract_arm(cylinder);
deploy_arm(ball).
+!prepare_arm(cylinder) :
arm_deployed(ball)
&lt;retract_arm(ball);
deploy_arm(cylinder).
+!prepare_arm(X) :
arm_deployed(X)
&lt;</p>
      <p>true.
+!reach_object(X,Y) : true
&lt;orientate_towards(X,Y),
go_to(X,Y).
+!pick_object(X) : true
&lt;activate_arm(X).
B. From Theory to Practice: Architecture of an AgentSpeak
Interpreter</p>
      <p>AgentSpeak represents a very flexible language to express
the behaviour of agents using a goal-oriented approach; in
order to allow the development of such agents, a proper
interpreter needs to be designed and implemented. To this
aim, we could imagine three different design approaches:
totally declarative, a mix of declarative and imperative, totally
imperative.</p>
      <p>The first approach, totally declarative, requires the
extension of AgentSpeak with additional statements in order to
derive a complete programming language. These statements
include constructs to modify variables and to apply them
mathematical/logical operators, as well as proper
generalpurpose library functions to interact with the environment
(i.e., sensors and actuators). Obviously, all the additional
statements and constructs must obey to the
declarative/goaloriented paradigm which is the basis of AgentSpeak; while
this aim can be easily achieved for variable assignment and
mathematical/logical operators (which could be, for instance,
borrowed from Prolog), the same is not straightforward to
be achieved for functions. After all, what is a “function”
in AgentSpeak? Given that in imperative programming a
“function” is the evolution of the concept of “subroutine”, in
a goal-oriented language like AgentSpeak the execution of a
function can be viewed as the achievement of a “sub-goal”.
Indeed, AgentSpeak semantics specifies that the presence of a
goal achievement statement +!at in a plan implies to suspend
the execution of the plan, perform sub-goal achievement and
then resume the plan; such a behaviour is therefore quite
similar to that of subroutine execution. Starting from these
considerations, a totally declarative approach seems a viable
solution, even if its validity, effectiveness and efficiency has
to be verified. Nevertheless, extending AgentSpeak to make it
a complete declarative language is beyond the scope of this
paper.</p>
      <p>
        The second approach, a mix of declarative and imperative
paradigms, is the easiest solution and implies the use of an
imperative language to build the interpreter of the AgentSpeak
program and then write, using the imperative language itself,
all the parts which cannot be implemented in AgentSpeak,
i.e. specific agent actions and event catching and signalling.
This is the approach exploited by Jason [
        <xref ref-type="bibr" rid="ref9">9</xref>
        ], the most widely
known AgentSpeak implementation which is written in Java3.
In Jason, agent’s behaviour has to be written using AgentSpeak
syntax (in a separate source file), while a set of specific classes,
to be developed in Java, are required to implement all the
atomic actions specified in the AgentSpeak file as well as
the computations needed to poll and generate external events.
While this approach is valid and effective, even if a bit na¨ıve, it
has some drawbacks. Indeed, there are two different “worlds”,
the declarative one with its syntax, semantics, objects and
identifiers, and the imperative one, with a different syntax and
semantics, somewhat “glued” to the declarative part in order to
allow the access to and manipulation of the knowledge-base
(beliefs). This is unfavourable for both program design and
execution: the former aspect forces a programmer to handle
different source files with different semantics while the latter
requires proper transformations to pass data between the two
domains which undoubtedly affect performances. Moreover,
the AgentSpeak file cannot be compiled, thus impeding to
deploy only binary files in a live system.
      </p>
      <p>The third approach, totally imperative, at first sight sounds
quite weird: how can we provide declarative semantics only
using imperative constructs? Indeed, to execute an AgentSpeak
program, a proper processing engine needs to be written, and
this can be done in an imperative language4; the same engine
could provide an API with proper function calls able to define
beliefs, goals and plans by using the imperative language
itself. Surely, a skilled software engineer can easily verify that
such an approach is even worse than mixing imperative and
declarative paradigms: the resulting source file could easily
become unreadable and hard to maintain since the declarative
semantics would be somewhat “hidden” in the API function
calls. But if the language is object-oriented and supports
operator overloading, such features can be exploited to solve
the problems above and provide an “all-in-one” environment.
This is described, in details, in the following Sections.</p>
      <p>3and probably, the sole AgentSpeak implementation at the time this paper
has been written.</p>
      <p>4After all, the Jason engine is written in Java!
III. EMBEDDING DECLARATIVE CONSTRUCTS INTO AN</p>
      <p>IMPERATIVE LANGUAGE</p>
      <p>Let us suppose we have an imperative language, let us call
it host language, and let us consider that we want to have
the possibility of writing AgentSpeak statements in the host
language, without changing the compiler and the (standard)
runtime library, and without introducing something like a
preprocessor able to suitably perform a syntactic transformation
of the declarative code5. The question to answer is: could we
write (and execute), for example in C, something like this?
+!reach_object(X,Y) : true
&lt;orientate_towards(X,Y);
go_to(X,Y).</p>
      <p>The answer is obviously “No!”, since syntax and semantics
are not in accordance with C rules. However, we could
reformulate the question as follows: could we try to modify
something, in the code above, in order to make it writable (and
executable), in an imperative language such as C? Let’s deal
with such a problem by taking into account syntax first, and
then semantics.</p>
      <p>From the syntactical point of view, the construct above
has three main problems, due to the presence of the
symbols “:”, “&lt;-” and “.”. Indeed, “+” and “!”
are valid operators, and reach_object(X,Y), true,
orientate_towards(X,Y) and go_to(X,Y) are valid
constructs (functions calls and symbol/variable evaluation).
Therefore, let us reasonably replace non-recognisable symbols
with other symbols which conform to C syntax. To this aim,
since symbol “:” can be interpreted as “such that”, a candidate
replacement is “|”; in a similar way, symbol “&lt;-”, whose
meaning is a sort of implication, can be replaced with “&lt;&lt;”.
The last symbol, the dot “.”, is employed to signal the ending
of the sequence of actions to be performed following plan
activation; in this case, its replacement entails to find a proper
way to represent a sequence or block of actions: the C block,
i.e. { ... }, is worth to be used in this case.</p>
      <p>According to such modifications, the AgentSpeak plan
above can be rewritten as:
+!reach_object(X,Y) | true &lt;&lt;
{
orientate_towards(X,Y);
go_to(X,Y);
}</p>
      <p>While this “syntactical replacement” has been quite
straightforward, dealing with semantics surely implies more problems.
The first remark is related to the symbol &lt;&lt; which, from
semantic point of view, is an operator and thus needs two
valid expression at both left-hand and right-hand side; while
for the LHS this is true, it is not the same for the RHS, since it
is a block of code, not an expression. But even if we could be
able to overcome the problem above, and thus find a suitable
construct to transform a block of code into an expression, the
P lan ::=</p>
      <p>“(” Head “)” “&lt;&lt;” “(” ActionList “)”
Head ::=
|
“(” Event “)” “|” “(” Condition “)”
“(” Event “)” “|” “(” Belief “)”
Condition ::=
|</p>
    </sec>
    <sec id="sec-2">
      <title>Belief “&amp;” Belief</title>
    </sec>
    <sec id="sec-3">
      <title>Condition “&amp;” Belief</title>
      <p>Event ::=
|</p>
    </sec>
    <sec id="sec-4">
      <title>GoalEvent</title>
    </sec>
    <sec id="sec-5">
      <title>Belief Event</title>
      <p>GoalEvent ::=
|
“+” “!” Belief
“-” “!” Belief
Belief Event ::=
|
“+” Belief
“-” Belief
ActionList ::=
|</p>
    </sec>
    <sec id="sec-6">
      <title>ActionList “,” Event</title>
    </sec>
    <sec id="sec-7">
      <title>ActionList “,” AtomicAction</title>
      <p>Fig. 3. Example of Operator Grammar for the Host Language
question remains the meaning of operators: “+” is the
unaryplus in C, but it should behave as “transform the belief or
goal into the related event”; similarly, “!” is the not operator,
while, in the AgentSpeak view, it should be interpreted as
“achieve the goal represented by the belief ”. This “change of
meaning” for operators is completely not allowed in C, but
if we think to C++ or, more generally, to an object-oriented
language, the operator overloading feature6 can surely help
us.</p>
      <p>Starting from these considerations, all the actors of a plan,
namely beliefs, goals and actions, should become expressions
evaluating to proper objects, whose class declarations include
the proper operator redefinition, suitably allowing the desired
“change of meaning”. Some operators and a grammar are
needed, in order to guide a designer to properly write the
code for operator overloading, which must obviously be in
accordance with AgentSpeak syntax. A reference grammar is
shown in Figure 3; it can be also used to derive the operators
needed to be redefined in the host language, together with their
meaning which are summarized in Figure 4.</p>
      <p>On the basis of the grammar and operators described, an
AgentSpeak plan such as:
+object_seen_at(300,400) | true &lt;&lt;
{
orientate_towards(300,400);
go_to(300,400);
would be rewritten as:
class Belief {</p>
      <p>// provide operator overloading here
}
};
5For instance, we do not want something like “SQL embedded in C”.
6if supported by the language
Transform a belief or an achievement goal into an addition event
Transform a belief or an achievement goal into a deleting event
Transform a belief into an achievement goal
Concatenate beliefs to represent the condition
Relate the event with the condition
Relate the head of the plan (event + condition) with the list of actions
Concatenate actions to represent the body of the plan
According to the code snippet above, when the expression
defining the plan is evaluated, first the object_seen_at
object is created and the operator unary-“+”, redefined in the
Belief class, is applied to it; the result (which will surely be
another proper object) is then passed, together with the true
object, to the code for operator “|”; finally, the result of the
last expression will be the first operand for operator “&lt;&lt;”,
whose RHS is an object of the Action class.</p>
      <p>This expression evaluation, which is performed when the
corresponding instruction of host language code
containing the plan is executed, from the declarative model point
of view does not correspond to plan execution, but to
plan definition: indeed, the real actions corresponding to
orientate_towards and go_to must be executed when
the belief object_seen_at(300,400) is “somewhat
asserted”.</p>
      <p>This separation between definition and execution of plans
has two major consequences. The first one is the need of
a proper processing engine as a part of the overall runtime
system; this engine has to embed structures to represent the
knowledge base and the plan library, also providing an API
for their manipulation: the code present in operator redefinition
methods will add the objects representing the plan, returned
by the plan expression evaluation, into the plan library;
subsequently, the generation of an external event, such as
assertion of a new belief, triggered by means of a suitable
API function/method call, will instruct the engine to find the
associated plan in the plan library and then really execute the
action: this is the reason for the presence of the execute
method—for late execution—in the Action class.</p>
      <p>The second important consequence is the role of variables
in a plan. If we would like to make the piece of code more
general, we should replace constants with variables, that is,
something like:
(+object_seen_at(X,Y) | true()) &lt;&lt;
(orientate_towards(X,Y),</p>
      <p>go_to(X,Y) );
However, variables X and Y are interpreted by the host
language during plan definition, and thus they not only need
to be defined but also bound to specific values. This is
quite undesirable since, according to the declarative paradigm,
variable binding must be done during plan execution: like
actions need late execution, a late binding feature should
be provided for variables. To this aim, we cannot use the
variables provided by the host languages, which do not permit
late binding, and, also in this case, we have to replace them
with proper “variable objects”. Therefore, by supposing the
definition of a class v to represent a variable, the code above
becomes:
( +object_seen_at(v("X"),v("Y")
| true() ) &lt;&lt;
( orientate_towards(v("X"),v("Y")),</p>
      <p>go_to(v("X"),v("Y")) );
Proper binding and interpretation of such variables in the
context of the execution of a plan is thus a task of the
processing engine. This and other implementation aspects will
be dealt with in the next Section.</p>
      <p>IV. ARCHITECTURE OF PROFETA</p>
      <p>We applied the implementation methodology described in
the Section above in the design of PROFETA, a framework
that allows programmers to define and execute plans expressed
in an AgentSpeak semantic, but written in a bare Python.</p>
      <p>This was made possible by the extended operator overloading
facilities given by Python itself. In principle, any object–
oriented language which provides operator overloading could
be used to implement PROFETA, and it is in our plans to
translate it also to C++, but Python proved to be very useful to
obtain a working proof–of–concept implementation in a couple
of weeks.</p>
      <p>The main components of the architecture of PROFETA
are presented in Figure 5. PROFETA provides the class
Attitude and its subclasses Belief and Goal that
represent the corresponding concepts of the BDI model (the
mental attitudes of the Agent). The terms of an attitude are
stored in the self._terms attribute. Condition models
the condition of a plan, i.e. a set of beliefs separated by ‘&amp;’.</p>
      <p>As described in the grammar, the behaviour of this operator
is overloaded so that it concatenates the beliefs, storing them
in the self.__conditions attribute. Action is just a
convenience abstract class: every agent’s action must derive
Action and implement the execute() method by
specifying the instructions to actually perform the action. As for
the attitudes, all the parameters of an Action are stored in the
self._terms attribute.</p>
      <p>An Intention, as defined in the original BDI model, is
composed by a list of Actions and does not require a class
on itself. Conversely, the class Intentions is a collection
of all active Intentions, i.e. of all the plans which have been
instantiated and can be executed since their triggering event
has happened and the corresponding condition is satisfied.</p>
      <p>Notice that the intentions stored into Intentions are a
subset of the Plan Library, which contains the set of all
plans written as te|cd &gt;&gt; h, according to the grammar
previously defined7.</p>
      <p>On the basis of the proposed methodology, triggering events
are defined as follows: the ‘+’ and ‘-’ operator have been
over</p>
      <p>7With respect to the grammar in Figure 3, we replaced operator “&lt;&lt;”
with “&gt;&gt;” since we argue that it is more appropriate for the concept of
“implication”.
&lt;&lt;Singleton&gt;&gt;</p>
      <p>E n g i n e
-knowledge_base
-plan_library
-intentions
+process_event()
+evaluate_condition()
+allocate_plans()
+generate_external_event()
K n o w l e d g e B a s e</p>
      <p>P l a n L i b r a r y</p>
      <p>I n t e n t i o n s
+add_belief()
+delete_belief()
+exists()
+belief_like()
+add_plan()</p>
      <p>+execute_intentions()</p>
      <p>A t t i t u d e
-condition
-terms
+set_condition()
+set_terms()
+get_terms()
+unify()</p>
      <p>Action
-terms
+execute()</p>
      <p>B e l i e f
+create_event()
+is_ground()
+match()
+match_name()</p>
      <p>Goal
+set_origin()
+create_event()</p>
      <p>C o n d i t i o n
-conditions
+evaluate()
loaded so that when they precede the instance of an Attitude,
an internal field is set accordingly. The corresponding
triggering event can be obtained by invoking create_event().</p>
      <p>The overloaded ‘|’ operator creates a new Condition
object, and store it in the self._condition attribute of
Attitude. Finally, the body of the plan is simply a Python
list whose elements are actions and/or triggering events. The
whole plan is added to the Plan Library using the overloaded
“&gt;&gt;” operator.</p>
      <p>When plans need variables, those will be bound to actual
values on the basis of the content of the knowledge base, and a
proper syntax is employed. In PROFETA, the special function
_(..) can be used to denote a variable within the scope of
a plan, e.g. to denote the variable X we will write _("X").</p>
      <p>Class Engine represents the processing engine which
implements the functionalities described in Section III. It holds
a reference to the agent’s Plan Library and Knowledge Base,
implemented in two suitable classes. The KnowledgeBase
class exposes an appropriate interface that allows to: (i) modify
the set of beliefs, (ii) test the presence of a particular belief,
(iii) obtain a specific subset of beliefs.</p>
      <p>The basic working scheme of Engine is to run a
continuous loop that (i) checks if an event has occurred, (ii) selects
an appropriate plan, (iii) evaluates the condition verifying
whether it is true, (iv) executes the plan8. While internal events
(i.e. events specified in the Body of a plan) are directly handled
by Engine, external events that are bound, for example, to
perception made by sensors must be notified to the Engine,
so that it can reason about them and determine the appropriate
plan(s)—if any. In this case, to let the Engine know that an
event has occurred, the generate_external_event()
method has to be used.</p>
      <p>V. CASE STUDY</p>
      <p>
        This Section shows how PROFETA has been actually used
to define strategies for an autonomous mobile robot taking
part in the Eurobot robotic contest [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ].
      </p>
      <p>Robots taking part to Eurobot are required to
implement a strategy which usually consists in repetitively (i)
approach/recognise an object, (ii) pick the object and store it,
(iii) put the object in an appropriate place. Objects are usually
of different type and colour, and thus need to be sorted, put
in different containers or in specific sequences to gain more
points, etc. For instance, in the 2010 edition, robots had to
collect and store orange and red balls (representing, respectively,
oranges and tomatoes) and white cylinders (representing ears
of corn). Game matches are played by two robots at the same
time, on a shared game table: in order not to get penalties,
robots have to avoid each other. Moreover, matches have a 90
seconds time limit. A winning game strategy should take into
account all the issues stated above and also foresee and handle
all the different unexpected situations that could happen during
the match.</p>
      <p>Figure 6 reports a fragment of an actual game strategy
written using PROFETA: during the first part of the match, we
wanted our robot to collect ears of corn. According to Eurobot
2010 rules, objects can be found on the game table in fixed,
a priori known, positions. We have hardcoded such positions
in a global accessible table and enumerated game elements—
e.g. all ears of corn have been given a code like “c0”, “c1”,
and so on—so that such codes can be used as keys to retrieve
the position of the specified object. There exists two kinds of
ears of corn: good and fake ones, the formers are painted in
white, the latter are painted in black. We wanted—of course—
the robot to collect good ones and to discard the others. On
this basis, the employed strategy aims at (i) detecting colour
configuration, (ii) picking the first three ears near the starting
area (called “c0”, “c3” and “c6”), (iii) depositing them into
the basket, (iv) picking other three ears near the basket, (v)
depositing the objects picked. In picking the ears, we have to
pay attention to their colour, and skip it if it is fake (black).</p>
      <p>As reported in Figure 6, we defined the beliefs
white_corn and black_corn, to represent a ear of
corn of the given colour; corns_in_robot, to count
the number of ears that the robot has already picked; and
no_more_corns to signal that, according to the
strategy, we are not interested in picking more ears. Three</p>
      <p>
        8Indeed, the real working scheme of a BDI engine is quite more complex
that described; due to space restrictions, we reported only a simplified version;
interested readers may refer to [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ], [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ].
goals are defined: go, which is the goal triggering the
starting of the overall game strategy; grab_corn,
aiming at reaching and picking a specified ear of corn; and
deposit_corns, which instructs the robot to release the
picked ears in the proper basket. Finally, required actions are:
detect_configuration, which instructs the vision
system to recognise the colour of the various ears and assert the
proper white_corn/black_corn beliefs; reach_corn,
triggering the robot to move in order to reach the position of
a specified ear; pick_corn, driving the mechanical arms to
pick the ear from the table (thus properly updating the number
of objects inside the robot); reach_deposit_zone,
making the robot to reach the basket; and open_tank, which
drives the actuators to open the tank and release the objects
(this action also resets the number of ears of corn in robot).
      </p>
      <p>As for the strategy, defined in function setup_strategy,
the first goal (go) implies to detect the corn colours, grab the
first three ears and then go to deposit them. The grab_corn
goal is subject to the condition related to ear colour: if it is
white, it can be picked, otherwise no action is needed. Goal
deposit_corns is instead subject to a condition on the
number of ears the robot has picked: if it is less than two9, it
could be worth to try to pick other ears (and thus save time)
before going to the basket. If we have instead an adequate
number of ears of corn, we can reach the basket, deposit them,
and then go to pick the other ears.</p>
      <p>Notice that the strategy example reported in Figure 6 is
written in bare Python. Operator overloading does all the
magic, making it possible to express all the strategy with a
pure declarative syntax. This approach allows to put strategies
in a separate Python module or in the same module where
Actions are defined, according to programmer’s preferences.</p>
      <p>Moreover, the preferred strategy can be easily selected before
each match just by calling the corresponding function, which
loads all the rules and initial beliefs into the engine, without
the need to recompile of parse again any source file.</p>
      <p>VI. CONCLUSIONS AND FUTURE WORK</p>
      <p>This paper has described an approach to seamless embed
declarative constructs able to write AgentSpeak programs
into an imperative object-oriented language. By exploiting
operator overloading, a feature proper of many object-oriented
languages, and a suitable software architecture, the proposed
approach allows a programmer to design, deploy and run a
complete agent system, based on a goal-oriented paradigm,
using a single programming language and runtime environment.</p>
      <p>As a proof of concepts, the paper has presented an
implementation of the proposed approach in a Python framework,
called PROFETA (Python RObotic Framework for dEsigning
sTrAtegies). As it has been described in the case-study, the tool
developed has proved its effectiveness in a typical real case,
which is the design and implementation of an autonomous
mobile robot.</p>
      <p>9because at least two ears among “c0”, “c3” and “c6” are black
class white_corn(Belief):</p>
      <p>pass
class black_corn(Belief):</p>
      <p>pass
class corns_in_robot(Belief):</p>
      <p>pass
class no_more_corns(Belief):</p>
      <p>pass
class go(Goal):</p>
      <p>pass
class grab_corn(Goal):</p>
      <p>pass
class deposit_corn(Goal):</p>
      <p>pass
class detect_configuration(Action):
def execute(self):</p>
      <p>## ...
class reach_corn(Action):
def execute(self):</p>
      <p>## ...
class pick_corn(Action):
def execute(self):</p>
      <p>## ...
class reach_deposit_zone(Action):
def execute(self):</p>
      <p>## ...
class open_tank(Action):
def execute(self):</p>
      <p>## ...
def setup_strategy():
(+∼go()) &gt;&gt; [ detect_configuration(), +∼grab_corn("c0"), +∼grab_corn("c3"),
+∼grab_corn("c6"), +∼deposit_corns()]
(+∼grab_corn(_("X")) | ( white_corn(_("X")) )) &gt;&gt; [ reach_corn(_("X")), pick_corn() ]
(+∼grab_corn(_("X")) | ( black_corn(_("X")) )) &gt;&gt; [ ]
(+∼deposit_corns() | ( corns_in_robot(_("X")) &amp; (lambda : X &gt; 1)) ) &gt;&gt;</p>
      <p>[ reach_deposit_zone(), open_tank(), +∼deposit_corns() ]
(+∼deposit_corns() | ( corns_in_robot(_("X")) &amp; (lambda : X &lt;= 1) &amp; no_more_corns() )) &gt;&gt;
[ # second part of the game ... ]
(+∼deposit_corns() | ( corns_in_robot(_("X")) &amp; (lambda : X &lt;= 1)) ) &gt;&gt;
[ +∼grab_corn("c11"), +∼grab_corn("c12"),</p>
      <p>+∼grab_corn("c13"), +no_more_corns(), +∼deposit_corns() ]
def start():
setup_strategy()
Engine.instance().generate_external_event( +∼go() )</p>
      <p>Fig. 6. An actual game strategy for Eurobot written in Python using PROFETA</p>
      <p>Future work will aim at further improving the tool by
introducing missing features, such as test goal and goal deletion, as
well as at studying appropriate optimisations to speed up plan
selection and execution. Once all the features of PROFETA
will be implemented, next step will be the implementation of
PROFETA in another object–oriented language, such as C++.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>F.</given-names>
            <surname>Bellifemine</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Poggi</surname>
          </string-name>
          , and G. Rimassa, “
          <article-title>Jade - a fipa-compliant agent framework</article-title>
          ,”
          <source>in Proceedings of the Practical Applications of Intelligent Agents</source>
          ,
          <year>1999</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>R.</given-names>
            <surname>Bordini</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Bazzan</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Jannone</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Basso</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Vicari</surname>
          </string-name>
          , and
          <string-name>
            <given-names>V.</given-names>
            <surname>Lesser</surname>
          </string-name>
          , “
          <article-title>Agent-speak (xl): efficient intention selection in bdi agents via decisiontheoretic task scheduling</article-title>
          ,”
          <source>in Proceedings of the 1st International Joint Conference on autonomous agents and multiagent systems</source>
          . ACM New York, NY, USA,
          <year>2002</year>
          , pp.
          <fpage>1294</fpage>
          -
          <lpage>1302</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>J. M.</given-names>
            <surname>Bradshaw</surname>
          </string-name>
          , Ed.,
          <string-name>
            <given-names>Software</given-names>
            <surname>Agents</surname>
          </string-name>
          . AAAI Press/The MIT Press,
          <year>1997</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <surname>M. d'Inverno</surname>
            and
            <given-names>M.</given-names>
          </string-name>
          <string-name>
            <surname>Luck</surname>
          </string-name>
          , “Engineering agentspeak(l):
          <article-title>A formal computational model</article-title>
          ,
          <source>” Journal of Logic and Computation</source>
          , vol.
          <volume>8</volume>
          , no.
          <issue>3</issue>
          , pp.
          <fpage>233</fpage>
          -
          <lpage>260</lpage>
          ,
          <year>1998</year>
          . [Online]. Available: http://eprints.ecs.soton.ac.uk/3846/
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>L.</given-names>
            <surname>Fichera</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Marletta</surname>
          </string-name>
          ,
          <string-name>
            <given-names>V.</given-names>
            <surname>Nicosia</surname>
          </string-name>
          , and
          <string-name>
            <given-names>C.</given-names>
            <surname>Santoro</surname>
          </string-name>
          , “
          <article-title>Flexible robot strategy design using belief-desire-intention model</article-title>
          ,”
          <source>in Proceedings of Eurobot Conference</source>
          <year>2010</year>
          ,
          <year>2010</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>K.</given-names>
            <surname>Hinddriks</surname>
          </string-name>
          , F. de Boer, W. van der Hoek, and J. C. Meyer, “
          <article-title>Agent programming in 3apl,”</article-title>
          <source>Int. J. of Autonomous Agents and Multi-agent Systems</source>
          , vol.
          <volume>2</volume>
          , pp.
          <fpage>357</fpage>
          -
          <lpage>401</lpage>
          ,
          <year>1999</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7] --, “
          <article-title>Agent programming with declarative goals,” in Intelligent Agents VII. Agent Theories, Architectures and Languages, ser</article-title>
          .
          <source>LNCS</source>
          , vol.
          <source>1986</source>
          . Springer-Verlag,
          <year>2000</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          [8]
          <string-name>
            <given-names>J.</given-names>
            <surname>Hubner</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Bordini</surname>
          </string-name>
          , and
          <string-name>
            <given-names>M.</given-names>
            <surname>Wooldridge</surname>
          </string-name>
          , “
          <article-title>Programming declarative goals using plan patterns</article-title>
          ,
          <source>” Lecture Notes in Computer Science</source>
          , vol.
          <volume>4327</volume>
          , p.
          <fpage>123</fpage>
          ,
          <year>2006</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          [9]
          <string-name>
            <given-names>Jason</given-names>
            <surname>Home</surname>
          </string-name>
          <string-name>
            <surname>Page</surname>
          </string-name>
          , “http://www.jason.sourceforge.net/,”
          <year>2004</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          [10]
          <string-name>
            <given-names>S.</given-names>
            <surname>Luke</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C.</given-names>
            <surname>Cioffi-Revilla</surname>
          </string-name>
          ,
          <string-name>
            <given-names>L.</given-names>
            <surname>Panait</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Sullivan</surname>
          </string-name>
          , and G. Balan, “
          <article-title>Mason: a multi-agent simulation environment</article-title>
          ,
          <source>” Simulation</source>
          , vol.
          <volume>81</volume>
          , no.
          <issue>7</issue>
          , pp.
          <fpage>517</fpage>
          -
          <lpage>527</lpage>
          ,
          <year>July 2005</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          [11]
          <string-name>
            <given-names>V.</given-names>
            <surname>Nicosia</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C.</given-names>
            <surname>Spampinato</surname>
          </string-name>
          , and
          <string-name>
            <given-names>C.</given-names>
            <surname>Santoro</surname>
          </string-name>
          , “
          <article-title>Software agents for autonomous robots: The eurobot 2006 experience</article-title>
          ,”
          <source>in Proceedings of the WOA 2006 Workshop</source>
          ,
          <year>2006</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          [12]
          <string-name>
            <given-names>A.</given-names>
            <surname>Rao</surname>
          </string-name>
          , “
          <string-name>
            <surname>AgentSpeak</surname>
          </string-name>
          (L):
          <article-title>BDI agents speak out in a logical computable language</article-title>
          ,
          <source>” Lecture Notes in Computer Science</source>
          , vol.
          <volume>1038</volume>
          , pp.
          <fpage>42</fpage>
          -
          <lpage>55</lpage>
          ,
          <year>1996</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          [13]
          <string-name>
            <given-names>A.</given-names>
            <surname>Rao</surname>
          </string-name>
          and M. Georgeff, “
          <article-title>BDI agents: From theory to practice,” in Proceedings of the first international conference on multi-agent systems (</article-title>
          <source>ICMAS-95)</source>
          . San Francisco, CA,
          <year>1995</year>
          , pp.
          <fpage>312</fpage>
          -
          <lpage>319</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref14">
        <mixed-citation>
          [14]
          <string-name>
            <given-names>S.</given-names>
            <surname>Russell</surname>
          </string-name>
          and
          <string-name>
            <given-names>P.</given-names>
            <surname>Norvig</surname>
          </string-name>
          , Artificial Intelligence: A Modern Approach/Second Edition. Prentice Hall,
          <year>2003</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref15">
        <mixed-citation>
          [15]
          <string-name>
            <given-names>C.</given-names>
            <surname>Santoro</surname>
          </string-name>
          , “
          <article-title>An erlang framework for autonomous mobile robots,” in Procedings of the 2007</article-title>
          <source>ACM SIGPLAN Workshop on Erlang. ACM</source>
          ,
          <year>2007</year>
          , pp.
          <fpage>85</fpage>
          -
          <lpage>92</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref16">
        <mixed-citation>
          [16]
          <string-name>
            <given-names>R.</given-names>
            <surname>Siegwart</surname>
          </string-name>
          and
          <string-name>
            <surname>I. Nourbakhsh</surname>
          </string-name>
          , Introduction to Autonomous Mobile Robots. MIT Press,
          <year>2004</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref17">
        <mixed-citation>
          [17]
          <string-name>
            <given-names>E.</given-names>
            <surname>Sklar</surname>
          </string-name>
          , “
          <article-title>Software review: Netlogo, a multi-agent simulation environment</article-title>
          ,
          <source>” Artificial Life</source>
          , vol.
          <volume>13</volume>
          , pp.
          <fpage>303</fpage>
          -
          <lpage>311</lpage>
          ,
          <year>2007</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref18">
        <mixed-citation>
          [18]
          <string-name>
            <given-names>E.</given-names>
            <surname>Tatara</surname>
          </string-name>
          , M. North, T. Howe,
          <string-name>
            <given-names>N. T.</given-names>
            <surname>Collier</surname>
          </string-name>
          , and
          <string-name>
            <given-names>J.</given-names>
            <surname>Vos</surname>
          </string-name>
          , “
          <article-title>An introduction to repast modelling using a simple predator-prey example</article-title>
          ,”
          <source>in Proceedings of Agents 2006 Conference on Social Agents: Results and Prospects</source>
          ,
          <year>2006</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref19">
        <mixed-citation>
          [19]
          <string-name>
            <given-names>P.</given-names>
            <surname>Terna</surname>
          </string-name>
          , “
          <article-title>Simulation tools for social scientists: Building agent based models with swarm</article-title>
          ,
          <source>” Journal of Artificial Societies and Social Simulation</source>
          , vol.
          <volume>1</volume>
          , no.
          <issue>2</issue>
          ,
          <year>1998</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref20">
        <mixed-citation>
          [20]
          <string-name>
            <given-names>G.</given-names>
            <surname>Weiss</surname>
          </string-name>
          , Ed.,
          <string-name>
            <given-names>Multiagent</given-names>
            <surname>Systems</surname>
          </string-name>
          . The MIT Press,
          <year>April 1999</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref21">
        <mixed-citation>
          [21]
          <string-name>
            <given-names>R.</given-names>
            <surname>Wray</surname>
          </string-name>
          and
          <string-name>
            <given-names>R.</given-names>
            <surname>Jones</surname>
          </string-name>
          , “
          <article-title>An introduction to soar as an agent architecture,” in Cognition and Multi-Agent interaction: from Cognitive Modelling to Social Simulation</article-title>
          , R. Sun, Ed. Cambridge University Press,
          <year>2005</year>
          , pp.
          <fpage>53</fpage>
          -
          <lpage>78</lpage>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>