示例#1
0
    def _espresso_to_symset(cls, sym_text):
        '''
        this funmction receives a single line of the espresso output and returns a single symbolset of that
        :param sym_text: (string) a single line of the espresso output .p
        :return: (PackedIntervalSet, String) string represents the function output
        '''
        assert sym_text, "fix this"

        dims_solutions = sym_text.split()
        dims_solutions, out_str = dims_solutions[:-1], dims_solutions[-1]
        max_val_per_dim = len(dims_solutions[0]) - 1
        all_dims_result = []

        for d in range(len(dims_solutions)):
            dim_solution = dims_solutions[d]

            start_indexs = [0] if dim_solution[0] == '1' else []
            for zo in cls._to_one_transition_pattern.finditer(dim_solution):
                start_indexs.append(zo.start() + 1)

            end_indexes = []
            for oz in cls._to_zero_transition_pattern.finditer(dim_solution):
                end_indexes.append(oz.start())

            if dim_solution[-1] == '1':
                end_indexes.append(max_val_per_dim)

            assert len(start_indexs) == len(end_indexes)

            all_dims_result.append([
                (s, e) for s, e in zip(start_indexs, end_indexes)
            ])

        new_symbol_set = PackedIntervalSet([])

        for p in product(*all_dims_result):
            left = tuple(s for s, _ in p)
            right = tuple(e for _, e in p)

            left_pt = PackedInput(left)
            right_pt = PackedInput(right)
            pi = PackedInterval(left_pt, right_pt)
            new_symbol_set.add_interval(pi)
        return new_symbol_set, out_str
示例#2
0
文件: utility.py 项目: j-c-w/APSim
def _replace_equivalent_symbols(symbol_dictionary_list, atms_list, max_val):
    '''
    :param atms: list of auotmatas
    :param symbol_dictionary_list: a dictionary from nodes to set of 1D numbers
    :param max_val new max value
    :return: a list of automatas with replaces symbols
    '''


    for atm, sym_dic in zip(atms_list, symbol_dictionary_list):
        node_edge_iter = atm.nodes if atm.is_homogeneous else atm.get_edges()

        for ne in node_edge_iter:

            if atm.is_homogeneous and ne.type == ElementsType.FAKE_ROOT:
                continue
            sym_set = ne.symbols if atm.is_homogeneous else ne[2]['symbol_set']

            new_symbol_set = PackedIntervalSet([])

            sym_set.mutable = False
            ivls = get_interval(list(sym_dic[sym_set]))
            sym_set.mutable = True

            for l, r in ivls:
                left_pt = PackedInput((l,))
                right_pt = PackedInput((r,))
                new_symbol_set.add_interval(PackedInterval(left_pt, right_pt))

            new_symbol_set.prone()
            new_symbol_set.merge() # this is not necessary. TODO remove it

            if atm.is_homogeneous:
                ne.symbols = new_symbol_set
            else:
                ne[2]['symbol_set'] = new_symbol_set

        atm.stride_value = 1
        atm.max_val_dim = max_val
示例#3
0
from automata import Automatanetwork
from automata.elemnts.ste import S_T_E,StartType, PackedIntervalSet, PackedInterval, PackedInput
import networkx



env = Environment(loader=FileSystemLoader('Templates'), extensions=['jinja2.ext.do'])
# template = env.get_template('LUT_match.template')
#
# rendered_content = template.render(intervals=[[(1, 2), (3, 5)], [(44, 76), (78, 99)]])
#
# print rendered_content


atm = Automatanetwork(id='temp_automata', is_homogenous=True, stride=2, max_val=255)
symbol_set1 = PackedIntervalSet([PackedInterval(PackedInput((1,2)),PackedInput((3,5))),
                                 PackedInterval(PackedInput((44,76)),PackedInput((78,99)))])

ste1 = S_T_E(start_type=StartType.start_of_data, is_report=False, is_marked=False,
             id=atm.get_new_id(), symbol_set=symbol_set1, adjacent_S_T_E_s=None, report_residual=0,
             report_code=0
             )
atm.add_element(ste1)

symbol_set2 = PackedIntervalSet([PackedInterval(PackedInput((10,20)),PackedInput((30,50))),
                                 PackedInterval(PackedInput((4,76)),PackedInput((7,99)))])

ste2 = S_T_E(start_type=StartType.non_start, is_report=True, is_marked=False,
             id=atm.get_new_id(), symbol_set=symbol_set2, adjacent_S_T_E_s=None, report_residual=0,
             report_code=0
             )
atm.add_element(ste2)
示例#4
0
ste2 = S_T_E(start_type=StartType.non_start,
             is_report=False,
             is_marked=False,
             id=my_Automata.get_new_id(),
             symbol_set=get_Symbol_type(True)(
                 [PackedInterval(PackedInput((2, )), PackedInput((2, )))]),
             adjacent_S_T_E_s=None,
             report_residual=0,
             report_code=-1)

