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_basic(self): arena = PSArena(magnitudes=range(10)) self.assertIsInstance(arena.element[0], PSElement) self.assertEqual(10, arena.Size()) self.assertEqual((3, 3), arena.element[3].Span()) arena.Append(magnitudes=(15, 160)) self.assertEqual(12, arena.Size()) self.assertEqual((11, 11), arena.element[11].Span()) self.assertEqual(160, arena.element[11].magnitude) gp = PSGroup(items=arena.element[2:5]) self.assertTrue(gp.InferSpans()) self.assertEqual((2, 4), gp.Span())
class PSWorkspace(object): def __init__(self): self.arena = PSArena(magnitudes=()) def InsertElements(self, magnitudes, log_msg=""): self.arena.Append(magnitudes=magnitudes, log_msg=log_msg) def KnownElementCount(self): return self.arena.KnownElementCount() def GetFirstElement(self): return self.arena.GetFirstElement() def SelectRandomElement(self): return self.arena.SelectRandomElement() def GetObjectToRight(self, item): return self.arena.GetObjectToRight(item)
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_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 test_hypothetical_insert(self): arena = PSArena(magnitudes=range(10), start=5) gp = PSGroup(items=arena.element[2:5]) self.assertTrue(gp.InferSpans()) self.assertEqual((7, 9), gp.Span()) gp2 = gp.HypotheticallyAddComponentBefore(arena.element[1]) self.assertEqual((7, 9), gp.Span()) self.assertEqual((2, 3, 4), gp.Structure()) self.assertEqual((6, 9), gp2.Span()) self.assertEqual((1, 2, 3, 4), gp2.Structure()) gp3 = gp2.HypotheticallyAddComponentAfter(arena.element[5]) self.assertEqual((6, 10), gp3.Span()) self.assertEqual((1, 2, 3, 4, 5), gp3.Structure())
def test_with_offset(self): arena = PSArena(magnitudes=range(10), start=5) self.assertIsInstance(arena.element[0], PSElement) self.assertEqual(10, arena.Size()) self.assertEqual((8, 8), arena.element[3].Span()) arena.Append(magnitudes=(15, 160)) self.assertEqual(12, arena.Size()) self.assertEqual((16, 16), arena.element[11].Span()) self.assertEqual(160, arena.element[11].magnitude) gp = PSGroup(items=arena.element[2:5]) self.assertTrue(gp.InferSpans()) self.assertEqual((7, 9), gp.Span()) self.assertTrue(arena.CheckTerms(start=7, magnitudes=(2, 3, 4))) self.assertTrue(arena.CheckTerms(start=16, magnitudes=(160, ))) self.assertFalse(arena.CheckTerms(start=8, magnitudes=(8, 9))) self.assertFalse(arena.CheckTerms(start=16, magnitudes=(1600, 1601))) self.assertRaises(ElementBeyondKnownSoughtException, arena.CheckTerms, start=16, magnitudes=(160, 161))
def __init__(self): self.arena = PSArena(magnitudes=())