Example #1
0
    def loadJSON(self, jsonstring):
        """
        loads a JSON string in the envelope
        """
        r = json.loads(jsonstring)

        if "to" in r:
            for a in r["to"]:
                aid = AID.aid()
                aid.setName(a["name"])
                for addr in a["addresses"]:
                    aid.addAddress(addr)
                self.addTo(aid)
        if "from" in r:
            aid = AID.aid()
            aid.setName(r["from"]["name"])
            for addr in r["from"]["addresses"]:
                aid.addAddress(addr)
            self.setFrom(aid)

        if "acl-representation" in r:
            self.setAclRepresentation(r["acl-representation"])
        if "payload-length" in r:
            self.setPayloadLength(r["payload-length"])
        if "payload-encoding" in r:
            self.setPayloadEncoding(r["payload-encoding"])
        if "date" in r:
            self.setDate(r["date"])
        if "intended-receiver" in r:
            for ag in r["intended-receiver"]:
                aid = AID.aid()
                aid.setName(ag["name"])
                for addr in ag["addresses"]:
                    aid.addAddress(addr)
                self.addIntendedReceiver(aid)
Example #2
0
    def loadJSON(self, jsonstring):
        """
        loads a JSON string in the envelope
        """
        r = json.loads(jsonstring)

        if "to" in r:
            for a in r["to"]:
                aid = AID.aid()
                aid.setName(a["name"])
                for addr in a["addresses"]:
                    aid.addAddress(addr)
                self.addTo(aid)
        if "from" in r:
            aid = AID.aid()
            aid.setName(r["from"]["name"])
            for addr in r["from"]["addresses"]:
                aid.addAddress(addr)
            self.setFrom(aid)

        if "acl-representation" in r:
            self.setAclRepresentation(r["acl-representation"])
        if "payload-length" in r:
            self.setPayloadLength(r["payload-length"])
        if "payload-encoding" in r:
            self.setPayloadEncoding(r["payload-encoding"])
        if "date" in r:
            self.setDate(r["date"])
        if "intended-receiver" in r:
            for ag in r["intended-receiver"]:
                aid = AID.aid()
                aid.setName(ag["name"])
                for addr in ag["addresses"]:
                    aid.addAddress(addr)
                self.addIntendedReceiver(aid)
Example #3
0
    def loadJSON(self, jsonstring):
        """
        loads a JSON string in the message
        """
        p = json.loads(jsonstring)

        if "performative" in p:
            self.setPerformative(p["performative"])

        if "sender" in p:
            s = AID.aid()
            s.loadJSON(p["sender"])
            self.setSender(s)

        if "receivers" in p:
            for i in p["receivers"]:
                s = AID.aid()
                s.loadJSON(i)
                self.addReceiver(s)

        if "content" in p:
            self.setContent(p["content"])

        if "reply-with" in p:
            self.setReplyWith(p["reply-with"])

        if "reply-by" in p:
            self.setReplyBy(p["reply-by"])

        if "in-reply-to" in p:
            self.setInReplyTo(p["in-reply-to"])

        if "reply-to" in p:
            for i in p["reply-to"]:
                s = AID.aid()
            s.loadJSON(i)
            self.addReplyTo(s)

        if "language" in p:
            self.setLanguage(p["language"])

        if "encoding" in p:
            self.setEncoding(p["encoding"])

        if "ontology" in p:
            self.setOntology(p['ontology'])

        if "protocol" in p:
            self.setProtocol(p['protocol'])

        if "conversation-id" in p:
            self.setConversationId(p["conversation-id"])
