| 1 | package FLAT::NFA; | ||||||||||||||
| 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::FA); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 6 | ▶ | use FLAT::Transition::Simple; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 6 | ▶ | use FLAT::Symbol::Regex; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | =head1 NAME | ||||||||||||||
| 11 | |||||||||||||||
| 12 | FLAT::NFA - Nondeterministic finite automata | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||
| 15 | |||||||||||||||
| 16 | A FLAT::NFA object is a finite automata whose transitions are labeled | ||||||||||||||
| 17 | either with characters or the empty string (epsilon). | ||||||||||||||
| 18 | |||||||||||||||
| 19 | =cut | ||||||||||||||
| 20 | |||||||||||||||
| 21 | 975 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 22 | 975 | my $pkg = shift; | |||||||||||||
| 23 | 975 | my $self = $pkg->SUPER::new(@_); | |||||||||||||
| 24 | 975 | $self->{TRANS_CLASS} = "FLAT::Transition::Simple"; | |||||||||||||
| 25 | 975 | $self->{ALPHA_CLASS} = "FLAT::Symbol::Regex"; | |||||||||||||
| 26 | 975 | return $self; | |||||||||||||
| 27 | } | ||||||||||||||
| 28 | |||||||||||||||
| 29 | 0 | ▶ | sub singleton { | ||||||||||||
|
Subroutine
singleton: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 30 | 0 | my ( $class, $char ) = @_; | |||||||||||||
| 31 | 0 | my $nfa = $class->new; | |||||||||||||
| 32 | |||||||||||||||
| 33 | 0 | ▶ | if ( not defined $char ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 34 | 0 | $nfa->add_states(1); | |||||||||||||
| 35 | 0 | $nfa->set_starting(0); | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | elsif ( $char eq "" ) { | ||||||||||||||
| 38 | 0 | $nfa->add_states(1); | |||||||||||||
| 39 | 0 | $nfa->set_starting(0); | |||||||||||||
| 40 | 0 | $nfa->set_accepting(0); | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | else { | ||||||||||||||
| 43 | 0 | $nfa->add_states(2); | |||||||||||||
| 44 | 0 | $nfa->set_starting(0); | |||||||||||||
| 45 | 0 | $nfa->set_accepting(1); | |||||||||||||
| 46 | 0 | $nfa->set_transition( 0, 1, $char ); | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | 0 | return $nfa; | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 6 | ▶ | sub as_nfa { $_[0]->clone } | ||||||||||||
|
Subroutine
as_nfa: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 3 | ▶ | sub union { | ||||||||||||
|
Subroutine
union: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 54 | 3 | my @nfas = map { $_->as_nfa } @_; | |||||||||||||
| 55 | 3 | my $result = $nfas[0]->clone; | |||||||||||||
| 56 | 3 | $result->_swallow($_) for @nfas[ 1 .. $#nfas ]; | |||||||||||||
| 57 | 3 | $result; | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 0 | ▶ | sub concat { | ||||||||||||
|
Subroutine
concat: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 61 | 0 | my @nfas = map { $_->as_nfa } @_; | |||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | my $result = $nfas[0]->clone; | |||||||||||||
| 64 | 0 | my @newstate = ( [ $result->get_states ] ); | |||||||||||||
| 65 | 0 | my @start = $result->get_starting; | |||||||||||||
| 66 | |||||||||||||||
| 67 | 0 | for ( 1 .. $#nfas ) { | |||||||||||||
| 68 | 0 | push @newstate, [ $result->_swallow( $nfas[$_] ) ]; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 0 | $result->unset_accepting( $result->get_states ); | |||||||||||||
| 72 | 0 | $result->unset_starting( $result->get_states ); | |||||||||||||
| 73 | 0 | $result->set_starting(@start); | |||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | for my $nfa_id ( 1 .. $#nfas ) { | |||||||||||||
| 76 | 0 | for my $s1 ( $nfas[ $nfa_id - 1 ]->get_accepting ) { | |||||||||||||
| 77 | 0 | for my $s2 ( $nfas[$nfa_id]->get_starting ) { | |||||||||||||
| 78 | 0 | $result->set_transition( $newstate[ $nfa_id - 1 ][$s1], $newstate[$nfa_id][$s2], "" ); | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | } | ||||||||||||||
| 81 | } | ||||||||||||||
| 82 | |||||||||||||||
| 83 | 0 | $result->set_accepting( @{ $newstate[-1] }[ $nfas[-1]->get_accepting ] ); | |||||||||||||
| 84 | |||||||||||||||
| 85 | 0 | $result; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 0 | ▶ | sub kleene { | ||||||||||||
|
Subroutine
kleene: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 89 | 0 | my $result = $_[0]->clone; | |||||||||||||
| 90 | |||||||||||||||
| 91 | 0 | my ( $newstart, $newfinal ) = $result->add_states(2); | |||||||||||||
| 92 | |||||||||||||||
| 93 | 0 | $result->set_transition( $newstart, $_, "" ) for $result->get_starting; | |||||||||||||
| 94 | 0 | $result->unset_starting( $result->get_starting ); | |||||||||||||
| 95 | 0 | $result->set_starting($newstart); | |||||||||||||
| 96 | |||||||||||||||
| 97 | 0 | $result->set_transition( $_, $newfinal, "" ) for $result->get_accepting; | |||||||||||||
| 98 | 0 | $result->unset_accepting( $result->get_accepting ); | |||||||||||||
| 99 | 0 | $result->set_accepting($newfinal); | |||||||||||||
| 100 | |||||||||||||||
| 101 | 0 | $result->set_transition( $newstart, $newfinal, "" ); | |||||||||||||
| 102 | 0 | $result->set_transition( $newfinal, $newstart, "" ); | |||||||||||||
| 103 | |||||||||||||||
| 104 | 0 | $result; | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 608 | ▶ | sub reverse { | ||||||||||||
|
Subroutine
reverse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 108 | 608 | my $self = $_[0]->clone; | |||||||||||||
| 109 | 608 | $self->_transpose; | |||||||||||||
| 110 | |||||||||||||||
| 111 | 608 | my @start = $self->get_starting; | |||||||||||||
| 112 | 608 | my @final = $self->get_accepting; | |||||||||||||
| 113 | |||||||||||||||
| 114 | 608 | $self->unset_accepting( $self->get_states ); | |||||||||||||
| 115 | 608 | $self->unset_starting( $self->get_states ); | |||||||||||||
| 116 | |||||||||||||||
| 117 | 608 | $self->set_accepting(@start); | |||||||||||||
| 118 | 608 | $self->set_starting(@final); | |||||||||||||
| 119 | |||||||||||||||
| 120 | 608 | $self; | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | ########### | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 3 | ▶ | sub is_empty { | ||||||||||||
|
Subroutine
is_empty: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 126 | 3 | my $self = shift; | |||||||||||||
| 127 | |||||||||||||||
| 128 | 3 | my @queue = $self->get_starting; | |||||||||||||
| 129 | 3 | my %seen = map { $_ => 1 } @queue; | |||||||||||||
| 130 | |||||||||||||||
| 131 | 3 | ▶ | while (@queue) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 18 | ▶ | return 0 if grep { $self->is_accepting($_) } @queue; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 18 | @queue = grep { !$seen{$_}++ } $self->successors( \@queue ); | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | 3 | return 1; | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | 0 | ▶ | sub is_finite { | ||||||||||||
|
Subroutine
is_finite: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||
| 139 | 0 | my $self = shift; | |||||||||||||
| 140 | |||||||||||||||
| 141 | 0 | my @alphabet = $self->alphabet; | |||||||||||||
| 142 | 0 | ▶ | return 1 if @alphabet == 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | |||||||||||||||
| 144 | 0 | my @queue = $self->get_starting; | |||||||||||||
| 145 | 0 | my %seen = map { $_ => 1 } @queue; | |||||||||||||
| 146 | |||||||||||||||
| 147 | 0 | ▶ | while (@queue) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 148 | 0 | @queue = grep { !$seen{$_}++ } $self->successors( \@queue ); | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 0 | for my $s ( grep { $self->is_accepting($_) } keys %seen ) { | |||||||||||||
| 152 | 0 | @queue = $self->epsilon_closure($s); | |||||||||||||
| 153 | 0 | %seen = map { $_ => 1 } @queue; | |||||||||||||
| 154 | |||||||||||||||
| 155 | 0 | ▶ | while (@queue) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 156 | 0 | my @next = $self->epsilon_closure( $self->successors( \@queue, \@alphabet ) ); | |||||||||||||
| 157 | |||||||||||||||
| 158 | 0 | ▶ | return 0 if grep { $s eq $_ } @next; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | 0 | @queue = grep { !$seen{$_}++ } @next; | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | } | ||||||||||||||
| 162 | 0 | return 1; | |||||||||||||
| 163 | } | ||||||||||||||
| 164 | |||||||||||||||
| 165 | 5142 | ▶ | sub epsilon_closure { | ||||||||||||
|
Subroutine
epsilon_closure: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 166 | 5142 | my ( $self, @states ) = @_; | |||||||||||||
| 167 | 5142 | my %seen = map { $_ => 1 } @states; | |||||||||||||
| 168 | 5142 | my @queue = @states; | |||||||||||||
| 169 | |||||||||||||||
| 170 | 5142 | ▶ | while (@queue) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | 3969 | @queue = grep { !$seen{$_}++ } $self->successors( \@queue, "" ); | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | |||||||||||||||
| 174 | 5142 | keys %seen; | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | |||||||||||||||
| 177 | 0 | ▶ | sub contains { | ||||||||||||
|
Subroutine
contains: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 178 | 0 | my ( $self, $string ) = @_; | |||||||||||||
| 179 | |||||||||||||||
| 180 | 0 | my @active = $self->epsilon_closure( $self->get_starting ); | |||||||||||||
| 181 | 0 | for my $char ( split //, $string ) { | |||||||||||||
| 182 | 0 | ▶ | return 0 if !@active; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 183 | 0 | @active = $self->epsilon_closure( $self->successors( \@active, $char ) ); | |||||||||||||
| 184 | } | ||||||||||||||
| 185 | 0 | return !!grep { $self->is_accepting($_) } @active; | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | |||||||||||||||
| 188 | 0 | ▶ | sub trace { | ||||||||||||
|
Subroutine
trace: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 189 | 0 | my ( $self, $string ) = @_; | |||||||||||||
| 190 | |||||||||||||||
| 191 | 0 | my @trace = ( [ $self->epsilon_closure( $self->get_starting ) ] ); | |||||||||||||
| 192 | |||||||||||||||
| 193 | 0 | for my $char ( split //, $string ) { | |||||||||||||
| 194 | 0 | push @trace, [ $self->epsilon_closure( $self->successors( $trace[-1], $char ) ) ]; | |||||||||||||
| 195 | } | ||||||||||||||
| 196 | 0 | return @trace; | |||||||||||||
| 197 | } | ||||||||||||||
| 198 | ############ | ||||||||||||||
| 199 | |||||||||||||||
| 200 | 12 | ▶ | sub _extend_alphabet { | ||||||||||||
|
Subroutine
_extend_alphabet: called CC=6 SCAR=26.2
| |||||||||||||||
| 201 | 12 | my ( $self, @alpha ) = @_; | |||||||||||||
| 202 | |||||||||||||||
| 203 | 12 | my %alpha = map { $_ => 1 } @alpha; | |||||||||||||
| 204 | 12 | delete $alpha{$_} for $self->alphabet; | |||||||||||||
| 205 | |||||||||||||||
| 206 | 12 | ▶ | return if not keys %alpha; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 207 | |||||||||||||||
| 208 | 0 | my $trash = $self->add_states(1); | |||||||||||||
| 209 | 0 | for my $state ( $self->get_states ) { | |||||||||||||
| 210 | 0 | ▶ | next if $state eq $trash; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 211 | 0 | for my $char ( keys %alpha ) { | |||||||||||||
| 212 | 0 | $self->add_transition( $state, $trash, $char ); | |||||||||||||
| 213 | } | ||||||||||||||
| 214 | } | ||||||||||||||
| 215 | 0 | $self->add_transition( $trash, $trash, $self->alphabet ); | |||||||||||||
| 216 | } | ||||||||||||||
| 217 | |||||||||||||||
| 218 | ######## transformations | ||||||||||||||
| 219 | |||||||||||||||
| 220 | 12 | ▶ | sub as_min_dfa { | ||||||||||||
|
Subroutine
as_min_dfa: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 221 | 12 | my $self = shift; | |||||||||||||
| 222 | 12 | return $self->as_dfa()->as_min_dfa(); | |||||||||||||
| 223 | } | ||||||||||||||
| 224 | |||||||||||||||
| 225 | # subset construction | ||||||||||||||
| 226 | 149 | ▶ | sub as_dfa { | ||||||||||||
|
Subroutine
as_dfa: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||
| 227 | 149 | my $self = shift; | |||||||||||||
| 228 | |||||||||||||||
| 229 | 149 | my $result = FLAT::DFA->new; | |||||||||||||
| 230 | 149 | my %subset; | |||||||||||||
| 231 | |||||||||||||||
| 232 | 149 | my %final = map { $_ => 1 } $self->get_accepting; | |||||||||||||
| 233 | 149 | my @start = sort { $a <=> $b } $self->epsilon_closure( $self->get_starting ); | |||||||||||||
| 234 | |||||||||||||||
| 235 | 149 | my $start = $subset{ _SET_ID(@start) } = $result->add_states(1); | |||||||||||||
| 236 | 149 | $result->set_starting($start); | |||||||||||||
| 237 | |||||||||||||||
| 238 | $result->set_accepting($start) | ||||||||||||||
| 239 | 149 | ▶ | if grep $_, @final{@start}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 240 | |||||||||||||||
| 241 | 149 | my @queue = ( \@start ); | |||||||||||||
| 242 | 149 | ▶ | while (@queue) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 243 | 1171 | my @states = @{ shift @queue }; | |||||||||||||
| 244 | 1171 | my $S = $subset{ _SET_ID(@states) }; | |||||||||||||
| 245 | |||||||||||||||
| 246 | 1171 | for my $symb ( $self->alphabet ) { | |||||||||||||
| 247 | 4993 | my @to = $self->epsilon_closure( $self->successors( \@states, $symb ) ); | |||||||||||||
| 248 | |||||||||||||||
| 249 | 4993 | ▶ | if ( not exists $subset{ _SET_ID(@to) } ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 250 | 1022 | push @queue, \@to; | |||||||||||||
| 251 | 1022 | my $T = $subset{ _SET_ID(@to) } = $result->add_states(1); | |||||||||||||
| 252 | $result->set_accepting($T) | ||||||||||||||
| 253 | 1022 | ▶ | if grep $_, @final{@to}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 254 | } | ||||||||||||||
| 255 | |||||||||||||||
| 256 | 4993 | $result->add_transition( $S, $subset{ _SET_ID(@to) }, $symb ); | |||||||||||||
| 257 | } | ||||||||||||||
| 258 | } | ||||||||||||||
| 259 | |||||||||||||||
| 260 | 149 | $result; | |||||||||||||
| 261 | } | ||||||||||||||
| 262 | |||||||||||||||
| 263 | ############ Formatted output | ||||||||||||||
| 264 | |||||||||||||||
| 265 | # Format that Dr. Sukhamay KUNDU likes to use in his assignments :) | ||||||||||||||
| 266 | # This format is just a undirected graph - so transition and state info is lost | ||||||||||||||
| 267 | |||||||||||||||
| 268 | 0 | ▶ | sub as_undirected { | ||||||||||||
|
Subroutine
as_undirected: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 269 | 0 | my $self = shift; | |||||||||||||
| 270 | 0 | my @symbols = $self->alphabet(); | |||||||||||||
| 271 | 0 | my @states = $self->get_states(); | |||||||||||||
| 272 | 0 | my %edges = (); | |||||||||||||
| 273 | 0 | foreach (@states) { | |||||||||||||
| 274 | 0 | my $s = $_; | |||||||||||||
| 275 | 0 | foreach (@symbols) { | |||||||||||||
| 276 | 0 | my $a = $_; | |||||||||||||
| 277 | |||||||||||||||
| 278 | # foreach state, get all nodes connected to it; ignore symbols and | ||||||||||||||
| 279 | # treat transitions simply as directed | ||||||||||||||
| 280 | 0 | push( @{ $edges{$s} }, $self->successors( $s, $a ) ); | |||||||||||||
| 281 | 0 | foreach ( $self->successors( $s, $a ) ) { | |||||||||||||
| 282 | 0 | push( @{ $edges{$_} }, $s ); | |||||||||||||
| 283 | } | ||||||||||||||
| 284 | } | ||||||||||||||
| 285 | } | ||||||||||||||
| 286 | 0 | my @lines = ( ( $#states + 1 ) ); | |||||||||||||
| 287 | 0 | foreach ( sort { $a <=> $b; } ( keys(%edges) ) ) { #<-- iterate over numerically sorted list of keys | |||||||||||||
| 288 | 0 | @{ $edges{$_} } = sort { $a <=> $b; } $self->array_unique( @{ $edges{$_} } ); #<- make items unique and sort numerically | |||||||||||||
| 289 | 0 | push( @lines, sprintf( "%s(%s):%s", $_, ( $#{ $edges{$_} } + 1 ), join( ' ', @{ $edges{$_} } ) ) ); | |||||||||||||
| 290 | } | ||||||||||||||
| 291 | 0 | return join( "\n", @lines ); | |||||||||||||
| 292 | } | ||||||||||||||
| 293 | |||||||||||||||
| 294 | # Format that Dr. Sukhamay KUNDU likes to use in his assignments :) | ||||||||||||||
| 295 | # This format is just a directed graph - so transition and state info is lost | ||||||||||||||
| 296 | |||||||||||||||
| 297 | 0 | ▶ | sub as_digraph { | ||||||||||||
|
Subroutine
as_digraph: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 298 | 0 | my $self = shift; | |||||||||||||
| 299 | 0 | my @symbols = $self->alphabet(); | |||||||||||||
| 300 | 0 | my @states = $self->get_states(); | |||||||||||||
| 301 | 0 | my @lines = (); | |||||||||||||
| 302 | 0 | foreach (@states) { | |||||||||||||
| 303 | 0 | my $s = $_; | |||||||||||||
| 304 | 0 | my @edges = (); | |||||||||||||
| 305 | 0 | foreach (@symbols) { | |||||||||||||
| 306 | 0 | my $a = $_; | |||||||||||||
| 307 | |||||||||||||||
| 308 | # foreach state, get all nodes connected to it; ignore symbols and | ||||||||||||||
| 309 | # treat transitions simply as directed | ||||||||||||||
| 310 | 0 | push( @edges, $self->successors( $s, $a ) ); | |||||||||||||
| 311 | } | ||||||||||||||
| 312 | 0 | @edges = sort { $a <=> $b; } $self->array_unique(@edges); #<- make items unique and sort numerically | |||||||||||||
| 313 | 0 | push( @lines, sprintf( "%s(%s): %s", $s, ( $#edges + 1 ), join( ' ', @edges ) ) ); | |||||||||||||
| 314 | } | ||||||||||||||
| 315 | 0 | return sprintf( "%s\n%s", ( $#states + 1 ), join( "\n", @lines ) ); | |||||||||||||
| 316 | } | ||||||||||||||
| 317 | |||||||||||||||
| 318 | # Graph Description Language, aiSee, etc | ||||||||||||||
| 319 | 0 | ▶ | sub as_gdl { | ||||||||||||
|
Subroutine
as_gdl: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 320 | 0 | my $self = shift; | |||||||||||||
| 321 | |||||||||||||||
| 322 | 0 | ▶ | my @states = map { sprintf qq{node: { title:"%s" shape:circle borderstyle: %s}\n}, $_, ( $self->is_accepting($_) ? "double bordercolor: red" : "solid" ) } $self->get_states; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 323 | |||||||||||||||
| 324 | 0 | my @trans; | |||||||||||||
| 325 | 0 | for my $s1 ( $self->get_states ) { | |||||||||||||
| 326 | 0 | for my $s2 ( $self->get_states ) { | |||||||||||||
| 327 | 0 | my $t = $self->get_transition( $s1, $s2 ); | |||||||||||||
| 328 | |||||||||||||||
| 329 | 0 | ▶ | if ( defined $t ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 330 | 0 | push @trans, sprintf qq[edge: { source: "%s" target: "%s" label: "%s" arrowstyle: line }\n], $s1, $s2, $t->as_string; | |||||||||||||
| 331 | } | ||||||||||||||
| 332 | } | ||||||||||||||
| 333 | } | ||||||||||||||
| 334 | |||||||||||||||
| 335 | 0 | return sprintf "graph: {\ndisplay_edge_labels: yes\n\n%s\n%s}\n", join( "", @states ), join( "", @trans ); | |||||||||||||
| 336 | } | ||||||||||||||
| 337 | |||||||||||||||
| 338 | # JFLAP, for importing into it | ||||||||||||||
| 339 | 0 | ▶ | sub as_jflap { | ||||||||||||
|
Subroutine
as_jflap: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||
| 340 | 0 | my $self = shift; | |||||||||||||
| 341 | 0 | my $XMLstart = <<END; | |||||||||||||
| 342 | <?xml version="1.0" encoding="UTF-8" standalone="no"?><!--Created with Perl-FLAT--> | ||||||||||||||
| 343 | <structure> | ||||||||||||||
| 344 | <type>fa</type> | ||||||||||||||
| 345 | <automaton> | ||||||||||||||
| 346 | END | ||||||||||||||
| 347 | 0 | my $XMLend = <<END; | |||||||||||||
| 348 | </automaton> | ||||||||||||||
| 349 | </structure> | ||||||||||||||
| 350 | END | ||||||||||||||
| 351 | 0 | ▶ | my @states = map { sprintf( qq{\n <state id="%s" name="%s">\n%s%s </state>\n}, $_, $_, ( $self->is_starting($_) ? " <initial/>\n" : '' ), ( $self->is_accepting($_) ? " <final/>\n" : '' ) ) } $self->get_states; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 352 | 0 | my @trans; | |||||||||||||
| 353 | 0 | for my $s1 ( $self->get_states ) { | |||||||||||||
| 354 | 0 | for my $s2 ( $self->get_states ) { | |||||||||||||
| 355 | 0 | my $t = $self->get_transition( $s1, $s2 ); | |||||||||||||
| 356 | 0 | ▶ | if ( defined $t ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 357 | 0 | my $label = $t->as_string; | |||||||||||||
| 358 | 0 | ▶ | $label = ( $label eq 'epsilon' ) ? '<read/>' : sprintf( "<read>%s</read>", $label ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 359 | 0 | push @trans, sprintf( "\n <transition>\n <from>%s</from>\n <to>%s</to>\n %s\n </transition>\n", $s1, $s2, $label ); | |||||||||||||
| 360 | } | ||||||||||||||
| 361 | } | ||||||||||||||
| 362 | } | ||||||||||||||
| 363 | |||||||||||||||
| 364 | 0 | return sprintf( "%s\n\n%s\n%s%s\n", $XMLstart, join( "", @states ), join( "", @trans ), $XMLend ); | |||||||||||||
| 365 | } | ||||||||||||||
| 366 | |||||||||||||||
| 367 | # Graphviz: dot, etc | ||||||||||||||
| 368 | ## digraph, directed | ||||||||||||||
| 369 | 0 | ▶ | sub as_graphviz { | ||||||||||||
|
Subroutine
as_graphviz: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||
| 370 | 0 | my $self = shift; | |||||||||||||
| 371 | |||||||||||||||
| 372 | 0 | ▶ | my @states = map { sprintf qq{%s [label="%s",shape=%s]\n}, $_, ( $self->is_starting($_) ? "start ($_)" : "$_" ), ( $self->is_accepting($_) ? "doublecircle" : "circle" ) } $self->get_states; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 373 | |||||||||||||||
| 374 | 0 | my @trans; | |||||||||||||
| 375 | 0 | for my $s1 ( $self->get_states ) { | |||||||||||||
| 376 | 0 | for my $s2 ( $self->get_states ) { | |||||||||||||
| 377 | 0 | my $t = $self->get_transition( $s1, $s2 ); | |||||||||||||
| 378 | |||||||||||||||
| 379 | 0 | ▶ | if ( defined $t ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 380 | 0 | push @trans, sprintf qq[%s -> %s [label="%s"]\n], $s1, $s2, $t->as_string; | |||||||||||||
| 381 | } | ||||||||||||||
| 382 | } | ||||||||||||||
| 383 | } | ||||||||||||||
| 384 | |||||||||||||||
| 385 | 0 | return sprintf "digraph G {\ngraph [rankdir=LR]\n\n%s\n%s}\n", join( "", @states ), join( "", @trans ); | |||||||||||||
| 386 | } | ||||||||||||||
| 387 | ## undirected | ||||||||||||||
| 388 | 0 | ▶ | sub as_undirected_graphviz { | ||||||||||||
|
Subroutine
as_undirected_graphviz: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 389 | 0 | my $self = shift; | |||||||||||||
| 390 | |||||||||||||||
| 391 | 0 | my @states = map { sprintf qq{%s [label="%s",shape=%s]\n}, $_, ("$_"), ("circle") } $self->get_states; | |||||||||||||
| 392 | |||||||||||||||
| 393 | 0 | my @trans; | |||||||||||||
| 394 | 0 | for my $s1 ( $self->get_states ) { | |||||||||||||
| 395 | 0 | for my $s2 ( $self->get_states ) { | |||||||||||||
| 396 | 0 | my $t = $self->get_transition( $s1, $s2 ); | |||||||||||||
| 397 | |||||||||||||||
| 398 | 0 | ▶ | if ( defined $t ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 399 | 0 | push @trans, sprintf qq[%s -- %s\n], $s1, $s2, $t->as_string; | |||||||||||||
| 400 | } | ||||||||||||||
| 401 | } | ||||||||||||||
| 402 | } | ||||||||||||||
| 403 | |||||||||||||||
| 404 | 0 | return sprintf "graph G {\ngraph [rankdir=LR]\n\n%s\n%s}\n", join( "", @states ), join( "", @trans ); | |||||||||||||
| 405 | } | ||||||||||||||
| 406 | |||||||||||||||
| 407 | #### end formatted output section - probably deserves its own module | ||||||||||||||
| 408 | |||||||||||||||
| 409 | 12328 | ▶ | sub _SET_ID { | ||||||||||||
|
Subroutine
_SET_ID: called CC=1 SCAR=0.0
| |||||||||||||||
| 410 | 12328 | return join "\0", sort { $a <=> $b } @_; | |||||||||||||
| 411 | } | ||||||||||||||
| 412 | |||||||||||||||
| 413 | 0 | ▶ | sub as_summary { | ||||||||||||
|
Subroutine
as_summary: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||
| 414 | 0 | my $self = shift; | |||||||||||||
| 415 | 0 | my $out = ''; | |||||||||||||
| 416 | 0 | $out .= sprintf("States : "); | |||||||||||||
| 417 | 0 | my @start; | |||||||||||||
| 418 | my @final; | ||||||||||||||
| 419 | 0 | foreach ( $self->get_states() ) { | |||||||||||||
| 420 | 0 | $out .= sprintf "'$_' "; | |||||||||||||
| 421 | 0 | ▶ | if ( $self->is_starting($_) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 422 | 0 | push( @start, $_ ); | |||||||||||||
| 423 | } | ||||||||||||||
| 424 | 0 | ▶ | if ( $self->is_accepting($_) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 425 | 0 | push( @final, $_ ); | |||||||||||||
| 426 | } | ||||||||||||||
| 427 | } | ||||||||||||||
| 428 | 0 | $out .= sprintf( "\nStart State : '%s'\n", join( '', @start ) ); | |||||||||||||
| 429 | 0 | $out .= sprintf("Final State(s) : "); | |||||||||||||
| 430 | 0 | foreach (@final) { | |||||||||||||
| 431 | 0 | $out .= sprintf "'$_' "; | |||||||||||||
| 432 | } | ||||||||||||||
| 433 | 0 | $out .= sprintf("\nAlphabet : "); | |||||||||||||
| 434 | 0 | foreach ( $self->alphabet() ) { | |||||||||||||
| 435 | 0 | $out .= sprintf "'$_' "; | |||||||||||||
| 436 | } | ||||||||||||||
| 437 | 0 | $out .= sprintf("\nTransitions :\n"); | |||||||||||||
| 438 | 0 | my @trans; | |||||||||||||
| 439 | 0 | for my $s1 ( $self->get_states ) { | |||||||||||||
| 440 | 0 | for my $s2 ( $self->get_states ) { | |||||||||||||
| 441 | 0 | my $t = $self->get_transition( $s1, $s2 ); | |||||||||||||
| 442 | 0 | ▶ | if ( defined $t ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 443 | 0 | push @trans, sprintf qq[%s -> %s on "%s"\n], $s1, $s2, $t->as_string; | |||||||||||||
| 444 | } | ||||||||||||||
| 445 | } | ||||||||||||||
| 446 | } | ||||||||||||||
| 447 | 0 | $out .= join( '', @trans ); | |||||||||||||
| 448 | 0 | return $out; | |||||||||||||
| 449 | } | ||||||||||||||
| 450 | |||||||||||||||
| 451 | 6 | 1; | |||||||||||||
| 452 | |||||||||||||||
| 453 | __END__ | ||||||||||||||