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