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
66ff51a8
Commit
66ff51a8
authored
Jun 13, 2019
by
jpalanca
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor. Added some helpers like pause_bdi, resume_bdi. Now the asl file in the…
Refactor. Added some helpers like pause_bdi, resume_bdi. Now the asl file in the constructor is mandatory.
parent
0cd7ef46
Show whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
212 additions
and
215 deletions
+212
-215
.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
+15
-15
counter.py
examples/counter.py
+29
-29
master.asl
examples/master.asl
+14
-9
receiver.py
examples/receiver.py
+10
-2
sender.asl
examples/sender.asl
+2
-1
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
+64
-74
test_spade_bdi.py
tests/test_spade_bdi.py
+0
-30
tox.ini
tox.ini
+1
-4
No files found.
.travis.yml
View file @
66ff51a8
...
...
@@ -3,9 +3,6 @@
language
:
python
python
:
-
3.6
-
3.5
-
3.4
-
2.7
# Command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
install
:
pip install -U tox-travis
...
...
@@ -17,13 +14,13 @@ script: tox
# create the Github repo and add it to Travis, run the
# following command to finish PyPI deployment setup:
# $ travis encrypt --add deploy.password
deploy
:
provider
:
pypi
distributions
:
sdist bdist_wheel
user
:
sfp932705
password
:
secure
:
PLEASE_REPLACE_ME
on
:
tags
:
true
repo
:
sfp932705/spade_bdi
python
:
3.6
#
deploy:
#
provider: pypi
#
distributions: sdist bdist_wheel
#
user: sfp932705
#
password:
#
secure: PLEASE_REPLACE_ME
#
on:
#
tags: true
#
repo: sfp932705/spade_bdi
#
python: 3.6
AUTHORS.rst
View file @
66ff51a8
...
...
@@ -10,4 +10,4 @@ Development Lead
Contributors
------------
None yet. Why not be the first?
* Javi Palanca <jpalanca@gmail.com>
examples/basic.py
View file @
66ff51a8
import
argparse
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
()
import
time
...
...
examples/control.py
View file @
66ff51a8
import
argparse
import
time
from
spade_bdi.bdi
import
BDIAgent
from
spade.template
import
Template
from
spade.behaviour
import
PeriodicBehaviour
from
spade.behaviour
import
TimeoutBehaviour
from
datetime
import
datetime
from
datetime
import
timedelta
from
spade.agent
import
Agent
class
MasterAgent
(
BDIAgent
):
async
def
setup
(
self
):
template
=
Template
(
metadata
=
{
"performative"
:
"BDI"
})
self
.
add_behaviour
(
self
.
BDIBehaviour
(),
template
)
template
=
Template
(
metadata
=
{
"performative"
:
"Modify"
})
self
.
add_behaviour
(
self
.
Modify
(
period
=
5
,
start_at
=
datetime
.
now
()),
template
)
self
.
add_behaviour
(
self
.
Modify
(
period
=
5
,
start_at
=
datetime
.
now
()),
template
)
template
=
Template
(
metadata
=
{
"performative"
:
"Ending"
})
self
.
add_behaviour
(
self
.
Behav4
(
start_at
=
datetime
.
now
()
+
timedelta
(
seconds
=
11
)),
template
)
self
.
add_behaviour
(
self
.
Behav4
(
start_at
=
datetime
.
now
()
+
timedelta
(
seconds
=
11
)),
template
)
class
Modify
(
PeriodicBehaviour
):
async
def
run
(
self
):
...
...
@@ -38,25 +35,40 @@ class MasterAgent(BDIAgent):
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
()
b
=
BDIAgent
(
"slave_1@localhost"
,
"bdisimple"
,
"slave.asl"
)
future
=
b
.
start
()
future
.
result
()
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
)
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
()
try
:
main
(
args
.
server
,
args
.
password
)
except
KeyboardInterrupt
:
print
(
"Exiting..."
)
examples/counter.asl
View file @
66ff51a8
co
ntado
r(8).
t
ipo
(dec).
!ini
cio
.
co
unte
r(8).
t
ype
(dec).
!ini
t
.
+!ini
cio
+!ini
t
<-
.print("
Iniciando
....");
.print("
Starting
....");
!obj2.
+!obj2: t
ipo
(inc)
+!obj2: t
ype
(inc)
<-
.print("Incre
mentando
");
?co
ntado
r(X);
-+co
ntado
r(X+1);
.print("Incre
asing
");
?co
unte
r(X);
-+co
unte
r(X+1);
.wait(1000);
!obj2.
+!obj2: t
ipo
(dec)
+!obj2: t
ype
(dec)
<-
.print("Decre
mentando
");
?co
ntado
r(X);
-+co
ntado
r(X-1);
.print("Decre
asing
");
?co
unte
r(X);
-+co
unte
r(X-1);
.wait(1000);
!obj2.
+!obj2: not t
ipo
(_)
+!obj2: not t
ype
(_)
<-
.print("
Esperando
");
.print("
Waiting
");
.wait(1000);
!obj2.
examples/counter.py
View file @
66ff51a8
import
argparse
from
spade_bdi.bdi
import
BDIAgent
from
spade.template
import
Template
from
spade.behaviour
import
PeriodicBehaviour
from
spade.behaviour
import
TimeoutBehaviour
from
datetime
import
datetime
from
datetime
import
timedelta
from
spade.agent
import
Agent
class
CounterAgent
(
BDIAgent
):
async
def
setup
(
self
):
template
=
Template
(
metadata
=
{
"performative"
:
"BDI"
})
self
.
add_behaviour
(
self
.
BDIBehaviour
(),
template
)
template
=
Template
(
metadata
=
{
"performative"
:
"B1"
})
self
.
add_behaviour
(
self
.
Behav1
(
period
=
1
,
start_at
=
datetime
.
now
()),
template
)
self
.
add_behaviour
(
self
.
Behav1
(
period
=
1
,
start_at
=
datetime
.
now
()),
template
)
template
=
Template
(
metadata
=
{
"performative"
:
"B2"
})
self
.
add_behaviour
(
self
.
Behav2
(
period
=
5
,
start_at
=
datetime
.
now
()),
template
)
self
.
add_behaviour
(
self
.
Behav2
(
period
=
5
,
start_at
=
datetime
.
now
()),
template
)
template
=
Template
(
metadata
=
{
"performative"
:
"B3"
})
self
.
add_behaviour
(
self
.
Behav3
(
period
=
10
,
start_at
=
datetime
.
now
()),
template
)
self
.
add_behaviour
(
self
.
Behav3
(
period
=
10
,
start_at
=
datetime
.
now
()),
template
)
template
=
Template
(
metadata
=
{
"performative"
:
"B4"
})
self
.
add_behaviour
(
self
.
Behav4
(
start_at
=
datetime
.
now
()
+
timedelta
(
seconds
=
60
)),
template
)
self
.
add_behaviour
(
self
.
Behav4
(
start_at
=
datetime
.
now
()
+
timedelta
(
seconds
=
60
)),
template
)
class
Behav1
(
PeriodicBehaviour
):
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
):
if
self
.
contador
!=
self
.
agent
.
bdi
.
get_belief_value
(
"contador"
)[
0
]:
self
.
contador
=
self
.
agent
.
bdi
.
get_belief_value
(
"contador"
)[
0
]
print
(
self
.
agent
.
bdi
.
get_belief
(
"contador"
))
if
self
.
counter
!=
self
.
agent
.
bdi
.
get_belief_value
(
"counter"
)[
0
]:
self
.
counter
=
self
.
agent
.
bdi
.
get_belief_value
(
"counter"
)[
0
]
print
(
self
.
agent
.
bdi
.
get_belief
(
"counter"
))
class
Behav2
(
PeriodicBehaviour
):
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
):
async
def
run
(
self
):
try
:
t
ipo
=
self
.
agent
.
bdi
.
get_belief_value
(
"tipo
"
)[
0
]
if
t
ipo
==
'inc'
:
self
.
agent
.
bdi
.
set_belief
(
't
ipo
'
,
'dec'
)
t
ype
=
self
.
agent
.
bdi
.
get_belief_value
(
"type
"
)[
0
]
if
t
ype
==
'inc'
:
self
.
agent
.
bdi
.
set_belief
(
't
ype
'
,
'dec'
)
else
:
self
.
agent
.
bdi
.
set_belief
(
't
ipo
'
,
'inc'
)
self
.
agent
.
bdi
.
set_belief
(
't
ype
'
,
'inc'
)
except
Exception
as
e
:
print
(
"No belief 't
ipo
'."
)
print
(
"No belief 't
ype
'."
)
class
Behav4
(
TimeoutBehaviour
):
async
def
run
(
self
):
self
.
agent
.
bdi
.
remove_belief
(
'tipo'
,
'inc'
)
self
.
agent
.
bdi
.
remove_belief
(
'tipo'
,
'dec'
)
self
.
agent
.
bdi
.
remove_belief
(
'type'
,
'inc'
)
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
.
start
()
import
time
time
.
sleep
(
1
)
print
(
"started"
)
a
=
CounterAgent
(
"counter@"
+
args
.
server
,
args
.
password
,
"counter.asl"
)
a
.
start
()
import
time
time
.
sleep
(
1
)
print
(
"started"
)
examples/master.asl
View file @
66ff51a8
...
...
@@ -7,22 +7,28 @@
+!obj2: tipo(inc)
<-
.send("slave_1@localhost", tell, incrementar(2));
.send("slave_2@localhost", tell, incrementar(5));
?slave1(X);
?slave2(Y);
.send(X, tell, incrementar(2));
.send(Y, tell, incrementar(5));
.wait(2000);
!obj2.
+!obj2: tipo(dec)
<-
.send("slave_1@localhost", tell, decrementar(2));
.send("slave_2@localhost", tell, decrementar(5));
?slave1(X);
?slave2(Y);
.send(X, tell, decrementar(2));
.send(Y, tell, decrementar(5));
.wait(2000);
!obj2.
+!obj2: not tipo(_)
<-
?slave1(X);
?slave2(Y);
.print("Finishing");
.send("slave_1@localhost", untell, incrementar(2));
.send("slave_2@localhost", untell, incrementar(5));
.send("slave_1@localhost", untell, decrementar(2));
.send("slave_2@localhost", untell, decrementar(5)).
\ No newline at end of file
.send(X, untell, incrementar(2));
.send(Y, untell, incrementar(5));
.send(X, untell, decrementar(2));
.send(Y, untell, decrementar(5)).
examples/receiver.py
View file @
66ff51a8
import
argparse
from
spade_bdi.bdi
import
BDIAgent
a
=
BDIAgent
(
"BDIReceiverAgent@localhost"
,
"receiverpasswd"
,
"receiver.asl"
)
a
.
start
()
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
=
BDIAgent
(
"BDIReceiverAgent@"
+
args
.
server
,
args
.
password
,
"receiver.asl"
)
a
.
start
()
examples/sender.asl
View file @
66ff51a8
...
...
@@ -2,6 +2,7 @@
+!start
<-
?receiver(X);
.print("sending a message ...");
.send(
"BDIReceiverAgent@localhost"
, achieve, hello("Hello World!"));
.send(
X
, achieve, hello("Hello World!"));
.print("sent a message").
examples/sender.py
View file @
66ff51a8
import
argparse
from
spade_bdi.bdi
import
BDIAgent
a
=
BDIAgent
(
"BDISenderAgent@localhost"
,
"senderpasswd"
,
"sender.asl"
)
a
.
start
()
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
=
BDIAgent
(
"BDISenderAgent@"
+
args
.
server
,
args
.
password
,
"sender.asl"
)
a
.
bdi
.
set_belief
(
"receiver"
,
"BDIReceiverAgent@"
+
args
.
server
)
a
.
start
()
examples/slave.asl
View file @
66ff51a8
contador(0).
+incrementar(Inc)[source(S)]:
.substring("master@localhost",S,R)
+incrementar(Inc)[source(S)]:
master(M) & .substring(M,S,R)
<-
.print("increasing");
?contador(X);
.print(X);
-+contador(X+Inc).
+decrementar(Dec)[source(S)]:
.substring("master@localhost",S,R)
+decrementar(Dec)[source(S)]:
master(M) & .substring(M,S,R)
<-
.print("decreasing");
?contador(X);
.print(X);
-+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").
-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").
requirements_dev.txt
View file @
66ff51a8
...
...
@@ -7,6 +7,7 @@ tox==3.5.2
coverage==4.5.1
Sphinx==1.8.1
twine==1.12.1
Click==7.0
pytest==3.8.2
pytest-runner==4.2
setup.cfg
View file @
66ff51a8
[bumpversion]
current_version = 0.1.0
commit = True
tag =
Tru
e
tag =
Fals
e
[bumpversion:file:setup.py]
search = version='{current_version}'
...
...
spade_bdi/bdi.py
View file @
66ff51a8
# -*- coding: utf-8 -*-
import
asyncio
import
json
import
time
from
ast
import
literal_eval
from
loguru
import
logger
from
collections
import
deque
...
...
@@ -12,82 +12,85 @@ from spade.agent import Agent
from
spade.template
import
Template
from
spade.message
import
Message
PERCEPT_TAG
=
frozenset
(
[
asp
.
Literal
(
"source"
,
(
asp
.
Literal
(
"percept"
),
))])
PERCEPT_TAG
=
frozenset
([
asp
.
Literal
(
"source"
,
(
asp
.
Literal
(
"percept"
),))])
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"
})
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
):
if
type
(
behaviour
)
==
self
.
BDIBehaviour
:
self
.
bdi
=
behaviour
self
.
set_env
()
super
()
.
add_behaviour
(
behaviour
,
template
)
def
set_asl
(
self
,
asl_file
=
None
):
if
not
asl_file
:
self
.
bdi_enabled
=
False
self
.
asl_file
=
None
else
:
def
set_asl
(
self
,
asl_file
:
str
):
self
.
asl_file
=
asl_file
self
.
_load_asl
()
def
_load_asl
(
self
):
self
.
pause_bdi
()
try
:
with
open
(
asl_file
)
as
source
:
self
.
bdi_agent
=
self
.
bdi_env
.
build_agent
(
source
,
self
.
bdi_actions
)
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
.
bdi_enabled
=
True
self
.
asl_file
=
asl_file
self
.
resume_bdi
()
except
FileNotFoundError
:
logger
.
info
(
"Warning: ASL specified for {} does not exist. Disabling BDI."
.
format
(
self
.
jid
))
self
.
bdi_enabled
=
False
logger
.
info
(
"Warning: ASL specified for {} does not exist. Disabling BDI."
.
format
(
self
.
jid
))
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
)
self
.
pause_bdi
()
class
BDIBehaviour
(
CyclicBehaviour
):
def
add_actions
(
self
):
@self.agent.bdi_actions.add
(
".send"
,
3
)
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
)
if
not
asp
.
is_atom
(
ilf
):
return
ilf_type
=
ilf
.
functor
mdata
=
{
"performative"
:
"BDI"
,
"ilf_type"
:
ilf_type
,
}
body
=
asp
.
asl_str
(
asp
.
freeze
(
term
.
args
[
2
],
intention
.
scope
,
{}))
mdata
=
{
"performative"
:
"BDI"
,
"ilf_type"
:
ilf_type
,
}
body
=
asp
.
asl_str
(
asp
.
freeze
(
term
.
args
[
2
],
intention
.
scope
,
{}))
msg
=
Message
(
to
=
receiver
,
body
=
body
,
metadata
=
mdata
)
self
.
agent
.
submit
(
self
.
send
(
msg
))
yield
@self.agent.bdi_actions.add
(
".custom_action"
,
1
)
def
_custom_action
(
agent
,
term
,
intention
):
arg_0
=
asp
.
grounded
(
term
.
args
[
0
],
intention
.
scope
)
print
(
arg_0
)
asp
.
grounded
(
term
.
args
[
0
],
intention
.
scope
)
yield
@self.agent.bdi_actions.add_function
(
".a_function"
,
(
int
,))
def
_a_function
(
x
):
return
x
**
4
return
x
**
4
@self.agent.bdi_actions.add_function
(
"literal_function"
,
(
asp
.
Literal
,))
def
_literal_function
(
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."""
new_args
=
()
for
x
in
args
:
...
...
@@ -107,7 +110,7 @@ class BDIAgent(Agent):
self
.
agent
.
bdi_intention_buffer
.
append
((
asp
.
Trigger
.
addition
,
asp
.
GoalType
.
belief
,
term
,
asp
.
runtime
.
Intention
()))
def
remove_belief
(
self
,
name
,
*
args
):
def
remove_belief
(
self
,
name
:
str
,
*
args
):
"""Remove an existing agent's belief."""
new_args
=
()
for
x
in
args
:
...
...
@@ -119,22 +122,25 @@ class BDIAgent(Agent):
self
.
agent
.
bdi_intention_buffer
.
append
((
asp
.
Trigger
.
removal
,
asp
.
GoalType
.
belief
,
term
,
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
<key> is returned. Keep <source> False to strip source."""
key
=
str
(
key
)
for
beliefs
in
self
.
agent
.
bdi_agent
.
beliefs
:
if
beliefs
[
0
]
==
key
:
raw_belief
=
(
str
(
list
(
self
.
agent
.
bdi_agent
.
beliefs
[
beliefs
])[
0
]))
if
')[source'
in
raw_belief
and
not
source
:
raw_belief
=
raw_belief
.
split
(
'['
)[
0
]
.
replace
(
'"'
,
''
)
raw_belief
=
(
str
(
list
(
self
.
agent
.
bdi_agent
.
beliefs
[
beliefs
])[
0
]))
raw_belief
=
self
.
_remove_source
(
raw_belief
,
source
)
belief
=
raw_belief
return
belief
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
<key> is returned"""
belief
=
self
.
get_belief
(
key
)
...
...
@@ -148,10 +154,8 @@ class BDIAgent(Agent):
belief_list
=
[]
for
beliefs
in
self
.
agent
.
bdi_agent
.
beliefs
:
try
:
raw_belief
=
(
str
(
list
(
self
.
agent
.
bdi_agent
.
beliefs
[
beliefs
])[
0
]))
if
')[source('
in
raw_belief
and
not
source
:
raw_belief
=
raw_belief
.
split
(
'['
)[
0
]
.
replace
(
'"'
,
''
)
raw_belief
=
(
str
(
list
(
self
.
agent
.
bdi_agent
.
beliefs
[
beliefs
])[
0
]))
raw_belief
=
self
.
_remove_source
(
raw_belief
,
source
)
belief_list
.
append
(
raw_belief
)
except
IndexError
:
pass
...
...
@@ -161,20 +165,7 @@ class BDIAgent(Agent):
"""Print agent's beliefs.Keep <source> False to strip source."""
for
beliefs
in
self
.
agent
.
bdi_agent
.
beliefs
.
values
():
for
belief
in
beliefs
:
if
')[source('
in
str
(
belief
)
and
not
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
))
print
(
self
.
_remove_source
(
str
(
belief
),
source
))
async
def
run
(
self
):
"""
...
...
@@ -195,31 +186,30 @@ class BDIAgent(Agent):
goal_type
=
asp
.
GoalType
.
achievement
trigger
=
asp
.
Trigger
.
addition
else
:
raise
asp
.
AslError
(
"unknown illocutionary force:
%
s"
%
ilf_type
)
raise
asp
.
AslError
(
"unknown illocutionary force: {}"
.
format
(
ilf_type
))
intention
=
asp
.
runtime
.
Intention
()
functor
,
args
=
await
parse_literal
(
msg
.
body
)
functor
,
args
=
parse_literal
(
msg
.
body
)
message
=
asp
.
Literal
(
functor
,
args
)
message
=
asp
.
freeze
(
message
,
intention
.
scope
,
{})
tagged_message
=
message
.
with_annotation
(
asp
.
Literal
(
"source"
,
(
asp
.
Literal
(
str
(
msg
.
sender
)),
)))
self
.
agent
.
bdi_intention_buffer
.
append
((
trigger
,
goal_type
,
tagged_message
,
intention
))
tagged_message
=
message
.
with_annotation
(
asp
.
Literal
(
"source"
,
(
asp
.
Literal
(
str
(
msg
.
sender
)),)))
self
.
agent
.
bdi_intention_buffer
.
append
((
trigger
,
goal_type
,
tagged_message
,
intention
))
if
self
.
agent
.
bdi_intention_buffer
:
temp_intentions
=
deque
(
self
.
agent
.
bdi_intention_buffer
)
for
trigger
,
goal_type
,
term
,
intention
in
temp_intentions
:
self
.
agent
.
bdi_agent
.
call
(
trigger
,
goal_type
,
term
,
intention
)
self
.
agent
.
bdi_agent
.
call
(
trigger
,
goal_type
,
term
,
intention
)
self
.
agent
.
bdi_agent
.
step
()
self
.
agent
.
bdi_intention_buffer
.
popleft
()
else
:
self
.
agent
.
bdi_agent
.
step
()
else
:
await
asyncio
.
sleep
(
0.1
)
async
def
parse_literal
(
msg
):
def
parse_literal
(
msg
):
functor
=
msg
.
split
(
"("
)[
0
]
if
"("
in
msg
:
args
=
msg
.
split
(
"("
)[
1
]
...
...
tests/test_spade_bdi.py
View file @
66ff51a8
...
...
@@ -5,34 +5,4 @@
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 @
66ff51a8
[tox]
envlist
=
py
27, py34, py35, py
36, flake8
envlist
=
py36, flake8
[travis]
python
=
3.6:
py36
3.5:
py35
3.4:
py34
2.7:
py27
[testenv:flake8]
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