File Coverage

blib/lib/RDF/Query/Node.pm
Criterion Covered Total %
statement 46 75 61.3
branch 8 20 40.0
condition 2 3 66.6
subroutine 14 20 70.0
pod 9 9 100.0
total 79 127 62.2


line stmt bran cond sub pod time code
1             # RDF::Query::Node
2             # -----------------------------------------------------------------------------
3              
4             =head1 NAME
5              
6             RDF::Query::Node - Base class for RDF Nodes
7              
8             =head1 VERSION
9              
10             This document describes RDF::Query::Node version 2.918.
11              
12             =head1 METHODS
13              
14             =over 4
15              
16             =cut
17              
18             package RDF::Query::Node;
19              
20 36     36   145953 use strict;
  36         47  
  36         901  
21 36     36   120 use warnings;
  36         40  
  36         836  
22 36     36   117 no warnings 'redefine';
  36         42  
  36         1022  
23 36     36   124 use Scalar::Util qw(blessed);
  36         34  
  36         1283  
24              
25 36     36   12527 use RDF::Query::Node::Blank;
  36         67  
  36         1435  
26 36     36   13145 use RDF::Query::Node::Literal;
  36         78  
  36         1617  
27 36     36   12629 use RDF::Query::Node::Resource;
  36         100  
  36         1557  
28 36     36   191 use RDF::Query::Node::Variable;
  36         54  
  36         3376  
