File Coverage

blib/lib/TM/Easy.pm
Criterion Covered Total %
statement 25 27 92.5
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 34 36 94.4


line stmt bran cond sub pod time code
1             package TM::Easy;
2              
3 1     1   845 use strict;
  1         2  
  1         33  
4 1     1   6 use warnings;
  1         2  
  1         55  
5              
6             our $VERSION = '0.03';
7              
8 1     1   607 use TM::Easy::Map;
  1         3  
  1         23  
9 1     1   621 use TM::Easy::Topic;
  1         2  
  1         27  
10 1     1   627 use TM::Easy::Association;
  1         2  
  1         24  
11              
12 1     1   474 use TM::Tied::Map;
  1         2  
  1         22  
13 1     1   571 use TM::Tied::Topic;
  1         2  
  1         21  
14 1     1   532 use TM::Tied::Association;
  1         3  
  1         98  
15              
16             =pod
17              
18             =head1 NAME
19              
20             TM::Easy - Topic Maps, Easy Usage
21              
22             =head1 SYNOPSIS
23              
24             use TM::Easy;
25              
26             # for the couch potatoes
27             my $mm = new TM::Easy (file => 'somewhere.atm');
28             # or, alternatively
29             my $mm = new TM::Easy (inline => ' # here is AsTMa');
30             # or, alternatively from LTM
31             my $mm = new TM::Easy (file => 'somewhereelse.ltm');
32              
33             # more flexibility when doing it in several steps:
34             # acquire a map from somewhere, any map should do (see TM)
35             use TM;
36             my $tm = new TM;
37              
38             # create an Easy Access (I'm sure some stupid company has a trademark on this)
39             my $mm = new TM::Easy::Map ($tm);
40              
41             # walking, walking, ...
42             foreach my $tid (keys %$mm) { # iterate over all toplet identifiers
43             ...
44             }
45              
46             warn "oh no" unless $mm->{hubert}; # check whether a topic exists with this local identifier
47             my $hubert = $mm->{hubert}; # using the local identifier
48              
49             print $hubert->{'!'}; # the local identifier, TMQL notation
50             my $add = $hubert->{'='}; # the subject address, TMQL notation
51             my @sin = $hubert->{'~'}; # the subject identifiers (as list reference, TMQL notation)
52              
53             print $hubert->{name_s} # get a list of names
54             print $hubert->{name} # get _some_ name (or undef if there is none)
55             print $hubert->{occurrence_s} # get a list of all occurrences
56             print $hubert->{occurrence} # get a scalar with _some_ occurrence
57             print $hubert->{blog_s} # list of all blog characteristics
58             print $hubert->{blog} # get occurrence (or name) with this type, one value
59              
60             my $a = $hubert->{-owner}; # get association where hubert is owner
61             my $a = $hubert->{'<- owner'}; # same, but with TMQL notation
62              
63             foreach my $role (keys %$a) { # iterate over all roles
64             warn $a->{$role} # output ONE player (does not work if there are several!)
65             warn $a->{"-> $role"} # same with TMQL notation
66              
67             warn $a->{"${role}_s"}; # get list of all role players
68             warn $a->{"-> ${role}_s"}; # same, with TMQL notation
69             }
70              
71             foreach (@{ $hubert->{'<-> likes'} }) { # iterate over all on the other side(s)
72             ...
73             }
74              
75              
76             =head1 ABSTRACT
77              
78             This package provides a HASH-like access to a topic map. For this purpose, first a given topic map
79             object will be tied to a hash and then the user can access certain aspects of a topic map via
80             keys. The same holds true for topics and associations.
81              
82             =head1 DESCRIPTION
83              
84             This abstraction layer provides a rather simplified view on topic map content. It pretends that a
85             topic map is a hash, a topic is a hash and an association is an hash. In that, this package offers
86             access to topics in maps, names and occurrences in topics and roles and players in associations via
87             keys.
88              
89             Unsurprisingly, this package does not harness all the beauty (or ugliness) of L.
90              
91             B: At the moment, we support only reading. That may change in the future.
92              
93             =head2 Maps as Hashes
94              
95             =over
96              
97             =item keys: get all local toplet identifiers
98              
99             keys I<%$map>
100              
101             =item fetch
102              
103             =over
104              
105             =item get the toplet with this toplet identifier
106              
107             I<$map>->{xxx}
108              
109             =item get the toplet with this subject identifier
110              
111             I<$map>->{'http://... ~'}
112              
113             =item same
114              
115             I<$map>->{'http://...'}
116              
117             =item get the toplet with this subject address
118              
119             I<$map>->{'http://... ='}
120              
121             =back
122              
123             =item exists: check whether the toplet exists
124              
125             exists I<$map>->{xxx}
126              
127             exists I<$map>->{'http://... ~'}
128              
129             exists I<$map>->{'http://... ='}
130              
131             =back
132              
133             =head2 Topics as Hashes
134              
135             =over
136              
137             =item identification
138              
139             I<$t>->{'!'} # get local identifier
140              
141             I<$t>->{'='} # get subject address (or undef)
142              
143             I<$t>->{'~'} # get subject identifiers (as list reference)
144              
145             =item characteristics
146              
147             I<$t>->{name} # get ONE name
148              
149             I<$t>->{name_s} # get ALL names (as list reference)
150              
151             I<$t>->{nickname} # get ONE name of this type
152              
153             I<$t>->{nickname_s} # get ALL names of this type (as list reference)
154              
155             I<$t>->{homepage} # get ONE occurrence of this type
156              
157             I<$t>->{homepage_s} # get ALL occurrences of this type (as list reference)
158              
159             =item role playing
160              
161             I<$t>->{'<- in_role'} # get ONE association where toplet plays C
162              
163             I<$t>->{-in_role} # same, shorter
164              
165             I<$t>->{'<- in_role_s'} # get ALL associations where toplet plays C
166              
167             I<$t>->{-in_role_s} # same, shorter
168              
169             =back
170              
171             =head2 Associations as Hashes
172              
173             =over
174              
175             =item keys
176              
177             keys I<%$a> # all roles (role types)
178              
179             =item fetch
180              
181             I<%$a>->{out_role} # get ONE toplet playing C
182              
183             I<%$a>->{'-> out_role'} # same longer
184              
185             I<%$a>->{out_role_s} # get ALL toplets playing C
186              
187             I<%$a>->{'-> out_role_s'} # same longer
188              
189             =back
190              
191              
192             =head2 TODOs?
193              
194             $someone->{'<- husband -> wife'};
195              
196              
197             =head1 INTERFACE
198              
199             =head2 Constructor
200              
201             The constructor accepts an optional hash specifying the source of the map:
202              
203             =over
204              
205             =item C:
206              
207             The string value specifies directly a map denoted in AsTMa.
208              
209             =item C:
210              
211             The string denotes a file from where the map is consumed. If the
212             filename ends with C<.atm>, then an AsTMa file is assumed. If it ends
213             with C<.ltm>, then it will be parsed as LTM file. Otherwise the
214             machinery falls back to AsTMa.
215              
216             =back
217              
218             TODO: support XTM x.x
219              
220             =cut
221              
222             sub new {
223             my $class = shift;
224             my %provenance = @_;
225              
226             my $tm;
227             if ($provenance{inline}) {
228 1     1   592 use TM::Materialized::AsTMa;
  0            
  0            
229             $tm = new TM::Materialized::AsTMa (inline => $provenance{inline})->sync_in;
230             } elsif ($provenance{file}) {
231             if ($provenance{file} =~ /\.atm$/i) { # we assume AsTMa
232             use TM::Materialized::AsTMa;
233             $tm = new TM::Materialized::AsTMa (file => $provenance{file})->sync_in;
234             } elsif ($provenance{file} =~ /\.ltm$/i) { # most likely LTM
235             use TM::Materialized::LTM;
236             $tm = new TM::Materialized::LTM (file => $provenance{file})->sync_in;
237             } elsif ($provenance{file} =~ /\.ctm$/i) { # most likely CTM
238             use TM::Materialized::CTM;
239             $tm = new TM::Materialized::CTM (file => $provenance{file})->sync_in;
240             } elsif ($provenance{file} =~ /\.xtm$/i) { # most likely XTM
241             use TM::Materialized::XTM;
242             $tm = new TM::Materialized::XTM (file => $provenance{file})->sync_in;
243             } else { # assume it is astma
244             use TM::Materialized::AsTMa;
245             $tm = new TM::Materialized::AsTMa (file => $provenance{file})->sync_in;
246             }
247             } elsif (keys %provenance) {
248             die "do not understand how to source the map";
249             } else { # keep it empty
250             use TM;
251             $tm = new TM;
252             }
253             my $mm = new TM::Easy::Map ($tm);
254             return $mm;
255             }
256              
257             =pod
258              
259             =head2 Methods
260              
261             =over
262              
263             =item C
264              
265             This read-only method gives you access to the underlying L object.
266              
267             =cut
268              
269             # parked in TM::Easy::Map
270              
271             =pod
272              
273             =back
274              
275             =head1 SEE ALSO
276              
277             L
278              
279             =head1 CREDITS
280              
281             All this was strongly inspired by the Mappa project (Lars Heuer).
282              
283             =head1 COPYRIGHT AND LICENSE
284              
285             Copyright 200[8] by Robert Barta, Edrrho@cpan.orgE
286              
287             This library is free software; you can redistribute it and/or modify it under the same terms as Perl
288             itself.
289              
290             Work under a Research Grant by the Austrian Research Centers Seibersdorf.
291              
292             =cut
293              
294             our $REVISION = '$Id$';
295              
296             1;