def test_insert_group(self): ws = Workspace() ws.InsertElements((5, 6)) gp = SAnchored.Create(ws.elements[:]) self.assertEqual((0, 1), gp.Span()) self.assertEqual((5, 6), gp.Structure()) ws.InsertGroup(gp) self.assertEqual(1, len(ws.groups))
def test_sanity(self): ws = Workspace() self.assertEqual(0, ws.num_elements) ws.InsertElement(SObject.Create([5])) self.assertEqual(1, ws.num_elements) self.assertEqual(5, ws.elements[0].object.magnitude) ws.InsertElements((6, 7)) self.assertEqual(6, ws.elements[1].object.magnitude) self.assertEqual((1, 1), ws.elements[1].Span()) self.assertEqual(3, ws.num_elements)
def test_conflicting_groups_more_complex(self): ws = Workspace() ws.InsertElements(range(0, 10)) helper_create_and_insert_groups(ws, ((1, 2, 3), (4, 5, 6), (7, 8))) self.assertEqual(4, len(ws.groups)) # An overlapping group not subsumed is fine: self.assertFalse( tuple(ws.GetConflictingGroups( helper_create_group_given_spans_of_items(ws, 0, (1, 3), 4)))) self.assertFalse( tuple(ws.GetConflictingGroups(helper_create_group_given_spans_of_items(ws, 0, (1, 3))))) # Subsumed that is not an element is problematic. self.assertTrue( tuple(ws.GetConflictingGroups(helper_create_group_given_spans_of_items( ws, (1, 3), (4, 6))))) # But if subsumed *is* an element, that is okay. # Here, the group being tested is an *existing* group. self.assertFalse( tuple(ws.GetConflictingGroups(helper_create_group_given_spans_of_items(ws, (1, 3))))) # If a new group is crafted out of existing parts, such that is aligns # exactly with an existing group, it is not in conflict. g1 = helper_create_group_given_spans_of_items(ws, 1, 2, 3) self.assertFalse(tuple(ws.GetConflictingGroups(g1)))
def test_supergroups(self): ws = Workspace() ws.InsertElements(range(0, 10)) helper_create_and_insert_groups(ws, ((1, 2, 3), (4, 5, 6), (7, 8))) self.assertEqual(1, len(tuple(ws.GetSuperGroups(ws.elements[1])))) g1 = helper_create_group_given_spans_of_items(ws, (1, 3)) self.assertEqual(1, len(tuple(ws.GetSuperGroups(g1)))) helper_create_and_insert_groups(ws, (3, 4)) g2 = helper_create_group_given_spans_of_items(ws, (3, 4)) self.assertEqual(0, len(tuple(ws.GetSuperGroups(g2)))) self.assertEqual(2, len(tuple(ws.GetSuperGroups(ws.elements[3]))))
def test_plonk_into_place(self): ws = Workspace() ws.InsertElements((7, 8, 7, 8, 9)) # Plonk an element... returns existing element. elt = SAnchored(SElement(8), (), 3, 3) self.assertEqual(ws.elements[3], ws._PlonkIntoPlace(elt)) # Plonk a group, one item of which is an existing element, one novel. The plonked group # has the existing element as a subgroup. elt0 = SAnchored(SElement(7), (), 0, 0) elt1 = SAnchored(SElement(8), (), 1, 1) elt2 = SAnchored(SElement(7), (), 2, 2) elt3 = SAnchored(SElement(8), (), 3, 3) elt4 = SAnchored(SElement(9), (), 4, 4) numeric_successor = NumericMapping(name="succ", category=Number()) numeric_sameness = NumericMapping(name="same", category=Number()) successor_mapping_based_cat = MappingBasedCategory(mapping=numeric_successor) next_ascending = StructuralMapping( category=MappingBasedCategory(mapping=numeric_successor), bindings_mapping=frozenset((('length', numeric_successor), ('start', numeric_sameness)))) gp1 = SAnchored.Create((elt0, elt1), underlying_mapping_set={numeric_successor}) gp2 = SAnchored.Create((elt2, elt3, elt4), underlying_mapping_set={numeric_successor}) gp3 = SAnchored.Create((gp1, gp2), underlying_mapping_set={next_ascending}) self.assertTrue(gp1.object.IsKnownAsInstanceOf(successor_mapping_based_cat)) plonked = ws._PlonkIntoPlace(gp3) self.assertEqual(((7, 8), (7, 8, 9)), plonked.Structure()) existing_groups = list(ws.GetGroupsWithSpan(Exactly(0), Exactly(1))) self.assertEqual(existing_groups[0], plonked.items[0]) self.assertTrue(plonked.items[0].object.IsKnownAsInstanceOf(successor_mapping_based_cat)) self.assertTrue(numeric_successor in plonked.items[0].object.underlying_mapping_set) self.assertTrue(next_ascending in plonked.object.underlying_mapping_set)
def test_conflicting_groups_simple(self): ws = Workspace() ws.InsertElements(range(0, 10)) helper_create_and_insert_groups(ws, (1, 2, 3), (4, 5, 6)) self.assertEqual(2, len(ws.groups)) # An overlapping group which overlaps by more than one subgroup is a conflict: self.assertTrue( tuple(ws.GetConflictingGroups(helper_create_group_given_spans_of_items(ws, 2, 3, 4)))) # Subsumed that is not an element is problematic. self.assertTrue( tuple(ws.GetConflictingGroups(helper_create_group_given_spans_of_items(ws, 2, 3)))) # But if subsumed *is* an element, that is okay. self.assertFalse( tuple(ws.GetConflictingGroups(helper_create_group_given_spans_of_items(ws, 2))))
def test_plonk_into_place(self): ws = Workspace() ws.InsertElements((7, 8, 7, 8, 9)) # Plonk an element... returns existing element. elt = SAnchored(SElement(8), (), 3, 3) self.assertEqual(ws.elements[3], ws._PlonkIntoPlace(elt)) # Plonk a group, one item of which is an existing element, one novel. The plonked group # has the existing element as a subgroup. elt0 = SAnchored(SElement(7), (), 0, 0) elt1 = SAnchored(SElement(8), (), 1, 1) elt2 = SAnchored(SElement(7), (), 2, 2) elt3 = SAnchored(SElement(8), (), 3, 3) elt4 = SAnchored(SElement(9), (), 4, 4) numeric_successor = NumericMapping(name="succ", category=Number()) numeric_sameness = NumericMapping(name="same", category=Number()) successor_mapping_based_cat = MappingBasedCategory( mapping=numeric_successor) next_ascending = StructuralMapping( category=MappingBasedCategory(mapping=numeric_successor), bindings_mapping=frozenset((('length', numeric_successor), ('start', numeric_sameness)))) gp1 = SAnchored.Create( (elt0, elt1), underlying_mapping_set={numeric_successor}) gp2 = SAnchored.Create( (elt2, elt3, elt4), underlying_mapping_set={numeric_successor}) gp3 = SAnchored.Create( (gp1, gp2), underlying_mapping_set={next_ascending}) self.assertTrue(gp1.object.IsKnownAsInstanceOf( successor_mapping_based_cat)) plonked = ws._PlonkIntoPlace(gp3) self.assertEqual(((7, 8), (7, 8, 9)), plonked.Structure()) existing_groups = list(ws.GetGroupsWithSpan(Exactly(0), Exactly(1))) self.assertEqual(existing_groups[0], plonked.items[0]) self.assertTrue(plonked.items[0].object.IsKnownAsInstanceOf( successor_mapping_based_cat)) self.assertTrue(numeric_successor in plonked.items[ 0].object.underlying_mapping_set) self.assertTrue( next_ascending in plonked.object.underlying_mapping_set)
def SetupTestingWS(items): workspace = Workspace() workspace.InsertElements(items) return workspace
def __init__(self, items=None): Controller.__init__(self, ui=BatchUI(controller_class=Controller)) workspace = self.workspace = Workspace() self.ltm = LTMGraph(empty_ok_for_test=True) if items: workspace.InsertElements(items)
def test_replacement(self): new_group = SAnchored.Create((SAnchored(SElement(7), (), 7, 7), SAnchored(SElement(8), (), 8, 8), SAnchored(SElement(9), (), 9, 9))) ws = Workspace() ws.InsertElements(range(0, 10)) helper_create_and_insert_groups(ws, ((1, 2, 3), (4, 5, 6), (7, 8))) existing_group = list(ws.GetGroupsWithSpan(Exactly(7), Exactly(8)))[0] # Cannot replace a group which is not the topmost. self.assertRaises(CannotReplaceSubgroupException, ws.Replace, existing_group, new_group) ws = Workspace() ws.InsertElements(range(0, 10)) helper_create_and_insert_groups(ws, (7, 8)) existing_group = list(ws.GetGroupsWithSpan(Exactly(7), Exactly(8)))[0] ws.Replace(existing_group, new_group) self.assertTrue(existing_group not in ws.groups) self.assertEqual(1, len(list(ws.GetGroupsWithSpan(Exactly(7), Exactly(9))))) # So now (7, 8, 9) is a group. Let's add a group (5, 6) and try and extend it to 5:8 helper_create_and_insert_groups(ws, (5, 6)) existing_group = list(ws.GetGroupsWithSpan(Exactly(5), Exactly(6)))[0] new_group = SAnchored.Create((SAnchored(SElement(5), (), 5, 5), SAnchored(SElement(6), (), 6, 6), SAnchored(SElement(7), (), 7, 7), SAnchored(SElement(8), (), 8, 8))) self.assertRaises(ConflictingGroupException, ws.Replace, existing_group, new_group) # The original group still exists self.assertTrue(existing_group in ws.groups)
def setUp(self): self.ws = ws = Workspace() ws.InsertElements((5, 6, 7, 8)) ws.InsertGroup(SAnchored.Create((ws.elements[0], ws.elements[1])))