Example #4
0
    def loadJSON(self, jsonstring):
        """
        loads a JSON string in the message
        """
        p = json.loads(jsonstring)

        if "performative" in p:
            self.setPerformative(p["performative"])

        if "sender" in p:
            s = AID.aid()
            s.loadJSON(p["sender"])
            self.setSender(s)

        if "receivers" in p:
            for i in p["receivers"]:
                s = AID.aid()
                s.loadJSON(i)
                self.addReceiver(s)

        if "content" in p:
            self.setContent(p["content"])

        if "reply-with" in p:
            self.setReplyWith(p["reply-with"])

        if "reply-by" in p:
            self.setReplyBy(p["reply-by"])

        if "in-reply-to" in p:
            self.setInReplyTo(p["in-reply-to"])

        if "reply-to" in p:
            for i in p["reply-to"]:
                s = AID.aid()
            s.loadJSON(i)
            self.addReplyTo(s)

        if "language" in p:
            self.setLanguage(p["language"])

        if "encoding" in p:
            self.setEncoding(p["encoding"])

        if "ontology" in p:
            self.setOntology(p['ontology'])

        if "protocol" in p:
            self.setProtocol(p['protocol'])

        if "conversation-id" in p:
            self.setConversationId(p["conversation-id"])
Example #5
0
File: AMS.py Project: exic/spade2
	def __init__(self, content = None, co = None):
		"""
		AAD constructor
		Optionally accepts a string containing a SL definition of the AAD
		or a ContentObject version of the AAD
		"""

		self.name = None #AID.aid()
		self.ownership = None
		self.state = "active"

		if co:
			try:
				self.name = AID.aid(co=co["fipa:aid"])
			except:
				self.name = None
			try:
				self.ownership = co["fipa:ownership"]
			except:
				self.ownership = None
			try:
				self.state = co["fipa:state"]
			except:
				self.state = None
		elif content != None:
			self.loadSL0(content)
Example #6
0
    def __init__(self, content=None, co=None):
        """
        AAD constructor
        Optionally accepts a string containing a SL definition of the AAD
        or a ContentObject version of the AAD
        """

        self.name = None  # AID.aid()
        self.ownership = None
        self.state = "active"

        if co:
            try:
                self.name = AID.aid(co=co["fipa:aid"])
            except:
                self.name = None
            try:
                self.ownership = co["fipa:ownership"]
            except:
                self.ownership = None
            try:
                self.state = co["fipa:state"]
            except:
                self.state = None
        elif content is not None:
            self.loadSL0(content)
Example #7
0
        def _process(self):
            self.result = []
	    self.msg.addReceiver(AID.aid(self.aname, addresses=["xmpp://"+self.aname]))
	    self.msg.setContent("MEMBERS")
	    self.myAgent.send(self.msg)
	    rep = None
	    rep = self._receive(True, 20)
	    if rep:
	        print "The members list arrived"
	        self.result = rep.getContent().split(",")
Example #8
0
            def _process(self):
                msg = None
                msg = self._receive(block=True)
                if msg:
                    #self.myAgent.DEBUG("AMS received presence message "+ str(msg),"info", "ams")
                    typ = msg.getType()
                    frm = msg.getFrom()
                    status = msg.getStatus()
                    show = msg.getShow()
                    reply_address = frm
                    if typ == "subscribe":
                        frm = AID.aid(name=str(frm),
                                      addresses=["xmpp://" + str(frm)])
                        aad = AmsAgentDescription()
                        aad.name = frm
                        if status:
                            aad.state = status
                        if show:
                            aad.ownership = show
                        else:
                            aad.ownership = frm.getName()

                        if frm.getName() not in self.myAgent.agentdb.keys():
                            self.myAgent.agentdb[frm.getName()] = aad
                        elif self.myAgent.agentdb[frm.getName()].getOwnership(
                        ) == aad.getOwnership():
                            self.myAgent.agentdb[frm.getName()] = aad
                        else:
                            presence = xmpp.Presence(reply_address,
                                                     typ="unsubscribed",
                                                     xmlns=xmpp.NS_CLIENT)
                            presence.setFrom(self.myAgent.JID)
                            self.myAgent.send(presence)
                            return

                        self.myAgent.DEBUG(
                            "AMS succesfully registered agent " +
                            frm.getName(), "ok", "ams")
                        presence = xmpp.Presence(reply_address,
                                                 typ="subscribed")
                        presence.setFrom(self.myAgent.JID)
                        self.myAgent.DEBUG("AMS sends " + str(presence),
                                           "info")
                        self.myAgent.send(presence)
                    elif typ == "unsubscribe":
                        if str(frm) in self.myAgent.agentdb.keys():
                            del self.myAgent.agentdb[str(frm)]
                            self.myAgent.DEBUG(
                                "Agent " + str(frm) + " deregistered from AMS",
                                "ok", "ams")
                        else:
                            self.myAgent.DEBUG(
                                "Agent " + str(frm) + " deregistered from AMS",
                                "error", "ams")
                return
