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
0
Issues
0
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
jpalanca
SPADE-BDI
Commits
54799e60
Commit
54799e60
authored
Mar 21, 2019
by
serfrape
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Will add some functionalities and example
parent
b07b6dea
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
122 additions
and
18 deletions
+122
-18
ejemplo.asl
examples/ejemplo.asl
+34
-0
ejemplo.py
examples/ejemplo.py
+49
-0
bdi.py
spade_bdi/bdi.py
+39
-18
No files found.
examples/ejemplo.asl
0 → 100644
View file @
54799e60
contador(8).
tipo(dec).
!inicio.
+!inicio
<-
.print("Iniciando....");
!obj2.
+!obj2: tipo(inc)
<-
.print("Incrementando");
?contador(X);
-+contador(X+1);
.wait(2000);
!obj2.
+!obj2: tipo(dec)
<-
.print("Decrementando");
?contador(X);
-+contador(X-1);
.wait(2000);
!obj2.
+!obj2: not tipo(_)
<-
.print("Esperando");
.wait(2000);
!obj2.
\ No newline at end of file
examples/ejemplo.py
0 → 100644
View file @
54799e60
from
spade_bdi.bdi
import
BDIAgent
from
spade.template
import
Template
from
spade.behaviour
import
PeriodicBehaviour
from
datetime
import
datetime
from
spade.agent
import
Agent
class
ExampleAgent
(
BDIAgent
):
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
)
template
=
Template
(
metadata
=
{
"performative"
:
"B2"
})
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
)
class
Behav1
(
PeriodicBehaviour
):
async
def
on_start
(
self
):
self
.
contador
=
self
.
agent
.
bdi
.
get_belief_value
(
"contador"
)[
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"
))
class
Behav2
(
PeriodicBehaviour
):
async
def
run
(
self
):
self
.
agent
.
bdi
.
set_belief
(
'contador'
,
0
)
class
Behav3
(
PeriodicBehaviour
):
async
def
run
(
self
):
tipo
=
self
.
agent
.
bdi
.
get_belief_value
(
"tipo"
)[
0
]
if
tipo
==
'inc'
:
self
.
agent
.
bdi
.
set_belief
(
'tipo'
,
'dec'
)
# else:
# self.agent.bdi.set_belief('tipo', 'inc')
a
=
ExampleAgent
(
"Agent@localhost"
,
"bditest"
,
"ejemplo.asl"
)
a
.
start
()
import
time
time
.
sleep
(
1
)
print
(
"started"
)
spade_bdi/bdi.py
View file @
54799e60
...
...
@@ -22,14 +22,16 @@ class BDIAgent(Agent):
def
add_behaviour
(
self
,
behaviour
,
template
=
None
):
# print("OVERRIDEN")
self
.
bdi
=
behaviour
if
type
(
behaviour
)
==
self
.
BDIBehaviour
:
self
.
bdi
=
behaviour
super
()
.
add_behaviour
(
behaviour
,
template
)
def
__init__
(
self
,
jid
,
password
,
asl
,
*
args
,
**
kwargs
):
self
.
asl_file
=
asl
# print("instantiated")
super
()
.
__init__
(
jid
,
password
,
*
args
,
**
kwargs
)
class
BDIBehaviour
(
CyclicBehaviour
,
metaclass
=
ABCMeta
):
class
BDIBehaviour
(
CyclicBehaviour
):
def
add_actions
(
self
):
@self.actions.add
(
".send"
,
3
)
def
_send
(
agent
,
term
,
intention
):
...
...
@@ -61,12 +63,14 @@ class BDIAgent(Agent):
def
_literal_function
(
x
):
return
x
# def set_belief(self, agent, term, intention):
def
set_belief
(
self
,
name
,
*
args
):
"""Set an agent's belief. If it already exists, updates it."""
new_args
=
()
for
x
in
args
:
new_args
+=
(
pyson
.
Literal
(
x
),)
if
type
(
x
)
==
str
:
new_args
+=
(
pyson
.
Literal
(
x
),)
else
:
new_args
+=
(
x
,)
term
=
pyson
.
Literal
(
name
,
tuple
(
new_args
),
PERCEPT_TAG
)
found
=
False
for
belief
in
list
(
self
.
bdi_agent
.
beliefs
[
term
.
literal_group
()]):
...
...
@@ -83,44 +87,61 @@ class BDIAgent(Agent):
"""Remove an existing agent's belief."""
new_args
=
()
for
x
in
args
:
new_args
+=
(
pyson
.
Literal
(
x
),)
if
type
(
x
)
==
str
:
new_args
+=
(
pyson
.
Literal
(
x
),)
else
:
new_args
+=
(
x
,)
term
=
pyson
.
Literal
(
name
,
tuple
(
new_args
),
PERCEPT_TAG
)
self
.
bdi_agent
.
call
(
pyson
.
Trigger
.
removal
,
pyson
.
GoalType
.
belief
,
term
,
pyson
.
runtime
.
Intention
())
def
get_belief
(
self
,
key
):
"""Get an existing agent's belief. The first belief matching
<key> is returned """
def
get_belief
(
self
,
key
,
pyson_format
=
False
):
"""Get an agent's existing belief. The first belief matching
<key> is returned. Keep <pyson_format> False to strip pyson
formatting."""
key
=
str
(
key
)
for
beliefs
in
self
.
bdi_agent
.
beliefs
:
if
beliefs
[
0
]
==
key
:
raw_belief
=
(
str
(
list
(
self
.
bdi_agent
.
beliefs
[
beliefs
])[
0
]))
if
'")[source'
in
raw_belief
:
raw_belief
=
raw_belief
.
split
(
'['
)[
0
]
.
replace
(
'"'
,
''
)
if
')[source'
in
raw_belief
and
not
pyson_format
:
raw_belief
=
raw_belief
.
split
(
'['
)[
0
]
.
replace
(
'"'
,
''
)
belief
=
raw_belief
break
return
belief
def
get_beliefs
(
self
):
"""Get agent's beliefs."""
return
belief
return
None
def
get_belief_value
(
self
,
key
):
"""Get an agent's existing value or values of the <key> belief. The first belief matching
<key> is returned"""
belief
=
self
.
get_belief
(
key
)
if
belief
:
return
tuple
(
belief
.
split
(
'('
)[
1
]
.
split
(
')'
)[
0
]
.
split
(
','
))
else
:
return
None
def
get_beliefs
(
self
,
pyson_format
=
False
):
"""Get agent's beliefs.Keep <pyson_format> False to strip pyson
formatting."""
belief_list
=
[]
for
beliefs
in
self
.
bdi_agent
.
beliefs
:
try
:
raw_belief
=
(
str
(
list
(
self
.
bdi_agent
.
beliefs
[
beliefs
])[
0
]))
if
')[source('
in
raw_belief
:
if
')[source('
in
raw_belief
and
not
pyson_format
:
raw_belief
=
raw_belief
.
split
(
'['
)[
0
]
.
replace
(
'"'
,
''
)
belief_list
.
append
(
raw_belief
)
except
IndexError
:
pass
return
belief_list
def
print_beliefs
(
self
):
def
print_beliefs
(
self
,
pyson_format
=
False
):
"""Print agent's beliefs.Keep <pyson_format> False to strip pyson
formatting."""
print
(
"PRINTING BELIEFS"
)
for
beliefs
in
self
.
bdi_agent
.
beliefs
.
values
():
for
belief
in
beliefs
:
if
')[source('
in
str
(
belief
):
if
')[source('
in
str
(
belief
)
and
not
pyson_format
:
belief
=
str
(
belief
)
.
split
(
'['
)[
0
]
.
replace
(
'"'
,
''
)
print
(
belief
)
...
...
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