Example #1
0
def automata_union(automata_list):
    """
    Do the union to the automatas.
    """
    
    final = Automata()

    final.start = 'q0'

    for auto in automata_list:
        for st in list(auto.transitions):
            if st not in final.states:
                final.states.append(st)
            if len(list(auto.transitions[st])) > 0:
                for letter in list(auto.transitions[st]):
                    if letter not in final.alphabet:
                        final.alphabet.append(letter)
                    for reach in auto.transitions[st][letter]: 
                        if reach not in final.states:
                            final.states.append(reach)
                        if st not in final.transitions.keys():
                            final.transitions[st] = {}
                        if letter not in final.transitions[st].keys():
                            final.transitions[st][letter] = []
                        if st not in final.transitions[st][letter]:
                            final.transitions[st][letter].append(reach)
            
            if st in auto.accept:
                if st not in final.accept:
                    final.accept.append(st)
    return final
Example #2
0
def main():
    archivo = open("prueba.java", "r")
    linea = 1
    estados_aceptacion = ("c0", "c1", "c2", "c3", "c4")
    imprimir_error = False
    conteo_errores = 0
    for line in archivo.readlines():
        if ("0" in line or "1" in line or "2" in line or "3" in line
                or "4" in line or "5" in line or "6" in line or "7" in line
                or "8" in line or "9" in line):
            palabras = line.split(" ")
            for pal in palabras:
                if ("0" in pal or "1" in pal or "2" in pal or "3" in pal
                        or "4" in pal or "5" in pal or "6" in pal or "7" in pal
                        or "8" in pal or "9" in pal):
                    auto = Automata()
                    #pal=pal.replace("\n","")
                    for c in pal:
                        #print(auto.estado)
                        #print(c)
                        auto.calcular_estado(c.lower())
                    if (auto.estado not in estados_aceptacion):
                        #print(auto.estado)
                        imprimir_error = True
                        conteo_errores += 1
        if imprimir_error:
            print("Error en linea: ", linea)
        imprimir_error = False
        linea += 1
    if conteo_errores == 0:
        print("No hay errores lexicos en el programa")
Example #3
0
def main():
    # one = angleClockwise((370.0, 90.0), (130.0, 90.0))
    # two = angleClockwise((50.0, 90.0), (130.0, 90.0))
    # print(one, two)
    # get data from file
    with open(FILEPATH) as f:
        data = f.readline()

    # pass data Automata
    aut = Automata(data)
    aut.print()

    # set up Tk root and frame
    root = Tk()
    root.geometry(str(WIDTH) + "x" + str(HEIGHT) + "+10+10")
    frame = Frame(width=FRAME_WIDTH, height=FRAME_HEIGHT, padx=PAD, pady=PAD)
    frame.pack(fill=BOTH, expand=1)

    # create automata canvas with Automata and Frame
    AutomataCanvas(frame=frame,
                   automata=aut,
                   width=FRAME_WIDTH,
                   height=FRAME_HEIGHT)

    # start Tk
    root.mainloop()
Example #4
0
def interseccion(archivo_automata1, archivo_automata2, archivo_automata):
    automata_1 = Automata.crear_desde_archivo_automata(archivo_automata1)
    automata_2 = Automata.crear_desde_archivo_automata(archivo_automata2)
    inter = automata_1.interseccion(automata_2)
    
    inter.minimizar()
    inter.escribir_archivo(archivo_automata)
Example #5
0
 def __init__(self):
     self.lista_liga = ListaLigada()
     self.automata = Automata()
     self.linea: int = 0
     self.linea_lista = ListaLigada()
     self.linea_errores: str = None
     self.log_error: str = None
    def __init__(self, timer, base, rule, state1, state2, state3, instruments, limit):
        pygame.init()
        pygame.display.set_caption('Automata')
        self.surface = pygame.display.set_mode((c.WIDTH, c.HEIGHT))
        self.surface.fill(c.BLACK)        

        self.grid = Grid(20, 20, 40, 10, 15, 15)
        self.grid2 = Grid(300, 20, 40, 10, 15, 15)
        self.grid3 = Grid(580, 20, 40, 10, 15, 15)

        self.instruments = instruments

        self.clock = pygame.time.Clock()
        self.timer = timer
        self.repaint_evt = pygame.USEREVENT + 1
        self.sound_evt = pygame.USEREVENT + 2
        self.automata1 = Automata(rule, base, limit, state1)
        self.automata2 = Automata(rule, base, limit, state2)
        self.automata3 = Automata(rule, base, limit, state3)
        self.iterator = 0
        self.iterator2 = 14
        self.iterator3 = 28
        self.j = 0

        pygame.time.set_timer(self.repaint_evt, timer)
        pygame.time.set_timer(self.sound_evt, timer+2)
