File Coverage

blib/lib/Soar/WM/Element.pm
Criterion Covered Total %
statement 67 68 98.5
branch 19 22 86.3
condition n/a
subroutine 10 10 100.0
pod 7 7 100.0
total 103 107 96.2


line stmt bran cond sub pod time code
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::Element;
10 3     3   15 use strict;
  3         6  
  3         111  
11 3     3   17 use warnings;
  3         4  
  3         135  
12              
13             our $VERSION = '0.04'; # VERSION
14             # ABSTRACT: Work with Soar working memory elements
15              
16 3     3   13 use Carp;
  3         5  
  3         2152  
17              
18             sub new {
19 12     12 1 580 my ( $class, $wm, $id ) = @_;
20 12 100       49 if ( !exists $wm->{$id} ) {
21 1         14 carp 'Given ID doesn\'t exist in given working memory';
22 1         620 return;
23             }
24              
25 11         57 my $self = bless {
26             wm => $wm,
27             id => $id,
28             node => $wm->{$id},
29             }, $class;
30 11         34 return $self;
31             }
32              
33             sub id {
34 3     3 1 1617 my ($self) = @_;
35 3         22 return $self->{id};
36             }
37              
38             sub atts {
39 2     2 1 3 my ($self) = @_;
40 2         3 my @atts = keys %{ $self->{node} };
  2         7  
41 2         8 return \@atts;
42             }
43              
44             sub vals {
45 3     3 1 1005 my ( $self, $query ) = @_;
46 3 100       11 if ( !$query ) {
47 1         27 carp 'missing argument attribute name';
48 1         627 return;
49             }
50 2 50       6 return [] unless exists $self->{node}->{$query};
51 2         3 my @values = @{ $self->{node}->{$query} };
  2         7  
52            
53             #find ones that are links and change them into WME instances
54 2         6 for ( 0 .. $#values ) {
55 4 100       12 if ( exists $self->{wm}->{ $values[$_] } ) {
56 2         6 $values[$_] = __PACKAGE__->new( $self->{wm}, $values[$_] );
57             }
58             }
59 2         7 return \@values;
60             }
61              
62             sub children {
63 2     2 1 4 my ($self , %args) = @_;
64            
65 2         3 my @children;
66 2         3 for my $key ( keys %{$self->{node}} ){
  2         6  
67 6         6 push @children, @{ $self->{node}->{$key} };
  6         15  
68             }
69            
70             #find ones that are links and change them into WME instances
71 2         5 for ( 0 .. $#children ) {
72 10 100       26 if ( exists $self->{wm}->{ $children[$_] } ) {
73 4         10 $children[$_] = __PACKAGE__->new( $self->{wm}, $children[$_] );
74             }
75             }
76 2         5 my $retVal;
77 2 100       6 if($args{links_only}){
78 1         2 my @links = grep {ref($_) eq 'Soar::WM::Element'} @children;
  5         9  
79 1         2 $retVal = \@links;
80             }else{
81 1         2 $retVal = \@children;
82             }
83 2         12 return $retVal;
84             }
85              
86             sub first_val {
87 4     4 1 1925 my ( $self, $query ) = @_;
88 4 100       16 if ( !$query ) {
89 1         14 carp 'missing argument attribute name';
90 1         542 return;
91             }
92 3 50       8 return unless exists $self->{node}->{$query};
93              
94             # grab only the first value
95 3         5 my $value = ${ $self->{node}->{$query} }[0];
  3         6  
96              
97 3 50       6 if(not defined $value){
98 0         0 return;
99             }
100            
101             #if value is a link, change it into a WME instance
102 3 100       8 if ( exists $self->{wm}->{$value} ) {
103 1         4 $value = __PACKAGE__->new( $self->{wm}, $value );
104             }
105 3         7 return $value;
106             }
107              
108             sub num_links {
109 1     1 1 2 my ($self) = @_;
110 1         2 my $count = 0;
111              
112             #iterate values of each attribute; a child will have its own entry in WM
113 1         2 for my $att ( @{ $self->atts } ) {
  1         2  
114 3         4 for my $val ( @{ $self->{node}->{$att} } ) {
  3         7  
115 5 100       16 $count++
116             if ( exists $self->{wm}->{$val} );
117             }
118             }
119 1         5 return $count;
120             }
121              
122             1;
123              
124             __END__