示例#1
0
class BDIECLiPSeTestCase(BDIPrologTestCase, unittest.TestCase):
    def setUp(self):
	self.a = BDIAgent("bdi_eclipse@" + host, "secret")
	self.a.kb.configure("ECLiPSe")
        #self.a.setDebugToScreen()
        self.a.start()
	self.set_up()
示例#2
0
class BDIFlora2TestCase(BDIPrologTestCase, unittest.TestCase):
    def setUp(self):
	self.a = BDIAgent("bdi_flora2@" + host, "secret")
	self.a.kb.configure("Flora2")
        #self.a.setDebugToScreen()
        self.a.start()
	self.set_up()
示例#3
0
class BDIFlora2TestCase(BDIPrologTestCase, unittest.TestCase):
    def setUp(self):
        self.a = BDIAgent("bdi_flora2@" + host, "secret")
        self.a.kb.configure("Flora2")
        #self.a.setDebugToScreen()
        self.a.start()
        self.set_up()
示例#4
0
class BDIECLiPSeTestCase(BDIPrologTestCase, unittest.TestCase):
    def setUp(self):
        self.a = BDIAgent("bdi_eclipse@" + host, "secret")
        self.a.kb.configure("ECLiPSe")
        #self.a.setDebugToScreen()
        self.a.start()
        self.set_up()
示例#5
0
class PlanTestCase(unittest.TestCase):

    def setUp(self):

        self.a = BDIAgent("bdi@" + host, "secret")
        self.a.start()

    def tearDown(self):
        self.a.stop()
        del self.a

    def testAddBelieve(self):
        self.a.addBelieve(expr("Value(TestValue)"))

        self.assertTrue(self.a.askBelieve("Value(TestValue)"))

    def testAddGoal(self):
        g = Goal("Value(2)")
        self.a.addGoal(g)

        self.assertEqual(len(self.a.bdiBehav.goals), 1)
        self.assertEqual(self.a.bdiBehav.goals[0].expression, "Value(2)")
示例#6
0
class PlanTestCase(unittest.TestCase):
    def setUp(self):

        self.a = BDIAgent("bdi@" + host, "secret")
        self.a.start()
        #self.a.setDebugToScreen()

    def tearDown(self):
        self.a.stop()
        del self.a

    def testAddBelieve(self):
        self.a.addBelieve(expr("Value(TestValue)"))

        self.assertTrue(self.a.askBelieve("Value(TestValue)"))

    def testAddGoal(self):
        g = Goal("Value(2)")
        self.a.addGoal(g)

        self.assertEqual(len(self.a.bdiBehav.goals), 1)
        self.assertEqual(self.a.bdiBehav.goals[0].expression, "Value(2)")
示例#7
0
        print "Service 1 running"
        self.addBelieve(expr("Value(1)"))


class Serv2(Service):
    def run(self):
        print "Service 2 running"
        self.addBelieve(expr("Value(2)"))


s1 = Serv1(P=expr("Value(0)"), Q=expr("Value(1)"))
s2 = Serv2(P=expr("Value(1)"), Q=expr("Value(2)"))

p = Plan(P=expr("Value(0)"), Q=expr("Value(2)"))

p.appendService(s1)
p.appendService(s2)

agent.addPlan(p)
agent.addGoal(g)

agent.start()

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    agent.stop()

