slides

Transcription

slides
From Agent-Oriented
Modelling to Agent
Programming
Prof Kuldar Taveter,
Tallinn University of
Technology, Estonia
Please join the mailing list!


[email protected]
Subscribing:
http://lists.ttu.ee/mailman/listinfo/aine.aom
Last time

Scenarios
Behaviour models

Service models

• Behavioural interface models
• Agent behaviour models
The scenario of socializing a
Tamagotchi
SCENARIO 5
Socialize Tamagotchi
Goal
MyTamagotchi
Initiator
The Tamagotchi is unhappy
Trigger
The Tamagotchi dies
Failure
DESCRIPTION
Condition
Step
Activity
1
Visit the friend
(Scenario 6)
2
Find a partner for the
Tamagotchi (Scenario 7)
Agent types and
roles
Person/Owner,
Tamagotchi/
MyTamagotchi,
Tamagotchi/
FriendTamagotchi
Person/Owner,
Tamagotchi/
Matchmaker
Resources
Present
Friend_list
Quality
goals
My
Tamagotchi
happy
My
Tamagotchi
happy
A behavioural interface model
for a Tamagotchi
Activity
Have meal
Go to the
friend’s place
Trigger
Meal
selection by
the owner
Request to
visit by the
owner
-
-
-
Pre-conditions
The Tamagotchi is
hungry
The selected meal is in
the Tamagotchi Shell
The connection to the
other Tamagotchi is
operational
The Tamagotchi is in
its Tamagotchi Shell
-
-
Post-conditions
The Tamagotchi’s level of
hunger has decreased
The Tamagotchi has
consumed the meal from its
Tamagotchi Shell
The Tamagotchis is back in
its Tamagotchi Shell
The Tamagotchi’s
happiness level is increased
The Tamagotchi holds any
items received as presents
The abstract agent architecture
revisited
The execution loop of an
abstract agent
while the agent is unfulfilled do
sense the environment;
update the knowledge base;
reason;
choose actions;
act;
end while
Tamagotchi
Tamagotchi
is unhappy
An agent
behaviour
model
of a Tamagotchi
R1
Person
I am not happy
Express unhappiness
Please visit
your friend
R2
Connect with
another Tamagotchi
There exists a
connected friend
R3
Visit the other Tamagotchi
R4
Tamagotchi
Request visit
Could I come to
your place?
R5
Please come!
Go to the friend’s place
Good day!
Give a present and play
Tamagotchi has
enough presents
R6
Give the friend a present
Please accept
my present
Play with the friend
May I play
with you?
Tamagotchi is
still unhappy
R7
Return home
Behaviour model as a statechart
Services for
Tamagotchis
<<Service>>
Shop
<<Service>>
Toilet
flush ()
select (Food food)
select (Item item)
Food buy ()
Item buy()
<<Service>>
StoreHouse
store(Food food)
store(Item item)
select (Food food)
select(Item item)
Food get()
Item get()
<<Service>>
Physician
<<Service>>
Mailbox
cure()
Letter getLetter()
<<Service>>
Communicator
tellOwner(String
msg)
connect()
visit()
send(String msg)
givePresent
(String msg,
Present pres)
String receive()
returnHome()
Password
goToTamagotchi
Town ()
returnFrom
TamagotchiTown
(Password pw)
<<Environment>>
TamagotchiShell
1
1
<<AgentType>>
Tamagotchi
1
*
Password
<<Service>>
ShoppingMall
<<Service>>
Workplace
enter
(Password pw)
Password exit()
enter
(Password pw)
select
(Item item)
Password buy
(Password pw)
Password exit()
<<Service>>
Food Court
*
1
<<Environment>>
TamagotchiTown
enter
(Password pw)
Password exit()
<<Service>>
TownHall
enter
(Password pw)
Password exit()
<<Service>>
School
<<Service>>
Pre-school
<<Service>>
Theatre
<<Service>>
GameCentre
<<Service>>
King’sCastle
enter
(Password pw)
Password exit()
enter
(Password pw)
Password exit()
enter
(Password pw)
Password exit()
enter
(Password pw)
Password exit()
enter
(Password pw)
Password exit()
enter
(Password pw)
select
(Food food)
Password buy
(Password pw)
Password exit()
<<Service>>
TravelAgency
enter
(Password pw)
select
(Item item)
Password buy
(Password pw)
Password exit()
Service invocations
Activity
Service invocation
Express
unhappiness
Communicator.tellOwner(“I am not happy”)
Connect with
another
Tamagotchi
Communicator.connect()
Request visit
Communicator.send(“Could I come to your place?”),
Communicator.receive()
Go to the
friend’s place
Communicator.visit(),
Communicator.send(“Good day!”),
Communicator.receive()
Give the friend a
present
Communicator.givePresent(“Please accept my
present”, Present), Communicator.receive()
Play with the
friend
Communicator.send(“May I play with you?”),
Communicator.receive()
Return home
Communicator.returnHome()
Overview of today







