Commit 47d798c5 by serfrape

Add more examples

parent 0e1b0552
!start.
+!start <-
+car(rojo);
.a_function(3,W);
.print("w =", W);
literal_function(rojo,Y);
.print("Y =", Y);
.custom_action(8);
+truck(azul).
+car(Color)
<- .print("El carro es ",Color).
\ No newline at end of file
from spade_bdi.bdi import BDIAgent
a = BDIAgent("BasicAgent@localhost", "basicpasswd", "basic.asl")
a.start()
import time
time.sleep(1)
a.bdi.set_belief("car", "azul", "big")
a.bdi.print_beliefs()
print("GETTING FIRST CAR BELIEF")
print(a.bdi.get_belief("car"))
a.bdi.print_beliefs()
a.bdi.remove_belief("car", 'azul', "big")
a.bdi.print_beliefs()
print(a.bdi.get_beliefs())
a.bdi.set_belief("car", 'amarillo')
...@@ -5,7 +5,7 @@ from datetime import datetime ...@@ -5,7 +5,7 @@ from datetime import datetime
from spade.agent import Agent from spade.agent import Agent
class BossAgent(BDIAgent): class MasterAgent(BDIAgent):
async def setup(self): async def setup(self):
template = Template(metadata={"performative": "BDI"}) template = Template(metadata={"performative": "BDI"})
self.add_behaviour(self.BDIBehaviour(), template) self.add_behaviour(self.BDIBehaviour(), template)
...@@ -30,10 +30,10 @@ future.result() ...@@ -30,10 +30,10 @@ 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 = BossAgent("Boss@localhost", "bdiboss") a = MasterAgent("master@localhost", "bdimaster")
future = a.start() future = a.start()
future.result() future.result()
a.set_asl("boss.asl") a.set_asl("master.asl")
import time import time
time.sleep(5) time.sleep(5)
print("Enabling BDI for slave2") print("Enabling BDI for slave2")
......
...@@ -8,26 +8,26 @@ tipo(dec). ...@@ -8,26 +8,26 @@ tipo(dec).
!obj2. !obj2.
+!obj2: tipo(inc) +!obj2: tipo(inc)
<- <-
.print("Incrementando"); .print("Incrementando");
?contador(X); ?contador(X);
-+contador(X+1); -+contador(X+1);
.wait(2000); .wait(1000);
!obj2. !obj2.
+!obj2: tipo(dec) +!obj2: tipo(dec)
<- <-
.print("Decrementando"); .print("Decrementando");
?contador(X); ?contador(X);
-+contador(X-1); -+contador(X-1);
.wait(2000); .wait(1000);
!obj2. !obj2.
+!obj2: not tipo(_)
<- +!obj2: not tipo(_)
.print("Esperando"); <-
.wait(2000); .print("Esperando");
!obj2. .wait(1000);
\ No newline at end of file !obj2.
\ No newline at end of file
...@@ -5,7 +5,7 @@ from datetime import datetime ...@@ -5,7 +5,7 @@ from datetime import datetime
from spade.agent import Agent from spade.agent import Agent
class ExampleAgent(BDIAgent): class CounterAgent(BDIAgent):
async def setup(self): async def setup(self):
template = Template(metadata={"performative": "BDI"}) template = Template(metadata={"performative": "BDI"})
self.add_behaviour(self.BDIBehaviour(), template) self.add_behaviour(self.BDIBehaviour(), template)
...@@ -42,7 +42,7 @@ class ExampleAgent(BDIAgent): ...@@ -42,7 +42,7 @@ class ExampleAgent(BDIAgent):
self.agent.bdi.set_belief('tipo', 'inc') self.agent.bdi.set_belief('tipo', 'inc')
a = ExampleAgent("Agent@localhost", "bditest", "ejemplo.asl") a = CounterAgent("counter@localhost", "bditest", "counter.asl")
a.start() a.start()
import time import time
time.sleep(1) time.sleep(1)
......
+!hello(Msg)[source(Sender)] <- +!hello(Msg)[source(Sender)] <-
.print("got a message from", Sender, "saying", Msg). .print("got a message from", Sender, "saying:\n", Msg).
\ No newline at end of file
\ No newline at end of file
from spade_bdi.bdi import BDIAgent from spade_bdi.bdi import BDIAgent
a = BDIAgent("RECEIVERBDIAgent@localhost", "bditest1", "receiver.asl") a = BDIAgent("ReceiverAgent@localhost", "receiverpasswd", "receiver.asl")
a.start() a.start()
!start. !start.
+!start <- +!start <-
+car(rojo); .print("sending a message ...");
//.a_function(3,W); .send("ReceiverAgent@localhost", achieve, hello("Hello World!"));
//.print("w =", W); .print("sent a message").
//literal_function(rojo,Y); \ No newline at end of file
//.print("Y =", Y);
//.custom_action(8);
.print("sending individual message ...");
.send("RECEIVERBDIAgent@localhost", achieve, hello(15));
.print("sent a message").
+truck(azul).
+car(Color)
<- .print("El carro es ",Color).
\ No newline at end of file
from spade_bdi.bdi import BDIAgent from spade_bdi.bdi import BDIAgent
a = BDIAgent("BDIAgent@localhost", "bditest", "sender.asl") a = BDIAgent("SenderAgent@localhost", "senderpasswd", "sender.asl")
a.start() a.start()
import time
time.sleep(1)
a.bdi.set_belief("car", "azul", "big")
a.bdi.print_beliefs()
print("GETTING FIRST CAR BELIEF")
print(a.bdi.get_belief("car"))
a.bdi.print_beliefs()
a.bdi.remove_belief("car", 'azul', "big")
a.bdi.print_beliefs()
print(a.bdi.get_beliefs())
a.bdi.set_belief("car", 'amarillo')
...@@ -15,8 +15,6 @@ from .ontology import X, Y, Z ...@@ -15,8 +15,6 @@ from .ontology import X, Y, Z
PERCEPT_TAG = frozenset( PERCEPT_TAG = frozenset(
[pyson.Literal("source", (pyson.Literal("percept"), ))]) [pyson.Literal("source", (pyson.Literal("percept"), ))])
PRECISION_Z = 0.5
PRECISION_X = 0.5
class BDIAgent(Agent): class BDIAgent(Agent):
...@@ -31,16 +29,23 @@ class BDIAgent(Agent): ...@@ -31,16 +29,23 @@ class BDIAgent(Agent):
self.set_env() self.set_env()
super().add_behaviour(behaviour, template) super().add_behaviour(behaviour, template)
def set_asl(self, asl): def set_asl(self, asl_file=None):
self.asl_file = asl if not asl_file:
if self.asl_file == None:
self.bdi_enabled = False self.bdi_enabled = False
self.asl_file = None
else: else:
with open(self.asl_file) as source: try:
self.bdi_agent = self.bdi_env.build_agent( with open(asl_file) as source:
source, self.bdi_actions) self.bdi_agent = self.bdi_env.build_agent(
self.bdi_agent.name = self.jid source, self.bdi_actions)
self.bdi_enabled = True self.bdi_agent.name = self.jid
self.bdi_enabled = True
self.asl_file = asl_file
except FileNotFoundError:
logger.info(
"Warning: ASL specified for {} does not exist. Disabling BDI.".format(self.jid))
self.bdi_enabled = False
self.asl_file = None
def set_env(self): def set_env(self):
self.bdi_env = pyson.runtime.Environment() self.bdi_env = pyson.runtime.Environment()
...@@ -87,7 +92,7 @@ class BDIAgent(Agent): ...@@ -87,7 +92,7 @@ class BDIAgent(Agent):
"""Set an agent's belief. If it already exists, updates it.""" """Set an agent's belief. If it already exists, updates it."""
new_args = () new_args = ()
for x in args: for x in args:
if type(x) == str: if type(x) == str:
new_args += (pyson.Literal(x),) new_args += (pyson.Literal(x),)
else: else:
new_args += (x,) new_args += (x,)
...@@ -116,7 +121,7 @@ class BDIAgent(Agent): ...@@ -116,7 +121,7 @@ class BDIAgent(Agent):
else: else:
new_args += (x,) new_args += (x,)
term = pyson.Literal(name, tuple(new_args), PERCEPT_TAG) term = pyson.Literal(name, tuple(new_args), PERCEPT_TAG)
self.agent.bdi_intention_buffer.append((pyson.Trigger.removal, pyson.GoalType.belief, belief, self.agent.bdi_intention_buffer.append((pyson.Trigger.removal, pyson.GoalType.belief, term,
pyson.runtime.Intention())) pyson.runtime.Intention()))
# self.agent.bdi_agent.call(pyson.Trigger.removal, pyson.GoalType.belief, term, # self.agent.bdi_agent.call(pyson.Trigger.removal, pyson.GoalType.belief, term,
# pyson.runtime.Intention()) # pyson.runtime.Intention())
......
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