| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of Soar-WM | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2012 by Nathan Glenn. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | package Soar::WM::Slurp; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 6 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 6 | our @EXPORT_OK = qw(read_wm_file read_wm); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 6 | our $VERSION = '0.04'; # VERSION | |||||||||||||||||||||||||||
| 21 | # ABSTRACT: Read and parse Soar working memory dumps | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 6 | ▶ | say Dump read_wm( file => $ARGV[0] ) unless caller; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | sub read_wm_file { | ||||||||||||||||||||||||||
|
Subroutine
read_wm_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 2 | my ($file) = @_; | |||||||||||||||||||||||||||
| 27 | 2 | return read_wm( file => $file ); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | #structure will be: | ||||||||||||||||||||||||||||
| 31 | # return_val->{$wme} = { $attr=>[@values]} | ||||||||||||||||||||||||||||
| 32 | # {'root_wme'} = 'S1' or some such | ||||||||||||||||||||||||||||
| 33 | #parse a WME dump file and create a WM object; return the WM hash and the name of the root WME. | ||||||||||||||||||||||||||||
| 34 | 8 | ▶ | sub read_wm { ## no critic (RequireArgUnpacking) | ||||||||||||||||||||||||||
|
Subroutine
read_wm: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 8 | my %args = ( | |||||||||||||||||||||||||||
| 36 | text => undef, | ||||||||||||||||||||||||||||
| 37 | file => undef, | ||||||||||||||||||||||||||||
| 38 | @_ | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | 8 | my $fh; | |||||||||||||||||||||||||||
| 41 | 8 | ▶ | if ( $args{text} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 42 | 5 | $fh = _get_fh_from_string( $args{text} ); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | elsif ( $args{file} ) { | ||||||||||||||||||||||||||||
| 45 | 3 | $fh = _get_fh( $args{file} ); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | else { | ||||||||||||||||||||||||||||
| 48 | 0 | $fh = \*STDIN; | |||||||||||||||||||||||||||
| 49 | 0 | print "Reading WME dump from standard in.\n"; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | #control variables | ||||||||||||||||||||||||||||
| 53 | 8 | my ( $hasOpenParen, $hasCloseParen ); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | #keep track of results/return value | ||||||||||||||||||||||||||||
| 56 | 8 | my ( $root_wme, %wme_hash ); | |||||||||||||||||||||||||||
| 57 | 8 | ▶ | while ( my $inline = <$fh> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 21 | chomp $inline; | |||||||||||||||||||||||||||
| 59 | 21 | ▶ | next if $inline eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 19 | my $line = ""; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | #note: do we need $hasOpenParen? | ||||||||||||||||||||||||||||
| 63 | 19 | $hasOpenParen = ( $inline =~ /^\s*\(/ ); | |||||||||||||||||||||||||||
| 64 | 19 | $hasCloseParen = ( $inline =~ /\)\s*$/ ); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | #read entire space between parentheses | ||||||||||||||||||||||||||||
| 67 | 19 | ▶ | while ( $hasOpenParen && !($hasCloseParen) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$hasOpenParen and not $hasCloseParen
MC/DC50%1/2
$hasOpenParen and not $hasCloseParen
$hasOpenParen
not $hasCloseParen
Truth table66%2/3
A and not(B)
A= $hasOpenParenB= not $hasCloseParen
| |||||||||||||||||||||||||||||
| 68 | 16 | chomp $inline; | |||||||||||||||||||||||||||
| 69 | 16 | $line .= $inline; | |||||||||||||||||||||||||||
| 70 | 16 | $inline = <$fh>; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | #if this line of the WME dump is incomplete, ignore it. | ||||||||||||||||||||||||||||
| 73 | 16 | ▶ | if ( !$inline ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | $inline = ''; | |||||||||||||||||||||||||||
| 75 | 1 | $line = ''; | |||||||||||||||||||||||||||
| 76 | 1 | last; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 15 | $hasCloseParen = ( $inline =~ /\)\s*$/ ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 19 | $line .= $inline; | |||||||||||||||||||||||||||
| 81 | 19 | ▶ | if ($line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | #separate wme and everything else [(<wme> ^the rest...)] | ||||||||||||||||||||||||||||
| 84 | 18 | my ( $wme, $rest ) = split " ", $line, 2; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # initiate the record | ||||||||||||||||||||||||||||
| 87 | 18 | my $rec = {}; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # hash each of the attr/val pairs | ||||||||||||||||||||||||||||
| 90 | 18 | my @attVals = split /\^/, $rest; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | #if line were 'S16 ^foo bar ^baz biff', then @attvals | ||||||||||||||||||||||||||||
| 93 | #now contains ['S16', 'foo bar', 'baz biff'] | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | #get rid of the WME ID | ||||||||||||||||||||||||||||
| 96 | 18 | shift @attVals; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 18 | foreach my $attVal (@attVals) { | |||||||||||||||||||||||||||
| 99 | 54 | my ( $attr, $val ) = split " ", $attVal; | |||||||||||||||||||||||||||
| 100 | 54 | ▶ | if ( !length($attr) ) { #note: would this ever happen? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | next; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | #get rid of final parenthesis | ||||||||||||||||||||||||||||
| 105 | 54 | $val =~ s/\)$//; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # store attr/val association in the record | ||||||||||||||||||||||||||||
| 108 | 54 | push @{ $rec->{"$attr"} }, $val; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | #strip opening parenthesis | ||||||||||||||||||||||||||||
| 112 | 18 | $wme =~ s/^\(//; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # $rec->{'#wmeval'} = $wme; | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | #rootwme is S1, or similar | ||||||||||||||||||||||||||||
| 117 | 18 | ▶ | $root_wme = $wme unless $root_wme; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # add the record to the wme hash | ||||||||||||||||||||||||||||
| 120 | 18 | $wme_hash{$wme} = $rec; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 8 | close $fh; | |||||||||||||||||||||||||||
| 124 | 8 | return \%wme_hash, $root_wme; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 5 | ▶ | sub _get_fh_from_string { | ||||||||||||||||||||||||||
|
Subroutine
_get_fh_from_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | 5 | my ($text) = @_; | |||||||||||||||||||||||||||
| 129 | 5 | open my $sh, '<', \$text; | |||||||||||||||||||||||||||
| 130 | 5 | return $sh; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 3 | ▶ | sub _get_fh { | ||||||||||||||||||||||||||
|
Subroutine
_get_fh: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 134 | 3 | my ($name) = @_; | |||||||||||||||||||||||||||
| 135 | 3 | ▶ | return $name if ref $name eq 'GLOB'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 2 | open my $fh, '<', $name; | |||||||||||||||||||||||||||
| 137 | 2 | return $fh; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 6 | 1; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | __END__ | ||||||||||||||||||||||||||||