此安装主要参考了spade官网。
https://spade-mas.readthedocs.io/en/latest/usage.html
1 XMPP安装
1.1 XMPP账号注册及安装:
首先登录https://xmpp.org/getting-started/ 下载相应的客户端注册XMPP账号
客户端选择如下:
- Conversations (Android)
- Swift.IM (OS X, Windows & Linux)
- Gajim (Windows & Linux)
- Monal IM (macOS & iOS)
- Converse.js (Browser)
移动端注册百度搜索Conversations下载该APP,ios系统可在应用商店搜索Monal进行安装
1.2 XMPP服务器安装:
登录https://xmpp.org/software/servers.html
XMPP提供了多种服务器,选择适合自己系统的服务器即可。
Project Name | Platforms |
Linux / Windows | |
Linux / macOS / Solaris / Windows | |
Linux / macOS / Windows | |
Windows | |
Linux / Windows | |
Linux / macOS | |
Linux | |
Linux / macOS | |
Linux / macOS / Solaris / Windows | |
BSD / Linux / macOS | |
Linux / macOS / Solaris / Windows | |
Linux / macOS / Solaris |
此处我选的是Prosody IM 。Prosody IM 服务器安装命令为:
sudo apt-get install prosody
此处输入y,再按回车键。
2 spade安装
2.1 Spade 代理安装:
在pycharm里安装spade包:
File->setting->project interpreter
点击右上角的“+”,然后输入spade搜索spade包,选中spade后点击“install package”安装。
同理,安装asyncio包,方法略。
2.2 测试:
用pycharm创建dummyagent.py一个文档
from spade import agent
class DummyAgent(agent.Agent):
async def setup(self):
print("Hello World! I'm agent {}".format(str(self.jid)))
#注意:修改成刚才注册时的账号和密码
dummy = DummyAgent("your_jid@your_xmpp_server", "your_password")dummy.start()
dummy.stop()
2.3 简单的例子:
例子1:An agent with a behaviour
新建AnAgentWithABehaviour.py文档
import timeimport asynciofrom spade.agent import Agentfrom spade.behaviour import CyclicBehaviour
class DummyAgent(Agent):
class MyBehav(CyclicBehaviour):
async def on_start(self):
print("Starting behaviour . . .")
self.counter = 0
async def run(self):
print("Counter: {}".format(self.counter))
self.counter += 1
await asyncio.sleep(1)
async def setup(self):
print("Agent starting . . .")
b = self.MyBehav()
self.add_behaviour(b)
if __name__ == "__main__":
#注意:修改成刚才注册时的账号和密码
dummy = DummyAgent("your_jid@your_xmpp_server", "your_password")
dummy.start()
print("Wait until user interrupts with ctrl+C")
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
break
dummy.stop()
例子2:kill a behaviour
新建killABehaviour.py:
import timeimport asynciofrom spade.agent import Agentfrom spade.behaviour import CyclicBehaviour
class DummyAgent(Agent):
class MyBehav(CyclicBehaviour):
async def on_start(self):
print("Starting behaviour . . .")
self.counter = 0
async def run(self):
print("Counter: {}".format(self.counter))
self.counter += 1
if self.counter > 3:
self.kill(exit_code=10)
return
await asyncio.sleep(1)
async def on_end(self):
print("Behaviour finished with exit code {}.".format(self.exit_code))
async def setup(self):
print("Agent starting . . .")
self.my_behav = self.MyBehav()
self.add_behaviour(self.my_behav)
if __name__ == "__main__":
dummy = DummyAgent("your_jid@your_xmpp_server", "your_password")
future = dummy.start()
future.result() # Wait until the start method is finished
# wait until user interrupts with ctrl+C
while not dummy.my_behav.is_killed():
try:
time.sleep(1)
except KeyboardInterrupt:
break
dummy.stop()
例子3:关闭spade
新建文档FinishingSPADE.py
from spade import quit_spade
from spade import agent
class DummyAgent(agent.Agent):
async def setup(self):
print("Hello World! I'm agent {}".format(str(self.jid)))
dummy = DummyAgent("your_jid@your_xmpp_server", "your_password")dummy.start()
dummy.stop()
quit_spade()
例4:在一个代理中创建另一个代理
新建文档CreatingAgentFromWithinAnotherAgent.py
from spade import agent
import Agentfrom spade.behaviour import OneShotBehaviour
class CreateBehav(OneShotBehaviour):
async def run(self):
agent2 = Agent("agent2@fake_server", "fake_password")
# This start is inside an async def, so it must be awaited
await agent2.start(auto_register=False)
agent1 = Agent("agent1@fake_server", "fake_password")agent1.add_behaviour(CreateBehav())# This start is in a synchronous piece of code, so it must NOT be awaitedagent1.start(auto_register=False)