Example #7
0
    def setUp(self):
        dom1 = '''
            <!DOCTYPE html>
            <html lang="en">
            <head> <meta charset="UTF-8"> <title></title> </head>
            <body>dom1</body>
            </html>
        '''
        state = State(dom1)
        self.automata = Automata()
        self.automata.add_state(state)
        self.assertEqual(len(self.automata.get_states()), 1)
        self.assertEqual(self.automata.get_initial_state().get_id(), self.automata.get_current_state().get_id())
        self.assertEqual(self.automata.get_current_state().get_id(), '0')

        # test adding and removing inputs and forms
        form1 = FormField('form1')
        form1.add_input(InputField('username', '//*[@id="username"]', 'castman'))
        form1.add_input(InputField('password', '', 'p@ssw0rd'))
        form1.add_input(InputField('password', '', 'p@ssw0rd'))
        self.assertEqual(len(form1.get_inputs()), 2)
        form1.remove_input(InputField('username', '//*[@id="username"]', 'castman'))
        self.assertEqual(len(form1.get_inputs()), 1)

        form2 = FormField('', '//*[@id="lst-ib"]')
        clickable = Clickable('', '//*[@id="btn1"]')
        clickable.add_form(form1)
        clickable.add_form(FormField('form1'))
        clickable.add_form(form2)
        self.assertEqual(len(clickable.get_forms()), 2)
        clickable.remove_form(FormField('', '//*[@id="lst-ib"]'))
        self.assertEqual(len(clickable.get_forms()), 1)

        # add the clickable into state 0
        self.automata.get_current_state().add_clickable(clickable)
def debugTestMain(folderpath, dirname):
    logging.info(" setting config...")
    config = SeleniumConfiguration(Browser.FireFox, "http://140.112.42.145:2000/demo/nothing/main.html")
    config.set_max_depth(1)
    config.set_max_length(5)
    config.set_trace_amount(1)
    config.set_max_states(100)
    config.set_folderpath(folderpath)
    config.set_dirname(dirname)
    config.set_automata_fname('automata.json')
    config.set_traces_fname('traces.json')
    config.set_frame_tags(['iframe'])
    config.set_dom_inside_iframe(True)
    config.set_simple_clickable_tags()
    config.set_simple_inputs_tags()
    config.set_simple_normalizers()

    logging.info(" setting executor...")
    executor = SeleniumExecutor(config.get_browserID(), config.get_url())

    logging.info(" setting crawler...")
    automata = Automata(config)
    databank = InlineDataBank("140.112.42.145:2000", "jeff", "zj4bj3jo37788", "test")
    algorithm = MonkeyCrawler() #DFScrawler()
    crawler = SeleniumCrawler(config, executor, automata, databank, algorithm)

    logging.info(" crawler start run...")
    crawler.run_algorithm()

    logging.info(" end! save automata...")
    algorithm.save_traces()
    automata.save_automata(config.get_automata_fname())
    Visualizer.generate_html('web', os.path.join(config.get_path('root'), config.get_automata_fname()))
    config.save_config('config.json')
def CBTestMain(folderpath, dirname,web_submit_id):
    logging.info(" Type: Cross Browser Testing")
    logging.info(" connect to mysql")
    print("")
    print("connect to sql")
    databank = MysqlDataBank("localhost", "root", "", "test")
    url, deep, time, b1, b2 = databank.get_websubmit(int(web_submit_id))
    basic_browserID = str(b1)
    other_browserID = str(b2)
    depth = int(deep)

    logging.info(" A new CBT begings...")
    logging.info(" setting config...")
    config = SeleniumConfiguration(int(basic_browserID),int(other_browserID), url)
    # max 3
    config.set_max_depth(int(depth))
    # max 3
    config.set_max_length(int(depth))
    # should be 1
    config.set_trace_amount(1)
    # should be 100 no use?
    config.set_max_states(5)
    config.set_folderpath(folderpath)
    config.set_dirname(dirname)
    config.set_automata_fname('automata.json')
    config.set_traces_fname('traces.json')
    #config.set_frame_tags(['iframe'])
    config.set_dom_inside_iframe(True)
    config.set_simple_clickable_tags()
    config.set_simple_inputs_tags()
    config.set_simple_normalizers()

    logging.info(" setting executor...")
    #nothing here
    executor = CBTExecutor(config.get_browserID(), config.get_url())

    logging.info(" setting crawler...")
    automata = Automata(config)
    #databank = InlineDataBank("140.112.42.145:2000", "jeff", "zj4bj3jo37788", "test")
    databank = InlineDataBank("localhost", "B00901138", "R124249166", "test")
    
    print('start Cross Browser Testing...')
    #acually it's CBT algorithm
    algorithm = CBTCrawler(int(other_browserID),url)

    crawler = SeleniumCrawler(config, executor, automata, databank, algorithm)

    logging.info(" crawler start runing...")
    crawler.run_algorithm()
    
    print(" end! save automata...")
    logging.info(" end! save automata...")
    algorithm.save_traces()
    automata.save_automata(config.get_automata_fname())
    Visualizer.generate_html('web', os.path.join(config.get_path('root'), config.get_automata_fname()))
    config.save_config('config.json')
Example #10
0
 def create_automata(self):
     # create automata from C++ library through a Python stab
     __automata = Automata()
     # construct the automata from the records in DB
     for line in self.__records:
         __automata.add_string(line)
         # construct the faik over links in the suffix tree...
     __automata.construct_fail_links()
     # return the automata to the callee...
     return __automata
Example #11
0
def create_stream(alphabet_size, stream_length, probabilities):
    '''
    function create streams over alphabet 1 to alphabet_sizes
    of length stream_length using probabilities
    param: alphabet_size
    param: stream_length
    param: probabilities - matrix of size alphabet_size x alphabet_size, which describes transition function for automata
    '''
    a = Automata(probabilities)
    return a.gen_stream(stream_length)
Example #12
0
 def __init__(self):
     self._initialize_buttons()
     self._initialize_button_events()
     self.device = sh1106(spi(device=0, port=0), rotate=2)
     self.device.contrast(255)
     self.automata = Automata(rules_list.current_rule(), self.DIMENSIONS)
     self._repopulate()
     self.start_time = time.time()
     self.paused = False
     self.average_fps = 0
     self.last_measured_time = time.time()
