File Coverage

blib/lib/RDF/Query/Expression/Unary.pm
Criterion Covered Total %
statement 50 52 96.1
branch 10 14 71.4
condition 3 3 100.0
subroutine 11 11 100.0
pod 3 3 100.0
total 77 83 92.7


line stmt bran cond sub pod time code
1             # RDF::Query::Expression::Unary
2             # -----------------------------------------------------------------------------
3              
4             =head1 NAME
5              
6             RDF::Query::Expression::Unary - Class for unary expressions
7              
8             =head1 VERSION
9              
10             This document describes RDF::Query::Expression::Unary version 2.916.
11              
12             =cut
13              
14             package RDF::Query::Expression::Unary;
15              
16 36     36   182 use strict;
  36         61  
  36         936  
17 36     36   175 use warnings;
  36         62  
  36         1038  
18 36     36   179 no warnings 'redefine';
  36         65  
  36         1237  
19 36     36   181 use base qw(RDF::Query::Expression);
  36         64  
  36         2600  
20              
21 36     36   224 use Data::Dumper;
  36         78  
  36         1855  
22 36     36   183 use Scalar::Util qw(blessed);
  36         67  
  36         1643  
23 36     36   499 use Carp qw(carp croak confess);
  36         73  
  36         2661  
24              
25             ######################################################################
26              
27             our ($VERSION);
28             BEGIN {
29 36     36   16676 $VERSION = '2.916';
30             }
31              
32             ######################################################################
33              
34             =head1 METHODS
35              
36             Beyond the methods documented below, this class inherits methods from the
37             L<RDF::Query::Expression> class.
38              
39             =over 4
40              
41             =cut
42              
43             =item C<< sse >>
44              
45             Returns the SSE string for this algebra expression.
46              
47             =cut
48              
49             sub sse {
50 3     3 1 6 my $self = shift;
51 3         8 my $context = shift;
52            
53             return sprintf(
54             '(%s %s)',
55             $self->op,
56 3         14 map { $_->sse( $context ) } $self->operands,
  3         16  
57             );
58             }
59              
60             =item C<< as_sparql >>
61              
62             Returns the SPARQL string for this algebra expression.
63              
64             =cut
65              
66             sub as_sparql {
67 2     2 1 5 my $self = shift;
68 2         3 my $context = shift;
69 2         4 my $indent = shift;
70 2         7 my $op = $self->op;
71 2         10 return sprintf("($op %s)", map { $_->as_sparql( $context, $indent ) } $self->operands);
  2         9  
72             }
73              
74             =item C<< evaluate ( $query, \%bound ) >>
75              
76             Evaluates the expression using the supplied bound variables.
77             Will return a RDF::Query::Node object.
78              
79             =cut
80              
81             sub evaluate {
82 6     6 1 19 my $self = shift;
83 6         12 my $query = shift;
84 6         8 my $bound = shift;
85 6         10 my $context = shift;
86 6         24 my $op = $self->op;
87 6         26 my ($data) = $self->operands;
88 6 100 100     42 if ($op eq '+' or $op eq '-') {
    50          
89             my $l = $data->isa('RDF::Query::Algebra')
90             ? $data->evaluate( $query, $bound, $context, @_ )
91             : ($data->isa('RDF::Query::Node::Variable'))
92 2 50       24 ? $bound->{ $data->name }
    50          
93             : $data;
94            
95 2         3 my $value;
96 2 100       10 if ($op eq '+') {
    50          
97 1         5 $value = $l->numeric_value;
98             } elsif ($op eq '-') {
99 1         5 $value = -1 * $l->numeric_value;
100             }
101 2         10 return RDF::Query::Node::Literal->new( $value, undef, $l->literal_datatype );
102             } elsif ($op eq '!') {
103 4         13 my $alg = RDF::Query::Expression::Function->new( "sparql:ebv", $data );
104 4         25 my $bool = $alg->evaluate( $query, $bound, $context, @_ );
105 4 100       13 if ($bool->literal_value eq 'true') {
106 1         10 return RDF::Query::Node::Literal->new( 'false', undef, 'http://www.w3.org/2001/XMLSchema#boolean' );
107             } else {
108 3         24 return RDF::Query::Node::Literal->new( 'true', undef, 'http://www.w3.org/2001/XMLSchema#boolean' );
109             }
110             } else {
111 0           warn "unknown unary op: $op";
112 0           throw RDF::Query::Error::ExecutionError -text => "Unknown unary op '$op'";
113             }
114             }
115              
116              
117             1;
118              
119             __END__
120              
121             =back
122              
123             =head1 AUTHOR
124              
125             Gregory Todd Williams <gwilliams@cpan.org>
126              
127             =cut