Example #9
0
    def startElement(self, localName, attributes):

        self.accumulator = ""

        if self.FIPA_MESSAGE_TAG == localName.lower():
            self.msg.setPerformative(attributes.getValue(self.ACT_TAG))
            try:
                self.msg.setConversationId(
                    attributes.getValue(self.CONVERSATION_ID_TAG))

            except:
                pass

        if self.SENDER_TAG == localName.lower():
            self.aid = AID.aid()
            self.aidTag = self.SENDER_TAG

        if self.RECEIVER_TAG == localName.lower():
            self.aid = AID.aid()
            self.aidTag = self.RECEIVER_TAG

        if self.REPLY_TO_TAG == localName.lower():
            self.aid = AID.aid()
            self.aidTag = self.REPLY_TO_TAG

        if self.RESOLVERS_TAG == localName.lower():
            self.aid = AID.aid()
            self.aidTag = self.RESOLVERS_TAG

        if self.REPLY_BY_TAG == localName.lower():
            self.msg.setReplyBy(
                BasicFipaDateTime(attributes.getValue(self.TIME_TAG)))

        if self.NAME_TAG == localName.lower():
            self.aid.setName(attributes.getValue(self.ID_TAG))

        if self.URL_TAG == localName.lower():
            self.aid.addAddress(attributes.getValue(self.HREF_TAG))
Example #10
0
File: AMS.py Project: exic/spade2
	def loadSL0(self,content):
		"""
		inits the AAD with a SL string representation
		"""
		if content != None:
			if "name" in content:
				self.name = AID.aid()
				self.name.loadSL0(content.name)

			if "ownership" in content:
				self.ownership = content.ownership[0]

			if "state" in content:
				self.state = content.state[0]
Example #11
0
    def loadSL0(self, content):
        """
        inits the AAD with a SL string representation
        """
        if content is not None:
            if "name" in content:
                self.name = AID.aid()
                self.name.loadSL0(content.name)

            if "ownership" in content:
                self.ownership = content.ownership[0]

            if "state" in content:
                self.state = content.state[0]
Example #12
0
    def startElement(self, localName, attributes):

        self.accumulator = ""

        if self.FIPA_MESSAGE_TAG == localName.lower():
            self.msg.setPerformative(attributes.getValue(self.ACT_TAG))
            try:
                self.msg.setConversationId(attributes.getValue(self.CONVERSATION_ID_TAG))

            except:
                pass

        if self.SENDER_TAG == localName.lower():
            self.aid = AID.aid()
            self.aidTag = self.SENDER_TAG

        if self.RECEIVER_TAG == localName.lower():
            self.aid = AID.aid()
            self.aidTag = self.RECEIVER_TAG

        if self.REPLY_TO_TAG == localName.lower():
            self.aid = AID.aid()
            self.aidTag = self.REPLY_TO_TAG

        if self.RESOLVERS_TAG == localName.lower():
            self.aid = AID.aid()
            self.aidTag = self.RESOLVERS_TAG

        if self.REPLY_BY_TAG == localName.lower():
            self.msg.setReplyBy(BasicFipaDateTime(attributes.getValue(self.TIME_TAG)))

        if self.NAME_TAG == localName.lower():
            self.aid.setName(attributes.getValue(self.ID_TAG))

        if self.URL_TAG == localName.lower():
            self.aid.addAddress(attributes.getValue(self.HREF_TAG))
Example #13
0
    def __init__(self, agent, jid, presence=''):
        self.myAgent = agent
        self._jid = jid
        self._presence = presence

        # Generate AID
        self._aid = AID.aid(name=jid, addresses=["xmpp://" + str(jid)])

        # Get subscription from roster
        roster = agent._roster
        if jid in roster:
            if "subscription" in roster[jid]:
                self._subscription = roster[jid]["subscription"]
            else:
                self._subscription = "none"
        else:
            self._subscription = "none"