Example #13
0
def main():
    texto = open("texto.txt", "r")
    auto = Automata()
    for letra in texto.read():
        if letra == "\n":
            letra = " "
        print(auto.estado)
        print(letra)
        auto.calcular_estado(letra)
    if "c0" in auto.estado or "c1" in auto.estado:
        print("No hay errores en tu escritura")
    else:
        print("Hay un error en el texto")
Example #14
0
 def objective_func(self, x):
     opts = self.vec_to_opts(x)
     print(opts)
     # run automata
     a = Automata(opts)
     a.run()
     stats = a.stats()
     fitness = np.float64(-stats["score_sum"])
     print("Fitness: ", fitness)
     # if fitness < self.best_fitness:
     #     self.best_fitness = fitness
     #     self.best_automata = a
     return fitness
Example #15
0
 def __init__(self, configuration, executor):
     self.automata = Automata()
     self.configuration = configuration
     self.executor = executor
     self.exe_stack = []  # stack of executed clickables (events)
     self.invariant_violation = []
     self.num_clickables = {
         'unexamined':
         0,  # num of candidate clickables found with rules in DomAnalyzer
         'true':
         0,  # num of clickables triggered new state (different screen dom)
         'false': 0,  # num of clickables not triggering new state
     }
Example #16
0
 def test_load_save(self):
     automata = Automata(fname='test_data/automata-example.json')
     config = B2gConfiguration('test-app-name', 'test-app-id', mkdir=False)
     config.set_path('root', 'test_data')
     config.set_path('dom', 'test_data/dom')
     config.set_path('state', 'test_data/screenshot/state')
     config.set_path('clickable', 'test_data/screenshot/clickable')
     saved_file_path = automata.save(config)
     import filecmp
     self.assertTrue(filecmp.cmp('test_data/automata-example.json', 'test_data/automata.json'))
     try:
         os.remove(saved_file_path)
     except OSError:
         pass
Example #17
0
class AnalizadorLexico(object):
    def __init__(self):
        self.lista_liga = ListaLigada()
        self.automata = Automata()
        self.linea: int = 0
        self.linea_lista = ListaLigada()
        self.linea_errores: str = None
        self.log_error: str = None

    def retornar_lista(self):
        return self.lista_liga

    def limpiar(self):
        self.lista_liga.cabeza = None
        self.linea = 0
        self.linea_lista.cabeza = None
        self.automata = Automata()

    def analizar_linea(self, linea_texto: str = None):

        self.linea += 1
        self.linea_lista = ListaLigada()
        self.linea_errores = ''
        logger_modulo.info(f"Procesando la linea {self.linea} : {linea_texto}")

        patron = f"({'|'.join(CARACTERES_ESPECIALES)})"
        palabras = re.sub(pattern=patron, repl=r" \1 ", string=linea_texto)
        try:
            palabras = shlex.split(palabras, posix=False)
        except ValueError:
            logger_modulo.error(
                f"error en linea {self.linea}: falto cierre de parentesis")
            return
        for p in palabras:
            nodo = Nodo(p)
            #            self.lista_liga.encolar(nodo)
            self.linea_lista.encolar(nodo)
            self.automata.establecer_estado(nodo, self.linea)
        logger_modulo.info(self.linea_lista)

    def retornar_texto(self):
        return "hola mundo"

    def retornar_texto_listal(self):
        return str(self.lista_liga)

    def verificar_balances(self):
        self.automata.verificar_pilas_vacias()
Example #18
0
def reachable(nfa):
    possible = []
    possible.append(nfa.start[0])
    next_state = []
    next_state.append(nfa.start[0])
    while not next_state == []:
        this_node = next_state[0]
        next_state.remove(this_node)
        for alpha in nfa.alphabet:
            for s in nfa.nodes[this_node].getTransitionState(alpha):
                if s not in possible:
                    possible.append(s)
                    if s not in next_state: next_state.append(s)
    possible.sort()
    new_accept = []
    for s in nfa.accepts:
        if s in possible: new_accept.append(s)
    new_transitions = []
    for x in nfa.transitions:
        if x[0] in possible and x[2] in possible: new_transitions.append(x)
    #print "all states: ",possible
    #print "stating point",nfa.start
    #print new_accept
    #for x in new_transitions: print x
    return Automata(possible, nfa.start, new_accept, new_transitions,
                    nfa.alphabet)
Example #19
0
def main():
    challenge = get_json('/automatabot/challenges/new')
    print_response(challenge)

    print(
        post_json(challenge['challengePath'],
                  Automata(challenge['challenge']).solve()))
Example #20
0
def pertenece_al_lenguaje(archivo_automata, cadena):
    automata_1 = Automata.crear_desde_archivo_automata(archivo_automata)

    if automata_1.acepta(cadena):
        print("TRUE")
    else:
        print("FALSE")
def ConstructAutomata(file):
    """Parses the supplied automata file, then constructs and returns an 
       Automata object.

       :param str | file file: File object or URI.
       :rtype: Automata
    """
    fa = FiniteAutomata.parseFile(file)
    # Alphabet check: if there are symbols in the transitions not in the
    # alphabet, throw an exception.
    for trans in fa.Transitions:
        for symbol in trans[1]:
            if symbol not in fa.Alphabet.asList():
                trans_str = trans[0] + ' \''\
                            + " \'".join(trans[1])\
                            + " --> " + trans[2]
                raise Exception("Alphabet Error! The transition:\n\n"\
                                + "    " + trans_str + "\n\n"\
                                + " contains the symbol \'" + symbol + "\' "\
                                + "which is not in the described alphabet!")

    # Note on fa.Start: parseResult objects always return values in lists,
    # so this must be dereferenced.
    return Automata(fa.States, fa.Start[0], fa.Accept, fa.Transitions,\
                    fa.Alphabet)