29              
30             our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS);
31             BEGIN {
32 36     36   64 $VERSION = '2.918';
33            
34 36         163 require Exporter;
35 36         2179 @ISA = qw(Exporter);
36 36         215 @EXPORT_OK = qw(iri blank literal variable);
37 36         7311 %EXPORT_TAGS = ( 'all' => [qw(iri blank literal variable)] );
38             }
39              
40             =item C<< is_variable >>
41              
42             Returns true if this RDF node is a variable, false otherwise.
43              
44             =cut
45              
46             sub is_variable {
47 1875     1875 1 300670 my $self = shift;
48 1875   66     9981 return (blessed($self) and $self->isa('RDF::Query::Node::Variable'));
49             }
50              
51             =item C<< compare ( $a, $b ) >>
52              
53             Returns -1, 0, or 1 if $a is less than, equal to, or greater than $b, respectively,
54             according to the SPARQL sorting rules.
55              
56             =cut
57              
58             sub compare {
59             my $a = shift;
60             my $b = shift;
61             warn 'compare';
62             for ($a, $b) {
63             unless ($_->isa('RDF::Query::Node')) {
64             $_ = RDF::Query::Node->from_trine( $_ );
65             }
66             }
67            
68             local($RDF::Query::Node::Literal::LAZY_COMPARISONS) = 1;
69             return $a <=> $b;
70             }
71              
72             =item C<< from_trine ( $node ) >>
73              
74             Returns a new RDF::Query::Node object with the same value as $node, a
75             RDF::Trine::Node object. This essentially promotes C<< $node >> to a
76             node object with extra functionality provided by the RDF::Query package
77             (like SPARQL-defined ordering).
78              
79             =cut
80              
81             sub from_trine {
82 1120     1120 1 1065 my $class = shift;
83 1120         932 my $n = shift;
84 1120 50       7505 if ($n->isa('RDF::Trine::Node::Variable')) {
    100          
    100          
    100          
    50          
85 0         0 return RDF::Query::Node::Variable->new( $n->name );
86             } elsif ($n->isa('RDF::Trine::Node::Literal')) {
87 294         652 return RDF::Query::Node::Literal->new( $n->literal_value, $n->literal_value_language, $n->literal_datatype );
88             } elsif ($n->isa('RDF::Trine::Node::Resource')) {
89 375         869 return RDF::Query::Node::Resource->new( $n->uri_value );
90             } elsif ($n->isa('RDF::Trine::Node::Blank')) {
91 93         232 return RDF::Query::Node::Blank->new( $n->blank_identifier );
92             } elsif ($n->isa('RDF::Trine::Node::Nil')) {
93 358         854 return $n;
94             } else {
95 36     36   178 use Data::Dumper;
  36         57  
  36         5422  
96 0         0 Carp::confess "from_trine called with unrecognized node type:" . Dumper($n);
97             }
98             }
99              
100             =item C<< from_attean ( $node ) >>
101              
102             Likewise, but from L<Attean>.
103              
104             =cut
105              
106             sub from_attean {
107 0     0 1 0 my $class = shift;
108 0         0 my $n = shift;
109 0 0       0 if ($n->does('Attean::API::Variable')) {
    0          
    0          
    0          
110 0         0 return RDF::Query::Node::Variable->new( $n->value );
111             } elsif ($n->does('Attean::API::Literal')) {
112 0         0 return RDF::Query::Node::Literal->new( $n->value, $n->language, $n->datatype );
113             } elsif ($n->does('Attean::API::IRI')) {
114 0         0 return RDF::Query::Node::Resource->new( $n->as_string );
115             } elsif ($n->does('Attean::API::Blank')) {
116 0         0 return RDF::Query::Node::Blank->new( $n->value );
117             } else {
118 36     36   160 use Data::Dumper;
  36         58  
  36         9821  
119 0         0 Carp::confess "from_attean called with unrecognized node type:" . Dumper($n);
120             }
121             }
122              
123             =item C<< explain >>
124              
125             Returns a string serialization of the node appropriate for display on the
126             command line. This method is primarily used by the C<< explain >> method of
127             the subclasses of RDF::Query::Plan.
128              
129             =cut
130              
131             sub explain {
132 0     0 1 0 my $self = shift;
133 0         0 my $s = shift;
134 0         0 my $count = shift;
135 0         0 my $indent = $s x $count;
136 0         0 my $string = "${indent}" . $self->as_sparql . "\n";
137 0         0 return $string;
138             }
139              
140             =back
141              
142             =head1 FUNCTIONS
143              
144             =over 4
145              
146             =item C<< compare ( $node_a, $node_b ) >>
147              
148             Returns -1, 0, or 1 if $node_a sorts less than, equal to, or greater than
149             $node_b in the defined SPARQL ordering, respectively. This function may be
150             used as the function argument to C<<sort>>.
151              
152             =cut
153              
154             sub compare {
155 0     0 1 0 my $a = shift;
156 0         0 my $b = shift;
157 0         0 warn 'compare';
158 0         0 for ($a, $b) {
159 0 0       0 unless ($_->isa('RDF::Query::Node')) {
160 0         0 $_ = RDF::Query::Node->from_trine( $_ );
161             }
162             }
163            
164 0         0 local($RDF::Query::Node::Literal::LAZY_COMPARISONS) = 1;
165 0         0 return $a <=> $b;
166             }
167              
168              
169             =item C<< iri ( $iri ) >>
170              
171             Returns a RDF::Query::Node::Resource object with the given IRI value.
172              
173             =cut
174              
175             sub iri {
176 25     25 1 12420 my $iri = shift;
177 25         134 return RDF::Query::Node::Resource->new( $iri );
178             }
179              
180             =item C<< blank ( $id ) >>
181              
182             Returns a RDF::Query::Node::Blank object with the given identifier.
183              
184             =cut
185              
186             sub blank {
187 0     0 1   my $id = shift;
188 0           return RDF::Query::Node::Blank->new( $id );
189             }
190              
191             =item C<< literal ( $value, $lang, $dt ) >>
192              
193             Returns a RDF::Query::Node::Literal object with the given value and optional
194             language/datatype.
195              
196             =cut
197              
198             sub literal {
199 0     0 1   return RDF::Query::Node::Literal->new( @_ );
200             }
201              
202             =item C<< variable ( $name ) >>
203              
204             Returns a RDF::Query::Node::Variable object with the given variable name.
205              
206             =cut
207              
208             sub variable {
209 0     0 1   my $name = shift;
210 0           return RDF::Query::Node::Variable->new( $name );
211             }
212              
213              
214             1;
215              
216             __END__
217              
218             =back
219              
220             =head1 AUTHOR
221              
222             Gregory Todd Williams <gwilliams@cpan.org>
223              
224             =cut