def test_creation(self): b1 = Boolean(True) b2 = Boolean(0.5) self.assertIsInstance(b1.bias, float) self.assertIsInstance(b2.bias, float) self.assertTrue(b1()) self.assertIsInstance(b1(), int) self.assertIsInstance(b1(bool), bool) self.assertIsInstance(b2(), int) self.assertIsInstance(b2(bool), bool)
def __init__(self, label: str, predecessors: list or dict, bf: BooleanFunction, init_state=r_bool()): self.__label = str(label) self.__predecessors = tuple(predecessors) self.__boolfun = bf self.__hash = hash(str(self.__label)) self.__init_state = bool(init_state) self.__state = Boolean(init_state)
def test_edit(self): dbf = BooleanFunction(3) v = dbf[(True, True, False)] dbf[(True, True, False)] = True self.assertIsInstance(dbf[(True, True, False)], Boolean) self.assertEqual(dbf[(True, True, False)], Boolean(True)) self.assertEqual(dbf[(True, True, False)], True) self.assertEqual(dbf[(True, True, False)], 1.0) self.assertEqual(dbf[(True, True, False)], 1)
def test_edit(self): t1 = NTree(1, [], 1) self.assertEqual(len(t1), 0) t1.value = Boolean(True) self.assertIsInstance(t1.value, Boolean) t1.children = [NTree(2, [], 2), NTree(3, [], 3)] self.assertEqual(len(t1), 2)
def test_serialization(self): dbf = BooleanFunction( 2, result_generator=lambda *args: Boolean(args[0] and args[1])) dbf_json = { 'arity': 2, 'truth_table': [{ 'params': { '0': False, '1': False }, 'hold': { '0': 1.0, '1': 0.0 } }, { 'params': { '0': False, '1': True }, 'hold': { '0': 1.0, '1': 0.0 } }, { 'params': { '0': True, '1': False }, 'hold': { '0': 1.0, '1': 0.0 } }, { 'params': { '0': True, '1': True }, 'hold': { '0': 0.0, '1': 1.0 } }] } self.assertDictEqual(dbf.to_json(), dbf_json)
def test_edit(self): b1 = Boolean(True) b2 = Boolean(0.5) tmp1 = copy.deepcopy(b1) tmp2 = copy.deepcopy(b2) b1.bias = 0.5 b2.bias = False self.assertIsInstance(b1.bias, float) self.assertIsInstance(b2.bias, float) self.assertNotEqual(b1.bias, tmp1.bias) self.assertNotEqual(b2.bias, tmp2.bias) self.assertFalse(b2()) self.assertIsInstance(b1(), int) self.assertIsInstance(b1(bool), bool) self.assertIsInstance(b2(), int) self.assertIsInstance(b2(bool), bool)
def test_deserialization(self): b1 = Boolean(True) self.assertEqual(Boolean.from_json({'0':0.0, '1':1.0}), b1)
def test_serialization(self): b1 = Boolean(True) self.assertDictEqual(b1.to_json(), {'0':0.0, '1':1.0})
def test_evaluation(self): b1 = Boolean(True) b2 = Boolean(0.5) self.assertTrue(b1()) self.assertIn(b2(), [True, False])