def reduce_nfa(aut, freq=None, ratio=.25, merge=True, th=.995, mf=.1): ''' Approximate NFA reduction. The reduction consists of pruning and merging based on packet frequency. Parameters ---------- aut : Nfa class the NFA to reduce freq : str, None PCAP filename, or file with packet frequencies, or None ratio : reduction ratio merge : use merging reduction before pruning th : merging threshold mf : maximal frequency merging parameter Returns ------- aut reduced NFA m the number of merged states ''' m = 0 if merge: cnt = aut.state_count #modified!!!! m = merging(aut, freq=freq, th=th, max_fr=mf) ratio = ratio * float(cnt) / (cnt - m) pruning(aut, ratio, freq=freq) return aut, m
def reduce_nfa(aut, freq=None, ratio=.2, merge=False, freq_pruning=False, th=.995, mf=.1): ''' Approximate NFA reduction. The reduction consists of pruning and merging based on packet frequency. Parameters ---------- aut : Nfa class the NFA to reduce freq : str, None PCAP filename, or file with packet frequencies, or None ratio : reduction ratio merge : use merging reduction before pruning freq_pruning: frequency based pruning method th : merging threshold mf : maximal frequency merging parameter Returns ------- aut reduced NFA m the number of merged states max_err in case of frequency pruning returns sum of frequencies of removed states, otherwise -1 (not computed) ''' max_err = -1 m = 0 if merge: cnt = aut.state_count #modified!!!! m = merging(aut, freq=freq, th=th, max_fr=mf) ratio = ratio * float(cnt) / (cnt - m) # use of new_pruning and merging was not tested, NOT RECOMMENDED if freq_pruning: if merge: sys.stderr.write( "Use of freq_pruning and merging was not tested, NOT RECOMMENDED." ) sys.exit() # call my new cool function :) naskle max_err = pruning_v2(aut, ratio, freq) else: # old pruning algorithm pruning(aut, ratio, freq=freq) return aut, m, max_err
def armc(aut, pcap, ratio=.25, th=.75, merge_empty=True): ''' NFA reduction based on merging similar sets of prefixes. Parameters ---------- aut : Nfa class the NFA to reduce pcap : str PCAP filename ratio : reduction ratio th : merging threshold merge_empty : if set, reduction merges states with empty sets together Returns ------- aut reduced NFA m the number of merged states ''' empty, eq = aut.get_armc_groups(pcap, th) mapping = {} # merge similar g = networkx.Graph(eq) for cluster in networkx.connected_component_subgraphs(g): l = list(cluster.nodes()) assert len(l) > 1 for i in l[1:]: mapping[i] = l[0] mapping.pop(aut._initial_state, None) m = len(mapping) if merge_empty: # merge states with empty sets of prefixes together fin = {s: f for f, ss in aut.fin_pred().items() for s in ss} mapping.update( {s: fin[s] for s in empty if not s in aut._final_states}) mapping.pop(aut._initial_state, None) aut.merge_states(mapping) else: aut.merge_states(mapping) freq = aut.get_freq(pcap) cnt = aut.state_count ratio = ratio * cnt / (cnt - m) pruning(aut, ratio, freq=freq) return aut, m