def test_sanity(self):
        myltm = LTMGraph(filename=self.filename)
        m1 = NumericMapping(name='succ', category=Prime())
        m2 = NumericMapping(name='succ', category=Prime())
        m3 = NumericMapping(name='pred', category=Prime())
        m4 = NumericMapping(name='succ', category=Number())
        self.assertEqual(m1, m2)
        self.assertNotEqual(m1, m3)
        self.assertNotEqual(m1, m4)
        self.assertNotEqual(m3, m4)

        # Also test this with parametrized categories.
        for content in (m1, m2, m3, m4):
            myltm.GetNode(content=content)
        myltm.DumpToFile()

        # Let's clear the memos for NumericMapping.
        NumericMapping.__memo__.clear()

        myltm2 = LTMGraph(filename=self.filename)
        self.assertEqual(5, len(myltm2.nodes))
        m1_like_node = NumericMapping(name='succ', category=Prime())
        self.assertNotEqual(m1_like_node, m1)
        self.assertNotEqual(m1_like_node.GetLTMStorableContent(),
                            m1.GetLTMStorableContent())
        myltm2.GetNode(content=m1_like_node)
        self.assertEqual(5, len(myltm2.nodes))
Exemple #2
0
def InitializeSeqseeLTM(ltm):
    """Called if ltm was empty (had no nodes)."""
    logging.info("Initializing Seqsee LTM")
    from farg.apps.seqsee.sobject import SElement
    # Creates nodes for elements corresponding to the integers 1 through 10.
    elements = [SElement(magnitude) for magnitude in range(1, 11)]
    for element in elements:
        ltm.GetNode(content=element)
    for idx, element in enumerate(elements[:-1]):
        ltm.AddEdge(element, elements[idx + 1])
        ltm.AddEdge(elements[idx + 1], element)
    from farg.apps.seqsee.categories import Prime, Squares, TriangularNumbers
    for prime_number in Prime.number_list[:10]:
        ltm.AddEdge(SElement(prime_number),
                    Prime(),
                    edge_type_set={LTMEdge.LTM_EDGE_TYPE_ISA})
    for square in Squares.number_list[:10]:
        ltm.AddEdge(SElement(square),
                    Squares(),
                    edge_type_set={LTMEdge.LTM_EDGE_TYPE_ISA})
    for triangular in TriangularNumbers.number_list[:10]:
        ltm.AddEdge(SElement(triangular),
                    TriangularNumbers(),
                    edge_type_set={LTMEdge.LTM_EDGE_TYPE_ISA})
Exemple #3
0
    def test_Prime(self):
        self.assertEqual(Prime(), Prime())
        self.assertTrue(Prime().IsInstance(SObject.Create([3])))
        self.assertFalse(Prime().IsInstance(SObject.Create([4])))
        self.assertFalse(Prime().IsInstance(SObject.Create([3, 5])))

        element = SObject.Create([17])
        binding = Prime().IsInstance(element)
        self.assertFalse(element.IsKnownAsInstanceOf(Prime()))
        self.assertTrue(binding)
        self.assertEqual(6, binding.GetBindingsForAttribute('index'))

        binding2 = element.DescribeAs(Prime())
        self.assertTrue(element.IsKnownAsInstanceOf(Prime()))
        self.assertTrue(binding2)
        self.assertEqual(6, binding2.GetBindingsForAttribute('index'))
        self.assertNotEqual(binding, binding2)

        # Same (stored) binding returned.
        binding3 = element.DescribeAs(Prime())
        self.assertEqual(binding3, binding2)

        element5 = SObject.Create([5])
        element7 = SObject.Create([7])
        mapping = Prime().GetMapping(element5, element7)
        self.assertTrue(isinstance(mapping, NumericMapping))
        self.assertEqual(Prime(), mapping.category)
        self.assertEqual("succ", mapping.name)

        self.assertEqual(11, mapping.Apply(element7).magnitude)
Exemple #4
0
 def test_object_categorizability(self):
     ws = self.ws
     el0 = ws.elements[0]
     self.assertIsInstance(el0.object, CategorizableMixin)
     binding = el0.object.DescribeAs(Prime())
     self.assertEqual({'index': 2}, binding.bindings)