def testComposePlanBestQoS(self): tbcbp = TBCBP() s1 = Service(P=["P(P1)", "P(P2)"], Q=["Q(Q1)", "Q(Q2)"], inputs=["i1"], outputs=["o1"], name="s1") s2 = Service(P=["P(P3)", "P(P4)"], Q=["Q(Q3)"], inputs=["o1"], outputs=["o2"], name="s2") s3 = Service(P=["P(P3)", "P(P4)"], Q=["Q(Q3)"], inputs=["o1"], outputs=["o2"], name="s3") tbcbp.registerService(s1, time=1, QoS=1) tbcbp.registerService(s2, time=1, QoS=1) tbcbp.registerService(s3, time=1, QoS=100) kb0 = KB() kb0.tell("P(P1)") kb0.tell("P(P2)") kb0.set("i1", 1024) g = Goal("Q(Q3)") plan = tbcbp.composePlan(Goal=g, kb=kb0, use_rl=False) for s in plan.getServices(): assert s in ["s1", "s3"] assert s != "s2"
def testComposePlan2Cases(self): tbcbp = TBCBP() s1 = Service(P=["P(P1)", "P(P2)"], Q=["Q(Q1)", "Q(Q2)"], inputs=["i1"], outputs=["o1"], name="s1") s2 = Service(P=["P(P3)", "P(P4)"], Q=["Q(Q3)"], inputs=["o1"], outputs=["o2"], name="s2") tbcbp.registerService(s1) tbcbp.registerService(s2) kb0 = KB() kb0.tell("P(P1)") kb0.tell("P(P2)") kb0.set("i1", 1024) g = Goal("Q(Q3)") plan = tbcbp.composePlan(Goal=g, kb=kb0) assert plan is not None assert plan.getQ() == ["Q(Q3)"] assert plan.getP() == ["P(P1)", "P(P2)"] assert len(plan) == 2
def testPlanMatchesInKB(self): tbcbp = TBCBP() case1 = Case(P=["P(P1)", "P(P2)"], Q=["Q(Q1)", "Q(Q2)"], inputs=["i1"], outputs=["o1"], services=["s1"]) kb0 = KB() kb0.tell("P(P1)") kb0.tell("P(P2)") kb0.set("i1", 1024) self.assertTrue(tbcbp.planMatchesInKB(case1, kb0)) case2 = Case(P=["P(P3)", "P(P2)"], Q=["q1", "q2"], inputs=["i1"], outputs=["o1"], services=["s1"]) self.assertFalse(tbcbp.planMatchesInKB(case2, kb0)) case3 = Case(P=["P(P1)", "P(P2)"], Q=["q1", "q2"], inputs=["i2"], outputs=["o1"], services=["s1"]) self.assertFalse(tbcbp.planMatchesInKB(case3, kb0))
def testComposePlan1CaseNoP(self): tbcbp = TBCBP() s1 = Service(P=["P(P1)", "P(P2)"], Q=["Q(Q1)"], inputs=["i1"], outputs=["o1"], name="s1") tbcbp.registerService(s1) kb0 = KB() kb0.tell("P(P1)") # Missing kb0.tell("P(P2)") kb0.set("i1", 1024) g = Goal("Q(Q1)") plan = tbcbp.composePlan(Goal=g, kb=kb0) assert plan is None
def testComposePlan1CaseNoP(self): tbcbp = TBCBP() s1 = Service(P=["P(P1)", "P(P2)"], Q=["Q(Q1)"], inputs=["i1"], outputs=["o1"], name="s1") tbcbp.registerService(s1) kb0 = KB() kb0.tell("P(P1)") #Missing kb0.tell("P(P2)") kb0.set("i1", 1024) g = Goal("Q(Q1)") plan = tbcbp.composePlan(Goal=g, kb=kb0) assert plan is None
class factsTestCase(unittest.TestCase): def setUp(self): self.kb = KB() def tearDown(self): pass def testInteger(self): self.kb.set("varname1", 1234) var = self.kb.get("varname1") assert var == 1234 assert isinstance(var, types.IntType) def testString(self): self.kb.set("varname2", "TestString") var = self.kb.get("varname2") assert var == "teststring" assert isinstance(var, types.StringType) def testFloat(self): self.kb.set("varname3", 1234.5678) var = self.kb.get("varname3") assert var == 1234.5678 assert isinstance(var, types.FloatType) def testList(self): self.kb.set("varname4", [1, 2, 3, 4]) var = self.kb.get("varname4") assert var == [1, 2, 3, 4] assert isinstance(var, types.ListType) def testListWithMultipleTypes(self): self.kb.set("varname5", [1, 2, 3.4, "5", [6, 7]]) var = self.kb.get("varname5") assert var == [1, 2, 3.4, "5", [6, 7]] assert isinstance(var, types.ListType) def testDict(self): self.kb.set("varname6", {'a': 123, 'b': 456, 789: "c"}) var = self.kb.get("varname6") assert var == {'a': 123, 'b': 456, 789: "c"} assert isinstance(var, types.DictType) def testDictWithComplexTypes(self): self.kb.set("varname7", {'a': [123.25], 'b': [4, 5, 6], 789: {'a': 1, 'b': 2}}) var = self.kb.get("varname7") assert var == {'a': [123.25], 'b': [4, 5, 6], 789: {'a': 1, 'b': 2}} assert isinstance(var, types.DictType) def testKeyNotString(self): self.assertRaises(KBNameNotString, self.kb.set, 123, "newvalue") def testValueNotKnown(self): class A: pass self.assertRaises(KBValueNotKnown, self.kb.set, "varname8", A())
class factsTestCase(unittest.TestCase): def setUp(self): self.kb = KB() def tearDown(self): pass def testInteger(self): self.kb.set("varname1", 1234) var = self.kb.get("varname1") assert var == 1234 assert isinstance(var, types.IntType) def testString(self): self.kb.set("varname2", "TestString") var = self.kb.get("varname2") assert var == "teststring" assert isinstance(var, types.StringType) def testFloat(self): self.kb.set("varname3", 1234.5678) var = self.kb.get("varname3") assert var == 1234.5678 assert isinstance(var, types.FloatType) def testList(self): self.kb.set("varname4", [1, 2, 3, 4]) var = self.kb.get("varname4") assert var == [1, 2, 3, 4] assert isinstance(var, types.ListType) def testListWithMultipleTypes(self): self.kb.set("varname5", [1, 2, 3.4, "5", [6, 7]]) var = self.kb.get("varname5") assert var == [1, 2, 3.4, "5", [6, 7]] assert isinstance(var, types.ListType) def testDict(self): self.kb.set("varname6", {'a': 123, 'b': 456, 789: "c"}) var = self.kb.get("varname6") assert var == {'a': 123, 'b': 456, 789: "c"} assert isinstance(var, types.DictType) def testDictWithComplexTypes(self): self.kb.set("varname7", { 'a': [123.25], 'b': [4, 5, 6], 789: { 'a': 1, 'b': 2 } }) var = self.kb.get("varname7") assert var == {'a': [123.25], 'b': [4, 5, 6], 789: {'a': 1, 'b': 2}} assert isinstance(var, types.DictType) def testKeyNotString(self): self.assertRaises(KBNameNotString, self.kb.set, 123, "newvalue") def testValueNotKnown(self): class A: pass self.assertRaises(KBValueNotKnown, self.kb.set, "varname8", A())