sys.exit(0)
示例#8
0
class BDITestCase(unittest.TestCase):

    def setUp(self):

        self.a = BDIAgent("bdi@" + host, "secret")
        #self.a.setDebugToScreen()
        self.a.start()

        self.s1 = spade.DF.Service(name="s1", owner=self.a.getAID(), inputs=["Value"], outputs=["Myoutput1"], P=["Var(Value,0,Int)"], Q=["Var(Myoutput1,1,Int)"])
        self.s2 = spade.DF.Service(name="s2", owner=self.a.getAID(), inputs=["Myoutput1"], outputs=["Myoutput2"], P=["Var(Myoutput1,1,Int)"], Q=["Var(Myoutput2,2,Int)"])
        self.s3 = spade.DF.Service(name="s3", owner=self.a.getAID(), inputs=["Value3"], outputs=["Myoutput3"], P=["Var(Value3,3,Int)"], Q=["Var(Myoutput3,4,Int)"])
        self.s4 = spade.DF.Service(name="s4", owner=self.a.getAID(), inputs=["Myoutput3"], outputs=["Myoutput4"], P=["Var(Myoutput3,4,Int)"], Q=["Var(Myoutput4,5,Int)"])

        time.sleep(1)

    def tearDown(self):
        s = spade.DF.Service()
        s.setOwner(self.a.getAID())
        self.a.deregisterService(s)
        self.a.stop()
        del self.a

    def testAddPlan(self):

        self.a.registerService(self.s1, s1_method)
        self.a.registerService(self.s2, s2_method)

        self.a.bdiBehav.discover()
        result = self.a.addPlan(inputs=["Value"], outputs=["Myoutput2"], P=["Var(Value,0,Int)"], Q=["Var(Myoutput2,2,Int)"], services=["s1", "s2"])

        self.assertTrue(result)

    def goalCompletedCB(self, goal):
        self.goalCompleted = True

    def testSimpleGoalCompleted(self):

        self.goalCompleted = False
        self.a.registerService(self.s1, s1_method)

        self.a.saveFact("Value", 0)

        self.a.setGoalCompletedCB(self.goalCompletedCB)

        self.a.addGoal(Goal("Var(Myoutput1,1,Int)"))

        counter = 0
        while not self.goalCompleted and counter < 10:
            time.sleep(1)
            counter += 1

        self.assertTrue(self.goalCompleted)
        self.assertTrue(self.a.askBelieve("Var(Myoutput1,1,Int)"))
        assert self.a.getFact("Myoutput1") == 1

    def testGoalCompleted_withPlan(self):

        self.goalCompleted = False
        self.a.registerService(self.s1, s1_method)
        self.a.registerService(self.s2, s2_method)

        self.a.saveFact("Value", 0)

        self.a.setGoalCompletedCB(self.goalCompletedCB)

        self.a.addGoal(Goal("Var(Myoutput2,2,Int)"))

        counter = 0
        while not self.goalCompleted and counter < 10:
            time.sleep(1)
            counter += 1

        self.assertTrue(self.goalCompleted)
        self.assertTrue(self.a.askBelieve("Var(Myoutput2,2,Int)"))
        assert self.a.getFact("Myoutput2") == 2

    def serviceCompletedCB(self, service):
        s = service.getName()
        if s == "s1":
                self.s1Completed = True
        if s == "s2":
                self.s2Completed = True
        if s == "s3":
                self.s3Completed = True
        if s == "s4":
                self.s4Completed = True

    def testMultiGoalCompleted(self):
        self.s1Completed = False
        self.s3Completed = False

        self.a.registerService(self.s1, s1_method)
        self.a.registerService(self.s3, s3_method)

        self.a.saveFact("Value", 0)
        self.a.saveFact("Value3", 3)

        self.a.setServiceCompletedCB(self.serviceCompletedCB)

        self.a.addGoal(Goal("Var(Myoutput3,4,Int)"))
        self.a.addGoal(Goal("Var(Myoutput1,1,Int)"))

        counter = 0
        while counter < 10 and (self.s1Completed is False or self.s3Completed is False):
            time.sleep(1)
            counter += 1

        self.assertTrue(self.s1Completed)
        self.assertTrue(self.s3Completed)
        self.assertTrue(self.a.askBelieve("Var(Myoutput1,1,Int)"))
        assert self.a.getFact("Myoutput1") == 1
        self.assertTrue(self.a.askBelieve("Var(Myoutput3,4,Int)"))
        assert self.a.getFact("Myoutput3") == 4

    def testMultiGoalCompleted_withMultiServices(self):
        self.s2Completed = False
        self.s4Completed = False

        self.a.registerService(self.s1, s1_method)
        self.a.registerService(self.s2, s2_method)
        self.a.registerService(self.s3, s3_method)
        self.a.registerService(self.s4, s4_method)

        self.a.saveFact("Value", 0)
        self.a.saveFact("Value3", 3)

        self.a.setServiceCompletedCB(self.serviceCompletedCB)

        self.a.addGoal(Goal("Var(Myoutput2,2,Int)"))
        self.a.addGoal(Goal("Var(Myoutput4,5,Int)"))

        counter = 0
        while counter < 10 and (self.s2Completed is False or self.s4Completed is False):
            time.sleep(1)
            counter += 1

        self.assertTrue(self.s2Completed)
        self.assertTrue(self.s4Completed)
        self.assertTrue(self.a.askBelieve("Var(Myoutput2,2,Int)"))
        assert self.a.getFact("Myoutput2") == 2
        self.assertTrue(self.a.askBelieve("Var(Myoutput4,5,Int)"))
        assert self.a.getFact("Myoutput4") == 5
示例#9
0
sys.path.append('..')

import spade
from spade.bdi import Goal
from spade.Agent import BDIAgent

def MygoalCompletedCB(goal):
    global goalCompleted
    goalCompleted = True


host = "127.0.0.1"

a = BDIAgent("clientbdi@"+host,"secret")
a.setDebugToScreen()
a.start()

goalCompleted=False

a.saveFact("Value",0)

a.setGoalCompletedCB(MygoalCompletedCB)

a.addGoal(Goal("Var(Myoutput2,2,Int)"))

import time
counter = 0
while not goalCompleted and counter<10:
    time.sleep(1)
    counter+=1
示例#10
0
class BDITestCase(unittest.TestCase):
    def setUp(self):

        self.a = BDIAgent("bdi@" + host, "secret")
        #self.a.setDebugToScreen()
        self.a.start()

        self.s1 = spade.DF.Service(name="s1",
                                   owner=self.a.getAID(),
                                   inputs=["Value"],
                                   outputs=["Myoutput1"],
                                   P=["Var(Value,0,Int)"],
                                   Q=["Var(Myoutput1,1,Int)"])
        self.s2 = spade.DF.Service(name="s2",
                                   owner=self.a.getAID(),
                                   inputs=["Myoutput1"],
                                   outputs=["Myoutput2"],
                                   P=["Var(Myoutput1,1,Int)"],
                                   Q=["Var(Myoutput2,2,Int)"])
        self.s3 = spade.DF.Service(name="s3",
                                   owner=self.a.getAID(),
                                   inputs=["Value3"],
                                   outputs=["Myoutput3"],
                                   P=["Var(Value3,3,Int)"],
                                   Q=["Var(Myoutput3,4,Int)"])
        self.s4 = spade.DF.Service(name="s4",
                                   owner=self.a.getAID(),
                                   inputs=["Myoutput3"],
                                   outputs=["Myoutput4"],
                                   P=["Var(Myoutput3,4,Int)"],
                                   Q=["Var(Myoutput4,5,Int)"])

        time.sleep(1)

    def tearDown(self):
        s = spade.DF.Service()
        s.setOwner(self.a.getAID())
        self.a.deregisterService(s)
        self.a.stop()
        del self.a

    def testAddPlan(self):

        self.a.registerService(self.s1, s1_method)
        self.a.registerService(self.s2, s2_method)

        self.a.bdiBehav.discover()
        result = self.a.addPlan(inputs=["Value"],
                                outputs=["Myoutput2"],
                                P=["Var(Value,0,Int)"],
                                Q=["Var(Myoutput2,2,Int)"],
                                services=["s1", "s2"])

        self.assertTrue(result)

    def goalCompletedCB(self, goal):
        self.goalCompleted = True

    def testSimpleGoalCompleted(self):

        self.goalCompleted = False
        self.a.registerService(self.s1, s1_method)

        self.a.saveFact("Value", 0)

        self.a.setGoalCompletedCB(self.goalCompletedCB)

        self.a.addGoal(Goal("Var(Myoutput1,1,Int)"))

        counter = 0
        while not self.goalCompleted and counter < 10:
            time.sleep(1)
            counter += 1

        self.assertTrue(self.goalCompleted)
        self.assertTrue(self.a.askBelieve("Var(Myoutput1,1,Int)"))
        assert self.a.getFact("Myoutput1") == 1

    def testGoalCompleted_withPlan(self):

        self.goalCompleted = False
        self.a.registerService(self.s1, s1_method)
        self.a.registerService(self.s2, s2_method)

        self.a.saveFact("Value", 0)

        self.a.setGoalCompletedCB(self.goalCompletedCB)

        self.a.addGoal(Goal("Var(Myoutput2,2,Int)"))

        counter = 0
        while not self.goalCompleted and counter < 10:
            time.sleep(1)
            counter += 1

        self.assertTrue(self.goalCompleted)
        self.assertTrue(self.a.askBelieve("Var(Myoutput2,2,Int)"))
        assert self.a.getFact("Myoutput2") == 2

    def serviceCompletedCB(self, service):
        s = service.getName()
        if s == "s1":
            self.s1Completed = True
        if s == "s2":
            self.s2Completed = True
        if s == "s3":
            self.s3Completed = True
        if s == "s4":
            self.s4Completed = True

    def testMultiGoalCompleted(self):
        self.s1Completed = False
        self.s3Completed = False

        self.a.registerService(self.s1, s1_method)
        self.a.registerService(self.s3, s3_method)

        self.a.saveFact("Value", 0)
        self.a.saveFact("Value3", 3)

        self.a.setServiceCompletedCB(self.serviceCompletedCB)

        self.a.addGoal(Goal("Var(Myoutput3,4,Int)"))
        self.a.addGoal(Goal("Var(Myoutput1,1,Int)"))

        counter = 0
        while counter < 10 and (self.s1Completed is False
                                or self.s3Completed is False):
            time.sleep(1)
            counter += 1

        self.assertTrue(self.s1Completed)
        self.assertTrue(self.s3Completed)
        self.assertTrue(self.a.askBelieve("Var(Myoutput1,1,Int)"))
        assert self.a.getFact("Myoutput1") == 1
        self.assertTrue(self.a.askBelieve("Var(Myoutput3,4,Int)"))
        assert self.a.getFact("Myoutput3") == 4

    def testMultiGoalCompleted_withMultiServices(self):
        self.s2Completed = False
        self.s4Completed = False

        self.a.registerService(self.s1, s1_method)
        self.a.registerService(self.s2, s2_method)
        self.a.registerService(self.s3, s3_method)
        self.a.registerService(self.s4, s4_method)

        self.a.saveFact("Value", 0)
        self.a.saveFact("Value3", 3)

        self.a.setServiceCompletedCB(self.serviceCompletedCB)

        self.a.addGoal(Goal("Var(Myoutput2,2,Int)"))
        self.a.addGoal(Goal("Var(Myoutput4,5,Int)"))

        counter = 0
        while counter < 10 and (self.s2Completed is False
                                or self.s4Completed is False):
            time.sleep(1)
            counter += 1

        self.assertTrue(self.s2Completed)
        self.assertTrue(self.s4Completed)
        self.assertTrue(self.a.askBelieve("Var(Myoutput2,2,Int)"))
        assert self.a.getFact("Myoutput2") == 2
        self.assertTrue(self.a.askBelieve("Var(Myoutput4,5,Int)"))
        assert self.a.getFact("Myoutput4") == 5
示例#11
0
import spade
from spade.bdi import Goal
from spade.Agent import BDIAgent


def MygoalCompletedCB(goal):
    global goalCompleted
    goalCompleted = True


host = "127.0.0.1"

a = BDIAgent("clientbdi@" + host, "secret")
a.setDebugToScreen()
a.start()

goalCompleted = False

a.saveFact("Value", 0)

a.setGoalCompletedCB(MygoalCompletedCB)

a.addGoal(Goal("Var(Myoutput2,2,Int)"))

import time
counter = 0
while not goalCompleted and counter < 10:
    time.sleep(1)
    counter += 1