Example #22
0
def read_automata(path):
    f = open(path, 'r')
    text = f.readlines()

    states = text[0].strip().strip('{}').split(",")
    alphabet = text[1].strip().strip('{}').split(",")
    initial = text[2].strip().strip('{}')
    final = text[3].strip().strip('{}').split(",")

    delta = []
    for line in text[4:]:
        line = line.replace("#", "").strip()
        s = line[0]
        for i in range(1, len(line), 2):
            [a, ns] = line[i:i + 2]
            if a == '&':
                a = 'λ'
            delta.append(((s, a), ns))

    automata = Automata(alphabet=alphabet,
                        states=states,
                        delta=delta,
                        initial=initial,
                        final=final)

    return automata
Example #23
0
    def build_basic_struct(value):
        state1 = 1
        state2 = 2
        basic = Automata()
        basic.set_start_state(state1)
        basic.add_final_states(state2)
        basic.add_transitions(state1, state2, value)

        return basic
Example #24
0
 def create_grid(self):
     lst = []
     for i in range(self.length):
         temp = []
         for j in range(self.width):
             temp.append(Automata('E', (i, j)))
         lst.append(temp)
     self.cellular_grid = np.array(lst)
Example #25
0
def str2auto(aut1):
    auto1 = Automata(aut1)
    auto1.add_node(1)
    auto1.add_node(2)
    auto1.add_connection(1, 2, aut1)
    auto1.final_state = 2
    return auto1
Example #26
0
    def construct_NFA(self):
        language = set()
        self.stack = []
        self.automata = []
        previous = Automata.empty()

        for char in self.regex:
            if char in self.alphabets:
                language.add(char)
                if previous != RegexToNFA.dot() and (
                        previous in self.alphabets or previous
                        in [RegexToNFA.closing_bracket(),
                            RegexToNFA.star()]):
                    self.add_operator_to_stack(RegexToNFA.dot())
                self.automata.append(
                    AutomataConstruction.basic_structure(char))
            elif char == RegexToNFA.opening_bracket():
                if previous != RegexToNFA.dot() and (
                        previous in self.alphabets or previous
                        in [RegexToNFA.closing_bracket(),
                            RegexToNFA.star()]):
                    self.add_operator_to_stack(RegexToNFA.dot())
                self.stack.append(char)
            elif char == RegexToNFA.closing_bracket():
                while (1):
                    if len(self.stack) == 0:
                        raise Exception("Empty stack!")
                    operator = self.stack.pop()
                    if operator == RegexToNFA.opening_bracket():
                        break
                    elif operator in self.operators:
                        self.construct_sNFA(operator)
            elif char == RegexToNFA.star():
                if previous in [
                        RegexToNFA.star(),
                        RegexToNFA.plus(),
                        RegexToNFA.opening_bracket()
                ]:
                    raise Exception("No matching!")
                self.construct_sNFA(char)
            elif char in self.operators:
                if previous in self.operators or previous == RegexToNFA.opening_bracket(
                ):
                    raise Exception("No matching!")
                else:
                    self.add_operator_to_stack(char)
            else:
                raise Exception("No matching!")

            previous = char

        while (len(self.stack) > 0):
            operator = self.stack.pop()
            self.construct_sNFA(operator)

        self.nfa = self.automata.pop()
        self.nfa.alphabets = language
Example #27
0
 def __init__(self, configuration, executor):
     self.automata = Automata()
     self.configuration = configuration
     self.executor = executor
     self.exe_stack = []  # stack of executed clickables (events)
     self.invariant_violation = []
     self.num_clickables = {
         'unexamined': 0,  # num of candidate clickables found with rules in DomAnalyzer
         'true':  0,  # num of clickables triggered new state (different screen dom)
         'false': 0,  # num of clickables not triggering new state
     }
Example #28
0
    def __init_states(self, in_file):
        """:return list of states that contains on file's next line"""

        states_names_list = self.__next_elements(in_file)
        states = {}

        for state_name in states_names_list:
            state = Automata.State(state_name, False, {})
            states[state_name] = state

        return states
Example #29
0
def check_example(params, example):
    '''Checks if an example is parameterized by these params
  Args:
    params: list of 6 parameters
    example: string to check
  Return:
    True if a match is found and it completely captures the example
  '''
    # for i in xrange(1, 4):
    #   if len(example) % i == 0:
    #     # chunk = example[0:len(example)/i]
    #     for j in xrange(0, i):
    #       chunk = example[j*len(example)/i:(j+1)*len(example)/i]
    #       if not check_subregex(params, chunk):
    #         break
    #     else:
    #       # goes here if the loop finishes completely without breaks
    #       return True
    # return False
    a = Automata(params)
    return a.test_string(example)
Example #30
0
def SeleniumMutationTrace(folderpath, dirname, config_fname, traces_fname,
                          trace_id, method_id, modes):
    logging.info(" loading config...")
    config = load_config(config_fname)
    config.set_folderpath(folderpath)
    config.set_dirname(dirname)
    config.set_mutation_trace(traces_fname, trace_id)
    config.set_mutation_method(method_id)
    config.set_mutation_modes(modes)

    logging.info(" setting executor...")
    executor = SeleniumExecutor(config.get_browserID(), config.get_url())

    logging.info(" setting crawler...")
    automata = Automata()
    databank = MysqlDataBank("localhost", "jeff", "zj4bj3jo37788", "test")
    crawler = SeleniumCrawler(config, executor, automata, databank)

    logging.info(" crawler start run...")
    crawler.run_mutant()

    logging.info(" end! save automata...")
    automata.save_traces(config)
    automata.save_automata(config)
    Visualizer.generate_html(
        'web',
        os.path.join(config.get_path('root'), config.get_automata_fname()))