ste3 = S_T_E(start_type=StartType.non_start,
             is_report=False,
             is_marked=False,
             id=my_Automata.get_new_id(),
             symbol_set=PackedIntervalSet(
                 [PackedInterval(PackedInput((3, )), PackedInput((3, )))]),
             adjacent_S_T_E_s=None,
             report_residual=0,
             report_code=-1)

ste4 = S_T_E(start_type=StartType.non_start,
             is_report=False,
             is_marked=False,
             id=my_Automata.get_new_id(),
             symbol_set=get_Symbol_type(True)(
                 [PackedInterval(PackedInput((4, )), PackedInput((4, )))]),
             adjacent_S_T_E_s=None,
             report_residual=0,
             report_code=0)

ste5 = S_T_E(start_type=StartType.non_start,
示例#5
0
import subprocess
from jinja2 import Environment, FileSystemLoader
from automata.elemnts.ste import PackedIntervalSet, PackedInterval, PackedInput

from automata.Espresso.espresso import get_splitted_sym_sets

import re

symset = PackedIntervalSet([])

symset.add_interval(PackedInterval(PackedInput((5, 5)), PackedInput((8, 9))))
symset.add_interval(PackedInterval(PackedInput((4, 5)), PackedInput((7, 9))))
symset.add_interval(PackedInterval(PackedInput((8, 1)), PackedInput((9, 6))))

a, b = get_splitted_sym_sets(symset=symset, max_val=15)
print a, b
示例#6
0
def thread_func(ds):
    try:

        full_atm = atma.parse_anml_file(anml_path[ds])
        full_atm.remove_ors()
        atms_list = full_atm.get_connected_components_as_automatas()
        atms_list = atms_list[:atms_count]

        with open(str(ds) + '4bit.csv', 'w') as csv_file:
            csv_writer = csv.writer(csv_file,
                                    delimiter=',',
                                    quotechar='"',
                                    quoting=csv.QUOTE_MINIMAL)
            csv_writer.writerow(filed_names)
            for curr_atm in atms_list:
                b_atm = atma.automata_network.get_bit_automaton(
                    curr_atm, original_bit_width=curr_atm.max_val_dim_bits_len)
                four_b_atm = atma.automata_network.get_strided_automata2(
                    atm=b_atm,
                    stride_value=4,
                    is_scalar=True,
                    base_value=2,
                    add_residual=True)
                curr_atm = four_b_atm.get_single_stride_graph()
                curr_atm.make_homogenous()
                minimize_automata(curr_atm)
                curr_atm.fix_split_all()

                match_graph = nx.Graph()

                parent_sym_to_product_sym_dic = {}
                for ste in curr_atm.nodes:
                    if ste.is_fake:
                        continue  # fake root is not considered

                    match_graph.add_node(ste)

                    # here we find the union of parents and find the version with false posetive versions
                    comb_sym_set = PackedIntervalSet(
                        []
                    )  # create an empty symbol sets to find union of parents symbol set
                    for pred in curr_atm.get_predecessors(ste):
                        if pred.is_fake:
                            continue
                        for ivl in pred.symbols.intervals:
                            comb_sym_set.add_interval(
                                ivl
                            )  # finding the union of parents symbol sets

                    #  making symbol set smaller
                    comb_sym_set.prone()
                    comb_sym_set.merge()
                    comb_sym_set = comb_sym_set.get_combinatorial_symbol_set()
                    parent_sym_to_product_sym_dic[ste] = comb_sym_set

                    # now having a graph with no edge, we add edge between nodes if they can be combined with each other
                    # Two nodes can be combined if these two conditions are satisfied. First, they should not have a common symbol
                    # second, their parrents should also not have common symbols

                    for n in match_graph.nodes():
                        if n == ste:
                            continue  # we do not check a node with itself

                        if is_there_common_sym(ste.symbols, n.symbols):
                            continue  # first condition has not been met
                        elif is_there_common_sym(
                                parent_sym_to_product_sym_dic[ste],
                                parent_sym_to_product_sym_dic[n]):
                            continue  # second condition has not been met
                        else:  # both condiotions have been met, we can create an edge between ste, n
                            match_graph.add_edge(ste, n)

                matching_result = matching_alg.max_weight_matching(match_graph)

                print "number of orignal nodes ", curr_atm.nodes_count
                print "number of matching nodes ", 2 * len(matching_result)
                csv_writer.writerow(
                    [curr_atm.nodes_count, 2 * len(matching_result)])
    except Exception as ex:
        print traceback.print_exc()