Commit 9df8a2dc by serfrape

Extend the examples

parent ca3bc24a
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
......@@ -14,27 +16,44 @@ class MasterAgent(BDIAgent):
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)
class Modify(PeriodicBehaviour):
async def run(self):
if self.agent.bdi_enabled:
tipo = self.agent.bdi.get_belief_value("type")[0]
if tipo == 'inc':
self.agent.bdi.set_belief('type', 'dec')
else:
self.agent.bdi.set_belief('type', 'inc')
try:
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')
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")
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")
import time
a.bdi.set_belief('tipo', 'dec')
time.sleep(5)
print("Enabling BDI for slave2")
c.set_asl("slave.asl")
......
......@@ -3,9 +3,9 @@ tipo(dec).
!inicio.
+!inicio
<-
.print("Iniciando....");
!obj2.
<-
.print("Iniciando....");
!obj2.
+!obj2: tipo(inc)
......@@ -18,16 +18,16 @@ tipo(dec).
+!obj2: tipo(dec)
<-
.print("Decrementando");
?contador(X);
-+contador(X-1);
.wait(1000);
!obj2.
<-
.print("Decrementando");
?contador(X);
-+contador(X-1);
.wait(1000);
!obj2.
+!obj2: not tipo(_)
<-
.print("Esperando");
<-
.print("Esperando");
.wait(1000);
!obj2.
\ No newline at end of file
!obj2.
\ No newline at end of file
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
......@@ -18,6 +20,9 @@ class CounterAgent(BDIAgent):
template = Template(metadata={"performative": "B3"})
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)
class Behav1(PeriodicBehaviour):
async def on_start(self):
......@@ -35,11 +40,19 @@ class CounterAgent(BDIAgent):
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')
try:
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')
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")
......
!start.
type(dec).
+!start <-
-start;
!obj2.
+!start
<-
-start;
!obj2.
+!obj2: type(inc) <-
.send("slave_1@localhost", tell, incrementar(0));
.send("slave_2@localhost", tell, incrementar(0));
.wait(2000);
!obj2.
+!obj2: tipo(inc)
<-
.send("slave_1@localhost", tell, incrementar(2));
.send("slave_2@localhost", tell, incrementar(5));
.wait(2000);
!obj2.
+!obj2: type(dec) <-
.send("slave_1@localhost", tell, decrementar(0));
.send("slave_2@localhost", tell, decrementar(0));
.wait(2000);
!obj2.
\ No newline at end of file
+!obj2: tipo(dec)
<-
.send("slave_1@localhost", tell, decrementar(2));
.send("slave_2@localhost", tell, decrementar(5));
.wait(2000);
!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).
\ No newline at end of file
!start.
+!start <-
.print("sending a message ...");
.send("BDIReceiverAgent@localhost", achieve, hello("Hello World!"));
.print("sent a message").
\ No newline at end of file
+!start
<-
.print("sending a message ...");
.send("BDIReceiverAgent@localhost", achieve, hello("Hello World!"));
.print("sent a message").
\ No newline at end of file
contador(0).
+incrementar(Inc) <-
+incrementar(Inc)[source(S)]: .substring("master@localhost",S,R)
<-
.print("increasing");
?contador(X);
-+contador(X+1);
-incrementar(_).
.print(X);
-+contador(X+Inc).
+decrementar(Dec) <-
+decrementar(Dec)[source(S)]: .substring("master@localhost",S,R)
<-
.print("decreasing");
?contador(X);
-+contador(X-1);
-decrementar(_).
.print(X);
-+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