Example #31
0
def thompson_concatenate(aut1, aut2):

    if type(aut1) == Automata:
        str1 = aut1.name
        auto1 = aut1
    else:
        str1 = aut1
        auto1 = str2auto(aut1)

    if type(aut2) == Automata:
        str2 = aut2.name
        auto2 = aut2
    else:
        str2 = aut2
        auto2 = str2auto(aut2)

    if debug:
        auto1.display()
        auto2.display()

    auto3 = Automata('(' + str1 + str2 + ')')
    auto3.set_initial_state(auto1.initial_state)
    auto3.set_final_state(auto1.final_state + auto2.final_state - 1)

    for node in auto1.node_list:
        if node.label != auto1.final_state:
            auto3.node_list.append(node)

    for node in auto2.node_list:
        new_node = Anode(node.label + int(auto1.final_state - 1))
        for dest, label in node.connections.items():
            new_node.add_connection(dest + int(auto1.final_state) - 1, label)
        auto3.node_list.append(new_node)

    return auto3
Example #32
0
def SeleniumMain(web_submit_id, folderpath=None, dirname=None):
    logging.info(" connect to mysql")
    print("connect to sql")
    databank = MysqlDataBank("localhost", "root", "", "test")
    url, deep, time = databank.get_websubmit(web_submit_id)

    logging.info(" setting config...")
    print(" setting config...")

    config = SeleniumConfiguration(Browser.PhantomJS, url, folderpath, dirname)
    config.set_max_depth(deep)
    config.set_max_time(int(time)*60)
    config.set_simple_clickable_tags()
    config.set_simple_inputs_tags()
    config.set_simple_normalizers()
    config.set_frame_tags(['iframe'])
    
    logging.info(" setting executor...")
    executor = SeleniumExecutor(config.get_browserID(), config.get_url())
    
    logging.info(" setting crawler...")
    automata = Automata()
    crawler = SeleniumCrawler(config, executor, automata, databank)
    
    logging.info(" crawler start run...")
    automata = crawler.run()
    crawler.close()
    
    logging.info(" end! save automata...")
    automata.save_automata(config)
    automata.save_traces(config)
    Visualizer.generate_html('web', os.path.join(config.get_path('root'), config.get_automata_fname()))
    config.save_config('config.json')
Example #33
0
def main():
    automataDict = automata_IO.dfa_json_importer("./resources/automata.json")

    alphabet = automataDict["alphabet"]
    initialState = automataDict["initial_state"]
    transitions = automataDict["transitions"]
    acceptingStates = automataDict["accepting_states"]

    automata_IO.dfa_to_dot(automataDict, "automata", "./resources")

    automata = Automata(alphabet, initialState, transitions, acceptingStates)

    _ = Screen("./resources/automata.dot.svg", automata)
Example #34
0
def main():
    automataDict = automata_IO.dfa_json_importer("./resources/AFD.json")

    alphabet = automataDict["alphabet"]
    initialState = automataDict["initial_state"]
    transitions = automataDict["transitions"]
    acceptingStates = automataDict["accepting_states"]
    '''Monta a imagem a partir do Json passado'''
    automata_IO.dfa_to_dot(automataDict, "AFD", "./resources")

    automata = Automata(alphabet, initialState, transitions, acceptingStates)
    '''Screen recebe como parametro o caminho da imagem e também o objeto automato'''
    _ = Screen("./resources/AFD.dot.svg", automata)
Example #35
0
def lireFichier():
    global automates

    print("")
    print("Veuillez entrer le nom du fichier contenant les règles.")
    print("Entrez une ligne vide pour retourner au menu principal.")
    print("")

    fichier = obtenirNomFichier()
    if fichier is None:
        return

    automate = Automata.creerAutomate(fichier)
    automates[automate.getNumero()] = automate
Example #36
0
def SeleniumMutationTrace(folderpath, dirname, config_fname, traces_fname, trace_id, method_id, modes):
    logging.info(" loading config...")
    config = load_config(config_fname)
    config.set_folderpath(folderpath)
    config.set_dirname(dirname)
    config.set_mutation_trace(traces_fname, trace_id)
    config.set_mutation_method(method_id)
    config.set_mutation_modes(modes)

    logging.info(" setting executor...")
    executor = SeleniumExecutor(config.get_browserID(), config.get_url())

    logging.info(" setting crawler...")
    automata = Automata()
    databank = MysqlDataBank("localhost", "B00901138", "R124249166", "test")
    crawler = SeleniumCrawler(config, executor, automata, databank)

    logging.info(" crawler start run...")
    crawler.run_mutant()

    logging.info(" end! save automata...")
    automata.save_traces(config)
    automata.save_automata(config)    
    Visualizer.generate_html('web', os.path.join(config.get_path('root'), config.get_automata_fname()))