Models at the depoyment layer
The viewpoint framework
Platform-independent vs. platformdependent models
Differences in concepts
BDI agent architecture
Agent programming language Jason
Agent platform JADE
The conceptual space
Types of models at different
layers



Motivation layer: goal models and motivational
scenarios, role and organisation models,
domain models
System design layer: agent models,
acquaintance models, interaction models,
knowledge models, scenarios, behaviour
models, service models
Deployment layer: agent interface and
interaction specifications, data models and
service models, agent behaviour
specifications
The viewpoint framework
Platform-independent models


Describe the design independently of
any specific programming language or
platform
Platform-dependent parts of design
models are shielded by services
Architecture-independent
models

Models that do not prescribe or imply the
usage of any specific agent architecture
for implementing man-made agents
Differences in concepts



Rule is used as a behavioural construct
A goal is modelled as achieved by a
sequence of concrete actions by a
concrete agent – plan
In contrast to: a goal as achieved by
interactions between agents comprising
a multi-agent system
The abstract agent architecture
revisited
BDI agent architecture
Beliefs


Correspond to concrete objects and
associations between them
An agent’s belief structure is decided
based on the set of knowledge items at
the system design layer
Desires or goals



Can reflect a high level purpose or
motivation
Behavioural constructs
Are derived from activities and rules of
the system design layer
Intentions

The set of goals the agent is currently
trying to achieve
Plans


Sequences of concrete actions for
achieving the agent goals
Fundamentally static, but can be
updated during agent execution
The execution loop of an
abstract agent
while the agent is unfulfilled do
sense the environment;
update the knowledge base;
reason;
choose actions;
act;
end while
The execution loop of a basic
BDI reasoner
while there are unachieved goals do
observe the environment;
update beliefs;
prioritise intentions to achieve;
choose a plan for the intention;
execute and monitor the plan;
end while
Events


Indicate updates to the agent’s beliefs
Correspond to perceptions of the system
design layer
An agent programmed in Jason




Set of beliefs
Set of plans
Goals
Events
Events in Jason


A change of an agent’s beliefs through
perception of the environment: denoted
by +belief or –belief
A change in the agent’s goals: denoted
by +!goal or –!goal
The general pattern for
representing Jason plans
event : context -> body;
Actions in Jason





Standard internal actions starting with ‘.’: for
example, .print("I'm not happy!");
Adding a goal: !goal, for example,
!givePresent(H);
Removing a goal: -!goal, for example,
-!play(Friend);
Evaluating a predicate: ?predicate, for example
?happyLevel(K);
Replacing a belief: -+predicate, for example,
-+presentList(T);
An agent
behaviour
model
of a Tamagotchi
revisited
Knowledge base
happyLevel(0).
myFriend(tamagotchi2).
myMaster(person).
!checkHappyLevel.
presentNumber(2).
presentList([toy,candy]).
Checking happiness level
@p1
+!checkHappyLevel: happyLevel(M) & M <=20
<- .print("I'm not happy!");
.my_name(I);
?happyLevel(K);
?myMaster(P);
.send(P, tell, unhappy(I,K)).
Requesting visit
@p2
+visitFriend: true
<.print("Could I come to your place?");
?myFriend(Friend);
.my_name(I);
.send(Friend, tell, could_I_come_to_your_place(I)).
Receiving acceptance/sending
greeting
@p3
+please_come(P): true
<- .print("Good day!");
.my_name(I);
?myFriend(Friend);
.send(Friend, tell, good_day(I)).
Receiving reply
@p4
+hello(Friend): true
<- !visit(Friend).
Setting the goals for sending a
present or playing
@p5
+!visit(Friend): presentNumber(M) & M >0
<- ?presentList([H|T]);
-+presentList(T);
!givePresent(H).
@p6
+!visit(Friend): presentNumber(M) & M =0
<- !play(Friend).
Sending a present
@p7
+!givePresent(Present): true
<- ?happyLevel(L);
.print("Please accept my present,",Present,"!(",L,")");
?presentNumber(N);
-+presentNumber(N -1);
-+happyLevel(L + 4);
?myFriend(Friend);
.my_name(I);
.send(Friend,tell,accept_my_present(I, Present)).
Requesting to play
@p8
+!play(Friend): happyLevel(M) & M <=20
<.print("May I play with you,",Friend,"?(",M,")");
.my_name(I);
.send(Friend,tell,play_with_me(I,M)).
Thanking for the present
@p9
+thanks(Friend,Present): true
<- !play(Friend).
Receiving OK
@p10
+ok(Friend,M): happyLevel(M) & M <=20
<- ?happyLevel(L);
-+happyLevel(L + 2);
!visit(Friend).
Jason in action
JADE (Java Agent Development
Environment)





