File Coverage

blib/lib/RDF/Helper.pm
Criterion Covered Total %
statement 22 25 88.0
branch 1 4 25.0
condition 1 6 16.6
subroutine 5 5 100.0
pod 1 1 100.0
total 30 41 73.1


line stmt bran cond sub pod time code
1             package RDF::Helper;
2 11     11   569961 use 5.10.1;
  11         28  
3 11     11   5001 use Moose;
  11         3314994  
  11         69  
4             our $VERSION = '1.99_02';
5              
6 11     11   58351 use RDF::Helper::Statement;
  11         31  
  11         454  
7 11     11   5979 use RDF::Helper::Object;
  11         29  
  11         3977  
8              
9             has backend => (
10             does => 'RDF::Helper::API',
11             is => 'ro',
12             required => 1,
13             handles => 'RDF::Helper::API',
14             );
15              
16             sub BUILDARGS {
17 11     11 1 6541117 my $this = shift;
18 11         109 my $args = $this->SUPER::BUILDARGS(@_);
19 11 50       138 return $args if $args->{backend};
20              
21 11         31 my $class = delete $args->{BaseInterface};
22              
23             $class = 'RDF::Redland'
24             if (!$class
25             && $args->{Model}
26 11 0 33     37 && $args->{Model}->isa('RDF::Redland::Model') );
      0        
27              
28 11         22 given ($class) {
29 11         79 when (qr/RDF::Helper::.*/) { }
30 11         37 when ('RDF::Redland') { $class = 'RDF::Helper::RDFRedland'; };
  0         0  
31 11         21 default { $class = 'RDF::Helper::RDFTrine' }
  11         39  
32             }
33              
34 11         61 Class::MOP::load_class($class);
35 0           my $backend = $class->new(%$args);
36 0           return { backend => $backend };
37             }
38              
39             1;
40             __END__
41              
42             =head1 NAME
43              
44             RDF::Helper - Provide a consistent, high-level API for working with RDF with Perl
45              
46             =head1 SYNOPSIS
47              
48             use RDF::Helper;
49              
50             my $rdf = RDF::Helper->new(
51             BaseInterface => 'RDF::Trine',
52             namespaces => {
53             dct => 'http://purl.org/dc/terms/',
54             rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
55             '#default' => "http://purl.org/rss/1.0/",
56             }
57             );
58              
59             =head1 DESCRIPTION
60              
61             This module intends to simplify, normalize and extend Perl's existing
62             facilites for interacting with RDF data.
63              
64             RDF::Helper's goal is to offer a syntactic sugar which will enable
65             developers to work more efficiently. To achieve this, it implements
66             methods to work with RDF in a way that would be familiar to Perl
67             programmers who are less experienced with RDF.
68              
69             It builds on L<RDF::Trine>, which in turn provides the low-level API
70             which is closer to RDF.
71              
72             =head1 CONSTRUCTOR OPTIONS
73              
74             my $rdf = RDF::Helper->new(
75             BaseInterface => 'RDF::Trine',
76             namespaces => {
77             dc => 'http://purl.org/dc/terms/',
78             rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
79             '#default' => "http://purl.org/rss/1.0/",
80             },
81             ExpandQNames => 1
82             );
83              
84             =head2 BaseInterface
85              
86             The C<BaseInterface> option expects a string that corresponds to the
87             class name of the underlying Perl RDF library that will be used by
88             this instance of the Helper. L<RDF::Trine> is the default, but
89             C<RDF::Redland> is retained as an option for historical reasons, but
90             may be removed in the future.
91              
92             =head2 Model
93              
94             The C<Model> option expects a blessed instance object of the RDF model
95             that will be operated on with this instance of the Helper. Obviously,
96             the type of object passed should correspond to the L<BaseInterface>
97             used (L<RDF::Trine::Model> for a BaseInterface of L<RDF::Trine>,
98             etc.). If this option is omitted, a new, in-memory model will be
99             created.
100              
101             =head2 namespaces
102              
103             The C<namespaces> option expects a hash reference of prefix/value
104             pairs for the namespaces that will be used with this instance of the
105             Helper. The special '#default' prefix is reserved for setting the
106             default namespace.
107              
108             For convenience, the L<RDF::Helper::Constants> class will export a
109             number of useful constants that can be used to set the namespaces for
110             common grammars:
111              
112             use RDF::Helper;
113             use RDF::Helper::Constants qw(:rdf :rss1 :foaf);
114              
115             my $rdf = RDF::Helper->new(
116             BaseInterface => 'RDF::Trine',
117             namespaces => {
118             rdf => RDF_NS,
119             rss => RSS1_NS,
120             foaf => FOAF_NS
121             },
122             ExpandQNames => 1
123             );
124              
125             =head2 ExpandQNames
126              
127             Setting a non-zero value for the C<ExpandQNames> option configures the
128             current instance of the Helper to allow for qualified URIs to be used
129             in the arguments to many of the Helper's convenience methods. For
130             example, given the L<namespaces> option for the previous example, with
131             C<ExpandQNames> turned on, the following will work as expected.
132              
133             $rdf->assert_resource( $uri, 'rdf:type', 'foaf:Person' );
134              
135             With C<ExpandQNames> turned off, you would have to pass the full URI
136             for both the C<rdf:type> predicate, and the C<foaf:Person> object to
137             achieve the same result.
138              
139             =head2 base_uri
140              
141             If specified, this option sets what the base URI will be when working
142             with so called abbreviated URIs, like C<#me>. If you do not specify
143             an explicit base_uri option, then one will be created automatically
144             for you. See
145             L<http://www.w3.org/TR/rdf-syntax-grammar/#section-Syntax-ID-xml-base>
146             for more information on abbreviated URIs.
147              
148             =head1 METHODS
149              
150             =head2 new_resource
151              
152             $res = $rdf->new_resource($uri)
153              
154             Creates and returns a new resource object that represents the supplied
155             URI. In many cases this is not necessary as the methods available in
156             L<RDF::Helper> will automatically convert a string URI to the
157             appropriate object type in the back-end RDF implementation.
158              
159             =head2 new_literal
160              
161             $lit = $rdf->new_literal($text)
162             $lit = $rdf->new_literal($text, $lang)
163             $lit = $rdf->new_literal($text, $lang, $type)
164              
165             Creates and returns a new literal text object that represents the
166             supplied string. In many cases this is not necessary as the methods
167             available in L<RDF::Helper> will automatically convert the value to
168             the appropriate object type in the back-end RDF implementation.
169              
170             When it is necessary to explicitly create a literal object is when you
171             want to specify the language or datatype of the text string. The
172             datatype argument expects a Resource object or a string URI.
173              
174             =head2 new_bnode
175              
176             $bnode = $rdf->new_bnode()
177              
178             Creates and returns a new "Blank Node" that can be used as the subject
179             or object in a new statement.
180              
181             =head2 assert_literal
182              
183             $rdf->assert_literal($subject, $predicate, $object)
184              
185             This method will assert, or "insert", a new statement whose value, or "object", is a literal.
186              
187             Both the subject and predicate arguments can either take a URI object, a URI string.
188              
189             Additionally, if you used the L</ExpandQNames> option when creating
190             the L<RDF::Helper> object, you can use QNames in place of the subject
191             and predicate values. For example, "rdf:type" would be properly
192             expanded to its full URI value.
193              
194             =head2 assert_resource
195              
196             $rdf->assert_resource($subject, $predicate, $object)
197              
198             This method will assert, or "insert", a new statement whose value, or "object", is a resource.
199              
200             The subject, predicate and object arguments can either take a URI object, or a URI string.
201              
202             Like L</assert_literal>, if you used the L</ExpandQNames> option when
203             creating the L<RDF::Helper> object, you can use QNames in place of any
204             of the arguments to this method. For example, "rdf:type" would be
205             properly expanded to its full URI value.
206              
207             =head2 remove_statements
208              
209             $count = $rdf->remove_statements()
210             $count = $rdf->remove_statements($subject)
211             $count = $rdf->remove_statements($subject, $predicate)
212             $count = $rdf->remove_statements($subject, $predicate, $object)
213              
214             This method is used to remove statements from the back-end RDF model
215             whose constituent parts match the supplied arguments. Any of the
216             arguments can be omitted, or passed in as C<undef>, which means any
217             value for that triple part will be matched and removed.
218              
219             For instance, if values for the predicate and object are given, but
220             the subject is left as "undef", then any statement will be removed
221             that matches the supplied predicate and object. If no arguments are
222             supplied, then all statements in the RDF model will be removed.
223              
224             The number of statements that were removed in this operation is returned.
225              
226             =head2 update_node
227              
228             $rdf->update_node($subject, $predicate, $object, $new_object)
229              
230             This method is used when you wish to change the object value of an
231             existing statement. This method acts as an intelligent wrapper around
232             the L</update_literal> and L</update_resource> methods, and will try
233             to auto-detect what type of object is currently in the datastore, and
234             will try to set the new value accordingly. If it can't make that
235             determination it will fallback to L</update_literal>.
236              
237             Keep in mind that if you need to change a statement from having a
238             Resource to a Literal, or vice versa, as its object, then you may need
239             to invoke the appropriate update method directly.
240              
241             =head2 update_literal
242              
243             $rdf->update_literal($subject, $predicate, $object, $new_object)
244              
245             Updates an existing statement's literal object value to a new one.
246             For more information on the operation of this method, see
247             L</update_node>.
248              
249             =head2 update_resource
250              
251             $rdf->update_resource($subject, $predicate, $object, $new_object)
252              
253             Updates an existing statement's resource object value to a new one.
254             For more information on the operation of this method, see
255             L</update_node>.
256              
257             =head2 get_statements
258              
259             @stmts = $rdf->get_statements()
260             @stmts = $rdf->get_statements($subject)
261             @stmts = $rdf->get_statements($subject, $predicate)
262             @stmts = $rdf->get_statements($subject, $predicate, $object)
263              
264             This method is used to fetch and return statements from the back-end
265             RDF model whose constituent parts match the supplied arguments. Any
266             of the arguments can be omitted, or passed in as C<undef>, which means
267             any value for that triple part will be matched and returned.
268              
269             For instance, if values for the predicate and object are given, but
270             the subject is left as "undef", then any statement will be returned
271             that matches the supplied predicate and object. If no arguments are
272             supplied, then all statements in the RDF model will be returned.
273              
274             Depending on which back-end type being used, different object types
275             will be returned. For instance, if L<RDF::Trine> is used, then all
276             the returned objects will be of type L<RDF::Trine::Statement>.
277              
278             =head2 get_triples
279              
280             @stmts = $rdf->get_triples()
281             @stmts = $rdf->get_triples($subject)
282             @stmts = $rdf->get_triples($subject, $predicate)
283             @stmts = $rdf->get_triples($subject, $predicate, $object)
284              
285             This method functions in the same way as L</get_statements>, except
286             instead of the statements being represented as objects, the
287             statement's values are broken down into plain strings and returned as
288             an anonymous array. Therefore, an individual element of the returned
289             array may look like this:
290              
291             [ "http://some/statement/uri", "http://some/predicate/uri", "some object value" ]
292              
293              
294             =head2 resourcelist
295              
296             @subjects = $rdf->resourcelist()
297             @subjects = $rdf->resourcelist($predicate)
298             @subjects = $rdf->resourcelist($predicate, $object)
299              
300             This method returns the unique list of subject URIs from within the
301             RDF model that optionally match the predicate and/or object arguments.
302             Like in L</get_statements>, either or all of the arguments to this
303             method can be C<undef>.
304              
305             =head2 exists
306              
307             $result = $rdf->exists()
308             $result = $rdf->exists($subject)
309             $result = $rdf->exists($subject, $predicate)
310             $result = $rdf->exists($subject, $predicate, $object)
311              
312             Returns a boolean value indicating if any statements exist in the RDF
313             model that matches the supplied arguments.
314              
315             =head2 count
316              
317             $count = $rdf->count()
318             $count = $rdf->count($subject)
319             $count = $rdf->count($subject, $predicate)
320             $count = $rdf->count($subject, $predicate, $object)
321              
322             Returns the number of statements that exist in the RDF model that
323             matches the supplied arguments. If no arguments are supplied, it
324             returns the total number of statements in the model are returned.
325              
326             =head2 include_model
327              
328             $rdf->include_model($model)
329              
330             Include the contents of another, already opened, RDF model into the
331             current model.
332              
333             =head2 include_rdfxml
334              
335             $rdf->include_rdfxml(xml => $xml_string)
336             $rdf->include_rdfxml(filename => $file_path)
337              
338             This method will import the RDF statements contained in an RDF/XML
339             document, either from a file or a string, into the current RDF model.
340             If a L</base_uri> was specified in the L<RDF::Helper>
341             L<constructor|/"CONSTRUCTOR OPTIONS">, then that URI is used as the
342             base for when the supplied RDF/XML is imported. For instance, if the
343             hash notation is used to reference an RDF node
344             (e.g. C<E<lt>rdf:Description rdf:about="#dahut"/E<gt>>), the
345             L</base_uri> will be prepended to the C<rdf:about> URI.
346              
347             =head2 serialize
348              
349             $string = $rdf->serialize()
350             $string = $rdf->serialize(format => 'ntriple')
351             $rdf->serialize(filename => 'out.rdf')
352             $rdf->serialize(filename => 'out.n3', format => 'ntriple')
353              
354             Serializes the back-end RDF model to a string, using the specified
355             format type, or defaulting to abbreviated RDF/XML. The serialization
356             types depends on which RDF back-end is in use. The L<RDF::Trine>
357             support within L<RDF::Helper> supports the following serialization
358             types:
359              
360             =over 4
361              
362             =item * ntriples
363             =item * nquads
364             =item * rdfxml
365             =item * rdfjson
366             =item * ntriples-canonical
367             =item * turtle
368              
369             =back
370              
371             =head2 new_query
372              
373             $query_object = $obj->new_query( $query, [$base_uri, $lang_uri, $lang_name] );
374              
375             Returns an instance of the class defined by the L<QueryInterface>
376             argument passed to the constructor (or the default class for the base
377             interface if none is explicityly set) that can be used to query the
378             currently selected model.
379              
380             =head1 PERLISH CONVENIENCE METHODS
381              
382             =head2 property_hash
383              
384             $hash_ref = $rdf->property_hash($subject)
385              
386             For instances when you don't know what properties are bound to an RDF
387             node, or when it is too cumbersome to iterate over the results of a
388             L</get_triples> method call, this method can be used to return all the
389             properties and values bound to an RDF node as a hash reference. The
390             key name will be the predicate URI (QName-encoded if a matching
391             namespace is found), and the value will be the object value of the
392             given predicate. Multiple object values for the same predicate URI
393             will be returned as an array reference.
394              
395             It is important to note that this is a read-only dump from the RDF
396             model. For a "live" alternative to this, see L</tied_property_hash>.
397              
398             =head2 deep_prophash
399              
400             $hashref = $rdf->deep_prophash($subject)
401              
402             This method is similar to the L</property_hash> method, except this
403             method will recurse over children nodes, in effect creating a nested
404             hashref datastructure representing a node and all of its associations.
405              
406             B<Note:> This method performs no checks to ensure that it doesn't get
407             stuck in a deep recursion loop, so be careful when using this.
408              
409             =head2 tied_property_hash
410              
411             $hash_ref = $rdf->tied_property_hash($subject)
412             $hash_ref = $rdf->tied_property_hash($subject, \%options)
413              
414             Like L</property_hash>, this method returns a hash reference
415             containing the predicates and objects bound to the given subject URI.
416             This method differs however in that any changes to the hash will
417             immediately be represented in the RDF model. So if a new value is
418             assigned to an existing hash key, if a new key is added, or a key is
419             deleted from the hash, that will transparently be represented as
420             updates, assertions or removal operations against the model.
421              
422             Optionally a hash can be passed to this method when tieing a property
423             hash to give additional instructions to the
424             L<RDF::Helper::RDFRedland::TiedPropertyHash> object. Please see the
425             documentation in that class for more information.
426              
427             =head2 get_object
428              
429             $obj = $rdf->get_object($subject, %options)
430             $obj = $rdf->get_object($subject, \%options)
431              
432             Returns an instance of L<RDF::Helper::Object> bound to the given
433             subject URI. This exposes that RDF node as an object-oriented class
434             interface, allowing you to interact with and change that RDF node and
435             its properties using standard Perl-like accessor methods. For more
436             information on the use of this method, please see
437             L<RDF::Helper::Object>.
438              
439              
440             =head2 arrayref2rdf
441              
442             $obj->arrayref2rdf(\@list, $subject, $predicate);
443             $obj->arrayref2rdf(\@list, undef, $predicate);
444              
445             Asserts a list of triples with the the subject C<$subject>, predicate
446             C<$predicate> and object(s) contained in C<\@list>. It the subject is
447             undefined, a new blank node will be used.
448              
449             =head2 hashref2rdf
450              
451             $object->hashref2rdf( \%hash );
452             $object->hashref2rdf( \%hash, $subject );
453              
454             This method is the reverse of L</property_hash> and L</deep_prophash>
455             in that it accpets a Perl hash reference and unwinds it into a setions
456             of triples in the RDF store. If the C<$subject> is missing or
457             undefined a new blank node will be used.
458              
459              
460             =head2 hashlist_from_statement
461              
462             @list = $rdf->hashlist_from_statement()
463             @list = $rdf->hashlist_from_statement($subject)
464             @list = $rdf->hashlist_from_statement($subject, $predicate)
465             @list = $rdf->hashlist_from_statement($subject, $predicate, $object)
466              
467             Accepting a sparsely populated triple pattern as its argument, this
468             methods return a list of subject/hash reference pairs for all
469             statements that match the pattern. Each member in the list will have
470             the following structure:
471              
472             [ $subject, $hash_reference ]
473              
474             =head1 ACCESSOR METHODS
475              
476             =head2 model
477              
478             $model = $rdf->model()
479             $rdf->model($new_model)
480              
481             An accessor method that can be used to retrieve or set the back-end
482             RDF model that this L<RDF::Helper> instance uses.
483              
484             =head2 query_interface
485              
486             $iface = $rdf->query_interface()
487             $rdf->query_interface($iface)
488              
489             Accessor method that is used to either set or retrieve the current
490             class name that should be used for composing and performing queries.
491              
492             =head1 SEE ALSO
493              
494             L<RDF::Helper::Object>; L<RDF::Trine>, L<RDF::Redland>; L<RDF::Query>
495              
496              
497             =head1 SUPPORT
498              
499             There is a mailing list at L<http://lists.perlrdf.org/listinfo/dev>.
500              
501             A bunch of people are also hanging out in C<#perlrdf> on C<irc.perl.org>.
502              
503              
504             =head1 AUTHOR
505              
506             Kip Hampton, E<lt>khampton@totalcinema.com<gt>
507              
508             =head1 COPYRIGHT AND LICENSE
509              
510             Copyright 2004-2011 by Kip Hampton, Chris Prather, Mike Nachbaur
511              
512             This library is free software; you can redistribute it and/or modify
513             it under the same terms as Perl itself.
514              
515             =cut