Example #37
0
def load_automata(fname):
    t_start = time.time()
    assert os.path.isfile(fname) and os.path.exists(fname)
    automata = Automata()
    with open(fname) as f:
        data = json.load(f)
        for state in data['state']:
            with open(os.path.join(os.path.dirname(os.path.realpath(fname)), state['dom_path']), 'r') as df:
                s = State(df.read())
                s.set_id(state['id'])
                for clickable in state['clickable']:
                    c = Clickable(clickable['id'], clickable['xpath'], clickable['tag'])
                    s.add_clickable(c)
                automata.add_state(s)
        for edge in data['edge']:
            from_state = automata.get_state_by_id(edge['from'])
            to_state = automata.get_state_by_id(edge['to'])
            clickable = from_state.get_clickable_by_id(edge['clickable'])
            assert from_state and to_state and clickable
            automata.add_edge(from_state, to_state, clickable)
    return automata
Example #38
0
def print_tst(states, alphabet, transitions, start, accept):
    print("\tstates:", states)
    print("\talphabet:", alphabet)
    print("\ttransitions:", transitions)
    print("\tstart:", start)
    print("\taccept:", accept)


def print_fsm(fsm):
    print_tst(fsm.states, fsm.alphabet, fsm.transitions, fsm.start, fsm.accept)


"""
Tests:
"""
fsm = Automata(states, alphabet, transitions, start, accept)
print("Original states:")
print_fsm(fsm)

print("Automata to next tests: ")

# test for RegGra
print("If we want a Regular Grammar equivalent of the automata, here it is:")
regular_grammar = fsm.toRegGra()
print("RegGra:", regular_grammar)
print("And to a FSM again!")
new_automata = fsm.fromRegGraToAutomata(regular_grammar, states[0])
print("Automata:")
print_fsm(new_automata)
Example #39
0
accept = states[2]

def print_tst(states, alphabet, transitions, start, accept):
    print("\tstates:",states)
    print("\talphabet:",alphabet)
    print("\ttransitions:",transitions)
    print("\tstart:", start)
    print("\taccept:", accept)

def print_fsm(fsm):
    print_tst(fsm.states,fsm.alphabet,fsm.transitions,fsm.start,fsm.accept)
        
"""
Tests:
"""
fsm = Automata(states, alphabet, transitions, start, accept)

print("Original states:")
print_fsm(fsm)

print("Is deterministic?",fsm.isdeterministic())
print("Now we know that automata is not deterministic, we need detereminize it!")

fsmD = fsm.determinize()

print("Here is the determinized automata:")
print_fsm(fsmD)

print("If we want a Regular Expression of the automata, here it is:")
regular_expression = fsmD.toRegEx()
print("RegEx:", regular_expression)
Example #40
0
class AutomataTestCase(unittest.TestCase):
    def setUp(self):
        dom1 = '''
            <!DOCTYPE html>
            <html lang="en">
            <head> <meta charset="UTF-8"> <title></title> </head>
            <body>dom1</body>
            </html>
        '''
        state = State(dom1)
        self.automata = Automata()
        self.automata.add_state(state)
        self.assertEqual(len(self.automata.get_states()), 1)
        self.assertEqual(self.automata.get_initial_state().get_id(), self.automata.get_current_state().get_id())
        self.assertEqual(self.automata.get_current_state().get_id(), '0')

        # test adding and removing inputs and forms
        form1 = FormField('form1')
        form1.add_input(InputField('username', '//*[@id="username"]', 'castman'))
        form1.add_input(InputField('password', '', 'p@ssw0rd'))
        form1.add_input(InputField('password', '', 'p@ssw0rd'))
        self.assertEqual(len(form1.get_inputs()), 2)
        form1.remove_input(InputField('username', '//*[@id="username"]', 'castman'))
        self.assertEqual(len(form1.get_inputs()), 1)

        form2 = FormField('', '//*[@id="lst-ib"]')
        clickable = Clickable('', '//*[@id="btn1"]')
        clickable.add_form(form1)
        clickable.add_form(FormField('form1'))
        clickable.add_form(form2)
        self.assertEqual(len(clickable.get_forms()), 2)
        clickable.remove_form(FormField('', '//*[@id="lst-ib"]'))
        self.assertEqual(len(clickable.get_forms()), 1)

        # add the clickable into state 0
        self.automata.get_current_state().add_clickable(clickable)

    def test_automata(self):
        dom1 = self.automata.get_current_state().get_dom()
        dom1 += '<custom></custom>'
        dom2 = dom1
        state1 = State(dom1)
        state2 = State(dom2)
        self.automata.add_state(state1)
        state3, is_newly_added = self.automata.add_state(state2)
        self.assertTrue(state3 == state1)
        self.assertFalse(is_newly_added)
        self.assertEqual(len(self.automata.get_states()), 2)

        clickable = self.automata.get_current_state().get_clickables()[0]
        clickable2 = Clickable('', '//html/body/button[3]')
        self.assertEqual(len(self.automata.get_current_state().get_clickables()), 1)
        self.automata.get_current_state().add_clickable(clickable)
        self.automata.get_current_state().add_clickable(clickable2)
        self.automata.get_current_state().add_clickable(clickable2)
        self.assertEqual(len(self.automata.get_current_state().get_clickables()), 2)

        self.automata.add_edge(self.automata.get_current_state(), state1, self.automata.get_current_state().get_clickables()[0])
        self.assertEqual(len(self.automata.get_edges()), 1)

        state1.add_prev_state(self.automata.get_current_state())

        self.assertEqual(self.automata.get_current_state().get_id(), '0')
        self.automata.change_state(state1)
        self.assertEqual(self.automata.get_initial_state().get_id(), '0')
        self.assertEqual(self.automata.get_current_state().get_id(), '1')
        self.assertEqual(self.automata.get_current_state().get_prev_states()[0].get_id(), '0')

        '''
        for s in self.automata.get_states():
            print s
            for c in s.get_clickables():
                print c
                for f in c.get_forms():
                    print f
                    for _i in f.get_inputs():
                        print _i
        for (state_from, state_to, clickable, cost) in self.automata.get_edges():
            print state_from, state_to, clickable, cost
        '''

    def test_get_shortest_path(self):
        automata = Automata()
        state0 = State('state0')
        state1 = State('state1')
        state2 = State('state2')
        state3 = State('state3')
        state4 = State('state4')
        state5 = State('state5')
        state6 = State('state6')
        automata.add_state(state0)
        automata.add_state(state1)
        automata.add_state(state2)
        automata.add_state(state3)
        automata.add_state(state4)
        automata.add_state(state5)
        automata.add_state(state6)
        automata.add_edge(state0, state1, Clickable('0-1'))
        automata.add_edge(state0, state2, Clickable('0-2'))
        automata.add_edge(state0, state3, Clickable('0-3'))
        automata.add_edge(state2, state4, Clickable('2-4'))
        automata.add_edge(state4, state5, Clickable('4-5'))
        automata.add_edge(state3, state5, Clickable('3-5'))
        automata.add_edge(state3, state5, Clickable('5-0'))
        automata.add_edge(state5, state6, Clickable('5-6'))
        self.assertEqual(automata.get_shortest_path(state0), [])
        edges = automata.get_shortest_path(state6)
        # 0-3, 3-5, 5-6
        self.assertEqual([int(e[0].get_id()) for e in edges], [0, 3, 5])
        #for e in edges:
        #    print e[0].get_id(), e[1].get_id(), e[2].get_id()

    def test_load_save(self):
        automata = Automata(fname='test_data/automata-example.json')
        config = B2gConfiguration('test-app-name', 'test-app-id', mkdir=False)
        config.set_path('root', 'test_data')
        config.set_path('dom', 'test_data/dom')
        config.set_path('state', 'test_data/screenshot/state')
        config.set_path('clickable', 'test_data/screenshot/clickable')
        saved_file_path = automata.save(config)
        import filecmp
        self.assertTrue(filecmp.cmp('test_data/automata-example.json', 'test_data/automata.json'))
        try:
            os.remove(saved_file_path)
        except OSError:
            pass