Distributed agent platform which can be split
among several hosts
Java Application Programmer’s Interface.
Graphical User Interface to manage several
agents from the same Remote Management
Agent
Library of FIPA interaction protocols, such as
Contract Net
Available at http://jade.cselt.it/
JADE Agent Platform
Agent life cycle
Creating and terminating JADE agents
import jade.core.Agent;
import jade.core.AID;
public class Tamagotchi extends Agent {
// Put agent initializations here
protected void setup() {
/** Register the ontology used by this agent */
getContentManager().registerOntology(ontology);
presentList = new LinkedList();
presentList.add(new Present ("food"));
presentList.add(new Present ("chocolate"));
presentList.add(new Present ("toy"));
// Adding cyclic behaviours
addBehaviour(new Mood_checker(this));
/** Create the Tamagotchi Shell for this Tamagotchi */
setTamagotchiShell(new TamagotchiShell(this));
// Creating a new User Interface
ui = new UserInterface(this);
ui.show();
}
// Put agent clean-up operations here
protected void takeDown() {
// Printout a dismissal message
System.out.println(“Tamagotchi “+getAID().getName()+” terminating.”);
}
}
Compiling and running JADE agents
javac –classpath <JADE-classes> Tamagotchi.java
java –classpath <JADE-classes> jade.Boot –container tama1:Tamagotchi
Classpath can/should be set beforehand.
Passing arguments to an agent
public class BookBuyerAgent extends Agent {
private String targetBookTitle;
// The list of known seller agents
private AID[] sellerAgents = {new AID(“seller1”, AID.ISLOCALNAME),
new AID(“seller2”, AID.ISLOCALNAME)};
// Put agent initializations here
protected void setup() {
// Printout a welcome message
System.out.println(“Hello! Buyer-agent“ +getAID().getName()+
” is ready.”);
// Get the title of the book to buy as a start-up argument
Object[] args = getArguments();
if (args != null && args.length > 0) {
targetBookTitle = (String) args[0];
System.out.println(“Trying to buy” + targetBookTitle);
}
else {
// Make the agent terminate immediately
System.out.println(“No book title specified“);
doDelete();
}
}
…
}
Running an agent with arguments
java jade.Boot –container buyer:BookBuyerAgent (The-Lord-ofthe-rings)
Concurrent tasks



An agent must be able to carry out
several concurrent tasks in response to
different external events
Every JADE agent is composed of a
single execution thread
Concurrent tasks are modelled and can
be implemented as instances of
jade.core.behaviours.Behaviour
Agent thread
Defining a behaviour
public class StrangeBehaviour extends Behaviour {
public void action() {
while (true) {
// do something
}
}
public boolean done() {
return true;
}
}

