def Test_123_123(self):
        controller = MockSeqseeController((1, 2, 3, 1, 2, 3))
        workspace = controller.workspace
        numeric_succesor_mapping = NumericMapping(name='succ',
                                                  category=Number())
        ascending_group = MappingBasedCategory(
            mapping=numeric_succesor_mapping)
        group1 = self.HelperCreateAndInsertGroup(workspace, (0, 1, 2),
                                                 {numeric_succesor_mapping})
        group2 = self.HelperCreateAndInsertGroup(workspace, (3, 4, 5),
                                                 {numeric_succesor_mapping})
        group1.object.DescribeAs(ascending_group)
        group2.object.DescribeAs(ascending_group)
        self.AssertFringeContains(controller, group1,
                                  {numeric_succesor_mapping: 0.9})

        from farg.apps.seqsee.subspaces.get_mapping import CF_FindAnchoredSimilarity
        self.AssertFringeOverlap(
            controller,
            group1,
            group2,
            0.4,
            expected_similarity_affordances=(CodeletPresenceSpec(
                CF_FindAnchoredSimilarity, {
                    'left': group1,
                    'right': group2
                }), ))

        controller.stream.FocusOn(group2)
        controller.Step()
        from farg.apps.seqsee.codelet_families.all import CF_FocusOn
        self.AssertCodeletPresent(CodeletPresenceSpec(CF_FocusOn),
                                  controller.coderack._codelets)
Ejemplo n.º 2
0
    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 Create(items, *, underlying_mapping_set=None):
        """Given a list of items, each a SAnchored, creates another SAnchored, provided that the
       items are contiguous. Raises a NonAdjacentGroupElementsException if they are
       non-adjacent.

       The only acceptable kwarg is 'underlying_mapping_set'
    """
        if not items:
            raise FargError(
                "Empty group creation attempted. An error at the moment.")
        if len(items) == 1:
            if isinstance(items[0], SAnchored):
                return items[0]
            else:
                raise FargError(
                    "Attempt to SAnchored.Create() from non-anchored parts: %s"
                    % items[0].__repr__())

        # So there are multiple items...
        for item in items:
            if not isinstance(item, SAnchored):
                raise FargError(
                    "Attempt to SAnchored.Create() from non-anchored parts: %s"
                    % item.__repr__())
        # .. Note:: This can probably be speeded up and cleaned up.
        left_edge, right_edge = items[0].Span()
        for item in items[1:]:
            left, right = item.Span()
            if left != right_edge + 1:
                raise NonAdjacentGroupElementsException(items=items)
            right_edge = right
        new_object = SObject.Create(list(x.object for x in items))
        new_object.underlying_mapping_set = (underlying_mapping_set if
                                             underlying_mapping_set else set())
        if underlying_mapping_set:
            from farg.apps.seqsee.categories import MappingBasedCategory
            for mapping in underlying_mapping_set:
                new_object.DescribeAs(MappingBasedCategory(mapping=mapping))
        return SAnchored(new_object, items, left_edge, right_edge)
Ejemplo n.º 4
0
    def test_mapping_based(self):
        Size2 = SizeNCategory(size=2)
        numeric_sameness = NumericMapping(name="same", category=Number())
        numeric_successor = NumericMapping(name="succ", category=Number())
        mapping_second_succ = StructuralMapping(
            category=Size2,
            bindings_mapping=frozenset((('pos_1', numeric_sameness),
                                        ('pos_2', numeric_successor))))
        mapping_first_succ = StructuralMapping(
            category=Size2,
            bindings_mapping=frozenset((('pos_2', numeric_sameness),
                                        ('pos_1', numeric_successor))))
        SecondSucc = MappingBasedCategory(mapping=mapping_second_succ)
        SecondSuccp = MappingBasedCategory(mapping=mapping_second_succ)
        FirstSucc = MappingBasedCategory(mapping=mapping_first_succ)

        self.assertEqual(SecondSucc, SecondSuccp)
        self.assertNotEqual(SecondSucc, FirstSucc)

        self.assertTrue(SecondSucc.IsInstance(
            SObject.Create([(3, 4), (3, 5)])))
        self.assertTrue(SecondSucc.IsInstance(SObject.Create([3, 5])))
        self.assertTrue(SecondSucc.IsInstance(
            SObject.Create([(3, 4), (3, 5), (3, 6)])))
        self.assertFalse(SecondSucc.IsInstance(SObject.Create([3, 4, 6])))
        self.assertFalse(SecondSucc.IsInstance(
            SObject.Create([(3, 4), (3, 6), (3, 7)])))

        group = SObject.Create([(3, 5), (3, 6)])
        binding = SecondSucc.IsInstance(group)
        self.assertFalse(group.IsKnownAsInstanceOf(SecondSucc))
        self.assertTrue(binding)
        self.assertEqual(
            (3, 5), binding.GetBindingsForAttribute('start').Structure())
        self.assertEqual(
            2, binding.GetBindingsForAttribute('length').magnitude)

        binding2 = group.DescribeAs(SecondSucc)
        self.assertTrue(group.IsKnownAsInstanceOf(SecondSucc))
        self.assertTrue(binding2)
        self.assertEqual(
            (3, 5), binding.GetBindingsForAttribute('start').Structure())
        self.assertEqual(
            2, binding.GetBindingsForAttribute('length').magnitude)
        self.assertNotEqual(binding, binding2)

        # Same (stored) binding returned.
        binding3 = group.DescribeAs(SecondSucc)
        self.assertEqual(binding3, binding2)

        element5 = SObject.Create([(13, 5), (13, 6)])
        element6 = SObject.Create([(14, 6), (14, 7), (14, 8)])
        controller = MockSeqseeController()
        mapping = SecondSucc.FindMapping(element5, element6,
                                         controller=controller,
                                         seqsee_ltm=controller.ltm)
        self.assertTrue(isinstance(mapping, StructuralMapping))
        self.assertEqual(SecondSucc, mapping.category)

        self.assertEqual(((15, 7), (15, 8), (15, 9), (15, 10)),
                         mapping.Apply(element6).Structure())