Example #41
0
#/usr/bin/env python
# -*- cofing: utf-8 -*-
from automata import Automata
import os
fichero=raw_input("introduce nombre fichero configuracion en carpeta /test \n")
a=Automata("../test/"+fichero)
print "forma tabular del automata\n"
a.tabular("AFD")
a.grafo("AFD")
a.minimoConexo()
print "forma tabular del minimo conexo\n"
a.tabular("AFD-conexo")
a.grafo("AFD-conexo")
f=open("par-AFD.txt","w")
f.write("digraph{compound=true; \n")
f.close()
os.system("cat AFD-conf.txt >> par-AFD.txt")
f=open("par-AFD.txt","a")
f.write("\n")
f.close()
os.system("cat AFD-conexo-conf.txt >> par-AFD.txt")
f=open("par-AFD.txt","a")
f.write("\n}")
f.close()
os.system("dot par-AFD.txt -Tpng -o AFD-conexo.png")
os.system("eog AFD-conexo.png && rm par-AFD.txt && rm AFD-conf.txt && rm AFD-conexo-conf.txt")
Example #42
0
    print("\tactual_state:", fsm.actual_state)
    fsm.walk(letter)
    print("\tactual_state:", fsm.actual_state)


def detect_tst(fsm, word):
    print("Detect test: word %s" % word)
    print("\tactual_state:", fsm.actual_state)
    print("\tdetected? ", fsm.detect(word))
    print("\tactual_state:", fsm.actual_state)


"""
Tests:
"""
fsm = Automata(states, alphabet, transitions, start, accept)
fsm1 = copy.deepcopy(fsm)
print("Original states:")
print_tst(states, alphabet, transitions, start, accept)
# simple_tst(states, alphabet, transitions, start, accept)

print("Automata to next tests: ")
# fsm1.addtransition({'q0' : ['b', 'q3']})
print_fsm(fsm1)
letter = "b"
walk_tst(fsm1, letter)
word = "aababcc"
fsm1.actual_state = fsm1.states[0]
detect_tst(fsm1, word)
print("Now we know that automata is not deterministic, we need detereminize it!")
fsm_original = copy.deepcopy(fsm)
Example #43
0
def complemento(archivo_automata1, archivo_automata):
    automata_1 = Automata.crear_desde_archivo_automata(archivo_automata1)
    automata_1.complemento()
    #complemento.minimizar()

    automata_1.escribir_archivo(archivo_automata)
Example #44
0
 def test_get_shortest_path(self):
     automata = Automata()
     state0 = State('state0')
     state1 = State('state1')
     state2 = State('state2')
     state3 = State('state3')
     state4 = State('state4')
     state5 = State('state5')
     state6 = State('state6')
     automata.add_state(state0)
     automata.add_state(state1)
     automata.add_state(state2)
     automata.add_state(state3)
     automata.add_state(state4)
     automata.add_state(state5)
     automata.add_state(state6)
     automata.add_edge(state0, state1, Clickable('0-1'))
     automata.add_edge(state0, state2, Clickable('0-2'))
     automata.add_edge(state0, state3, Clickable('0-3'))
     automata.add_edge(state2, state4, Clickable('2-4'))
     automata.add_edge(state4, state5, Clickable('4-5'))
     automata.add_edge(state3, state5, Clickable('3-5'))
     automata.add_edge(state3, state5, Clickable('5-0'))
     automata.add_edge(state5, state6, Clickable('5-6'))
     self.assertEqual(automata.get_shortest_path(state0), [])
     edges = automata.get_shortest_path(state6)
     # 0-3, 3-5, 5-6
     self.assertEqual([int(e[0].get_id()) for e in edges], [0, 3, 5])