Example #14
0
            def _process(self):
                msg = None
                msg = self._receive(block=True)
                if msg:
                    #self.myAgent.DEBUG("AMS received presence message "+ str(msg),"info", "ams")
                    typ = msg.getType()
                    frm = msg.getFrom()
                    status = msg.getStatus()
                    show = msg.getShow()
                    reply_address = frm
                    if typ == "subscribe":
                        frm = AID.aid(name=str(frm), addresses=["xmpp://" + str(frm)])
                        aad = AmsAgentDescription()
                        aad.name = frm
                        if status:
                            aad.state = status
                        if show:
                            aad.ownership = show
                        else:
                            aad.ownership = frm.getName()

                        if frm.getName() not in self.myAgent.agentdb.keys():
                            self.myAgent.agentdb[frm.getName()] = aad
                        elif self.myAgent.agentdb[frm.getName()].getOwnership() == aad.getOwnership():
                            self.myAgent.agentdb[frm.getName()] = aad
                        else:
                            presence = xmpp.Presence(reply_address, typ="unsubscribed", xmlns=xmpp.NS_CLIENT)
                            presence.setFrom(self.myAgent.JID)
                            self.myAgent.send(presence)
                            return

                        self.myAgent.DEBUG("AMS succesfully registered agent " + frm.getName(), "ok", "ams")
                        presence = xmpp.Presence(reply_address, typ="subscribed")
                        presence.setFrom(self.myAgent.JID)
                        self.myAgent.DEBUG("AMS sends " + str(presence), "info")
                        self.myAgent.send(presence)
                    elif typ == "unsubscribe":
                        if str(frm) in self.myAgent.agentdb.keys():
                            del self.myAgent.agentdb[str(frm)]
                            self.myAgent.DEBUG("Agent " + str(frm) + " deregistered from AMS", "ok", "ams")
                        else:
                            self.myAgent.DEBUG("Agent " + str(frm) + " deregistered from AMS", "error", "ams")
                return
Example #15
0
    def processAID(self, _aid):
        """
        parses an AID.
        returns a pyparsing.ParseResult class
        """

        aid = AID.aid()

        if 'name' in _aid:
            aid.setName(_aid['name'])

        if 'addresses' in _aid:
            addr = _aid['addresses']
            for i in addr:
                aid.addAddress(i)

        if 'resolvers' in _aid:
            res = _aid['resolvers']
            for i in res:
                aid.addResolvers(self.processAID(i))

        return aid
Example #16
0
    def processAID(self, _aid):
        """
        parses an AID.
        returns a pyparsing.ParseResult class
        """

        aid = AID.aid()

        if 'name' in _aid:
            aid.setName(_aid['name'])

        if 'addresses' in _aid:
            addr = _aid['addresses']
            for i in addr:
                aid.addAddress(i)

        if 'resolvers' in _aid:
            res = _aid['resolvers']
            for i in res:
                aid.addResolvers(self.processAID(i))

        return aid
Example #17
0
import networkx
N = 500                 # order (number of nodes) of the network
kmean = 5                 # mean node degree
phi = (kmean + 0.0) / N   # probability of attachment between two nodes chosen at random

# create the network
g = networkx.erdos_renyi_graph(N, phi)

pInfected = 0.2
pInfect = 0.2
pRecover = 0.05
pRemoveEdge = 1.0
pAddEdge = 1.0
maxTime = 50000
model = AID()

params = dict()
params[AID.P_INFECTED] = pInfected
params[AID.P_INFECT] = pInfect
params[AID.P_RECOVER] = pRecover
params[AID.P_REMOVE_EDGE] = pRemoveEdge
params[AID.P_ADD_EDGE] = pAddEdge
params[AID.MAX_TIME] = maxTime

# run the simulation
f = SynchronousDynamics(model, g)
sync = f.set(params).run()

# save the results for later
with open('sync.pickle', 'wb') as handle: