How to use the spade.Behaviour.OneShotBehaviour function in spade

To help you get started, we’ve selected a few spade examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github javipalanca / spade / examples / unittests / p2pTestCase.py View on Github external
class p2pAnswerMsgBehav(spade.Behaviour.OneShotBehaviour):

    def __init__(self,method):
        spade.Behaviour.OneShotBehaviour.__init__(self)
        self.method = method

    def _process(self):
        msg = None
        msg = self._receive(block=True,timeout=10)
        if msg != None:
			content = msg.getContent()
			msg = msg.createReply()
			msg.setContent(content)
			self.myAgent.send(msg, method=self.method)

class p2pSendMultiMsgBehav(spade.Behaviour.OneShotBehaviour):

    def __init__(self,method, nmsg=1):
        spade.Behaviour.OneShotBehaviour.__init__(self)
        self.method = method
        self.nmsg = int(nmsg)

    def _process(self):
        self.myAgent.routeTag = []
        msg = spade.ACLMessage.ACLMessage()
        msg.setPerformative("inform")
        msg.addReceiver(spade.AID.aid("b@"+host,["xmpp://b@"+host]))
        
        socket = None

        for i in range (self.nmsg):
            msg.setContent(str(i+1))
github javipalanca / spade / examples / unittests / basicTestCase.py View on Github external
class GetPIBehav(spade.Behaviour.OneShotBehaviour):

	def _process(self):
		self.myAgent.pi = self.myAgent.getPlatformInfo()
		
class SendMsgBehav(spade.Behaviour.OneShotBehaviour):

    def _process(self):
        msg = spade.ACLMessage.ACLMessage()
        msg.setPerformative("inform")
        msg.addReceiver(spade.AID.aid("b@"+host,["xmpp://b@"+host]))
        msg.setContent("testSendMsg")
        
        self.myAgent.send(msg)

class RecvMsgBehav(spade.Behaviour.OneShotBehaviour):

    def _process(self):
        self.myAgent.msg = self._receive(block=True,timeout=10)

class SendAndRecvMsgBehav(spade.Behaviour.OneShotBehaviour):

    def _process(self):
        msg = spade.ACLMessage.ACLMessage()
        msg.setPerformative("inform")
        msg.addReceiver(spade.AID.aid("b@"+host,["xmpp://b@"+host]))
        msg.setContent("testSendAndRecvMsg")

        self.myAgent.send(msg)
        self.myAgent.msg = None
        self.myAgent.msg = self._receive(block=True,timeout=10)
github javipalanca / spade / examples / unittests / amsTestCase.py View on Github external
def _setup(self):
            self.search = None
            
class SearchBehav(spade.Behaviour.OneShotBehaviour):
    
        def __init__(self, s):
            self.s = s
            spade.Behaviour.OneShotBehaviour.__init__(self)

        def _process(self):

            aad = spade.AMS.AmsAgentDescription()
            aad.setAID(spade.AID.aid(self.s+"@"+host,["xmpp://"+self.s+"@"+host]))
            self.myAgent.search = self.myAgent.searchAgent(aad)
            
class ModifyBehav(spade.Behaviour.OneShotBehaviour):

        def __init__(self, s):
            self.s = s
            spade.Behaviour.OneShotBehaviour.__init__(self)

        def _process(self):

            aad = spade.AMS.AmsAgentDescription()
            #aad.setAID(spade.AID.aid(self.s+"@"+host,["xmpp://"+self.s+"@"+host]))
            aad.ownership = "UNITTEST" 
            self.myAgent.result = self.myAgent.modifyAgent(aad)

            aad = spade.AMS.AmsAgentDescription()
            aad.setAID(spade.AID.aid(self.s+"@"+host,["xmpp://"+self.s+"@"+host]))
            self.myAgent.search = self.myAgent.searchAgent(aad)
github javipalanca / spade / examples / unittests / amsTestCase.py View on Github external
def __init__(self, s):
            self.s = s
            spade.Behaviour.OneShotBehaviour.__init__(self)

        def _process(self):

            aad = spade.AMS.AmsAgentDescription()
            #aad.setAID(spade.AID.aid(self.s+"@"+host,["xmpp://"+self.s+"@"+host]))
            aad.ownership = "UNITTEST" 
            self.myAgent.result = self.myAgent.modifyAgent(aad)

            aad = spade.AMS.AmsAgentDescription()
            aad.setAID(spade.AID.aid(self.s+"@"+host,["xmpp://"+self.s+"@"+host]))
            self.myAgent.search = self.myAgent.searchAgent(aad)
            
class NotModifyBehav(spade.Behaviour.OneShotBehaviour):

        def __init__(self, s):
            self.s = s
            spade.Behaviour.OneShotBehaviour.__init__(self)

        def _process(self):

            aad = spade.AMS.AmsAgentDescription()
            aad.setAID(spade.AID.aid(self.s+"@"+host,["xmpp://"+self.s+"@"+host]))
            aad.ownership = "NOT_ALLOWED" 
            self.myAgent.result = self.myAgent.modifyAgent(aad)

            aad = spade.AMS.AmsAgentDescription()
            aad.setAID(spade.AID.aid(self.s+"@"+host,["xmpp://"+self.s+"@"+host]))
            self.myAgent.search = self.myAgent.searchAgent(aad)
github javipalanca / spade / examples / unittests / amsTestCase.py View on Github external
def __init__(self, s):
            self.s = s
            spade.Behaviour.OneShotBehaviour.__init__(self)
github javipalanca / spade / examples / benchmark / bench1_receiver.py View on Github external
class ReceptorBenchmark1(spade.Agent.Agent):
	
	def __init__(self, jid, password, smethod="p2p"):
		spade.Agent.Agent.__init__(self, jid, password, p2p=True)
		self.nemisores = 0
                self.smethod = str(smethod)
		self._debug = True

	def _setup(self):
            temp = spade.Behaviour.ACLTemplate()
            temp.setPerformative("request")
	    t = spade.Behaviour.MessageTemplate(spade.Behaviour.ACLTemplate())
	    self.addBehaviour(self.behav(),t)
	
	class behav(spade.Behaviour.OneShotBehaviour):
	
	    def _process(self):

    		print "Soy "+self.myAgent.getName()+". Arranco"

    		ntotal = 0
                while True:
    			msg = self._receive(True)  #//esperem missatge des de'l emisor
    			self.DEBUG("Llego mensaje de "+msg.getSender().getName(), "info")
                        msg = msg.createReply()
    			#msg.addReceiver(msg.getSender())		#//tornem el missatge al emisor
                        self.myAgent.send(msg, self.myAgent.smethod)
    			ntotal+=1
github javipalanca / spade / examples / fsm.py View on Github external
self.myAgent.counter = self.myAgent.counter + 1
			if self.myAgent.counter > 2:
				self._exitcode = self.myAgent.TRANSITION_TO_TWO
			else:
				self._exitcode = self.myAgent.TRANSITION_DEFAULT

 	class StateTwo(spade.Behaviour.OneShotBehaviour):
		def _process(self):
			print "This is State Two..."
			self.myAgent.counter = self.myAgent.counter + 1
			if self.myAgent.counter > 5:
				self._exitcode = self.myAgent.TRANSITION_TO_THREE
			else:
				self._exitcode = self.myAgent.TRANSITION_DEFAULT
 
	class StateThree(spade.Behaviour.OneShotBehaviour):
		def _process(self):
			print "This is State Three..."
			self.myAgent.counter = self.myAgent.counter + 1
			if self.myAgent.counter > 8:
				self._exitcode = self.myAgent.TRANSITION_TO_FOUR
			else:
				self._exitcode = self.myAgent.TRANSITION_DEFAULT
 
	class StateFour(spade.Behaviour.OneShotBehaviour):
		def _process(self):
			print "This is State Four..."
			self.myAgent.counter = self.myAgent.counter + 1
			if self.myAgent.counter > 11:
				print "Counter ", self.myAgent.counter
				print "Bye Bye"
				self.myAgent._kill()
github javipalanca / spade / examples / dalmuti / jugador.py View on Github external
sys.path.append("../..")

import spade
import random
import time
import string
from cartas import Carta
from cartas import jugada

servidor="localhost"

nombreAgente = ""

class jugador(spade.Agent.Agent):

    class inicioBehav(spade.Behaviour.OneShotBehaviour):
        def _process(self):
            self.msg = None
            
            # Esperamos indefinidamente hasta que empiece la partida
            self.msg = self._receive(True)
        
            # En este punto habra empezado
            print nombreAgente+": Ok!! A ver si hay suerte..."

            # Escogemos una carta al azar para establecer los
            # roles iniciales de la partida
            lim = string.atoi(self.msg.getContent())

            carta = random.randint(0, lim-1)

            print nombreAgente+": Elijo la carta "+str(carta)
github javipalanca / spade / examples / basic.py View on Github external
This file shows a simple agent which just asks for the 
Platform Information (pi) to the AMS agent and prints it 
to the debug system.
It uses a OneShot Behaviour
You need to be running a SPADE platform on the same host
'''

import os
import sys
sys.path.append('..'+os.sep+'trunk')
sys.path.append('..')

import spade

class MyAgent(spade.Agent.Agent):
	class MyBehav(spade.Behaviour.OneShotBehaviour):
		def onStart(self):
			self.myAgent.DEBUG("Starting behaviour . . .")

		def _process(self):
			self.myAgent.DEBUG("Hello World from a OneShot")
			pi = self.myAgent.getPlatformInfo()
			self.myAgent.DEBUG(str(pi))

		def onEnd(self):
			self.myAgent.DEBUG("Ending behaviour . . .")

	def _setup(self):
		self.DEBUG("MyAgent starting . . .")
		b = self.MyBehav()
		self.addBehaviour(b, None)
github javipalanca / spade / examples / rpc.py View on Github external
This file shows a simple agent which just asks for the 
Platform Information (pi) to the AMS agent and prints it 
to the debug system.
It uses a OneShot Behaviour
You need to be running a SPADE platform on the same host
'''

import os
import sys
sys.path.append('..'+os.sep+'trunk')
sys.path.append('..')

import spade

class MyAgent(spade.Agent.Agent):
	class MyBehav(spade.Behaviour.OneShotBehaviour):
	    
		def onStart(self):
			self.myAgent.DEBUG("Starting behaviour . . .",'ok')
			
			s = spade.DF.Service(name="RPCmethod",owner=self.myAgent.getAID(),P="P")			
			
			self.myAgent.registerService(s,self.myAgent.RPCmethod)
			
			self.myAgent.DEBUG("Service registered "+str(s),'ok')

		def _process(self):
		    self.myAgent.DEBUG("Starting process . . .")
		    
		    s = self.myAgent.searchService(spade.DF.Service(name="RPCmethod"))
		    self.myAgent.DEBUG("Found service " + str(s[0]),'ok')
		    self.myAgent.DEBUG("Invoking service",'ok')