Commit 66ff51a8 by jpalanca

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
...@@ -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
...@@ -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>
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
......
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)
contador(8). counter(8).
tipo(dec). type(dec).
!inicio. !init.
+!inicio +!init
<- <-
.print("Iniciando...."); .print("Starting....");
!obj2. !obj2.
+!obj2: tipo(inc) +!obj2: type(inc)
<- <-
.print("Incrementando"); .print("Increasing");
?contador(X); ?counter(X);
-+contador(X+1); -+counter(X+1);
.wait(1000); .wait(1000);
!obj2. !obj2.
+!obj2: tipo(dec) +!obj2: type(dec)
<- <-
.print("Decrementando"); .print("Decreasing");
?contador(X); ?counter(X);
-+contador(X-1); -+counter(X-1);
.wait(1000); .wait(1000);
!obj2. !obj2.
+!obj2: not tipo(_) +!obj2: not type(_)
<- <-
.print("Esperando"); .print("Waiting");
.wait(1000); .wait(1000);
!obj2. !obj2.
\ No newline at end of file
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.contador = self.agent.bdi.get_belief_value("contador")[0] self.counter = self.agent.bdi.get_belief_value("counter")[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('contador', 0) self.agent.bdi.set_belief('counter', 0)
class Behav3(PeriodicBehaviour): class Behav3(PeriodicBehaviour):
async def run(self): async def run(self):
try: try:
tipo = self.agent.bdi.get_belief_value("tipo")[0] type = self.agent.bdi.get_belief_value("type")[0]
if tipo == 'inc': if type == 'inc':
self.agent.bdi.set_belief('tipo', 'dec') self.agent.bdi.set_belief('type', 'dec')
else: else:
self.agent.bdi.set_belief('tipo', 'inc') self.agent.bdi.set_belief('type', 'inc')
except Exception as e: except Exception as e:
print("No belief 'tipo'.") print("No belief 'type'.")
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")
...@@ -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
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()
...@@ -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
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()
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").
...@@ -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
[bumpversion] [bumpversion]
current_version = 0.1.0 current_version = 0.1.0
commit = True commit = True
tag = True tag = False
[bumpversion:file:setup.py] [bumpversion:file:setup.py]
search = version='{current_version}' search = version='{current_version}'
......
...@@ -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] [tox]
envlist = py27, py34, py35, py36, 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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment