Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
SPADE-BDI
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
4
Issues
4
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
serfrape
SPADE-BDI
Commits
a1d9b3f0
Unverified
Commit
a1d9b3f0
authored
Jun 13, 2019
by
Sergio Frayle Pérez
Committed by
GitHub
Jun 13, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request
#1
from javipalanca/master
Refactor. Added some helpers like pause_bdi, resume_bdi. Now the asl …
parents
0cd7ef46
66ff51a8
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
219 additions
and
224 deletions
+219
-224
.travis.yml
.travis.yml
+10
-13
AUTHORS.rst
AUTHORS.rst
+1
-1
basic.py
examples/basic.py
+8
-1
control.py
examples/control.py
+41
-29
counter.asl
examples/counter.asl
+16
-17
counter.py
examples/counter.py
+29
-29
master.asl
examples/master.asl
+16
-11
receiver.py
examples/receiver.py
+10
-2
sender.asl
examples/sender.asl
+3
-3
sender.py
examples/sender.py
+11
-2
slave.asl
examples/slave.asl
+4
-4
requirements_dev.txt
requirements_dev.txt
+1
-0
setup.cfg
setup.cfg
+1
-1
bdi.py
spade_bdi/bdi.py
+67
-77
test_spade_bdi.py
tests/test_spade_bdi.py
+0
-30
tox.ini
tox.ini
+1
-4
No files found.
.travis.yml
View file @
a1d9b3f0
...
@@ -3,9 +3,6 @@
...
@@ -3,9 +3,6 @@
language
:
python
language
:
python
python
:
python
:
-
3.6
-
3.6
-
3.5
-
3.4
-
2.7
# Command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
# Command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
install
:
pip install -U tox-travis
install
:
pip install -U tox-travis
...
@@ -17,13 +14,13 @@ script: tox
...
@@ -17,13 +14,13 @@ script: tox
# create the Github repo and add it to Travis, run the
# create the Github repo and add it to Travis, run the
# following command to finish PyPI deployment setup:
# following command to finish PyPI deployment setup:
# $ travis encrypt --add deploy.password
# $ travis encrypt --add deploy.password
deploy
:
#
deploy:
provider
:
pypi
#
provider: pypi
distributions
:
sdist bdist_wheel
#
distributions: sdist bdist_wheel
user
:
sfp932705
#
user: sfp932705
password
:
#
password:
secure
:
PLEASE_REPLACE_ME
#
secure: PLEASE_REPLACE_ME
on
:
#
on:
tags
:
true
#
tags: true
repo
:
sfp932705/spade_bdi
#
repo: sfp932705/spade_bdi
python
:
3.6
#
python: 3.6
AUTHORS.rst
View file @
a1d9b3f0
...
@@ -10,4 +10,4 @@ Development Lead
...
@@ -10,4 +10,4 @@ Development Lead
Contributors
Contributors
------------
------------
None yet. Why not be the first?
* Javi Palanca <jpalanca@gmail.com>
examples/basic.py
View file @
a1d9b3f0
import
argparse
from
spade_bdi.bdi
import
BDIAgent
from
spade_bdi.bdi
import
BDIAgent
a
=
BDIAgent
(
"BasicAgent@localhost"
,
"basicpasswd"
,
"basic.asl"
)
parser
=
argparse
.
ArgumentParser
(
description
=
'spade bdi master-server example'
)
parser
.
add_argument
(
'--server'
,
type
=
str
,
default
=
"localhost"
,
help
=
'XMPP server address.'
)
parser
.
add_argument
(
'--password'
,
type
=
str
,
default
=
"bdipassword"
,
help
=
'XMPP password for the agents.'
)
args
=
parser
.
parse_args
()
a
=
BDIAgent
(
"BasicAgent@"
+
args
.
server
,
args
.
password
,
"basic.asl"
)
a
.
start
()
a
.
start
()
import
time
import
time
...
...
examples/control.py
View file @
a1d9b3f0
import
argparse
import
time
from
spade_bdi.bdi
import
BDIAgent
from
spade_bdi.bdi
import
BDIAgent
from
spade.template
import
Template
from
spade.template
import
Template
from
spade.behaviour
import
PeriodicBehaviour
from
spade.behaviour
import
PeriodicBehaviour
from
spade.behaviour
import
TimeoutBehaviour
from
spade.behaviour
import
TimeoutBehaviour
from
datetime
import
datetime
from
datetime
import
datetime
from
datetime
import
timedelta
from
datetime
import
timedelta
from
spade.agent
import
Agent
class
MasterAgent
(
BDIAgent
):
class
MasterAgent
(
BDIAgent
):
async
def
setup
(
self
):
async
def
setup
(
self
):
template
=
Template
(
metadata
=
{
"performative"
:
"BDI"
})
self
.
add_behaviour
(
self
.
BDIBehaviour
(),
template
)
template
=
Template
(
metadata
=
{
"performative"
:
"Modify"
})
template
=
Template
(
metadata
=
{
"performative"
:
"Modify"
})
self
.
add_behaviour
(
self
.
Modify
(
self
.
add_behaviour
(
self
.
Modify
(
period
=
5
,
start_at
=
datetime
.
now
()),
template
)
period
=
5
,
start_at
=
datetime
.
now
()),
template
)
template
=
Template
(
metadata
=
{
"performative"
:
"Ending"
})
template
=
Template
(
metadata
=
{
"performative"
:
"Ending"
})
self
.
add_behaviour
(
self
.
Behav4
(
self
.
add_behaviour
(
self
.
Behav4
(
start_at
=
datetime
.
now
()
+
timedelta
(
seconds
=
11
)),
template
)
start_at
=
datetime
.
now
()
+
timedelta
(
seconds
=
11
)),
template
)
class
Modify
(
PeriodicBehaviour
):
class
Modify
(
PeriodicBehaviour
):
async
def
run
(
self
):
async
def
run
(
self
):
...
@@ -38,25 +35,40 @@ class MasterAgent(BDIAgent):
...
@@ -38,25 +35,40 @@ class MasterAgent(BDIAgent):
self
.
agent
.
bdi
.
remove_belief
(
'tipo'
,
'dec'
)
self
.
agent
.
bdi
.
remove_belief
(
'tipo'
,
'dec'
)
import
time
def
main
(
server
,
password
):
b
=
BDIAgent
(
"slave_1@{}"
.
format
(
server
),
password
,
"slave.asl"
)
b
.
bdi
.
set_belief
(
"master"
,
"master@{}"
.
format
(
server
))
future
=
b
.
start
()
future
.
result
()
c
=
BDIAgent
(
"slave_2@{}"
.
format
(
server
),
password
,
"slave.asl"
)
c
.
pause_bdi
()
future
=
c
.
start
()
future
.
result
()
a
=
MasterAgent
(
"master@{}"
.
format
(
server
),
password
,
"master.asl"
)
a
.
bdi
.
set_belief
(
"slave1"
,
"slave_1@{}"
.
format
(
server
))
a
.
bdi
.
set_belief
(
"slave2"
,
"slave_2@{}"
.
format
(
server
))
a
.
bdi
.
set_belief
(
'tipo'
,
'dec'
)
future
=
a
.
start
()
future
.
result
()
time
.
sleep
(
5
)
print
(
"Enabling BDI for slave2"
)
c
.
set_asl
(
"slave.asl"
)
c
.
bdi
.
set_belief
(
"master"
,
"master@{}"
.
format
(
server
))
time
.
sleep
(
5
)
print
(
"Disabling BDI for slave2"
)
c
.
pause_bdi
()
if
__name__
==
'__main__'
:
parser
=
argparse
.
ArgumentParser
(
description
=
'spade bdi master-server example'
)
parser
.
add_argument
(
'--server'
,
type
=
str
,
default
=
"localhost"
,
help
=
'XMPP server address.'
)
parser
.
add_argument
(
'--password'
,
type
=
str
,
default
=
"bdipassword"
,
help
=
'XMPP password for the agents.'
)
args
=
parser
.
parse_args
()
b
=
BDIAgent
(
"slave_1@localhost"
,
"bdisimple"
,
"slave.asl"
)
try
:
future
=
b
.
start
()
main
(
args
.
server
,
args
.
password
)
future
.
result
()
except
KeyboardInterrupt
:
print
(
"Exiting..."
)
c
=
BDIAgent
(
"slave_2@localhost"
,
"bdisimple3"
)
future
=
c
.
start
()
future
.
result
()
a
=
MasterAgent
(
"master@localhost"
,
"bdimaster"
)
future
=
a
.
start
()
future
.
result
()
a
.
set_asl
(
"master.asl"
)
a
.
bdi
.
set_belief
(
'tipo'
,
'dec'
)
time
.
sleep
(
5
)
print
(
"Enabling BDI for slave2"
)
c
.
set_asl
(
"slave.asl"
)
time
.
sleep
(
5
)
print
(
"Disabling BDI for slave2"
)
c
.
set_asl
(
None
)
examples/counter.asl
View file @
a1d9b3f0
co
ntado
r(8).
co
unte
r(8).
t
ipo
(dec).
t
ype
(dec).
!ini
cio
.
!ini
t
.
+!ini
cio
+!ini
t
<-
<-
.print("
Iniciando
....");
.print("
Starting
....");
!obj2.
!obj2.
+!obj2: t
ipo
(inc)
+!obj2: t
ype
(inc)
<-
<-
.print("Incre
mentando
");
.print("Incre
asing
");
?co
ntado
r(X);
?co
unte
r(X);
-+co
ntado
r(X+1);
-+co
unte
r(X+1);
.wait(1000);
.wait(1000);
!obj2.
!obj2.
+!obj2: t
ipo
(dec)
+!obj2: t
ype
(dec)
<-
<-
.print("Decre
mentando
");
.print("Decre
asing
");
?co
ntado
r(X);
?co
unte
r(X);
-+co
ntado
r(X-1);
-+co
unte
r(X-1);
.wait(1000);
.wait(1000);
!obj2.
!obj2.
+!obj2: not t
ipo
(_)
+!obj2: not t
ype
(_)
<-
<-
.print("
Esperando
");
.print("
Waiting
");
.wait(1000);
.wait(1000);
!obj2.
!obj2.
\ No newline at end of file
examples/counter.py
View file @
a1d9b3f0
import
argparse
from
spade_bdi.bdi
import
BDIAgent
from
spade_bdi.bdi
import
BDIAgent
from
spade.template
import
Template
from
spade.template
import
Template
from
spade.behaviour
import
PeriodicBehaviour
from
spade.behaviour
import
PeriodicBehaviour
from
spade.behaviour
import
TimeoutBehaviour
from
spade.behaviour
import
TimeoutBehaviour
from
datetime
import
datetime
from
datetime
import
datetime
from
datetime
import
timedelta
from
datetime
import
timedelta
from
spade.agent
import
Agent
class
CounterAgent
(
BDIAgent
):
class
CounterAgent
(
BDIAgent
):
async
def
setup
(
self
):
async
def
setup
(
self
):
template
=
Template
(
metadata
=
{
"performative"
:
"BDI"
})
self
.
add_behaviour
(
self
.
BDIBehaviour
(),
template
)
template
=
Template
(
metadata
=
{
"performative"
:
"B1"
})
template
=
Template
(
metadata
=
{
"performative"
:
"B1"
})
self
.
add_behaviour
(
self
.
Behav1
(
self
.
add_behaviour
(
self
.
Behav1
(
period
=
1
,
start_at
=
datetime
.
now
()),
template
)
period
=
1
,
start_at
=
datetime
.
now
()),
template
)
template
=
Template
(
metadata
=
{
"performative"
:
"B2"
})
template
=
Template
(
metadata
=
{
"performative"
:
"B2"
})
self
.
add_behaviour
(
self
.
Behav2
(
self
.
add_behaviour
(
self
.
Behav2
(
period
=
5
,
start_at
=
datetime
.
now
()),
template
)
period
=
5
,
start_at
=
datetime
.
now
()),
template
)
template
=
Template
(
metadata
=
{
"performative"
:
"B3"
})
template
=
Template
(
metadata
=
{
"performative"
:
"B3"
})
self
.
add_behaviour
(
self
.
Behav3
(
self
.
add_behaviour
(
self
.
Behav3
(
period
=
10
,
start_at
=
datetime
.
now
()),
template
)
period
=
10
,
start_at
=
datetime
.
now
()),
template
)
template
=
Template
(
metadata
=
{
"performative"
:
"B4"
})
template
=
Template
(
metadata
=
{
"performative"
:
"B4"
})
self
.
add_behaviour
(
self
.
Behav4
(
self
.
add_behaviour
(
self
.
Behav4
(
start_at
=
datetime
.
now
()
+
timedelta
(
seconds
=
60
)),
template
)
start_at
=
datetime
.
now
()
+
timedelta
(
seconds
=
60
)),
template
)
class
Behav1
(
PeriodicBehaviour
):
class
Behav1
(
PeriodicBehaviour
):
async
def
on_start
(
self
):
async
def
on_start
(
self
):
self
.
co
ntador
=
self
.
agent
.
bdi
.
get_belief_value
(
"contado
r"
)[
0
]
self
.
co
unter
=
self
.
agent
.
bdi
.
get_belief_value
(
"counte
r"
)[
0
]
async
def
run
(
self
):
async
def
run
(
self
):
if
self
.
contador
!=
self
.
agent
.
bdi
.
get_belief_value
(
"contador"
)[
0
]:
if
self
.
counter
!=
self
.
agent
.
bdi
.
get_belief_value
(
"counter"
)[
0
]:
self
.
contador
=
self
.
agent
.
bdi
.
get_belief_value
(
"contador"
)[
self
.
counter
=
self
.
agent
.
bdi
.
get_belief_value
(
"counter"
)[
0
]
0
]
print
(
self
.
agent
.
bdi
.
get_belief
(
"counter"
))
print
(
self
.
agent
.
bdi
.
get_belief
(
"contador"
))
class
Behav2
(
PeriodicBehaviour
):
class
Behav2
(
PeriodicBehaviour
):
async
def
run
(
self
):
async
def
run
(
self
):
self
.
agent
.
bdi
.
set_belief
(
'co
ntado
r'
,
0
)
self
.
agent
.
bdi
.
set_belief
(
'co
unte
r'
,
0
)
class
Behav3
(
PeriodicBehaviour
):
class
Behav3
(
PeriodicBehaviour
):
async
def
run
(
self
):
async
def
run
(
self
):
try
:
try
:
t
ipo
=
self
.
agent
.
bdi
.
get_belief_value
(
"tipo
"
)[
0
]
t
ype
=
self
.
agent
.
bdi
.
get_belief_value
(
"type
"
)[
0
]
if
t
ipo
==
'inc'
:
if
t
ype
==
'inc'
:
self
.
agent
.
bdi
.
set_belief
(
't
ipo
'
,
'dec'
)
self
.
agent
.
bdi
.
set_belief
(
't
ype
'
,
'dec'
)
else
:
else
:
self
.
agent
.
bdi
.
set_belief
(
't
ipo
'
,
'inc'
)
self
.
agent
.
bdi
.
set_belief
(
't
ype
'
,
'inc'
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
"No belief 't
ipo
'."
)
print
(
"No belief 't
ype
'."
)
class
Behav4
(
TimeoutBehaviour
):
class
Behav4
(
TimeoutBehaviour
):
async
def
run
(
self
):
async
def
run
(
self
):
self
.
agent
.
bdi
.
remove_belief
(
'tipo'
,
'inc'
)
self
.
agent
.
bdi
.
remove_belief
(
'type'
,
'inc'
)
self
.
agent
.
bdi
.
remove_belief
(
'tipo'
,
'dec'
)
self
.
agent
.
bdi
.
remove_belief
(
'type'
,
'dec'
)
if
__name__
==
"__main__"
:
parser
=
argparse
.
ArgumentParser
(
description
=
'spade bdi master-server example'
)
parser
.
add_argument
(
'--server'
,
type
=
str
,
default
=
"localhost"
,
help
=
'XMPP server address.'
)
parser
.
add_argument
(
'--password'
,
type
=
str
,
default
=
"bdipassword"
,
help
=
'XMPP password for the agents.'
)
args
=
parser
.
parse_args
()
a
=
CounterAgent
(
"counter@localhost"
,
"bditest"
,
"counter.asl"
)
a
=
CounterAgent
(
"counter@"
+
args
.
server
,
args
.
password
,
"counter.asl"
)
a
.
start
()
a
.
start
()
import
time
import
time
time
.
sleep
(
1
)
time
.
sleep
(
1
)
print
(
"started"
)
print
(
"started"
)
examples/master.asl
View file @
a1d9b3f0
...
@@ -6,23 +6,29 @@
...
@@ -6,23 +6,29 @@
!obj2.
!obj2.
+!obj2: tipo(inc)
+!obj2: tipo(inc)
<-
<-
.send("slave_1@localhost", tell, incrementar(2));
?slave1(X);
.send("slave_2@localhost", tell, incrementar(5));
?slave2(Y);
.send(X, tell, incrementar(2));
.send(Y, tell, incrementar(5));
.wait(2000);
.wait(2000);
!obj2.
!obj2.
+!obj2: tipo(dec)
+!obj2: tipo(dec)
<-
<-
.send("slave_1@localhost", tell, decrementar(2));
?slave1(X);
.send("slave_2@localhost", tell, decrementar(5));
?slave2(Y);
.send(X, tell, decrementar(2));
.send(Y, tell, decrementar(5));
.wait(2000);
.wait(2000);
!obj2.
!obj2.
+!obj2: not tipo(_)
+!obj2: not tipo(_)
<-
<-
?slave1(X);
?slave2(Y);
.print("Finishing");
.print("Finishing");
.send("slave_1@localhost", untell, incrementar(2));
.send(X, untell, incrementar(2));
.send("slave_2@localhost", untell, incrementar(5));
.send(Y, untell, incrementar(5));
.send("slave_1@localhost", untell, decrementar(2));
.send(X, untell, decrementar(2));
.send("slave_2@localhost", untell, decrementar(5)).
.send(Y, untell, decrementar(5)).
\ No newline at end of file
examples/receiver.py
View file @
a1d9b3f0
import
argparse
from
spade_bdi.bdi
import
BDIAgent
from
spade_bdi.bdi
import
BDIAgent
a
=
BDIAgent
(
"BDIReceiverAgent@localhost"
,
"receiverpasswd"
,
"receiver.asl"
)
if
__name__
==
'__main__'
:
a
.
start
()
parser
=
argparse
.
ArgumentParser
(
description
=
'spade bdi master-server example'
)
parser
.
add_argument
(
'--server'
,
type
=
str
,
default
=
"localhost"
,
help
=
'XMPP server address.'
)
parser
.
add_argument
(
'--password'
,
type
=
str
,
default
=
"bdipassword"
,
help
=
'XMPP password for the agents.'
)
args
=
parser
.
parse_args
()
a
=
BDIAgent
(
"BDIReceiverAgent@"
+
args
.
server
,
args
.
password
,
"receiver.asl"
)
a
.
start
()
examples/sender.asl
View file @
a1d9b3f0
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
+!start
+!start
<-
<-
?receiver(X);
.print("sending a message ...");
.print("sending a message ...");
.send("BDIReceiverAgent@localhost", achieve, hello("Hello World!"));
.send(X, achieve, hello("Hello World!"));
.print("sent a message").
.print("sent a message").
\ No newline at end of file
examples/sender.py
View file @
a1d9b3f0
import
argparse
from
spade_bdi.bdi
import
BDIAgent
from
spade_bdi.bdi
import
BDIAgent
a
=
BDIAgent
(
"BDISenderAgent@localhost"
,
"senderpasswd"
,
"sender.asl"
)
if
__name__
==
'__main__'
:
a
.
start
()
parser
=
argparse
.
ArgumentParser
(
description
=
'spade bdi master-server example'
)
parser
.
add_argument
(
'--server'
,
type
=
str
,
default
=
"localhost"
,
help
=
'XMPP server address.'
)
parser
.
add_argument
(
'--password'
,
type
=
str
,
default
=
"bdipassword"
,
help
=
'XMPP password for the agents.'
)
args
=
parser
.
parse_args
()
a
=
BDIAgent
(
"BDISenderAgent@"
+
args
.
server
,
args
.
password
,
"sender.asl"
)
a
.
bdi
.
set_belief
(
"receiver"
,
"BDIReceiverAgent@"
+
args
.
server
)
a
.
start
()
examples/slave.asl
View file @
a1d9b3f0
contador(0).
contador(0).
+incrementar(Inc)[source(S)]:
.substring("master@localhost",S,R)
+incrementar(Inc)[source(S)]:
master(M) & .substring(M,S,R)
<-
<-
.print("increasing");
.print("increasing");
?contador(X);
?contador(X);
.print(X);
.print(X);
-+contador(X+Inc).
-+contador(X+Inc).
+decrementar(Dec)[source(S)]:
.substring("master@localhost",S,R)
+decrementar(Dec)[source(S)]:
master(M) & .substring(M,S,R)
<-
<-
.print("decreasing");
.print("decreasing");
?contador(X);
?contador(X);
.print(X);
.print(X);
-+contador(X-Dec).
-+contador(X-Dec).
-incrementar(Inc)[source(S)]:
.substring("master@localhost",S,R)
-incrementar(Inc)[source(S)]:
master(M) & .substring(M,S,R)
<-
<-
.print("DELETING incrementar BELIEF from an untell message").
.print("DELETING incrementar BELIEF from an untell message").
-decrementar(Dec)[source(S)]:
.substring("master@localhost",S,R)
-decrementar(Dec)[source(S)]:
master(M) & .substring(M,S,R)
<-
<-
.print("DELETING decrementar BELIEF from an untell message").
.print("DELETING decrementar BELIEF from an untell message").
requirements_dev.txt
View file @
a1d9b3f0
...
@@ -7,6 +7,7 @@ tox==3.5.2
...
@@ -7,6 +7,7 @@ tox==3.5.2
coverage==4.5.1
coverage==4.5.1
Sphinx==1.8.1
Sphinx==1.8.1
twine==1.12.1
twine==1.12.1
Click==7.0
pytest==3.8.2
pytest==3.8.2
pytest-runner==4.2
pytest-runner==4.2
setup.cfg
View file @
a1d9b3f0
[bumpversion]
[bumpversion]
current_version = 0.1.0
current_version = 0.1.0
commit = True
commit = True
tag =
Tru
e
tag =
Fals
e
[bumpversion:file:setup.py]
[bumpversion:file:setup.py]
search = version='{current_version}'
search = version='{current_version}'
...
...
spade_bdi/bdi.py
View file @
a1d9b3f0
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import
asyncio
import
asyncio
import
json
import
time
from
ast
import
literal_eval
from
ast
import
literal_eval
from
loguru
import
logger
from
loguru
import
logger
from
collections
import
deque
from
collections
import
deque
...
@@ -12,82 +12,85 @@ from spade.agent import Agent
...
@@ -12,82 +12,85 @@ from spade.agent import Agent
from
spade.template
import
Template
from
spade.template
import
Template
from
spade.message
import
Message
from
spade.message
import
Message
PERCEPT_TAG
=
frozenset
(
PERCEPT_TAG
=
frozenset
([
asp
.
Literal
(
"source"
,
(
asp
.
Literal
(
"percept"
),))])
[
asp
.
Literal
(
"source"
,
(
asp
.
Literal
(
"percept"
),
))])
class
BDIAgent
(
Agent
):
class
BDIAgent
(
Agent
):
async
def
setup
(
self
):
def
__init__
(
self
,
jid
:
str
,
password
:
str
,
asl
:
str
,
*
args
,
**
kwargs
):
self
.
asl_file
=
asl
self
.
bdi_enabled
=
False
self
.
bdi_intention_buffer
=
deque
()
self
.
bdi
=
None
self
.
bdi_agent
=
None
super
()
.
__init__
(
jid
,
password
,
*
args
,
**
kwargs
)
while
not
self
.
loop
:
time
.
sleep
(
0.01
)
template
=
Template
(
metadata
=
{
"performative"
:
"BDI"
})
template
=
Template
(
metadata
=
{
"performative"
:
"BDI"
})
self
.
add_behaviour
(
self
.
BDIBehaviour
(),
template
)
self
.
add_behaviour
(
self
.
BDIBehaviour
(),
template
)
await
super
()
.
setup
()
self
.
bdi_env
=
asp
.
runtime
.
Environment
()
self
.
bdi_actions
=
asp
.
Actions
(
asp
.
stdlib
.
actions
)
self
.
bdi
.
add_actions
()
self
.
_load_asl
()
def
pause_bdi
(
self
):
self
.
bdi_enabled
=
False
def
resume_bdi
(
self
):
self
.
bdi_enabled
=
True
def
add_behaviour
(
self
,
behaviour
,
template
=
None
):
def
add_behaviour
(
self
,
behaviour
,
template
=
None
):
if
type
(
behaviour
)
==
self
.
BDIBehaviour
:
if
type
(
behaviour
)
==
self
.
BDIBehaviour
:
self
.
bdi
=
behaviour
self
.
bdi
=
behaviour
self
.
set_env
()
super
()
.
add_behaviour
(
behaviour
,
template
)
super
()
.
add_behaviour
(
behaviour
,
template
)
def
set_asl
(
self
,
asl_file
=
None
):
def
set_asl
(
self
,
asl_file
:
str
):
if
not
asl_file
:
self
.
asl_file
=
asl_file
self
.
bdi_enabled
=
False
self
.
_load_asl
()
def
_load_asl
(
self
):
self
.
pause_bdi
()
try
:
with
open
(
self
.
asl_file
)
as
source
:
self
.
bdi_agent
=
self
.
bdi_env
.
build_agent
(
source
,
self
.
bdi_actions
)
self
.
bdi_agent
.
name
=
self
.
jid
self
.
resume_bdi
()
except
FileNotFoundError
:
logger
.
info
(
"Warning: ASL specified for {} does not exist. Disabling BDI."
.
format
(
self
.
jid
))
self
.
asl_file
=
None
self
.
asl_file
=
None
else
:
self
.
pause_bdi
()
try
:
with
open
(
asl_file
)
as
source
:
self
.
bdi_agent
=
self
.
bdi_env
.
build_agent
(
source
,
self
.
bdi_actions
)
self
.
bdi_agent
.
name
=
self
.
jid
self
.
bdi_enabled
=
True
self
.
asl_file
=
asl_file
except
FileNotFoundError
:
logger
.
info
(
"Warning: ASL specified for {} does not exist. Disabling BDI."
.
format
(
self
.
jid
))
self
.
bdi_enabled
=
False
self
.
asl_file
=
None
def
set_env
(
self
):
self
.
bdi_env
=
asp
.
runtime
.
Environment
()
self
.
bdi_actions
=
asp
.
Actions
(
asp
.
stdlib
.
actions
)
def
__init__
(
self
,
jid
,
password
,
asl
=
None
,
*
args
,
**
kwargs
):
self
.
asl_file
=
asl
self
.
bdi_enabled
=
False
self
.
bdi_intention_buffer
=
deque
()
super
()
.
__init__
(
jid
,
password
,
*
args
,
**
kwargs
)
class
BDIBehaviour
(
CyclicBehaviour
):
class
BDIBehaviour
(
CyclicBehaviour
):
def
add_actions
(
self
):
def
add_actions
(
self
):
@self.agent.bdi_actions.add
(
".send"
,
3
)
@self.agent.bdi_actions.add
(
".send"
,
3
)
def
_send
(
agent
,
term
,
intention
):
def
_send
(
agent
,
term
,
intention
):
receiver
=
asp
.
grounded
(
term
.
args
[
0
],
intention
.
scope
)
receiver
=
str
(
asp
.
grounded
(
term
.
args
[
0
],
intention
.
scope
)
)
ilf
=
asp
.
grounded
(
term
.
args
[
1
],
intention
.
scope
)
ilf
=
asp
.
grounded
(
term
.
args
[
1
],
intention
.
scope
)
if
not
asp
.
is_atom
(
ilf
):
if
not
asp
.
is_atom
(
ilf
):
return
return
ilf_type
=
ilf
.
functor
ilf_type
=
ilf
.
functor
mdata
=
{
"performative"
:
"BDI"
,
mdata
=
{
"performative"
:
"BDI"
,
"ilf_type"
:
ilf_type
,
}
"ilf_type"
:
ilf_type
,
}
body
=
asp
.
asl_str
(
asp
.
freeze
(
term
.
args
[
2
],
intention
.
scope
,
{}))
body
=
asp
.
asl_str
(
asp
.
freeze
(
term
.
args
[
2
],
intention
.
scope
,
{}))
msg
=
Message
(
to
=
receiver
,
body
=
body
,
metadata
=
mdata
)
msg
=
Message
(
to
=
receiver
,
body
=
body
,
metadata
=
mdata
)
self
.
agent
.
submit
(
self
.
send
(
msg
))
self
.
agent
.
submit
(
self
.
send
(
msg
))
yield
yield
@self.agent.bdi_actions.add
(
".custom_action"
,
1
)
@self.agent.bdi_actions.add
(
".custom_action"
,
1
)
def
_custom_action
(
agent
,
term
,
intention
):
def
_custom_action
(
agent
,
term
,
intention
):
arg_0
=
asp
.
grounded
(
term
.
args
[
0
],
intention
.
scope
)
asp
.
grounded
(
term
.
args
[
0
],
intention
.
scope
)
print
(
arg_0
)
yield
yield
@self.agent.bdi_actions.add_function
(
".a_function"
,
(
int
,))
@self.agent.bdi_actions.add_function
(
".a_function"
,
(
int
,))
def
_a_function
(
x
):
def
_a_function
(
x
):
return
x
**
4
return
x
**
4
@self.agent.bdi_actions.add_function
(
"literal_function"
,
(
asp
.
Literal
,))
@self.agent.bdi_actions.add_function
(
"literal_function"
,
(
asp
.
Literal
,))
def
_literal_function
(
x
):
def
_literal_function
(
x
):
return
x
return
x
def
set_belief
(
self
,
name
,
*
args
):
def
set_belief
(
self
,
name
:
str
,
*
args
):
"""Set an agent's belief. If it already exists, updates it."""
"""Set an agent's belief. If it already exists, updates it."""
new_args
=
()
new_args
=
()
for
x
in
args
:
for
x
in
args
:
...
@@ -107,7 +110,7 @@ class BDIAgent(Agent):
...
@@ -107,7 +110,7 @@ class BDIAgent(Agent):
self
.
agent
.
bdi_intention_buffer
.
append
((
asp
.
Trigger
.
addition
,
asp
.
GoalType
.
belief
,
term
,
self
.
agent
.
bdi_intention_buffer
.
append
((
asp
.
Trigger
.
addition
,
asp
.
GoalType
.
belief
,
term
,
asp
.
runtime
.
Intention
()))
asp
.
runtime
.
Intention
()))
def
remove_belief
(
self
,
name
,
*
args
):
def
remove_belief
(
self
,
name
:
str
,
*
args
):
"""Remove an existing agent's belief."""
"""Remove an existing agent's belief."""
new_args
=
()
new_args
=
()
for
x
in
args
:
for
x
in
args
:
...
@@ -119,22 +122,25 @@ class BDIAgent(Agent):
...
@@ -119,22 +122,25 @@ class BDIAgent(Agent):
self
.
agent
.
bdi_intention_buffer
.
append
((
asp
.
Trigger
.
removal
,
asp
.
GoalType
.
belief
,
term
,
self
.
agent
.
bdi_intention_buffer
.
append
((
asp
.
Trigger
.
removal
,
asp
.
GoalType
.
belief
,
term
,
asp
.
runtime
.
Intention
()))
asp
.
runtime
.
Intention
()))
def
get_belief
(
self
,
key
,
source
=
False
):
def
get_belief
(
self
,
key
:
str
,
source
=
False
):
"""Get an agent's existing belief. The first belief matching
"""Get an agent's existing belief. The first belief matching
<key> is returned. Keep <source> False to strip source."""
<key> is returned. Keep <source> False to strip source."""
key
=
str
(
key
)
key
=
str
(
key
)
for
beliefs
in
self
.
agent
.
bdi_agent
.
beliefs
:
for
beliefs
in
self
.
agent
.
bdi_agent
.
beliefs
:
if
beliefs
[
0
]
==
key
:
if
beliefs
[
0
]
==
key
:
raw_belief
=
(
raw_belief
=
(
str
(
list
(
self
.
agent
.
bdi_agent
.
beliefs
[
beliefs
])[
0
]))
str
(
list
(
self
.
agent
.
bdi_agent
.
beliefs
[
beliefs
])[
0
]))
raw_belief
=
self
.
_remove_source
(
raw_belief
,
source
)
if
')[source'
in
raw_belief
and
not
source
:
raw_belief
=
raw_belief
.
split
(
'['
)[
0
]
.
replace
(
'"'
,
''
)
belief
=
raw_belief
belief
=
raw_belief
return
belief
return
belief
return
None
return
None
def
get_belief_value
(
self
,
key
):
@staticmethod
def
_remove_source
(
belief
,
source
):
if
')[source'
in
belief
and
not
source
:
belief
=
belief
.
split
(
'['
)[
0
]
.
replace
(
'"'
,
''
)
return
belief
def
get_belief_value
(
self
,
key
:
str
):
"""Get an agent's existing value or values of the <key> belief. The first belief matching
"""Get an agent's existing value or values of the <key> belief. The first belief matching
<key> is returned"""
<key> is returned"""
belief
=
self
.
get_belief
(
key
)
belief
=
self
.
get_belief
(
key
)
...
@@ -148,10 +154,8 @@ class BDIAgent(Agent):
...
@@ -148,10 +154,8 @@ class BDIAgent(Agent):
belief_list
=
[]
belief_list
=
[]
for
beliefs
in
self
.
agent
.
bdi_agent
.
beliefs
:
for
beliefs
in
self
.
agent
.
bdi_agent
.
beliefs
:
try
:
try
:
raw_belief
=
(
raw_belief
=
(
str
(
list
(
self
.
agent
.
bdi_agent
.
beliefs
[
beliefs
])[
0
]))
str
(
list
(
self
.
agent
.
bdi_agent
.
beliefs
[
beliefs
])[
0
]))
raw_belief
=
self
.
_remove_source
(
raw_belief
,
source
)
if
')[source('
in
raw_belief
and
not
source
:
raw_belief
=
raw_belief
.
split
(
'['
)[
0
]
.
replace
(
'"'
,
''
)
belief_list
.
append
(
raw_belief
)
belief_list
.
append
(
raw_belief
)
except
IndexError
:
except
IndexError
:
pass
pass
...
@@ -161,20 +165,7 @@ class BDIAgent(Agent):
...
@@ -161,20 +165,7 @@ class BDIAgent(Agent):
"""Print agent's beliefs.Keep <source> False to strip source."""
"""Print agent's beliefs.Keep <source> False to strip source."""
for
beliefs
in
self
.
agent
.
bdi_agent
.
beliefs
.
values
():
for
beliefs
in
self
.
agent
.
bdi_agent
.
beliefs
.
values
():
for
belief
in
beliefs
:
for
belief
in
beliefs
:
if
')[source('
in
str
(
belief
)
and
not
source
:
print
(
self
.
_remove_source
(
str
(
belief
),
source
))
belief
=
str
(
belief
)
.
split
(
'['
)[
0
]
.
replace
(
'"'
,
''
)
print
(
belief
)
async
def
on_start
(
self
):
"""
Coroutine called before the behaviour is started.
"""
self
.
add_actions
()
if
self
.
agent
.
asl_file
:
self
.
agent
.
set_asl
(
self
.
agent
.
asl_file
)
else
:
logger
.
info
(
"Warning: no ASL specified for {}."
.
format
(
self
.
agent
.
jid
))
async
def
run
(
self
):
async
def
run
(
self
):
"""
"""
...
@@ -195,31 +186,30 @@ class BDIAgent(Agent):
...
@@ -195,31 +186,30 @@ class BDIAgent(Agent):
goal_type
=
asp
.
GoalType
.
achievement
goal_type
=
asp
.
GoalType
.
achievement
trigger
=
asp
.
Trigger
.
addition
trigger
=
asp
.
Trigger
.
addition
else
:
else
:
raise
asp
.
AslError
(
raise
asp
.
AslError
(
"unknown illocutionary force: {}"
.
format
(
ilf_type
))
"unknown illocutionary force:
%
s"
%
ilf_type
)
intention
=
asp
.
runtime
.
Intention
()
intention
=
asp
.
runtime
.
Intention
()
functor
,
args
=
await
parse_literal
(
msg
.
body
)
functor
,
args
=
parse_literal
(
msg
.
body
)
message
=
asp
.
Literal
(
functor
,
args
)
message
=
asp
.
Literal
(
functor
,
args
)
message
=
asp
.
freeze
(
message
,
intention
.
scope
,
{})
message
=
asp
.
freeze
(
message
,
intention
.
scope
,
{})
tagged_message
=
message
.
with_annotation
(
tagged_message
=
message
.
with_annotation
(
asp
.
Literal
(
"source"
,
(
asp
.
Literal
(
str
(
msg
.
sender
)),)))
asp
.
Literal
(
"source"
,
(
asp
.
Literal
(
str
(
msg
.
sender
)),
)))
self
.
agent
.
bdi_intention_buffer
.
append
((
trigger
,
goal_type
,
tagged_message
,
intention
))
self
.
agent
.
bdi_intention_buffer
.
append
((
trigger
,
goal_type
,
tagged_message
,
intention
))
if
self
.
agent
.
bdi_intention_buffer
:
if
self
.
agent
.
bdi_intention_buffer
:
temp_intentions
=
deque
(
self
.
agent
.
bdi_intention_buffer
)
temp_intentions
=
deque
(
self
.
agent
.
bdi_intention_buffer
)
for
trigger
,
goal_type
,
term
,
intention
in
temp_intentions
:
for
trigger
,
goal_type
,
term
,
intention
in
temp_intentions
:
self
.
agent
.
bdi_agent
.
call
(
self
.
agent
.
bdi_agent
.
call
(
trigger
,
goal_type
,
term
,
intention
)
trigger
,
goal_type
,
term
,
intention
)
self
.
agent
.
bdi_agent
.
step
()
self
.
agent
.
bdi_agent
.
step
()
self
.
agent
.
bdi_intention_buffer
.
popleft
()
self
.
agent
.
bdi_intention_buffer
.
popleft
()
else
:
else
:
self
.
agent
.
bdi_agent
.
step
()
self
.
agent
.
bdi_agent
.
step
()
else
:
await
asyncio
.
sleep
(
0.1
)
async
def
parse_literal
(
msg
):
def
parse_literal
(
msg
):
functor
=
msg
.
split
(
"("
)[
0
]
functor
=
msg
.
split
(
"("
)[
0
]
if
"("
in
msg
:
if
"("
in
msg
:
args
=
msg
.
split
(
"("
)[
1
]
args
=
msg
.
split
(
"("
)[
1
]
...
...
tests/test_spade_bdi.py
View file @
a1d9b3f0
...
@@ -5,34 +5,4 @@
...
@@ -5,34 +5,4 @@
import
pytest
import
pytest
from
click.testing
import
CliRunner
from
spade_bdi
import
spade_bdi
from
spade_bdi
import
cli
@pytest.fixture
def
response
():
"""Sample pytest fixture.
See more at: http://doc.pytest.org/en/latest/fixture.html
"""
# import requests
# return requests.get('https://github.com/audreyr/cookiecutter-pypackage')
def
test_content
(
response
):
"""Sample pytest test function with the pytest fixture as an argument."""
# from bs4 import BeautifulSoup
# assert 'GitHub' in BeautifulSoup(response.content).title.string
def
test_command_line_interface
():
"""Test the CLI."""
runner
=
CliRunner
()
result
=
runner
.
invoke
(
cli
.
main
)
assert
result
.
exit_code
==
0
assert
'spade_bdi.cli.main'
in
result
.
output
help_result
=
runner
.
invoke
(
cli
.
main
,
[
'--help'
])
assert
help_result
.
exit_code
==
0
assert
'--help Show this message and exit.'
in
help_result
.
output
tox.ini
View file @
a1d9b3f0
[tox]
[tox]
envlist
=
py
27, py34, py35, py
36, flake8
envlist
=
py36, flake8
[travis]
[travis]
python
=
python
=
3.6:
py36
3.6:
py36
3.5:
py35
3.4:
py34
2.7:
py27
[testenv:flake8]
[testenv:flake8]
basepython
=
python
basepython
=
python
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment