Ejemplo n.º 1
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
Ejemplo n.º 2
0
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

a.stop()
sys.exit()