Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds behaviour return feature #52

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 112 additions & 112 deletions pade/behaviours/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,115 +10,115 @@

class BaseBehaviour(Behaviour):

''' BaseBehaviour class ihnerits from Behaviour class and
implements the basic methods for scheduled behaviours.
'''

def __init__(self, agent):
''' This method initializes a new instance of BaseBehaviour
class and explicitly calls the Behaviour.__init__() method.
'''
super().__init__(agent)
# Queue of received messages by the agent and unread by this behaviour
self.messages = queue.Queue()
# Lock object (to ensure the mutual exclusion, when needed)
self.__lock = None


def read(self, block = True):
''' It gets the first message in the local message queue.
'''
if block:
return self.messages.get()
else:
try:
return self.messages.get_nowait()
except queue.Empty:
return None


def send(self, message):
''' This method gets a message and passes it to self.agent.send() method.
It was coded just to complement the pair read/send in the BaseBehaviour class.
The method self.agent.send() can still be used directly in the code.
'''
self.agent.send(message)


def read_timeout(self, timeout):
''' It tries to read a message twice until the end of timeout.
In cases of no messages, this method returns None
'''
message = self.read(block = False)
if message != None:
return message
else:
sleep(timeout)
return self.read(block = False)


def receive(self, message):
''' It sets a new message on local messages queue.
'''
if isinstance(message, ACLMessage):
self.messages.put(message)
else:
raise ValueError('message object type must be ACLMessage!')



def action(self):
''' This is an abstract method that must be overridden in the
child classes, writing the main actions of this behaviour.
'''
pass


def done(self):
''' This is an abstract method that must be overridden in the
child classes, dealing with the finish of this behaviour.
'''
pass


def wait(self, timeout):
''' This method sleeps a behaviour until occurs a timeout. The
behaviour will execute normally afterwards.
'''
sleep(timeout)


def on_end(self):
''' The scheduler will calls this method after the self.done()
method returns True and before the end of this behaviour. It is
the last action of a behaviour.
'''
pass


def has_messages(self):
''' A method to returns if this behaviour has messages in its
received messages queue.
'''
return self.messages.qsize() != 0


def add_lock(self, lock):
''' Adds a threading.Lock object to this behaviour, allowing
it to execute the mutual exclusion correctly.
'''
self.__lock = lock

def lock(self):
''' Tries to acquire the lock of the threading.Lock object.
The behaviour will block if fails.
'''
self.__lock.acquire()

def unlock(self):
''' Releases the lock of the threading.Lock object, allowing
the other behaviours to acquire it.
'''
self.__lock.release()
''' BaseBehaviour class ihnerits from Behaviour class and
implements the basic methods for scheduled behaviours.
'''

def __init__(self, agent):
''' This method initializes a new instance of BaseBehaviour
class and explicitly calls the Behaviour.__init__() method.
'''
super().__init__(agent)
# Queue of received messages by the agent and unread by this behaviour
self.messages = queue.Queue()
# Lock object (to ensure the mutual exclusion, when needed)
self.__lock = None


def read(self, block = True):
''' It gets the first message in the local message queue.
'''
if block:
return self.messages.get()
else:
try:
return self.messages.get_nowait()
except queue.Empty:
return None


def send(self, message):
''' This method gets a message and passes it to self.agent.send() method.
It was coded just to complement the pair read/send in the BaseBehaviour class.
The method self.agent.send() can still be used directly in the code.
'''
self.agent.send(message)


def read_timeout(self, timeout):
''' It tries to read a message twice until the end of timeout.
In cases of no messages, this method returns None
'''
message = self.read(block = False)
if message != None:
return message
else:
sleep(timeout)
return self.read(block = False)


def receive(self, message):
''' It sets a new message on local messages queue.
'''
if isinstance(message, ACLMessage):
self.messages.put(message)
else:
raise ValueError('message object type must be ACLMessage!')



def action(self):
''' This is an abstract method that must be overridden in the
child classes, writing the main actions of this behaviour.
'''
pass


def done(self):
''' This is an abstract method that must be overridden in the
child classes, dealing with the finish of this behaviour.
'''
pass


def wait(self, timeout):
''' This method sleeps a behaviour until occurs a timeout. The
behaviour will execute normally afterwards.
'''
sleep(timeout)


def on_end(self):
''' The scheduler will calls this method after the self.done()
method returns True and before the end of this behaviour. It is
the last action of a behaviour.
'''
pass


def has_messages(self):
''' A method to returns if this behaviour has messages in its
received messages queue.
'''
return self.messages.qsize() != 0


def add_lock(self, lock):
''' Adds a threading.Lock object to this behaviour, allowing
it to execute the mutual exclusion correctly.
'''
self.__lock = lock

def lock(self):
''' Tries to acquire the lock of the threading.Lock object.
The behaviour will block if fails.
'''
self.__lock.acquire()

def unlock(self):
''' Releases the lock of the threading.Lock object, allowing
the other behaviours to acquire it.
'''
self.__lock.release()
Loading