What is wrong with this behaviour?
Hierarchy of behaviours
Defining behaviours
public class MyOneShotBehaviour extends OneShotBehaviour {
public void action() {
// perform operation X
}
}
public class MyCyclicBehaviour extends CyclicBehaviour {
public void action() {
// perform operation Y
}
}
Adding a behaviour
public class Tamagotchi extends Agent {
// Put agent initializations here
protected void setup() {
…
addBehaviour(new MoodChecker(this));
}
// Put agent clean-up operations here
protected void takeDown() {
// Printout a dismissal message
System.out.println(“Tamagotchi “+getAID().getName()+” terminating.”);
}
}
Sending messages
ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
msg.addReceiver(new AID(“Peter”, AID.ISLOCALNAME));
msg.setLanguage(“English”);
msg.setOntology(“Weather-forecast-ontology”);
msg.setContent(“Today it’s raining”);
send(msg);
// Message carrying a request for offer
ACLMessage cfp = new ACLMessage(ACLMessage.CFP);
for (int i = 0; i < sellerAgents.lenght; ++i) {
cfp.addReceiver(sellerAgents[i]);
}
cfp.setContent(targetBookTitle);
myAgent.send(cfp);
Receiving messages
ACLMessage msg = receive();
if (msg != null) {
// Process the message
}
Tamagotchi
Tamagotchi
is unhappy
An agent
behaviour
model
of a Tamagotchi
R1
Person
I am not happy
Express unhappiness
Please visit
your friend
R2
Connect with
another Tamagotchi
There exists a
connected friend
R3
Visit the other Tamagotchi
R4
Tamagotchi
Request visit
Could I come to
your place?
R5
Please come!
Go to the friend’s place
Good day!
Give a present and play
Tamagotchi has
enough presents
R6
Give the friend a present
Please accept
my present
Play with the friend
May I play
with you?
Tamagotchi is
still unhappy
R7
Return home
“Visit the other Tamagotchi” in
JADE, 1
class Visit_the_other_Tamagotchi extends SequentialBehaviour {
/**
* Implements a visit between the two interconnected Tamagotchis
* where the initiating Tamagotchi goes to the hosting
* Tamagotchi's environment.
*/
/** The "host" Tamagotchi of this behaviour */
Tamagotchi thisTamagotchi;
/** The message to be sent */
private ACLMessage msg;
/** Constructor of the behaviour */
public Visit_the_other_Tamagotchi(Tamagotchi tamagotchi) {
super(tamagotchi);
thisTamagotchi = tamagotchi;
}
“Visit the other Tamagotchi” in
JADE, 2 and 3 (→)
/** Actual implementation of the behaviour */
public void onStart() {
Request_visit rv = new Request_visit(thisTamagotchi);
addSubBehaviour(rv);
rv.action();
removeSubBehaviour(rv);
/** Retrieves the local environment */
TamagotchiShell shell = thisTamagotchi.getTamagotchiShell();
String reply = shell.getCommunicator().receive();
thisTamagotchi.getUserInterface().
appendMessage("Received: " + reply);
if (reply.equals("Please come!")) {
/** Invoke the visit() operation of the Communicator
* service */
if (shell.getCommunicator().visit()) {
/** If going to the other Tamagotchi's environment
* was successful */
/** Inform the owner that the Tamagotchi is away from its shell */
thisTamagotchi.getUserInterface().appendMessage("Away");
Go_to_the_friends_place eg = new Go_to_the_friends_place
(thisTamagotchi);
addSubBehaviour(eg);
eg.action();
removeSubBehaviour(eg);
}
while (thisTamagotchi.getHappinessLevel() < 20) {
Give_a_present_and_play b = new Give_a_present_and_play
(thisTamagotchi);
addSubBehaviour(b);
b.onStart();
removeSubBehaviour(b);
}
}
Return_home rh = new Return_home(thisTamagotchi);
addSubBehaviour(rh);
}
}
Blocking a behaviour (strongly
suggested!)
public void action() {
ACLMessage msg = myAgent.receive();
if (msg != null) {
// Message received. Process it
...
}
else {
block();
}
}
Elements of ontologies






Concepts: (Person :name John)
Primitives, such as strings and integers
Predicates: (Works-for (Person :name John)
(Company :name TILAB))
Agent actions: (Sell (Book :title “The Lord of the
rings”) (Person :name John))
Identifying Referential Expressions (IRE): (all ?x
(Works-for ?x (Company :name TILAB))
Variables, such as ?x.
Message Content Reference Model
Car rental ontology (1)
public class CarRentalOntology extends Ontology {
// The name identifying this ontology
public static final String ONTOLOGY_NAME = "Car-Rental-Ontology";
// VOCABULARY
public static final String RENTAL_ORDER = "RentalOrder";
public static final String ORDER_CAR_GROUP_ID = "carGroupID";
public static final String ORDER_PICK_UP_TIME = "pickUpTime";
public static final String ORDER_DROP_OFF_TIME = "dropOffTime";
public static final String ORDER_PICK_UP_BRANCH_ID =
"pickUpBranchID";
public static final String ORDER_DROP_OFF_BRANCH_ID =
"dropOffBranchID";
public static final String RENTAL_CAR = "RentalCar";
...
public static final String PROVIDE_CAR = "provideCar";
public static final String PROVIDE_CAR_CAR = "rentalCar";
...
Car rental ontology (2)
// Private constructor
private CarRentalOntology() {
// The car rental ontology extends the basic ontology
super(ONTOLOGY_NAME, BasicOntology.getInstance());
try {
add(new ConceptSchema(RENTAL_ORDER), RentalOrder.class);
add(new AgentActionSchema(PROVIDE_CAR), provideCar.class);
...
// Structure of the schema for the RentalOrder concept
ConceptSchema cs = (ConceptSchema) getSchema(RENTAL_ORDER);
cs.add(ORDER_CAR_GROUP_ID,
(PrimitiveSchema)getSchema(BasicOntology.STRING));
cs.add(ORDER_PICK_UP_TIME,
(PrimitiveSchema)getSchema(BasicOntology.DATE));
...
// Structure of the schema for the provideCar agent action
AgentActionSchema as1 = (AgentActionSchema)
getSchema(PROVIDE_CAR);
as1.add(PROVIDE_CAR_CAR, (ConceptSchema)
getSchema(RENTAL_CAR));
...
}
catch (OntologyException oe) {
oe.printStackTrace();
}
}
JADE Remote Monitoring Agent
JADE Sniffer Agent
Today

Workshop on using Jason and JADE
agent platforms
Next time

On 14 April 2010:
• Lecture "The Prometheus Agent-Oriented
•
Software Engineering Methodology“
Workshop on using JADE agent platform
(changed)