Example #45
0
class B2gCrawler(Crawler):
    def __init__(self, configuration, executor):
        self.automata = Automata()
        self.configuration = configuration
        self.executor = executor
        self.exe_stack = []  # stack of executed clickables (events)
        self.invariant_violation = []
        self.num_clickables = {
            'unexamined': 0,  # num of candidate clickables found with rules in DomAnalyzer
            'true':  0,  # num of clickables triggered new state (different screen dom)
            'false': 0,  # num of clickables not triggering new state
        }

    def run(self):
        self.executor.restart_app()
        initial_state = State(self.executor.get_source())
        self.automata.add_state(initial_state)
        self.save_screenshot(initial_state.get_id() + '.png', self.executor.get_screenshot(), 'state')
        self.save_dom(initial_state)
        self.crawl(1)
        self.invariant_violation = sorted(self.invariant_violation, key=lambda k: int(k['state']))
        return self.automata, self.invariant_violation, self.num_clickables

    def crawl(self, depth, prev_state=None):
        if depth <= self.configuration.get_max_depth():
            cs = self.automata.get_current_state()
            if not self.violate_invariant(cs.get_dom(), cs.get_id()):
                candidate_clickables = DomAnalyzer.get_clickables(cs.get_dom(), prev_state.get_dom() if prev_state else None)
                self.num_clickables['unexamined'] += len(candidate_clickables)
                for clickable in candidate_clickables:
                    # prefetch image of the clickable
                    time.sleep(0.2)  # time for correctly fetching image
                    img_name = cs.get_id() + '-' + clickable.get_id() + '.png'
                    img_data = self.executor.get_screenshot(clickable)

                    # fire the clickable
                    logger.debug('Fire event in state %s', cs.get_id())
                    self.executor.empty_form(clickable)
                    self.executor.fill_form(clickable)
                    ft = FireEventThread(self.executor, clickable)
                    ft.start()
                    ft.join(self.configuration.get_sleep_time()*2)  # time out after sleep_time*2 seconds
                    if ft.is_alive():  # timed out
                        logger.error('No response while firing an event. Execution sequences:')
                        self.exe_stack.append(clickable)  # add the clickable triggering No Response
                        for c in self.exe_stack:
                            logger.error(c)
                        logger.error('Total clickables found: %d (true: %d, false: %d, unexamined: %d)',
                                     self.num_clickables['unexamined'] + self.num_clickables['true'] + self.num_clickables['false'],
                                     self.num_clickables['true'],
                                     self.num_clickables['false'],
                                     self.num_clickables['unexamined']
                                     )
                        logger.error('Program terminated.')
                        sys.exit()
                    time.sleep(self.configuration.get_sleep_time())
                    self.num_clickables['unexamined'] -= 1

                    new_dom = self.executor.get_source()
                    if DomAnalyzer.is_equal(cs.get_dom(), new_dom):
                        self.num_clickables['false'] += 1
                    else:
                        self.num_clickables['true'] += 1
                        cs.add_clickable(clickable)
                        self.exe_stack.append(clickable)
                        self.save_screenshot(img_name, img_data, 'clickable')
                        ns, is_newly_added = self.automata.add_state(State(new_dom))
                        self.automata.add_edge(cs, ns, clickable)
                        if is_newly_added:
                            self.save_screenshot(ns.get_id() + '.png', self.executor.get_screenshot(), 'state')
                            self.save_dom(ns)
                            self.automata.change_state(ns)
                            self.crawl(depth+1, cs)
                        self.exe_stack.pop(-1)
                        self.automata.change_state(cs)
                        self.backtrack(cs)

    def backtrack(self, state):
        logger.debug('Backtrack to state %s', state.get_id())
        edges = self.automata.get_shortest_path(state)
        self.executor.restart_app()
        for (state_from, state_to, clickable, cost) in edges:
            time.sleep(self.configuration.get_sleep_time())
            self.executor.empty_form(clickable)
            self.executor.fill_form(clickable)
            self.executor.fire_event(clickable)

    def save_screenshot(self, fname, b64data, my_type):
        path = os.path.join(self.configuration.get_abs_path(my_type), fname)
        imgdata = base64.b64decode(b64data)
        with open(path, 'wb') as f:
            f.write(imgdata)

    def save_dom(self, state):
        with open(os.path.join(self.configuration.get_abs_path('dom'), state.get_id() + '.txt'), 'w') as f:
            f.write(state.get_dom())

    def violate_invariant(self, dom, statd_id):
        is_violated = False
        for inv in self.configuration.get_invariants():
            if inv.check(dom):
                is_violated = True
                violation = {
                    'state': statd_id,
                    'name': str(inv),
                    'sequence': list(self.exe_stack)  # shallow copy of clickables
                }
                self.invariant_violation.append(violation)
        return is_violated
Example #46
0
def equivalentes(archivo_automata1, archivo_automata2):
    A = Automata.crear_desde_archivo_automata(archivo_automata1)
    B = Automata.crear_desde_archivo_automata(archivo_automata2)
    print A.equivalente(B)