Esempio n. 1
0
    def test_one_node(self):
        dfa = DFAParser.from_dict({
            'accepts': {0},
            'start': 0,
            'transitions': {
                0: {
                    'a': 0
                }
            }
        })

        teacher = csteacher.CharacteristicSetTeacher(dfa)
        result = teacher.construct_char_set()
        self.assertCountEqual(result[0], ['a'])
        self.assertCountEqual(result[1], [])
Esempio n. 2
0
    def test_two_circular(self):
        dfa = DFAParser.from_dict(
            {'accepts': {0, 1},
             'start': 0,
             'transitions': {0: {'a': 1}, 1: {'a': 0}}})

        t = csteacher.CharacteristicSetTeacher(dfa)
        samples = t.construct_char_set()

        l = Learner(drawsteps=False)
        learned_dfa = l.rpni(samples[0], samples[1])
        for pos_sample in samples[0]:
            self.assertTrue(learned_dfa.recognizes(pos_sample))

        for neg_sample in samples[1]:
            self.assertFalse(learned_dfa.recognizes(neg_sample))
Esempio n. 3
0
    def test_big_dfa(self):
        dfa = DFAParser.from_dict(
            {'accepts': {'aa','aaa','aaab','aba','b','babab' },    
             'start': 'start',
             'transitions': {'a': {'a': 'aa', 'b': 'ab'}, 'aa': {'a': 'aaa'}, 'aaa': {'b': 'aaab'}, 'aaab':{}, 'ab':{'a':'aba'}, 'aba': {}, 'b' : {'a': 'ba'}, 'ba': {'b': 'bab'}, 'bab': {'a': 'baba'}, 'baba': {'b': 'babab'}, 'babab': {}, 'sink': {}, 'start': {'a': 'a', 'b': 'b'}}})

        t = csteacher.CharacteristicSetTeacher(dfa)
        samples = t.construct_char_set()

        l = Learner(drawsteps=False)
        learned_dfa = l.rpni(samples[0], samples[1])
        
        for pos_sample in samples[0]:
            self.assertTrue(learned_dfa.recognizes(pos_sample))

        for neg_sample in samples[1]:
            self.assertFalse(learned_dfa.recognizes(neg_sample))
Esempio n. 4
0
    def test_single_path(self):
        dfa = DFAParser.from_dict({
            'accepts': {3},
            'start': 0,
            'transitions': {
                0: {
                    'a': 1
                },
                1: {
                    'a': 2
                },
                2: {
                    'b': 3
                }
            }
        })

        teacher = csteacher.CharacteristicSetTeacher(dfa)
        result = teacher.construct_char_set()
        self.assertCountEqual(result[0], ['aab'])
        self.assertCountEqual(result[1], ['a', 'aa'])
    def __init__(self, state):
        self.state = state
        self.children = {} # children are stored like {transistion: Node(state)}

    def add_child(self, trans, obj):
        self.children[trans] = obj


def recursive_print_tree(node, trans='s', depth=0):
    '''Printing the tree in a more human readable form'''
    print('   ' * depth + trans + ': ' + str(node.state))
    for k, v in node.children.items():
        recursive_print_tree(v, k, depth+1)


if __name__ == '__main__':
    dfa = DFAParser.from_dict(
    {'accepts': {2},
    'start': 0,
    'transitions': {0: {'a': 1  , 'b': 1}, 1: {'a': 0, 'b': 2}, 2: {'a': 2, 'b': 0}}}
    )
    
    teacher = CharacteristicSetTeacher(dfa)
    tree = teacher.construct_tree()
    print("Degree: " + str(teacher.degree(tree[-1])))
    recursive_print_tree(tree[-1])

    print(teacher.char_set)
    for i in range(50):
        print(teacher.get_pos_example())
        print(teacher.get_neg_example())