| 1 | package Set::FA; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use parent 'Set::Object'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | our $VERSION = '2.01'; | |||||||||
| 8 | |||||||||||
| 9 | # ----------------------------------------------- | ||||||||||
| 10 | |||||||||||
| 11 | sub accept | ||||||||||
| 12 | 2 | ▶ | { | ||||||||
|
Subroutine
accept: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 13 | 2 | my($self, $input) = @_; | |||||||||
| 14 | 2 | my($set) = (ref $self) -> new; | |||||||||
| 15 | |||||||||||
| 16 | 2 | for my $automaton ($self -> members) | |||||||||
| 17 | { | ||||||||||
| 18 | 30 | ▶ | $set -> insert($automaton) if ($automaton -> accept($input) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 2 | return $set; | |||||||||
| 22 | |||||||||||
| 23 | } # End of accept. | ||||||||||
| 24 | |||||||||||
| 25 | # ----------------------------------------------- | ||||||||||
| 26 | |||||||||||
| 27 | sub advance | ||||||||||
| 28 | 0 | ▶ | { | ||||||||
|
Subroutine
advance: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 29 | 0 | my($self, $input) = @_; | |||||||||
| 30 | |||||||||||
| 31 | 0 | for my $automaton ($self -> members) | |||||||||
| 32 | { | ||||||||||
| 33 | 0 | $automaton -> advance($input); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | } # End of advance. | ||||||||||
| 37 | |||||||||||
| 38 | # ----------------------------------------------- | ||||||||||
| 39 | |||||||||||
| 40 | sub final | ||||||||||
| 41 | 5 | ▶ | { | ||||||||
|
Subroutine
final: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 42 | 5 | my($self) = @_; | |||||||||
| 43 | 5 | my($set) = (ref $self) -> new; | |||||||||
| 44 | |||||||||||
| 45 | 5 | for my $automaton ($self -> members) | |||||||||
| 46 | { | ||||||||||
| 47 | 75 | ▶ | $set -> insert($automaton) if ($automaton -> final); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 5 | return $set; | |||||||||
| 51 | |||||||||||
| 52 | } # End of final. | ||||||||||
| 53 | |||||||||||
| 54 | # ----------------------------------------------- | ||||||||||
| 55 | |||||||||||
| 56 | sub id | ||||||||||
| 57 | 1 | ▶ | { | ||||||||
|
Subroutine
id: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 58 | 1 | my($self, $id) = @_; | |||||||||
| 59 | 1 | my($set) = (ref $self) -> new; | |||||||||
| 60 | |||||||||||
| 61 | 1 | for my $automaton ($self -> members) | |||||||||
| 62 | { | ||||||||||
| 63 | 15 | ▶ | $set -> insert($automaton) if ($automaton -> id eq $id); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 1 | return $set; | |||||||||
| 67 | |||||||||||
| 68 | } # End of id. | ||||||||||
| 69 | |||||||||||
| 70 | # ----------------------------------------------- | ||||||||||
| 71 | |||||||||||
| 72 | sub in_state | ||||||||||
| 73 | 8 | ▶ | { | ||||||||
|
Subroutine
in_state: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 74 | 8 | my($self, $state) = @_; | |||||||||
| 75 | 8 | my($set) = (ref $self) -> new; | |||||||||
| 76 | |||||||||||
| 77 | 8 | for my $automaton ($self -> members) | |||||||||
| 78 | { | ||||||||||
| 79 | 120 | ▶ | $set -> insert($automaton) if ($automaton -> state eq $state); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 8 | return $set; | |||||||||
| 83 | |||||||||||
| 84 | } # End of in_state. | ||||||||||
| 85 | |||||||||||
| 86 | # ----------------------------------------------- | ||||||||||
| 87 | |||||||||||
| 88 | sub reset | ||||||||||
| 89 | 2 | ▶ | { | ||||||||
|
Subroutine
reset: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 90 | 2 | my($self) = @_; | |||||||||
| 91 | |||||||||||
| 92 | 2 | for my $automaton ($self -> members) | |||||||||
| 93 | { | ||||||||||
| 94 | 27 | $automaton -> reset; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | } # End of reset. | ||||||||||
| 98 | |||||||||||
| 99 | # ----------------------------------------------- | ||||||||||
| 100 | |||||||||||
| 101 | sub step | ||||||||||
| 102 | 1 | ▶ | { | ||||||||
|
Subroutine
step: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 103 | 1 | my($self, $input) = @_; | |||||||||
| 104 | |||||||||||
| 105 | 1 | for my $automaton ($self -> members) | |||||||||
| 106 | { | ||||||||||
| 107 | 15 | $automaton -> step($input); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | } # End of step. | ||||||||||
| 111 | |||||||||||
| 112 | # ----------------------------------------------- | ||||||||||
| 113 | |||||||||||
| 114 | 2 | 1; | |||||||||
| 115 | |||||||||||
| 116 | =pod | ||||||||||
| 117 | |||||||||||
| 118 | =head1 NAME | ||||||||||
| 119 | |||||||||||
| 120 | Set::FA - A Set of Discrete Finite Automata | ||||||||||
| 121 | |||||||||||
| 122 | =head1 Synopsis | ||||||||||
| 123 | |||||||||||
| 124 | This is scripts/synopsis.1.pl: | ||||||||||
| 125 | |||||||||||
| 126 | #!/usr/bin/perl | ||||||||||
| 127 | |||||||||||
| 128 | use strict; | ||||||||||
| 129 | use warnings; | ||||||||||
| 130 | |||||||||||
| 131 | use Set::FA; | ||||||||||
| 132 | use Set::FA::Element; | ||||||||||
| 133 | |||||||||||
| 134 | # -------------------------- | ||||||||||
| 135 | |||||||||||
| 136 | my(@a) = map | ||||||||||
| 137 | { | ||||||||||
| 138 | Set::FA::Element -> new | ||||||||||
| 139 | ( | ||||||||||
| 140 | accepting => ['ping'], | ||||||||||
| 141 | id => "a.$_", | ||||||||||
| 142 | start => 'ping', | ||||||||||
| 143 | transitions => | ||||||||||
| 144 | [ | ||||||||||
| 145 | ['ping', 'a', 'pong'], | ||||||||||
| 146 | ['ping', '.', 'ping'], | ||||||||||
| 147 | ['pong', 'b', 'ping'], | ||||||||||
| 148 | ['pong', '.', 'pong'], | ||||||||||
| 149 | ], | ||||||||||
| 150 | ) | ||||||||||
| 151 | } (0 .. 2); | ||||||||||
| 152 | |||||||||||
| 153 | my(@b) = map | ||||||||||
| 154 | { | ||||||||||
| 155 | Set::FA::Element -> new | ||||||||||
| 156 | ( | ||||||||||
| 157 | accepting => ['pong'], | ||||||||||
| 158 | id => "b.$_", | ||||||||||
| 159 | start => 'ping', | ||||||||||
| 160 | transitions => | ||||||||||
| 161 | [ | ||||||||||
| 162 | ['ping', 'a', 'pong'], | ||||||||||
| 163 | ['ping', '.', 'ping'], | ||||||||||
| 164 | ['pong', 'b', 'ping'], | ||||||||||
| 165 | ['pong', '.', 'pong'], | ||||||||||
| 166 | ], | ||||||||||
| 167 | ) | ||||||||||
| 168 | } (0 .. 4); | ||||||||||
| 169 | |||||||||||
| 170 | my($set) = Set::FA -> new(@a, @b); | ||||||||||
| 171 | my($sub_a) = $set -> accept('aaabbaaabdogbbbbbababa'); | ||||||||||
| 172 | my($sub_b) = $set -> final; | ||||||||||
| 173 | |||||||||||
| 174 | print 'Size of $sub_a: ', $sub_a -> size, ' (expect 3). ', | ||||||||||
| 175 | 'Size of @a: ', scalar @a, ' (expect 3). ', | ||||||||||
| 176 | 'Size of $sub_b: ', $sub_b -> size, ' (expect 5). ', | ||||||||||
| 177 | 'Size of @b: ', scalar @b, ' (expect 5). ', "\n", | ||||||||||
| 178 | |||||||||||
| 179 | =head1 Description | ||||||||||
| 180 | |||||||||||
| 181 | L<Set::FA> provides a mechanism to define and run a set of DFAs. | ||||||||||
| 182 | |||||||||||
| 183 | =head1 Installation | ||||||||||
| 184 | |||||||||||
| 185 | Install L<Set::FA> as you would for any C<Perl> module: | ||||||||||
| 186 | |||||||||||
| 187 | Run: | ||||||||||
| 188 | |||||||||||
| 189 | cpanm Set::FA | ||||||||||
| 190 | |||||||||||
| 191 | or run: | ||||||||||
| 192 | |||||||||||
| 193 | sudo cpan Set::FA | ||||||||||
| 194 | |||||||||||
| 195 | or unpack the distro, and then either: | ||||||||||
| 196 | |||||||||||
| 197 | perl Build.PL | ||||||||||
| 198 | ./Build | ||||||||||
| 199 | ./Build test | ||||||||||
| 200 | sudo ./Build install | ||||||||||
| 201 | |||||||||||
| 202 | or: | ||||||||||
| 203 | |||||||||||
| 204 | perl Makefile.PL | ||||||||||
| 205 | make (or dmake or nmake) | ||||||||||
| 206 | make test | ||||||||||
| 207 | make install | ||||||||||
| 208 | |||||||||||
| 209 | =head1 Constructor and Initialization | ||||||||||
| 210 | |||||||||||
| 211 | =head2 Parentage | ||||||||||
| 212 | |||||||||||
| 213 | This class extends L<Set::Object>, meaning L<Set::FA> is a subclass of L<Set::Object>. | ||||||||||
| 214 | |||||||||||
| 215 | For the (long) list of methods available and provided by L<Set::Object>, see that object's | ||||||||||
| 216 | documentation. | ||||||||||
| 217 | |||||||||||
| 218 | =head2 Using new() | ||||||||||
| 219 | |||||||||||
| 220 | L</new([@list_of_dfas])> is called as C<< my($set) = Set::FA -> new(@list_of_dfas) >>. | ||||||||||
| 221 | |||||||||||
| 222 | It returns a new object of type C<Set::FA>. | ||||||||||
| 223 | |||||||||||
| 224 | You may supply a list of L<Set::FA::Element> objects to L</new([@list_of_dfas])>. | ||||||||||
| 225 | |||||||||||
| 226 | If the list is empty, you will need to call $set -> insert(@list_of_dfas) to do anything meaningful | ||||||||||
| 227 | with $set. | ||||||||||
| 228 | |||||||||||
| 229 | The new object is a set whose members are all L<Set::FA::Element> objects. | ||||||||||
| 230 | |||||||||||
| 231 | This class allows you to operate on all members of the set simultaneously, as in the | ||||||||||
| 232 | synopsis. | ||||||||||
| 233 | |||||||||||
| 234 | =head1 Methods | ||||||||||
| 235 | |||||||||||
| 236 | =head2 accept($input) | ||||||||||
| 237 | |||||||||||
| 238 | Calls L<Set::FA::Element/accept($input)> on all members of the set. This in turn calls | ||||||||||
| 239 | L<Set::FA::Element/advance($input)> on each member. | ||||||||||
| 240 | |||||||||||
| 241 | Note: This does I<not> mean it calls C<advance()> on the set object. | ||||||||||
| 242 | |||||||||||
| 243 | Returns a L<Set::FA> object containing just the members of the original set which have ended up | ||||||||||
| 244 | in their respective accepting states. | ||||||||||
| 245 | |||||||||||
| 246 | =head2 advance($input) | ||||||||||
| 247 | |||||||||||
| 248 | Calls L<Set::FA::Element/advance($input)> on all members of the set. | ||||||||||
| 249 | |||||||||||
| 250 | Returns nothing. | ||||||||||
| 251 | |||||||||||
| 252 | =head2 final() | ||||||||||
| 253 | |||||||||||
| 254 | Calls L<Set::FA::Element/final()> on all members of the set. | ||||||||||
| 255 | |||||||||||
| 256 | Returns a L<Set::FA> object containing just the members of the original set which are | ||||||||||
| 257 | in their respective accepting states. | ||||||||||
| 258 | |||||||||||
| 259 | =head2 id($id) | ||||||||||
| 260 | |||||||||||
| 261 | Returns a L<Set::FA> object containing just the members of the original set whose ids | ||||||||||
| 262 | match the $id parameter. | ||||||||||
| 263 | |||||||||||
| 264 | =head2 in_state($state) | ||||||||||
| 265 | |||||||||||
| 266 | Returns a L<Set::FA> object containing just the members of the original set who current | ||||||||||
| 267 | state matches the $state parameter. | ||||||||||
| 268 | |||||||||||
| 269 | =head2 new([@list_of_dfas]) | ||||||||||
| 270 | |||||||||||
| 271 | Here, the [] indicate an optional parameter. | ||||||||||
| 272 | |||||||||||
| 273 | The constructor. See L</Constructor and Initialization>. | ||||||||||
| 274 | |||||||||||
| 275 | =head2 reset() | ||||||||||
| 276 | |||||||||||
| 277 | Calls L<Set::FA::Element/reset()> on all members of the set. | ||||||||||
| 278 | |||||||||||
| 279 | Returns nothing. | ||||||||||
| 280 | |||||||||||
| 281 | =head2 step($input) | ||||||||||
| 282 | |||||||||||
| 283 | Calls L<Set::FA::Element/step($input)> on all members of the set. | ||||||||||
| 284 | |||||||||||
| 285 | Returns nothing. | ||||||||||
| 286 | |||||||||||
| 287 | =head1 FAQ | ||||||||||
| 288 | |||||||||||
| 289 | See L<Set::FA::Element/FAQ>. | ||||||||||
| 290 | |||||||||||
| 291 | =head1 Machine-Readable Change Log | ||||||||||
| 292 | |||||||||||
| 293 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||||||||||
| 294 | |||||||||||
| 295 | =head1 Version Numbers | ||||||||||
| 296 | |||||||||||
| 297 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||||||||||
| 298 | |||||||||||
| 299 | =head1 Credit | ||||||||||
| 300 | |||||||||||
| 301 | The code was rewritten to perform exactly as did earlier versions (pre-1.00) of L<Set::FA> and | ||||||||||
| 302 | L<Set::FA::Element>, and hence is essentially the same, line for line. | ||||||||||
| 303 | |||||||||||
| 304 | I've reformatted it, and changed the OO nature and the logging, obviously, but Mark Rogaski, the | ||||||||||
| 305 | author of L<Set::FA> gets the credit for the code. | ||||||||||
| 306 | |||||||||||
| 307 | I've rewritten the documentation from scratch. | ||||||||||
| 308 | |||||||||||
| 309 | =head1 See Also | ||||||||||
| 310 | |||||||||||
| 311 | Before adopting L<Set::FA> (for L<Graph::Easy::Marpa>'s lexer), here are some other DFA modules I | ||||||||||
| 312 | found on L<https://metacpan.org>: | ||||||||||
| 313 | |||||||||||
| 314 | =over 4 | ||||||||||
| 315 | |||||||||||
| 316 | =item o L<DFA::Kleene> | ||||||||||
| 317 | |||||||||||
| 318 | The author definitely knows what he's doing, but this module addresses a different issue than I | ||||||||||
| 319 | face. It outputs regexps corresponding to the transitions you specify. | ||||||||||
| 320 | |||||||||||
| 321 | =item o L<DFA::Statemap> | ||||||||||
| 322 | |||||||||||
| 323 | This is a wrapper around the State Machine Compiler, to output a SM in Perl. SMC requires Java, | ||||||||||
| 324 | and can output in a range of languages. See L<http://smc.sourceforge.net/>. | ||||||||||
| 325 | |||||||||||
| 326 | SMC looks sophisticated, but it's a rather indirect way of doing things when Perl modules such | ||||||||||
| 327 | as L<Set::FA::Element> are already available. | ||||||||||
| 328 | |||||||||||
| 329 | =item o L<Dict::FSA> | ||||||||||
| 330 | |||||||||||
| 331 | This module is a perl wrapper around C<fsa>, a set of tools based on finite state automata. | ||||||||||
| 332 | |||||||||||
| 333 | See L<http://www.eti.pg.gda.pl/~jandac/fsa.html>. | ||||||||||
| 334 | |||||||||||
| 335 | =item o L<DMA::FSM> | ||||||||||
| 336 | |||||||||||
| 337 | Uses a very old-fashioned style of writing Perl. | ||||||||||
| 338 | |||||||||||
| 339 | =item o FLAT::DFA. See L<FLAT> | ||||||||||
| 340 | |||||||||||
| 341 | A toolkit for manipulating DFAs. | ||||||||||
| 342 | |||||||||||
| 343 | =item o L<FSA::Engine> | ||||||||||
| 344 | |||||||||||
| 345 | A Moose Role to convert an object into a Finite State Machine. | ||||||||||
| 346 | |||||||||||
| 347 | =item o L<FSA::Rules> | ||||||||||
| 348 | |||||||||||
| 349 | Build simple rules-based state machines in Perl. | ||||||||||
| 350 | |||||||||||
| 351 | =item o L<IDS::Algorithm::DFA> | ||||||||||
| 352 | |||||||||||
| 353 | Uses an old-fashioned style of writing Perl. | ||||||||||
| 354 | |||||||||||
| 355 | =item o L<MooseX::FSM> | ||||||||||
| 356 | |||||||||||
| 357 | Looks like an unfinished thought-bubble. | ||||||||||
| 358 | |||||||||||
| 359 | =item o L<Parse::FSM> | ||||||||||
| 360 | |||||||||||
| 361 | Outputs a Perl module implementing the FSM you define. | ||||||||||
| 362 | |||||||||||
| 363 | =item o L<The Ragel State Machine Compiler|http://www.complang.org/ragel/> | ||||||||||
| 364 | |||||||||||
| 365 | A non-Perl solution. | ||||||||||
| 366 | |||||||||||
| 367 | That page has lots of interesting links. | ||||||||||
| 368 | |||||||||||
| 369 | =item o L<Shishi> | ||||||||||
| 370 | |||||||||||
| 371 | Doesn't use a transition table, but does allow you to modify the SM while it's running. | ||||||||||
| 372 | You build up a transition network diagram, labouriously, with 1 line of code at a time. | ||||||||||
| 373 | |||||||||||
| 374 | =back | ||||||||||
| 375 | |||||||||||
| 376 | See also L<this Wikipedia article|http://en.wikipedia.org/wiki/Deterministic_finite-state_machine>. | ||||||||||
| 377 | |||||||||||
| 378 | =head1 Repository | ||||||||||
| 379 | |||||||||||
| 380 | L<https://github.com/ronsavage/Set-FA> | ||||||||||
| 381 | |||||||||||
| 382 | =head1 Support | ||||||||||
| 383 | |||||||||||
| 384 | Email the author, or log a bug on RT: | ||||||||||
| 385 | |||||||||||
| 386 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=Set::FA> | ||||||||||
| 387 | |||||||||||
| 388 | =head1 Author | ||||||||||
| 389 | |||||||||||
| 390 | L<Set::FA> was written by Mark Rogaski and Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2011. | ||||||||||
| 391 | |||||||||||
| 392 | My homepage: L<http://savage.net.au/index.html> | ||||||||||
| 393 | |||||||||||
| 394 | =head1 Copyright | ||||||||||
| 395 | |||||||||||
| 396 | Australian copyright (c) 2011, Ron Savage. | ||||||||||
| 397 | |||||||||||
| 398 | All Programs of mine are 'OSI Certified Open Source Software'; | ||||||||||
| 399 | you can redistribute them and/or modify them under the terms of | ||||||||||
| 400 | The Perl License, a copy of which is available at: | ||||||||||
| 401 | http://www.opensource.org/licenses/index.html | ||||||||||
| 402 | |||||||||||
| 403 | =cut | ||||||||||