| 1 | package FLAT::DFA; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use parent qw(FLAT::NFA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use FLAT::DFA::Minimal; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 292 | ▶ | sub set_starting { | ||||||||||||||||||||||||||
|
Subroutine
set_starting: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 292 | my $self = shift; | |||||||||||||||||||||||||||
| 12 | 292 | $self->SUPER::set_starting(@_); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 292 | my $num = () = $self->get_starting; | |||||||||||||||||||||||||||
| 15 | 292 | ▶ | confess "DFA must have exactly one starting state" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | if $num != 1; | ||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 6 | ▶ | sub complement { | ||||||||||||||||||||||||||
|
Subroutine
complement: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 6 | my $self = $_[0]->clone; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 6 | for my $s ( $self->get_states ) { | |||||||||||||||||||||||||||
| 23 | 80 | ▶ | $self->is_accepting($s) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | ? $self->unset_accepting($s) | ||||||||||||||||||||||||||||
| 25 | : $self->set_accepting($s); | ||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 6 | return $self; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1520 | ▶ | sub _TUPLE_ID { join "\0", @_ } | ||||||||||||||||||||||||||
|
Subroutine
_TUPLE_ID: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 6 | ▶ | sub _uniq { | ||||||||||||||||||||||||||
|
Subroutine
_uniq: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 6 | my %seen; | |||||||||||||||||||||||||||
| 35 | 6 | grep { !$seen{$_}++ } @_; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | ## this method still needs more work.. | ||||||||||||||||||||||||||||
| 39 | 6 | ▶ | sub intersect { | ||||||||||||||||||||||||||
|
Subroutine
intersect: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 6 | my @dfas = map { $_->as_dfa } @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 6 | my $return = FLAT::DFA->new; | |||||||||||||||||||||||||||
| 43 | 6 | my %newstates; | |||||||||||||||||||||||||||
| 44 | 6 | my @alpha = _uniq( map { $_->alphabet } @dfas ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 6 | $_->_extend_alphabet(@alpha) for @dfas; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 6 | my @start = map { $_->get_starting } @dfas; | |||||||||||||||||||||||||||
| 49 | 6 | my $start = $newstates{ _TUPLE_ID(@start) } = $return->add_states(1); | |||||||||||||||||||||||||||
| 50 | 6 | $return->set_starting($start); | |||||||||||||||||||||||||||
| 51 | $return->set_accepting($start) | ||||||||||||||||||||||||||||
| 52 | 6 | ▶ | if !grep { !$dfas[$_]->is_accepting( $start[$_] ) } 0 .. $#dfas; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 6 | my @queue = ( \@start ); | |||||||||||||||||||||||||||
| 55 | 6 | ▶ | while (@queue) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 80 | my @tuple = @{ shift @queue }; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 80 | for my $char (@alpha) { | |||||||||||||||||||||||||||
| 59 | 480 | my @next = map { $dfas[$_]->successors( $tuple[$_], $char ) } 0 .. $#dfas; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | #warn "[@tuple] --> [@next] via $char\n"; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 480 | ▶ | if ( not exists $newstates{ _TUPLE_ID(@next) } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 74 | my $s = $newstates{ _TUPLE_ID(@next) } = $return->add_states(1); | |||||||||||||||||||||||||||
| 65 | $return->set_accepting($s) | ||||||||||||||||||||||||||||
| 66 | 74 | ▶ | if !grep { !$dfas[$_]->is_accepting( $next[$_] ) } 0 .. $#dfas; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 74 | push @queue, \@next; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 480 | $return->add_transition( $newstates{ _TUPLE_ID(@tuple) }, $newstates{ _TUPLE_ID(@next) }, $char ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 6 | return $return; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # this is meant to enforce 1 starting state for a DFA, but it is getting us into trouble | ||||||||||||||||||||||||||||
| 78 | # when a DFA object calls unset_starting | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub unset_starting { | ||||||||||||||||||||||||||
|
Subroutine
unset_starting: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 81 | 0 | $self->SUPER::unset_starting(@_); | |||||||||||||||||||||||||||
| 82 | 0 | my $num = () = $self->unset_starting; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | croak "DFA must have exactly one starting state" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | if $num != 1; | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | #### transformations | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 125 | ▶ | sub trim_sinks { | ||||||||||||||||||||||||||
|
Subroutine
trim_sinks: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 125 | my $self = shift; | |||||||||||||||||||||||||||
| 91 | 125 | my $result = $self->clone(); | |||||||||||||||||||||||||||
| 92 | 125 | foreach my $state ( $self->array_complement( [ $self->get_states() ], [ $self->get_accepting() ] ) ) { | |||||||||||||||||||||||||||
| 93 | 575 | my @ret = $self->successors( $state, [ $self->alphabet ] ); | |||||||||||||||||||||||||||
| 94 | 575 | ▶ | if (@ret) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 575 | ▶ | if ( $ret[0] == $state ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 130 | ▶ | $result->delete_states($state) if ( $result->is_state($state) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 125 | return $result; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # classical DFA minimization using equivalence classes | ||||||||||||||||||||||||||||
| 104 | 137 | ▶ | sub as_min_dfa { | ||||||||||||||||||||||||||
|
Subroutine
as_min_dfa: called CC=13 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 137 | my $self = shift()->clone; | |||||||||||||||||||||||||||
| 106 | 137 | my $N = $self->num_states; | |||||||||||||||||||||||||||
| 107 | 137 | my @alphabet = $self->alphabet; | |||||||||||||||||||||||||||
| 108 | 137 | my ($start) = $self->get_starting; | |||||||||||||||||||||||||||
| 109 | 137 | my %final = map { $_ => 1 } $self->get_accepting; | |||||||||||||||||||||||||||
| 110 | 137 | my @equiv = map [ (0) x ( $_ + 1 ), (1) x ( $N - $_ - 1 ) ], 0 .. $N - 1; | |||||||||||||||||||||||||||
| 111 | 137 | while (1) { | |||||||||||||||||||||||||||
| 112 | 639 | my $changed = 0; | |||||||||||||||||||||||||||
| 113 | 639 | for my $s1 ( 0 .. $N - 1 ) { | |||||||||||||||||||||||||||
| 114 | 5309 | for my $s2 ( grep { $equiv[$s1][$_] } 0 .. $N - 1 ) { | |||||||||||||||||||||||||||
| 115 | 14974 | ▶ | if ( 1 == grep defined, @final{ $s1, $s2 } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 2189 | $changed = 1; | |||||||||||||||||||||||||||
| 117 | 2189 | $equiv[$s1][$s2] = 0; | |||||||||||||||||||||||||||
| 118 | 2189 | next; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 12785 | for my $char (@alphabet) { | |||||||||||||||||||||||||||
| 121 | 67508 | my @t = sort { $a <=> $b } $self->successors( [ $s1, $s2 ], $char ); | |||||||||||||||||||||||||||
| 122 | 67508 | ▶ | next if @t == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 30951 | ▶ | if ( not $equiv[ $t[0] ][ $t[1] ] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 6478 | $changed = 1; | |||||||||||||||||||||||||||
| 126 | 6478 | $equiv[$s1][$s2] = 0; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 639 | ▶ | last if !$changed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 137 | my $result = FLAT::DFA::Minimal->new; | |||||||||||||||||||||||||||
| 134 | 137 | my %newstate; | |||||||||||||||||||||||||||
| 135 | my @classes; | ||||||||||||||||||||||||||||
| 136 | 137 | for my $s ( 0 .. $N - 1 ) { | |||||||||||||||||||||||||||
| 137 | 1011 | ▶ | next if exists $newstate{$s}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 798 | my @c = ( $s, grep { $equiv[$s][$_] } 0 .. $N - 1 ); | |||||||||||||||||||||||||||
| 140 | 798 | push @classes, \@c; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 798 | @newstate{@c} = ( $result->add_states(1) ) x @c; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 137 | for my $c (@classes) { | |||||||||||||||||||||||||||
| 145 | 798 | my $s = $c->[0]; | |||||||||||||||||||||||||||
| 146 | 798 | for my $char (@alphabet) { | |||||||||||||||||||||||||||
| 147 | 2970 | my ($next) = $self->successors( $s, $char ); | |||||||||||||||||||||||||||
| 148 | 2970 | $result->add_transition( $newstate{$s}, $newstate{$next}, $char ); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 137 | $result->set_starting( $newstate{$start} ); | |||||||||||||||||||||||||||
| 152 | 137 | $result->set_accepting( $newstate{$_} ) for $self->get_accepting; | |||||||||||||||||||||||||||
| 153 | 137 | $result->set_equivalence_classes( \@classes ); | |||||||||||||||||||||||||||
| 154 | 137 | return $result; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # the validity of a given string <-- executes symbols over DFA | ||||||||||||||||||||||||||||
| 158 | # if there is not transition for given state and symbol, it fails immediately | ||||||||||||||||||||||||||||
| 159 | # if the current state we're in is not final when symbols are exhausted, then it fails | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 1727 | ▶ | sub is_valid_string { | ||||||||||||||||||||||||||
|
Subroutine
is_valid_string: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 1727 | my $self = shift; | |||||||||||||||||||||||||||
| 163 | 1727 | my $string = shift; | |||||||||||||||||||||||||||
| 164 | 1727 | chomp $string; | |||||||||||||||||||||||||||
| 165 | 1727 | my $OK = undef; | |||||||||||||||||||||||||||
| 166 | 1727 | my @stack = split( '', $string ); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # this is confusing all funcs return arrays | ||||||||||||||||||||||||||||
| 169 | 1727 | my @current = $self->get_starting(); | |||||||||||||||||||||||||||
| 170 | 1727 | my $current = pop @current; | |||||||||||||||||||||||||||
| 171 | 1727 | foreach (@stack) { | |||||||||||||||||||||||||||
| 172 | 11214 | my @next = $self->successors( $current, $_ ); | |||||||||||||||||||||||||||
| 173 | 11214 | ▶ | if ( !@next ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | return $OK; #<--returns undef bc no transition found | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 11214 | $current = $next[0]; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 1727 | ▶ | $OK++ if ( $self->is_accepting($current) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 1727 | return $OK; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # Need to implement! http://www.ianab.com/hyper/ | ||||||||||||||||||||||||||||
| 183 | # DFA hyper-minimization using equivalence classes | ||||||||||||||||||||||||||||
| 184 | 0 | ▶ | sub as_hyper_min_dfa { | ||||||||||||||||||||||||||
|
Subroutine
as_hyper_min_dfa: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 185 | 0 | my $self = shift()->clone; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | # DFT stuff in preparation for DFA pump stuff; | ||||||||||||||||||||||||||||
| 189 | 250 | ▶ | sub as_node_list { | ||||||||||||||||||||||||||
|
Subroutine
as_node_list: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 190 | 250 | my $self = shift; | |||||||||||||||||||||||||||
| 191 | 250 | my %node = (); | |||||||||||||||||||||||||||
| 192 | 250 | for my $s1 ( $self->get_states ) { | |||||||||||||||||||||||||||
| 193 | 1148 | $node{$s1} = {}; # initialize | |||||||||||||||||||||||||||
| 194 | 1148 | for my $s2 ( $self->get_states ) { | |||||||||||||||||||||||||||
| 195 | 6416 | my $t = $self->get_transition( $s1, $s2 ); | |||||||||||||||||||||||||||
| 196 | 6416 | ▶ | if ( defined $t ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | # array of symbols that $s1 will go to $s2 on... | ||||||||||||||||||||||||||||
| 199 | 1246 | push( @{ $node{$s1}{$s2} }, split( ',', $t->as_string ) ); | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 250 | return %node; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | sub as_acyclic_strings { | ||||||||||||||||||||||||||
|
Subroutine
as_acyclic_strings: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 207 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 208 | 0 | my %dflabel = (); # lookup table for dflable | |||||||||||||||||||||||||||
| 209 | 0 | my %backtracked = (); # lookup table for backtracked edges | |||||||||||||||||||||||||||
| 210 | 0 | my $lastDFLabel = 0; | |||||||||||||||||||||||||||
| 211 | 0 | my @string = (); | |||||||||||||||||||||||||||
| 212 | 0 | my %nodes = $self->as_node_list(); | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | # output format is the actual PRE followed by all found strings | ||||||||||||||||||||||||||||
| 215 | 0 | $self->acyclic( $self->get_starting(), \%dflabel, $lastDFLabel, \%nodes, \@string ); | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 0 | ▶ | sub acyclic { | ||||||||||||||||||||||||||
|
Subroutine
acyclic: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 219 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 220 | 0 | my $startNode = shift; | |||||||||||||||||||||||||||
| 221 | 0 | my $dflabel_ref = shift; | |||||||||||||||||||||||||||
| 222 | 0 | my $lastDFLabel = shift; | |||||||||||||||||||||||||||
| 223 | 0 | my $nodes = shift; | |||||||||||||||||||||||||||
| 224 | 0 | my $string = shift; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # tree edge detection | ||||||||||||||||||||||||||||
| 227 | 0 | ▶ | if ( !exists( $dflabel_ref->{$startNode} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 0 | $dflabel_ref->{$startNode} = ++$lastDFLabel; # the order inwhich this link was explored | |||||||||||||||||||||||||||
| 229 | 0 | foreach my $adjacent ( keys( %{ $nodes->{$startNode} } ) ) { | |||||||||||||||||||||||||||
| 230 | 0 | ▶ | if ( !exists( $dflabel_ref->{$adjacent} ) ) { # initial tree edge | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 0 | foreach my $symbol ( @{ $nodes->{$startNode}{$adjacent} } ) { | |||||||||||||||||||||||||||
| 232 | 0 | push( @{$string}, $symbol ); | |||||||||||||||||||||||||||
| 233 | 0 | $self->acyclic( $adjacent, \%{$dflabel_ref}, $lastDFLabel, \%{$nodes}, \@{$string} ); | |||||||||||||||||||||||||||
| 234 | 0 | ▶ | if ( $self->array_is_subset( [$adjacent], [ $self->get_accepting() ] ) ) { #< proof of concept | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 0 | printf( "%s\n", join( '', @{$string} ) ); | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | 0 | pop( @{$string} ); | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # remove startNode entry to facilitate acyclic path determination | ||||||||||||||||||||||||||||
| 244 | 0 | delete( $dflabel_ref->{$startNode} ); | |||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | #$lastDFLabel--; | ||||||||||||||||||||||||||||
| 247 | 0 | return; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 0 | ▶ | sub as_dft_strings { | ||||||||||||||||||||||||||
|
Subroutine
as_dft_strings: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 251 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 252 | 0 | my $depth = 1; | |||||||||||||||||||||||||||
| 253 | 0 | ▶ | $depth = shift if ( 1 < $_[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 0 | my %dflabel = (); # scoped lookup table for dflable | |||||||||||||||||||||||||||
| 255 | 0 | my %nodes = $self->as_node_list(); | |||||||||||||||||||||||||||
| 256 | 0 | foreach ( keys(%nodes) ) { | |||||||||||||||||||||||||||
| 257 | 0 | $dflabel{$_} = []; # initialize container (array) for multiple dflables for each node | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | 0 | my $lastDFLabel = 0; | |||||||||||||||||||||||||||
| 260 | 0 | my @string = (); | |||||||||||||||||||||||||||
| 261 | 0 | $self->dft( $self->get_starting(), [ $self->get_accepting() ], \%dflabel, $lastDFLabel, \%nodes, \@string, $depth ); | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | ▶ | sub dft { | ||||||||||||||||||||||||||
|
Subroutine
dft: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 265 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 266 | 0 | my $startNode = shift; | |||||||||||||||||||||||||||
| 267 | 0 | my $goals_ref = shift; | |||||||||||||||||||||||||||
| 268 | 0 | my $dflabel_ref = shift; | |||||||||||||||||||||||||||
| 269 | 0 | my $lastDFLabel = shift; | |||||||||||||||||||||||||||
| 270 | 0 | my $nodes = shift; | |||||||||||||||||||||||||||
| 271 | 0 | my $string = shift; | |||||||||||||||||||||||||||
| 272 | 0 | my $DEPTH = shift; | |||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # add start node to path | ||||||||||||||||||||||||||||
| 275 | 0 | my $c1 = @{ $dflabel_ref->{$startNode} }; # get number of elements | |||||||||||||||||||||||||||
| 276 | 0 | ▶ | if ( $DEPTH >= $c1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 0 | push( @{ $dflabel_ref->{$startNode} }, ++$lastDFLabel ); | |||||||||||||||||||||||||||
| 278 | 0 | foreach my $adjacent ( keys( %{ $nodes->{$startNode} } ) ) { | |||||||||||||||||||||||||||
| 279 | 0 | my $c2 = @{ $dflabel_ref->{$adjacent} }; | |||||||||||||||||||||||||||
| 280 | 0 | ▶ | if ( $DEPTH > $c2 ) { # "initial" tree edge | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 0 | foreach my $symbol ( @{ $nodes->{$startNode}{$adjacent} } ) { | |||||||||||||||||||||||||||
| 282 | 0 | push( @{$string}, $symbol ); | |||||||||||||||||||||||||||
| 283 | 0 | $self->dft( $adjacent, [ @{$goals_ref} ], $dflabel_ref, $lastDFLabel, $nodes, [ @{$string} ], $DEPTH ); | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | # assumes some base path found | ||||||||||||||||||||||||||||
| 286 | 0 | ▶ | if ( $self->array_is_subset( [$adjacent], [ @{$goals_ref} ] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 0 | printf( "%s\n", join( '', @{$string} ) ); | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | 0 | pop( @{$string} ); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | } # remove startNode entry to facilitate acyclic path determination | ||||||||||||||||||||||||||||
| 293 | 0 | pop( @{ $dflabel_ref->{$startNode} } ); | |||||||||||||||||||||||||||
| 294 | 0 | $lastDFLabel--; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | # | ||||||||||||||||||||||||||||
| 299 | # String gen using iterators, supports delimter; default is q{} | ||||||||||||||||||||||||||||
| 300 | # | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 849 | ▶ | sub get_acyclic_sub { | ||||||||||||||||||||||||||
|
Subroutine
get_acyclic_sub: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 303 | 849 | my $self = shift; | |||||||||||||||||||||||||||
| 304 | 849 | my ( $start, $nodelist_ref, $dflabel_ref, $string_ref, $accepting_ref, $lastDFLabel, $delimiter ) = @_; | |||||||||||||||||||||||||||
| 305 | 849 | ▶ | $delimiter //= q{}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$delimiter //= ""
MC/DC100%1/1
$delimiter //= ""
$delimiter
Truth table100%2/2
A
A= $delimiter
| |||||||||||||||||||||||||||||
| 306 | 849 | my @ret = (); | |||||||||||||||||||||||||||
| 307 | 849 | foreach my $adjacent ( keys( %{ $nodelist_ref->{$start} } ) ) { | |||||||||||||||||||||||||||
| 308 | 798 | $lastDFLabel++; | |||||||||||||||||||||||||||
| 309 | 798 | ▶ | if ( !exists( $dflabel_ref->{$adjacent} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 707 | $dflabel_ref->{$adjacent} = $lastDFLabel; | |||||||||||||||||||||||||||
| 311 | 707 | foreach my $symbol ( @{ $nodelist_ref->{$start}{$adjacent} } ) { | |||||||||||||||||||||||||||
| 312 | 724 | push( @{$string_ref}, $symbol ); | |||||||||||||||||||||||||||
| 313 | 724 | my $string_clone = dclone($string_ref); | |||||||||||||||||||||||||||
| 314 | 724 | my $dflabel_clone = dclone($dflabel_ref); | |||||||||||||||||||||||||||
| 315 | 724 | ▶ | push( @ret, sub { return $self->get_acyclic_sub( $adjacent, $nodelist_ref, $dflabel_clone, $string_clone, $accepting_ref, $lastDFLabel, $delimiter ); }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 316 | 724 | pop @{$string_ref}; | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | # returns a complex data structure in the form of a hash reference | ||||||||||||||||||||||||||||
| 323 | return { | ||||||||||||||||||||||||||||
| 324 | substack => [@ret], | ||||||||||||||||||||||||||||
| 325 | lastDFLabel => $lastDFLabel, | ||||||||||||||||||||||||||||
| 326 | 849 | ▶ | string => ( $self->array_is_subset( [$start], $accepting_ref ) ? join( $delimiter, @{$string_ref} ) : undef ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | }; | ||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 125 | ▶ | sub init_acyclic_iterator { | ||||||||||||||||||||||||||
|
Subroutine
init_acyclic_iterator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 331 | 125 | my ($self, $delimiter) = @_; | |||||||||||||||||||||||||||
| 332 | 125 | my %dflabel = (); | |||||||||||||||||||||||||||
| 333 | 125 | my @string = (); | |||||||||||||||||||||||||||
| 334 | 125 | my $lastDFLabel = 0; | |||||||||||||||||||||||||||
| 335 | 125 | my %nodelist = $self->as_node_list(); | |||||||||||||||||||||||||||
| 336 | 125 | my @accepting = $self->get_accepting(); | |||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | # initialize | ||||||||||||||||||||||||||||
| 339 | 125 | my @substack = (); | |||||||||||||||||||||||||||
| 340 | 125 | my $r = $self->get_acyclic_sub( $self->get_starting(), \%nodelist, \%dflabel, \@string, \@accepting, $lastDFLabel, $delimiter ); | |||||||||||||||||||||||||||
| 341 | 125 | push( @substack, @{ $r->{substack} } ); | |||||||||||||||||||||||||||
| 342 | 382 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 343 | 382 | while (1) { | |||||||||||||||||||||||||||
| 344 | 849 | ▶ | if ( !@substack ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 125 | return undef; | |||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | 724 | my $s = pop @substack; | |||||||||||||||||||||||||||
| 348 | 724 | my $r = $s->(); | |||||||||||||||||||||||||||
| 349 | 724 | push( @substack, @{ $r->{substack} } ); | |||||||||||||||||||||||||||
| 350 | 724 | ▶ | if ( $r->{string} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 257 | return $r->{string}; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | 125 | } | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 125 | ▶ | sub new_acyclic_string_generator { | ||||||||||||||||||||||||||
|
Subroutine
new_acyclic_string_generator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 358 | 125 | my ($self,$delim) = @_; | |||||||||||||||||||||||||||
| 359 | 125 | my $iterator = $self->init_acyclic_iterator($delim); | |||||||||||||||||||||||||||
| 360 | 125 | return $iterator; | |||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 2338 | ▶ | sub get_deepdft_sub { | ||||||||||||||||||||||||||
|
Subroutine
get_deepdft_sub: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 364 | 2338 | my $self = shift; | |||||||||||||||||||||||||||
| 365 | 2338 | my ( $start, $nodelist_ref, $dflabel_ref, $string_ref, $accepting_ref, $lastDFLabel, $MAXLEVEL, $delimiter ) = @_; | |||||||||||||||||||||||||||
| 366 | 2338 | my @ret = (); | |||||||||||||||||||||||||||
| 367 | 2338 | ▶ | my $c1 = ( ref $dflabel_ref eq 'HASH' ) ? @{ $dflabel_ref->{$start} } : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | 2338 | ▶ | if ( $MAXLEVEL and $c1 < $MAXLEVEL ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$MAXLEVEL and $c1 < $MAXLEVEL
MC/DC50%1/2
$MAXLEVEL and $c1 < $MAXLEVEL
$MAXLEVEL
$c1 < $MAXLEVEL
Truth table66%2/3
A and B
A= $MAXLEVELB= $c1 < $MAXLEVEL
| |||||||||||||||||||||||||||||
| 369 | 2337 | push( @{ $dflabel_ref->{$start} }, ++$lastDFLabel ); | |||||||||||||||||||||||||||
| 370 | 2337 | foreach my $adjacent ( keys( %{ $nodelist_ref->{$start} } ) ) { | |||||||||||||||||||||||||||
| 371 | 2294 | my $c2 = @{ $dflabel_ref->{$adjacent} }; | |||||||||||||||||||||||||||
| 372 | 2294 | ▶ | if ( $c2 < $MAXLEVEL ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 1554 | foreach my $symbol ( @{ $nodelist_ref->{$start}{$adjacent} } ) { | |||||||||||||||||||||||||||
| 374 | 2213 | push( @{$string_ref}, $symbol ); | |||||||||||||||||||||||||||
| 375 | 2213 | my $string_clone = dclone($string_ref); | |||||||||||||||||||||||||||
| 376 | 2213 | my $dflabel_clone = dclone($dflabel_ref); | |||||||||||||||||||||||||||
| 377 | push( | ||||||||||||||||||||||||||||
| 378 | @ret, | ||||||||||||||||||||||||||||
| 379 | 2213 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 380 | 2213 | return $self->get_deepdft_sub( $adjacent, $nodelist_ref, $dflabel_clone, $string_clone, $accepting_ref, $lastDFLabel, $MAXLEVEL, $delimiter ); | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | 2213 | ); | |||||||||||||||||||||||||||
| 383 | 2213 | pop @{$string_ref}; | |||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | return { | ||||||||||||||||||||||||||||
| 389 | substack => [@ret], | ||||||||||||||||||||||||||||
| 390 | lastDFLabel => $lastDFLabel, | ||||||||||||||||||||||||||||
| 391 | 2338 | ▶ | string => ( $self->array_is_subset( [$start], $accepting_ref ) ? join( $delimiter, @{$string_ref} ) : undef ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | }; | ||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 125 | ▶ | sub init_deepdft_iterator { | ||||||||||||||||||||||||||
|
Subroutine
init_deepdft_iterator: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 396 | 125 | my ($self, $MAXLEVEL, $delimiter) = @_; | |||||||||||||||||||||||||||
| 397 | 125 | ▶ | $MAXLEVEL //= 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$MAXLEVEL //= 1
MC/DC0%0/1
$MAXLEVEL //= 1
$MAXLEVEL
Truth table50%1/2
A
A= $MAXLEVEL
| |||||||||||||||||||||||||||||
| 398 | 125 | ▶ | $delimiter //= q{}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$delimiter //= ""
MC/DC0%0/1
$delimiter //= ""
$delimiter
Truth table50%1/2
A
A= $delimiter
| |||||||||||||||||||||||||||||
| 399 | 125 | my %dflabel = (); | |||||||||||||||||||||||||||
| 400 | 125 | my @string = (); | |||||||||||||||||||||||||||
| 401 | 125 | my $lastDFLabel = 0; | |||||||||||||||||||||||||||
| 402 | 125 | my %nodelist = $self->as_node_list(); | |||||||||||||||||||||||||||
| 403 | 125 | foreach my $node ( keys(%nodelist) ) { | |||||||||||||||||||||||||||
| 404 | 574 | $dflabel{$node} = []; # initializes anonymous arrays for all nodes | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | 125 | my @accepting = $self->get_accepting(); | |||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | # initialize | ||||||||||||||||||||||||||||
| 409 | 125 | my @substack = (); | |||||||||||||||||||||||||||
| 410 | 125 | my $r = $self->get_deepdft_sub( $self->get_starting(), \%nodelist, \%dflabel, \@string, \@accepting, $lastDFLabel, $MAXLEVEL, $delimiter ); | |||||||||||||||||||||||||||
| 411 | 125 | push( @substack, @{ $r->{substack} } ); | |||||||||||||||||||||||||||
| 412 | 1595 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 413 | 1595 | while (1) { | |||||||||||||||||||||||||||
| 414 | 2338 | ▶ | if ( !@substack ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 415 | 125 | return undef; | |||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||
| 417 | 2213 | my $s = pop @substack; | |||||||||||||||||||||||||||
| 418 | 2213 | my $r = $s->(); | |||||||||||||||||||||||||||
| 419 | 2213 | push( @substack, @{ $r->{substack} } ); | |||||||||||||||||||||||||||
| 420 | 2213 | ▶ | if ( $r->{string} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | 1470 | return $r->{string}; | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | 125 | } | |||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 125 | ▶ | sub new_deepdft_string_generator { | ||||||||||||||||||||||||||
|
Subroutine
new_deepdft_string_generator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 428 | 125 | my ($self, $MAXLEVEL, $delimiter) = @_; | |||||||||||||||||||||||||||
| 429 | 125 | return $self->init_deepdft_iterator($MAXLEVEL, $delimiter); | |||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | 6 | 1; | |||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | __END__ | ||||||||||||||||||||||||||||