Commit 9df8a2dc by serfrape

Extend the examples

parent ca3bc24a
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 datetime import datetime from datetime import datetime
from datetime import timedelta
from spade.agent import Agent from spade.agent import Agent
...@@ -14,27 +16,44 @@ class MasterAgent(BDIAgent): ...@@ -14,27 +16,44 @@ class MasterAgent(BDIAgent):
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"})
self.add_behaviour(self.Behav4(
start_at=datetime.now() + timedelta(seconds=11)), template)
class Modify(PeriodicBehaviour): class Modify(PeriodicBehaviour):
async def run(self): async def run(self):
if self.agent.bdi_enabled: if self.agent.bdi_enabled:
tipo = self.agent.bdi.get_belief_value("type")[0] try:
if tipo == 'inc': tipo = self.agent.bdi.get_belief_value("tipo")[0]
self.agent.bdi.set_belief('type', 'dec') if tipo == 'inc':
else: self.agent.bdi.set_belief('tipo', 'dec')
self.agent.bdi.set_belief('type', 'inc') else:
self.agent.bdi.set_belief('tipo', 'inc')
except Exception as e:
print("No belief 'tipo'.", e)
class Behav4(TimeoutBehaviour):
async def run(self):
self.agent.bdi.remove_belief('tipo', 'inc')
self.agent.bdi.remove_belief('tipo', 'dec')
import time
b = BDIAgent("slave_1@localhost", "bdisimple", "slave.asl") b = BDIAgent("slave_1@localhost", "bdisimple", "slave.asl")
future = b.start() future = b.start()
future.result() future.result()
c = BDIAgent("slave_2@localhost", "bdisimple3") c = BDIAgent("slave_2@localhost", "bdisimple3")
future = c.start() future = c.start()
future.result() future.result()
a = MasterAgent("master@localhost", "bdimaster") a = MasterAgent("master@localhost", "bdimaster")
future = a.start() future = a.start()
future.result() future.result()
a.set_asl("master.asl") a.set_asl("master.asl")
import time a.bdi.set_belief('tipo', 'dec')
time.sleep(5) time.sleep(5)
print("Enabling BDI for slave2") print("Enabling BDI for slave2")
c.set_asl("slave.asl") c.set_asl("slave.asl")
......
...@@ -3,9 +3,9 @@ tipo(dec). ...@@ -3,9 +3,9 @@ tipo(dec).
!inicio. !inicio.
+!inicio +!inicio
<- <-
.print("Iniciando...."); .print("Iniciando....");
!obj2. !obj2.
+!obj2: tipo(inc) +!obj2: tipo(inc)
...@@ -18,16 +18,16 @@ tipo(dec). ...@@ -18,16 +18,16 @@ tipo(dec).
+!obj2: tipo(dec) +!obj2: tipo(dec)
<- <-
.print("Decrementando"); .print("Decrementando");
?contador(X); ?contador(X);
-+contador(X-1); -+contador(X-1);
.wait(1000); .wait(1000);
!obj2. !obj2.
+!obj2: not tipo(_) +!obj2: not tipo(_)
<- <-
.print("Esperando"); .print("Esperando");
.wait(1000); .wait(1000);
!obj2. !obj2.
\ No newline at end of file \ No newline at end of file
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 datetime import datetime from datetime import datetime
from datetime import timedelta
from spade.agent import Agent from spade.agent import Agent
...@@ -18,6 +20,9 @@ class CounterAgent(BDIAgent): ...@@ -18,6 +20,9 @@ class CounterAgent(BDIAgent):
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"})
self.add_behaviour(self.Behav4(
start_at=datetime.now() + timedelta(seconds=60)), template)
class Behav1(PeriodicBehaviour): class Behav1(PeriodicBehaviour):
async def on_start(self): async def on_start(self):
...@@ -35,11 +40,19 @@ class CounterAgent(BDIAgent): ...@@ -35,11 +40,19 @@ class CounterAgent(BDIAgent):
class Behav3(PeriodicBehaviour): class Behav3(PeriodicBehaviour):
async def run(self): async def run(self):
tipo = self.agent.bdi.get_belief_value("tipo")[0] try:
if tipo == 'inc': tipo = self.agent.bdi.get_belief_value("tipo")[0]
self.agent.bdi.set_belief('tipo', 'dec') if tipo == 'inc':
else: self.agent.bdi.set_belief('tipo', 'dec')
self.agent.bdi.set_belief('tipo', 'inc') else:
self.agent.bdi.set_belief('tipo', 'inc')
except Exception as e:
print("No belief 'tipo'.")
class Behav4(TimeoutBehaviour):
async def run(self):
self.agent.bdi.remove_belief('tipo', 'inc')
self.agent.bdi.remove_belief('tipo', 'dec')
a = CounterAgent("counter@localhost", "bditest", "counter.asl") a = CounterAgent("counter@localhost", "bditest", "counter.asl")
......
!start. !start.
type(dec).
+!start <- +!start
-start; <-
!obj2. -start;
!obj2.
+!obj2: type(inc) <- +!obj2: tipo(inc)
.send("slave_1@localhost", tell, incrementar(0)); <-
.send("slave_2@localhost", tell, incrementar(0)); .send("slave_1@localhost", tell, incrementar(2));
.wait(2000); .send("slave_2@localhost", tell, incrementar(5));
!obj2. .wait(2000);
!obj2.
+!obj2: type(dec) <- +!obj2: tipo(dec)
.send("slave_1@localhost", tell, decrementar(0)); <-
.send("slave_2@localhost", tell, decrementar(0)); .send("slave_1@localhost", tell, decrementar(2));
.wait(2000); .send("slave_2@localhost", tell, decrementar(5));
!obj2. .wait(2000);
\ No newline at end of file !obj2.
+!obj2: not tipo(_)
<-
.print("Esperando");
.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
+!hello(Msg)[source(Sender)] <- +!hello(Msg)[source(Sender)]
<-
.print("got a message from", Sender, "saying:\n", Msg). .print("got a message from", Sender, "saying:\n", Msg).
\ No newline at end of file
!start. !start.
+!start <- +!start
.print("sending a message ..."); <-
.send("BDIReceiverAgent@localhost", achieve, hello("Hello World!")); .print("sending a message ...");
.print("sent a message"). .send("BDIReceiverAgent@localhost", achieve, hello("Hello World!"));
\ No newline at end of file .print("sent a message").
\ No newline at end of file
contador(0). contador(0).
+incrementar(Inc) <- +incrementar(Inc)[source(S)]: .substring("master@localhost",S,R)
<-
.print("increasing"); .print("increasing");
?contador(X); ?contador(X);
-+contador(X+1); .print(X);
-incrementar(_). -+contador(X+Inc).
+decrementar(Dec) <- +decrementar(Dec)[source(S)]: .substring("master@localhost",S,R)
<-
.print("decreasing"); .print("decreasing");
?contador(X); ?contador(X);
-+contador(X-1); .print(X);
-decrementar(_). -+contador(X-Dec).
-incrementar(Inc)[source(S)]: .substring("master@localhost",S,R)
<-
.print("DELETING incrementar BELIEF from an untell message").
-decrementar(Dec)[source(S)]: .substring("master@localhost",S,R)
<-
.print("DELETING decrementar BELIEF from an untell message").
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