def test_find_reln(self): o1 = PSObjectFromStructure(4) o2 = PSObjectFromStructure(8) self.assertTrue(o1.IsKnownAsInstanceOf(CategoryInteger())) self.assertIn(CategoryInteger(), o1.CategoriesSharedWith(o2)) r = o1.GetRelationTo(o2) r.FindCategories(end_category=CategoryInteger()) self.assertTrue(r.IsKnownAsInstanceOf(CategoryInteger.DeltaReln())) self.assertEqual( 4, r.DescribeAs( CategoryInteger.DeltaReln()).Attributes()['delta'].magnitude) self.assertTrue(r.IsKnownAsInstanceOf(CategoryInteger.RatioReln())) self.assertEqual( 2, r.DescribeAs( CategoryInteger.RatioReln()).Attributes()['ratio'].magnitude) o3 = PSObjectFromStructure(3) o4 = PSObjectFromStructure(12) r2 = o3.GetRelationTo(o4) r2.FindCategoriesUsingEndCategories() self.assertEqual( 9, r2.DescribeAs( CategoryInteger.DeltaReln()).Attributes()['delta'].magnitude) self.assertEqual( 4, r2.DescribeAs( CategoryInteger.RatioReln()).Attributes()['ratio'].magnitude)
def Tester(test, category): for p in positive: obj = PSObjectFromStructure(p) test.assertTrue( obj.DescribeAs(category), "Describing %s as %s" % (obj.Structure(), category.BriefLabel())) for n in negative: obj = PSObjectFromStructure(n) test.assertFalse( obj.DescribeAs(category), "Describing %s as %s" % (obj.Structure(), category.BriefLabel()))
def test_reln_cat(self): class SameStructureReln(PSCategory): _Checks = ( '_INSTANCE.first.Structure() == _INSTANCE.second.Structure()', ) o1 = PSObjectFromStructure(4) o2 = PSObjectFromStructure(8) o3 = PSObjectFromStructure(4) r_12 = o1.GetRelationTo(o2) r_13 = o1.GetRelationTo(o3) self.assertTrue(r_13.DescribeAs(SameStructureReln())) self.assertFalse(r_12.DescribeAs(SameStructureReln()))
def test_extra_attribs(self): c1 = CategoryEvenInteger() o1 = PSObjectFromStructure(4) l = o1.DescribeAs(c1) self.assertTrue(l) self.assertNotIn('half', l.Attributes(), "No attribute 'half' yet") c1.TurnOnAttribute('half') o2 = PSObjectFromStructure(6) l2 = o2.DescribeAs(c1) self.assertTrue(l2) self.assertIn('half', l2.Attributes(), "Attribute 'half' now present") self.assertEqual(3, l2.GetAttributeOrNone(attribute='half').magnitude)
def test_creation(self): """Test creation given attributes.""" c1 = C.RepeatedIntegerCategory() assert_creation(self, c1, (6, 6, 6, 6), magnitude=PSObjectFromStructure(6), length=PSObjectFromStructure(4)) assert_creation_failure(self, c1, logic.InsufficientAttributesException, length=PSObjectFromStructure(8))
def test_instancehood(self): c1 = C.RepeatedIntegerCategory() tester = CategoryTester(positive=((2, 2), (2, ), ()), negative=(9, 2, (2, 3))) tester(self, c1) CategoryLogicTester(test=self, item=PSObjectFromStructure((7, )), cat=c1, tester=StructureTester(magnitude=7, length=1)) CategoryLogicTester(test=self, item=PSObjectFromStructure((8, 8, 8)), cat=c1, tester=StructureTester(magnitude=8, length=3))
def test_group_insertion_deeper(self): """Make sure deeper features of the logic get copied.""" arena = PSArena(magnitudes=(7, 8)) gp = PSObjectFromStructure((7, 8)) logic = gp.DescribeAs(BasicSuccessorCategory()) CategoryEvenInteger().TurnOnAttribute('half') gp.items[1].DescribeAs(CategoryEvenInteger()).Attributes()[ 'half'].DescribeAs(CategoryEvenInteger()) inner_logic = logic.Attributes()['end'].DescribeAs( CategoryEvenInteger()) self.assertEqual(4, inner_logic.Attributes()['half'].magnitude) inner_logic.Attributes()['half'].DescribeAs(CategoryEvenInteger()) CategoryEvenInteger().TurnOffAttribute('half') gp.SetSpanStart(0) merged = arena.MergeObject(gp) self.assertEqual((7, 8), merged.Structure()) self.assertEqual(arena.element[1], merged.items[1]) self.assertTrue(arena.element[1].IsKnownAsInstanceOf( CategoryEvenInteger())) logic = arena.element[1].categories[CategoryEvenInteger()] self.assertTrue(logic.Attributes()[ 'half'].IsKnownAsInstanceOf(CategoryEvenInteger())) self.assertTrue(merged.DescribeAs(BasicSuccessorCategory()).Attributes()[ 'end'].IsKnownAsInstanceOf(CategoryEvenInteger()))
def test_cat_with_arg(self): class MyCat(PSCategory): _Context = dict(PSObjectFromStructure=PSObjectFromStructure, len=len) _Attributes = set(('length', 'each')) _Rules = ("length: PSObjectFromStructure(len(_INSTANCE.items))", "each: _INSTANCE.items[0]") def __init__(self, *, index_with_one): self.index_with_one = index_with_one self._Constructors = { ('length', 'each'): self.ConstructFromLengthAndEach } PSCategory.__init__(self) def ConstructFromLengthAndEach(self, length, each): args = [] for x in range(length.magnitude): if x == self.index_with_one: args.append(1) else: args.append(each.magnitude) return PSObjectFromStructure(tuple(args)) CategoryLogicTester(test=self, item=PSObjectFromStructure((7, 7, 7, 1, 7)), cat=MyCat(index_with_one=3), tester=StructureTester(each=7, length=5))
def ConstructFromLengthAndEach(self, length, each): args = [] for x in range(length.magnitude): if x == self.index_with_one: args.append(1) else: args.append(each.magnitude) return PSObjectFromStructure(tuple(args))
def test_sanity(self): o1 = PSObjectFromStructure(4) o2 = PSObjectFromStructure(8) # This creates a relationship with no categories. We can call DescribeAs() with various relation # types. r = o1.GetRelationTo(o2) self.assertIsInstance(r, PSRelation) self.assertEqual(o1, r.first) self.assertEqual(o2, r.second) self.assertEqual(r, o1.GetRelationTo(o2), 'You get back the same relation') self.assertNotEqual(r, o2.GetRelationTo(o1), 'Not the same relation in the reverse direction') self.assertIsInstance(o1.GetRelationTo(o1), PSRelation, 'Self relations are fine')
def test_instancehood(self): c1 = C.BasicPredecessorCategory() tester = CategoryTester(positive=((3, 2), (2, ), ()), negative=(9, 2, (2, 2), (6, 5, 4, 2))) tester(self, c1) CategoryLogicTester(test=self, item=PSObjectFromStructure((7, )), cat=c1, tester=StructureTester(start=7, end=7, length=1)) CategoryLogicTester(test=self, item=PSObjectFromStructure((8, 7, 6)), cat=c1, tester=StructureTester(start=8, end=6, length=3)) CategoryLogicTester(test=self, item=PSObjectFromStructure(()), cat=c1, tester=StructureTester(length=0))
def test_argumentless_cat(self): class MyCat(PSCategory): _Context = dict(PSObjectFromStructure=PSObjectFromStructure) _Attributes = set(('end', 'foo', 'start', 'length')) _Rules = ( "end: PSObjectFromStructure(start.magnitude + length.magnitude - 1)", "foo: PSObjectFromStructure((start.magnitude, end.magnitude))", "start: _INSTANCE.items[0]", "end: _INSTANCE.items[1].items[1]") _Checks = ("foo.items[0].magnitude == start.magnitude", "foo.items[1].magnitude == end.magnitude") def __init__(self): self._Constructors = { ('foo', 'start'): self.ConstructFromFooAndStart } PSCategory.__init__(self) def ConstructFromFooAndStart(self, foo, start): return PSObjectFromStructure( (start.magnitude, foo.Structure())) CategoryLogicTester(test=self, item=PSObjectFromStructure((7, (7, 11))), cat=MyCat(), tester=StructureTester(start=7, end=11, foo=(7, 11))) self.assertEqual(set(['end', 'start', 'length', 'foo']), MyCat().Attributes()) self.assertEqual((7, (7, 11)), MyCat().CreateInstance( start=PSObjectFromStructure(7), length=PSObjectFromStructure(5)).Structure()) self.assertRaises(logic.InsufficientAttributesException, MyCat().CreateInstance, start=PSObjectFromStructure(7)) self.assertEqual( (7, (7, 13)), MyCat().CreateInstance(start=PSObjectFromStructure(7), end=PSObjectFromStructure(13)).Structure()) self.assertRaises(logic.InconsistentAttributesException, MyCat().CreateInstance, start=PSObjectFromStructure(7), end=PSObjectFromStructure(9), foo=PSObjectFromStructure((7, 10)))
def test_group_insertion(self): arena = PSArena(magnitudes=range(10)) # Create a group with structure ((5, 6), (7, 8)), and add it to arena. gp_56_78 = PSObjectFromStructure(((5, 6), (7, 8))) gp_56_78.items[0].items[0].DescribeAs(CategoryPrime()) self.assertFalse(arena.element[5].IsKnownAsInstanceOf(CategoryPrime())) # Cannot insert this without spans added... we won't know where it # should go. self.assertRaises(CannotInsertGroupWithoutSpans, arena.MergeObject, gp_56_78) self.assertFalse(arena.GetObjectsWithSpan((5, 8))) gp_56_78.SetSpanStart(5) inserted_gp = arena.MergeObject(gp_56_78) self.assertEqual((5, 8), inserted_gp.Span()) self.assertEqual(arena.element[5], inserted_gp.items[0].items[0]) self.assertEqual(arena.element[8], inserted_gp.items[1].items[1]) self.assertEqual(inserted_gp, arena.GetObjectsWithSpan( (5, 8))[((5, 6), (7, 8))]) self.assertEqual(inserted_gp.items[ 0], arena.GetObjectsWithSpan((5, 6))[(5, 6)]) self.assertEqual(inserted_gp.items[ 1], arena.GetObjectsWithSpan((7, 8))[(7, 8)]) self.assertTrue(arena.element[5].IsKnownAsInstanceOf(CategoryPrime())) gp_10_11 = PSObjectFromStructure((10, 11)) gp_10_11.SetSpanStart(2) self.assertRaises(UnmergableObjectException, arena.MergeObject, gp_10_11) gp_11_12 = PSObjectFromStructure((11, 12)) gp_11_12.SetSpanStart(11) self.assertRaises(ElementWayBeyondKnownSoughtException, arena.MergeObject, gp_11_12) gp_9_10_11 = PSObjectFromStructure((9, (10, 11))) gp_9_10_11.SetSpanStart(9) arena.MergeObject(gp_9_10_11) self.assertEqual(12, arena.Size())
def test_affordance(self): """What codelets does this suggest? It can own the relevant codelet families... This test is currently very speculative, as I try to firm up the interface a bit.""" c1 = C.RepeatedIntegerCategory() o1 = PSObjectFromStructure((7, 7)) self.assertTrue(o1.DescribeAs(c1)) affordances = c1.GetAffordanceForInstance(o1) self.assertTrue(affordances) # This is currently fake...
def test_reln_cat_with_arg(self): class DeltaReln(PSCategory): _Attributes = ('delta', ) _RequiredAttributes = ('delta', ) _Rules = ( 'delta: PSElement(magnitude=(_INSTANCE.second.magnitude - ' '_INSTANCE.first.magnitude))', ) _Checks = ('delta.magnitude == _INSTANCE.second.magnitude - ' '_INSTANCE.first.magnitude', ) _Context = dict(PSElement=PSElement) o1 = PSObjectFromStructure(4) o2 = PSObjectFromStructure(8) o3 = PSObjectFromStructure(4) r_12 = o1.GetRelationTo(o2) r_13 = o1.GetRelationTo(o3) l_12 = r_12.DescribeAs(DeltaReln()) self.assertTrue(l_12) l_13 = r_13.DescribeAs(DeltaReln()) self.assertTrue(l_13) self.assertEqual(4, l_12.Attributes()['delta'].magnitude) self.assertEqual(0, l_13.Attributes()['delta'].magnitude)
def test_categories_on_logic(self): c1 = C.RepeatedIntegerCategory() o1 = PSObjectFromStructure((7, 7)) self.assertTrue(o1.DescribeAs(c1)) self.assertTrue(o1.IsKnownAsInstanceOf(c1)) logic = o1.DescribeAs(c1) logic.Attributes()['magnitude'].DescribeAs(CategoryPrime()) self.assertTrue(logic.Attributes()['magnitude'].IsKnownAsInstanceOf( CategoryPrime())) # Get it again, see that the inner categorization has "stuck". logic = o1.DescribeAs(c1) self.assertTrue(logic.Attributes()['magnitude'].IsKnownAsInstanceOf( CategoryPrime()))
def test_arena_merge(self): class SameStructureReln(PSCategory): _Checks = ( '_INSTANCE.first.Structure() == _INSTANCE.second.Structure()', ) gp = PSObjectFromStructure((5, 5)) r = gp.items[0].GetRelationTo(gp.items[1]) self.assertTrue(r.DescribeAs(SameStructureReln())) gp.SetSpanStart(0) a = PSArena(magnitudes=(5, 5, 5)) r2 = gp.items[1].GetRelationTo(a.element[2]) self.assertTrue(r2.DescribeAs(SameStructureReln())) self.assertFalse(a.element[0].GetRelationTo( a.element[1]).IsKnownAsInstanceOf(SameStructureReln())) self.assertFalse(a.element[1].GetRelationTo( a.element[2]).IsKnownAsInstanceOf(SameStructureReln())) a.MergeObject(gp) self.assertTrue(a.element[0].GetRelationTo( a.element[1]).IsKnownAsInstanceOf(SameStructureReln())) self.assertTrue(a.element[1].GetRelationTo( a.element[2]).IsKnownAsInstanceOf(SameStructureReln()))
def _InitializePySeqseeLTM(ltm): for magnitude in range(10): ltm.GetNode(content=PSObjectFromStructure(magnitude))
def test_instancehood(self): c1 = C.CompoundCategory( base_category=C.BasicSuccessorCategory(), attribute_categories=(('end', C.BasicSuccessorCategory()), ('length', C.BasicSuccessorCategory()), ('start', C.RepeatedIntegerCategory()))) c2 = C.CompoundCategory( base_category=C.BasicSuccessorCategory(), attribute_categories=(('end', C.BasicSuccessorCategory()), ('length', C.BasicSuccessorCategory()), ('start', C.RepeatedIntegerCategory()))) self.assertEqual(c1, c2, "Cached properly") # In creation, attribute_categories must be sorted and contain categories. self.assertRaises(C.BadCategorySpec, C.CompoundCategory, base_category=C.BasicSuccessorCategory(), attribute_categories=(('length', C.BasicSuccessorCategory()), ('end', C.BasicSuccessorCategory()), ('start', C.RepeatedIntegerCategory()))) c3 = C.CompoundCategory( base_category=C.BasicSuccessorCategory(), attribute_categories=(('end', C.BasicSuccessorCategory()), ('length', C.RepeatedIntegerCategory()), ('start', C.BasicSuccessorCategory()))) self.assertNotEqual(c1, c3, "Different categories") tester = CategoryTester(positive=(((2, 3), (2, 3, 4), (2, 3, 4, 5)), ((2, 3), ), ((2, ), ), ()), negative=(9, 2, (2, 3), ((2, 3), (3, 4)), ((2, 3), (2, 3, 4), (2, 3, 4, 5, 6)))) tester(self, c1) tester = CategoryTester(positive=(((2, 3), (3, 4), (4, 5)), ((2, 3), ), ((2, ), ), ()), negative=(9, 2, (2, 3), ((2, 3), (2, 3, 4)), ((2, 3), (2, 3, 4), (2, 3, 4, 5, 6)))) tester(self, c3) CategoryLogicTester(test=self, item=PSObjectFromStructure(((7, ), )), cat=c1, tester=StructureTester(start=(7, ), end=(7, ), length=1)) CategoryLogicTester(test=self, item=PSObjectFromStructure( ((7, 8), (7, 8, 9), (7, 8, 9, 10))), cat=c1, tester=StructureTester(start=(7, 8), end=(7, 8, 9, 10), length=3)) CategoryLogicTester(test=self, item=PSObjectFromStructure(()), cat=c1, tester=StructureTester(length=0))
def CreateFromMagAndLength(self, _mag, _length): return PSObjectFromStructure((_mag, ) * _length)
def ConstructFromFooAndStart(self, foo, start): return PSObjectFromStructure( (start.magnitude, foo.Structure()))
def CreateFromStartAndEnd(self, _start, _end): return PSObjectFromStructure(tuple(range(_start, _end - 1, -1)))
def test_creation(self): """Test creation given attributes.""" c1 = C.BasicPredecessorCategory() assert_creation(self, c1, (6, 5, 4, 3), start=PSObjectFromStructure(6), length=PSObjectFromStructure(4)) assert_creation(self, c1, (7, 6, 5, 4, 3), start=PSObjectFromStructure(7), end=PSObjectFromStructure(3)) assert_creation(self, c1, (7, 6, 5, 4, 3, 2), end=PSObjectFromStructure(2), length=PSObjectFromStructure(6)) assert_creation(self, c1, (8, 7, 6, 5, 4, 3, 2), end=PSObjectFromStructure(2), length=PSObjectFromStructure(7), start=PSObjectFromStructure(8)) assert_creation_failure(self, c1, logic.InconsistentAttributesException, end=PSObjectFromStructure(8), length=PSObjectFromStructure(7), start=PSObjectFromStructure(4)) assert_creation_failure(self, c1, logic.InsufficientAttributesException, end=PSObjectFromStructure(8))
def CopyByStructure(self): from farg.apps.pyseqsee.utils import PSObjectFromStructure return PSObjectFromStructure(self.Structure())
def CreateFromMagAndLength(self, _mag, _length): return PSObjectFromStructure( (_mag, ) * _length, log_msg='RepeatedIntegerCategory constructor')