Simple.pm

filter: Statementstmt 73.9% 236 / 319 Branchbran 56.5% 121 / 214 Conditioncond 51.3% 57 / 111 MC/DCmcdc 21.5% 14 / 65 Subroutinesub 92.3% 24 / 26 Podpod 100% 18 / 18 totaltotal 62.4% 470 / 753 CC 164 SCAR 40.9
CC
164
Cov
64%
addHashref
182.0
new
97.0
is_uri
58.9
CRAP 59.7
↑ Summary t1.pl »
1 package SKOS::Simple;
2 {
3 6 $SKOS::Simple::VERSION = '0.09_01';
4 }
5 #ABSTRACT: Create simple SKOS data with entailment
6  
7 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Turtle::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 DESCRIPTION
15  
16 This module provides a simple class to create and handle classifications,
17 thesauri and similar systems in Simple Knowledge Organization System (SKOS)
18 data model. Most features of SKOS, as specified at
19 L<http://www.w3.org/TR/skos-reference/>, are supported. In addition there
20 are some useful constraints, which mostly derive from best-practice.
21  
22 In contrast to other RDF-related modules, SKOS::Simple does not depend on
23 any non-core Perl modules, so you can install it by just copying one file.
24 The module implements basic entailment rules of the SKOS standard without
25 the burden of a full RDF reasoning engine. Actually, you can use this
26 module without having to deal with any details of RDF.
27  
28 The current version of this class is optimized form creating and serializing
29 valid SKOS schemes, but not for reading and modifying them. A common use case
30 of SKOS::Simple is to transform a given terminology from some custom format
31 to SKOS, which is then L<serialized|/"SERIALIZATION METHODS"> in Terse RDF
32 Triple language (Turtle). You can then publish the Turtle data and/or process
33 them with general RDF and SKOS tools.
34  
35 =head1 SYNOPSIS
36  
37 my $skos = SKOS::Simple->new(
38 base => 'http://example.com/kos/',
39 title => 'My little Knowledge Organization System',
40 hierarchy => 'tree' # check classification constraints
41 );
42  
43 $skos->addConcept( pref => { en => 'foo', ru => 'baz' } );
44 $skos->addConcept( notation => '42.X-23' );
45  
46 print $skos->turtle;
47  
48 =cut
49  
50 6 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 6 our @EXPORT_OK = qw(skos);
52  
53 6 our %NAMESPACES = (
54 rdf => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
55 skos => 'http://www.w3.org/2004/02/skos/core#',
56 dc => 'http://purl.org/dc/elements/1.1/',
57 foaf => 'http://xmlns.com/foaf/0.1/',
58 void => 'http://rdfs.org/ns/void#',
59 dct => 'http://purl.org/dc/terms/',
60 xsd => 'http://www.w3.org/2001/XMLSchema#',
61 skosxl => 'http://www.w3.org/2008/05/skos-xl#',
62 owl => 'http://www.w3.org/2002/07/owl#',
63 );
64  
65 =head1 SKOS CONFORMANCE
66  
67 The following classes and properties from the SKOS vocabulary are supported.
68 The numbers in brackets (B<[Sxx]>) refer to integrity conditions from the
69 SKOS specification. Additional constraints are marked by numbers like B<[Cxx]>.
70  
71 =over 4
72  
73 =item L<Concepts|http://www.w3.org/TR/skos-reference/#concepts>
74  
75 Instances of C<skos:Concept> B<[S1]> are not represented as objects but as
76 parts of a SKOS::Simple object, so every Concepts must belong to a Concept
77 Scheme B<[C1]>. You can attach Concepts to a scheme with L</addConcept>.
78  
79 =item L<Concept Schemes|http://www.w3.org/TR/skos-reference/#notations>
80  
81 Instances of C<skos:ConceptScheme> are implemented as objects of type
82 SKOS::Simple B<[S2]>. Concepts added to a scheme are automatically connected
83 to the scheme via C<skos:inScheme> B<[S3-S4]> (only serialized if requested).
84 Concepts can be selected as top concepts (C<skos:hasTopConcept> /
85 C<skos:topConceptOf> B<[S5-S8]>). In contrast to the SKOS specification,
86 (L<see 4.6.3|http://www.w3.org/TR/skos-reference/#L2446>) the top concepts
87 of a scheme cannot have broader concepts in the same scheme B<[C2]>.
88  
89 Concepts and concept schemes must be disjoint B<[S9]>. This is ensured only
90 if you do not use a C<scheme> but a C<base> parameter.
91  
92 =item L<Lexical Labels|http://www.w3.org/TR/skos-reference/#labels>
93  
94 The label types C<skos:prefLabel>, C<skos:altLabel>, and C<skos:hiddenLabel>
95 are supported. In addition this module supports the label properties
96 C<dc:title> and C<dc:identifier>, which are not explicitly included in the
97 SKOS reference.
98  
99 C<rdfs:label> as super-property of C<skos:prefLabel>, C<skos:altLabel>,
100 and C<skos:hiddenLabel> will be supported in a later version.
101  
102 =item L<Notations|http://www.w3.org/TR/skos-reference/#notations>
103  
104 Notations (C<skos:notation>) must be unique per concept and scheme
105 B<[C3]>.
106  
107 =cut
108  
109 6 our %LABEL_PROPERTIES = map { $_ => 1 } qw(prefLabel altLabel hiddenLabel);
110  
111 6 our %LABEL_TYPES = (
112 pref => 'skos:prefLabel', # TODO: sub-property of rdfs:label
113 alt => 'skos:altLabel', # dito
114 hidden => 'skos:hiddenLabel', # dito
115 title => 'dc:title'
116 );
117  
118 =item L<Semantic Relations|http://www.w3.org/TR/skos-reference/#semantic-relations>
119  
120 So called "semantic" relations (C<skos:semanticRelation>) include
121 C<skos:broader>, C<skos:narrower>, and C<skos:related>.
122  
123 The C<skos:broaderTransitive> and C<skos:narrowerTransitive> are currently
124 not supported.
125  
126 With this module you can only model semantic relations between concepts
127 in the same scheme.
128  
129 =cut
130  
131 6 our %RELATION_PROPERTIES = map { $_ => 1 } qw(semanticRelation
132 broader narrower related broaderTransitive narrowerTransitive);
133  
134 =item L<Documentation properties|http://www.w3.org/TR/skos-reference/#notes>
135  
136 C<skos:note>, C<skos:changeNote>, C<skos:definition>, C<skos:editorialNote>,
137 C<skos:example>, C<skos:historyNote> and C<skos:scopeNote> can be used to
138 document concepts. In contrast to the SKOS specification their range is
139 limited to literal values B<[C4]>.
140  
141 =cut
142  
143 6 our %NOTE_PROPERTIES = map { $_ => 1 } qw(note
144 changeNote definition editorialNote example historyNote scopeNote);
145  
146 =back
147  
148 Concept Collections (C<skos:Collection>, C<skos:OrderedCollection>,
149 C<skos:member>, C<skos:memberList>) and SKOS extension for labels
150 (C<skosxl:Label> etc.) are not supported. Mapping Properties
151 (C<skos:mappingRelation>, C<skos:closeMatch>, C<skos:exactMatch>,
152 C<skos:broadMatch>, C<skos:narrowMatch>, C<skos:relatedMatch>) will
153 probably be implemented in another module.
154  
155 =cut
156  
157 6 our %MAPPING_PROPERTIES = map { $_ => 1 } qw(mappingRelation
158 closeMatch exactMatch broadMatch narrowMatch relatedMatch);
159  
160 # dublin core elements
161 6 our %DC_PROPERTIES = map { $_ => "dc:$_" }
162 qw(contributor coverage creator date description format identifier
163 language publisher relation rights source subject title type);
164  
165 # To add RDF data beyond the SKOS data model, you can use the
166 # L<serializing methods|/"EXPORTABLE FUNCTIONS">.
167  
168 =head1 ADDITIONAL CONSTRAINTS
169  
170 The current version of this module aims at classifications.
171 Support for thesauri will be implemented later.
172  
173 An instance of SKOS::Simple holds exactely one skos:ConceptScheme with
174 the following properties:
175  
176 =over 4
177  
178 =item *
179  
180 All concepts share a common URI base. By default this common prefix is
181 also the URI of the concept scheme as whole.
182  
183 =item *
184  
185 All concepts must be identifyable by a unique string, that is refered
186 to as the concept identifier. The URI of a concept is build of the
187 common URI prefix and the concept's identifier. The identifier must
188 either be the skos:notation (so every concept must have one), or the
189 skos:prefLabel in one fixed language for all concepts. The only exception
190 to this rule are filters, for instance to uri-encode the prefLabel/notation.
191  
192 =item *
193  
194 Empty strings as literal values are ignored. In most cases you can use
195 C<undef> and C<""> interchangeably.
196  
197 =item *
198  
199 All notations have the same Datatype URI (this may be changed).
200  
201 =item *
202  
203 The range of all documentation properties (C<skos:note>, C<skos:example>,
204 C<skos:scopeNote> etc.) is the plain literals instead of any resource.
205  
206 =item *
207  
208 I<...sure there are some more limitations...>
209  
210 =back
211  
212 =head1 METHODS
213  
214 =head2 new( [ %properties ] )
215  
216 Creates a new concept scheme with the following properties:
217  
218 =over 4
219  
220 =item base
221  
222 The URI prefix that is used for all concepts (not required but recommended).
223  
224 =item scheme
225  
226 The URI of the whole concept scheme (C<skos:conceptScheme>).
227 By default the C<base> property is used as concept scheme URI.
228  
229 =item title
230  
231 Title(s) of the concept scheme. Must be either a string or a
232 hash that maps language tags to strings.
233  
234 =item namespaces
235  
236 An optional hash with additional namespaces. You can also override standard
237 namespaces (e.g. C<skos>). All namespaces explicitly specified by this
238 parameter are always included as C<< @prefix >> in the Turtle output.
239  
240 =item language
241  
242 Language tag of the default language. By default set to C<en>.
243  
244 =item hierarchy
245  
246 Either C<tree> or C<thesaurus> or the empty string for no hierarchy check
247 (default). At the moment only C<tree> is supported.
248  
249 =item identity
250  
251 Specifies which property is used as concept identifier. Possible
252 values are C<label> (C<skos:prefLabel>), C<notation> (C<skos:notation>,
253 and C<identifier> (C<dc:identifier>). If no value is given, you must
254 either specify C<label> or C<notation> as C<unique>.
255  
256 =item label
257  
258 Specifies how to encode concept labels. Possible values are C<pref>
259 (C<skos:prefLabel>), which is the default value and implied if
260 C<< identify => 'label' >>, C<alt> (C<skos:altLabel>,
261 C<hidden> (C<skos:hiddenLabel>) and C<title> (C<dc:title>).
262  
263 =item notation
264  
265 Specifies whether to check notations to be unique (C<unique>) and/or
266 mandatory (C<mandatory>) per concept. C<< identity => 'notation' >>
267 implies C<< notation => 'unique' >>.
268  
269 =item properties
270  
271 Additional properties as structured Turtle. Triples with predicates
272 C<a>, C<dc:title>, and C<skos:...> are not allowed but removed.
273  
274 =item description
275  
276 not supported yet.
277  
278 =back
279  
280 =cut
281  
282 15 ▶sub new {
Subroutine new: called CC=44 SCAR=45.7
Pod documented
283 15 my $class = shift;
284 15 my (%arg) = @_;
285  
286 my $self = bless( {
287 concepts => { },
288  
289 prop => { },
290 related => { },
291 top => { }, # ids of top concepts
292  
293 u_notation => { },
294 u_label => { },
295  
296 hierarchy => ($arg{hierarchy} || ""), # tree|thesaurus|
297 base => ($arg{base} || ""),
298 scheme => ($arg{scheme} || ""),
299 title => ($arg{title} || ""),
300 namespaces => ($arg{namespaces} || { }), # TODO: count usage (?)
301 language => ($arg{language} || "en"), # TODO: check tag
302 description => ($arg{description} || ""), # TODO: add
303  
304 12 ▶ idescape => $arg{idescape} || sub { $_[0]; }, # TODO: uri-escape
Subroutine __ANON__: called CC=1 SCAR=0.0
305  
306 identity => ($arg{identity} || ""),
307 label => ($arg{label} || ""), # label type
308 15 ▶ notation => ($arg{notation} || ""),
Condition78%18/23
$arg{'hierarchy'} || ''
l!l
015
$arg{'base'} || ''
l!l
510
$arg{'scheme'} || ''
l!l
213
$arg{'title'} || ''
l!l
312
$arg{'namespaces'} || {}
l!l
312
$arg{'language'} || 'en'
l!l
213
$arg{'description'} || ''
l!l
015
$arg{'idescape'} || sub { $_[0]; }
l!l&&r!l&&!r
0150
$arg{'identity'} || ''
l!l
114
$arg{'label'} || ''
l!l
015
$arg{'notation'} || ''
l!l
213
MC/DC0%0/1
$arg{'hierarchy'} || ''
$arg{'hierarchy'}
MC/DC100%1/1
$arg{'base'} || ''
$arg{'base'}
MC/DC100%1/1
$arg{'scheme'} || ''
$arg{'scheme'}
MC/DC100%1/1
$arg{'title'} || ''
$arg{'title'}
MC/DC100%1/1
$arg{'namespaces'} || {}
$arg{'namespaces'}
MC/DC100%1/1
$arg{'language'} || 'en'
$arg{'language'}
MC/DC0%0/1
$arg{'description'} || ''
$arg{'description'}
MC/DC0%0/2
$arg{'idescape'} || sub { $_[0]; }
$arg{'idescape'} sub { $_[0]; }
MC/DC100%1/1
$arg{'identity'} || ''
$arg{'identity'}
MC/DC0%0/1
$arg{'label'} || ''
$arg{'label'}
MC/DC100%1/1
$arg{'notation'} || ''
$arg{'notation'}
Truth table50%1/2
A
A= $arg{'hierarchy'}
Aresult
11
00
Truth table100%2/2
A
A= $arg{'base'}
Aresult
11
00
Truth table100%2/2
A
A= $arg{'scheme'}
Aresult
11
00
Truth table100%2/2
A
A= $arg{'title'}
Aresult
11
00
Truth table100%2/2
A
A= $arg{'namespaces'}
Aresult
11
00
Truth table100%2/2
A
A= $arg{'language'}
Aresult
11
00
Truth table50%1/2
A
A= $arg{'description'}
Aresult
11
00
Truth table33%1/3
A || B
A= $arg{'idescape'}B= sub { $_[0]; }
ABresult
1X1
011
000
Truth table100%2/2
A
A= $arg{'identity'}
Aresult
11
00
Truth table50%1/2
A
A= $arg{'label'}
Aresult
11
00
Truth table100%2/2
A
A= $arg{'notation'}
Aresult
11
00
309  
310 }, $class );
311  
312 # TODO: croak "base of scheme missing" unless $self->{base};
313 15 ▶ croak "base must be an URI" unless uri_or_empty($self->{base});
Branch: 1 branch
BranchTrueFalseTotal
unless uri_or_empty($self->{"base"}) 0 15 15
314 15 ▶ croak "scheme must be an URI" unless uri_or_empty($self->{scheme});
Branch: 1 branch
BranchTrueFalseTotal
unless uri_or_empty($self->{"scheme"}) 0 15 15
315 15 ▶ $self->{scheme} = "" if $self->{scheme} eq $self->{base};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"scheme"} eq $self->{"base"} 11 4 15
316  
317 #### identifier, label, notation
318  
319 15 ▶ if ( !$self->{identity} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->{'identity'}) { } 14 1 15
elsif (not grep({$self->{'identity'} eq $_;} 'label', 'notation', 'identifier')) { } 0 1 1
320 14 ▶ if ( $self->{label} eq 'unique' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'label'} eq 'unique') { } 0 14 14
elsif ($self->{'notation'} eq 'unique') { } 2 12 14
321 0 $self->{identity} = 'label';
322 } elsif ( $self->{notation} eq 'unique' ) {
323 2 $self->{identity} = 'notation';
324 } else {
325 # TODO: do we want this?
326 12 $self->{identity} = 'label';
327 # croak "concept identification not specified";
328 }
329 3 } elsif ( !(grep { $self->{identity} eq $_ } qw(label notation identifier)) ) {
330 0 croak "concept identification must be 'label', 'notation', or 'identifier'";
331 } # NOTE: we may add 'id' (internal id) as another method
332  
333 15 ▶ if ( $self->{label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'label'}) { } 0 15 15
334 croak "label type must be one of " . join(", ", keys %LABEL_TYPES)
335 0 ▶ unless ( grep { $self->{label} eq $_ } keys %LABEL_TYPES );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$self->{"label"} eq $_;} keys %SKOS::Simple::LABEL_TYPES 0 0 0
336 } else {
337 15 $self->{label} = 'pref';
338 }
339  
340 15 ▶ if ( $self->{notation} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"notation"}) 2 13 15
341 croak "notation type can only be 'unique' or 'mandatory'"
342 2 ▶ unless ( grep { $self->{notation} eq $_ } qw(unique mandatory) );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$self->{"notation"} eq $_;} "unique", "mandatory" 0 2 2
343 }
344  
345 # mandatory namespaces
346  
347 $self->{namespaces}->{skos} = $NAMESPACES{skos}
348 15 ▶ unless $self->{namespaces}->{skos};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"namespaces"}{"skos"} 13 2 15
349  
350 $self->{namespaces}->{void} = $NAMESPACES{void}
351 15 ▶ if ( $arg{void} and not $self->{namespaces}->{void} );
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"void"} and not $self->{"namespaces"}{"void"} 0 15 15
Condition33%1/3
$arg{"void"} and not $self->{"namespaces"}{"void"}
!ll&&!rl&&r
1500
MC/DC0%0/2
$arg{"void"} and not $self->{"namespaces"}{"void"}
$arg{"void"} not $self->{"namespaces"}{"void"}
Truth table33%1/3
A and not(B)
A= $arg{"void"}B= not $self->{"namespaces"}{"void"}
ABresult
0X0
100
111
352  
353 $self->{namespaces}->{dc} = $NAMESPACES{dc}
354 15 ▶ if ( ($self->{title} || $self->{description}) && not $self->{namespaces}->{dc});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"title"} || $self->{"description"} and not $self->{"namespaces"}{"dc"} 2 13 15
Condition83%5/6
$self->{"title"} || $self->{"description"} and not $self->{"namespaces"}{"dc"}
!ll&&!rl&&r
1212
$self->{"title"} or $self->{"description"}
l!l&&r!l&&!r
3012
MC/DC66%2/3
$self->{"title"} || $self->{"description"} and not $self->{"namespaces"}{"dc"}
$self->{"title"} $self->{"description"} not $self->{"namespaces"}{"dc"}
Truth table60%3/5
A or B and not(C)
A= $self->{"title"}B= $self->{"description"}C= not $self->{"namespaces"}{"dc"}
ABCresult
00X0
1X00
0100
1X11
0111
355  
356 # Add default language, if title without language given
357 15 my $lang = $self->{language};
358 15 ▶ if ($self->{title} && not ref($self->{title}) && $lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"title"} and not ref $self->{"title"} && $lang) 2 13 15
Condition83%5/6
$self->{"title"} and not ref $self->{"title"} && $lang
!ll&&!rl&&r
1212
ref $self->{'title'} && $lang
!ll&&!rl&&r
201
MC/DC66%2/3
$self->{"title"} and not ref $self->{"title"} && $lang
$self->{"title"} ref $self->{'title'} $lang
Truth table75%3/4
A and not(B && C)
A= $self->{"title"}B= ref $self->{'title'}C= $lang
ABCresult
0XX0
1110
10X1
1101
359 2 $self->{title} = { $lang => $self->{title} };
360 }
361  
362 15 my @types = ('skos:ConceptScheme');
363  
364 15 ▶ if ( my $pattern = delete $arg{idpattern} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pattern = delete $arg{"idpattern"}) 0 15 15
365 0 $self->{idregexp} = qr/^$pattern$/;
366 0 my $basepattern = $self->{base};
367 0 $basepattern =~ s/[.]/\\./g;
368 0 $basepattern =~ s/[?]/\\?/g; # TODO: escape more nasty stuff in base URI
369 0 $self->{idpattern} = "^$basepattern$pattern\$";
370 }
371  
372 # TODO: license (dct:license)
373  
374 15 ▶ if ( $arg{void} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"void"}) 0 15 15
375 0 $self->{void} = 1; # TODO: document this
376 0 ▶ push @types, 'void:Dataset' if $arg{void};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"void"} 0 0 0
377 0 $self->{prop}->{'void:uriSpace'} = turtle_uri($self->{base});
378 0 $self->{prop}->{'void:rootResource'} = turtle_uri($self->{base});
379 0 ▶ if ( $self->{idpattern} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"idpattern"}) 0 0 0
380 0 $self->{prop}->{'void:uriRegexPattern'} = turtle_literal($self->{idpattern});
381 }
382 }
383  
384 # TODO: allow additional types
385  
386 15 $self->{prop}->{a} = \@types;
387  
388 # additional properties
389 15 ▶ if ( $arg{properties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"properties"}) 1 14 15
390 1 my $p = $arg{properties};
391 1 my $a = delete $p->{a};
392  
393 # TODO: also filter other namespaces and full URI forms
394 1 my @s = grep { $_ =~ /skos:/ } keys %$p;
395 1 delete $p->{$_} for @s;
396  
397 1 ▶ while ( my ($key, $value) = each %$p ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$p) 1 1 2
398 1 $self->{prop}->{$key} = $value;
399 }
400 }
401  
402 $self->{prop}->{'dc:title'} =
403 15 ▶ $self->{title} eq '' ? '' : turtle_literal_list( $self->{title} );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'title'} eq '' ? : 12 3 15
404  
405 15 ▶ my $page = (delete $arg{page} || "");
Condition50%1/2
delete $arg{'page'} || ''
l!l
015
MC/DC0%0/1
delete $arg{'page'} || ''
delete $arg{'page'}
Truth table50%1/2
A
A= delete $arg{'page'}
Aresult
11
00
406  
407 15 ▶ $self->{prop}->{'foaf:page'} =
Branch: 1 branch
BranchTrueFalseTotal
$page eq '' ? : 15 0 15
408 $page eq '' ? '' : turtle_uri( $page );
409  
410 $self->{namespaces}->{foaf} = $NAMESPACES{foaf}
411 15 ▶ if ( $page ne "" and not $self->{namespaces}->{foaf} );
Branch: 1 branch
BranchTrueFalseTotal
if $page ne "" and not $self->{"namespaces"}{"foaf"} 0 15 15
Condition33%1/3
$page ne "" and not $self->{"namespaces"}{"foaf"}
!ll&&!rl&&r
1500
MC/DC0%0/2
$page ne "" and not $self->{"namespaces"}{"foaf"}
$page ne "" not $self->{"namespaces"}{"foaf"}
Truth table33%1/3
A and not(B)
A= $page ne ""B= not $self->{"namespaces"}{"foaf"}
ABresult
0X0
100
111
412  
413 15 return $self;
414 }
415  
416 70 ▶sub _values {
Subroutine _values: called CC=4 SCAR=15.0
417 # TODO: also allow hashref (?)
418 70 ▶ my @values = map { (reftype($_) && reftype($_) eq 'ARRAY') ? @$_ : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($_) && builtin::reftype($_) eq 'ARRAY' ? : 0 70 70
Condition33%1/3
builtin::reftype($_) and builtin::reftype($_) eq "ARRAY"
!ll&&!rl&&r
7000
MC/DC0%0/2
builtin::reftype($_) and builtin::reftype($_) eq "ARRAY"
builtin::reftype($_) builtin::reftype($_) eq "ARRAY"
Truth table33%1/3
A and B
A= builtin::reftype($_)B= builtin::reftype($_) eq "ARRAY"
ABresult
0X0
100
111
419 70 ▶ @values = grep { $_ and $_ ne '' } @values;
Condition66%2/3
$_ && $_ ne ''
!ll&&!rl&&r
6604
MC/DC50%1/2
$_ && $_ ne ''
$_ $_ ne ''
Truth table66%2/3
A && B
A= $_B= $_ ne ''
ABresult
0X0
100
111
420 70 return @values;
421 }
422  
423 =head2 addConcept ( %properties )
424  
425 Adds a concept with given properties. Only the identifying property to be
426 used as concept id (C<notation>, C<label>, C<identifier>, or C<id> ) is
427 mandatory. If there already is a concept with the same id, both are merged!
428  
429 Returns the id of the added or modfied concept.
430  
431 =cut
432  
433 7 ▶sub addConcept {
Subroutine addConcept: called CC=27 SCAR=39.1
Pod documented
434 7 my $self = shift;
435  
436 # ensures we have an 'id' and 'label' is a hashref
437 7 my $arg = $self->conceptentification( @_ );
438 7 my $id = $arg->{id};
439 7 my $lang = $self->{language};
440  
441 # TODO: check: Concepts and concept schemes must be disjoint [S9].
442  
443 # only for internal use
444 7 my $nocheck = delete $arg->{_nocheck};
445  
446 # connections to other concepts
447 7 my @related = _values( delete $arg->{related} );
448 7 my @broader = _values( delete $arg->{broader} );
449 7 my @narrower = _values( delete $arg->{narrower} );
450
451 7 my $concept = $self->{concepts}->{$id};
452 7 ▶ unless ($self->{concepts}->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"concepts"}{$id}) 5 2 7
453 5 $concept = $self->{concepts}->{$id} = {
454 notation => "",
455 broader => { },
456 narrower => { },
457 related => { },
458 };
459 }
460  
461 7 my $notation = delete $arg->{notation};
462 7 ▶ $notation = "" unless defined $notation;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $notation 1 6 7
463  
464 7 my $identifier = delete $arg->{identifier};
465 7 ▶ if ( defined $identifier ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $identifier) 0 7 7
466 0 $concept->{identifier} = $identifier;
467 0 $self->{uses_dc} = 1;
468 }
469  
470 7 ▶ if ( $self->{notation} ) { # 'mandatory' or 'unique'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"notation"}) 6 1 7
471 6 ▶ $concept->{notation} = $notation
Branch: 1 branch
BranchTrueFalseTotal
if defined $notation and $notation ne "" 6 0 6
Condition33%1/3
defined $notation and $notation ne ""
!ll&&!rl&&r
006
MC/DC0%0/2
defined $notation and $notation ne ""
defined $notation $notation ne ""
Truth table33%1/3
A and B
A= defined $notationB= $notation ne ""
ABresult
0X0
100
111
472 if ( defined $notation and $notation ne '' );
473 croak 'Concepts must have a notation'
474 6 ▶ if $concept->{notation} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $concept->{"notation"} eq "" 0 6 6
475 6 ▶ if ( $self->{notation} eq 'unique' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"notation"} eq "unique") 6 0 6
476 # TODO: check uniqueness, if notation is not already the idtype
477 # croak 'Concepts must be unique per notation'
478 # if $self->{u_notation}->{ $notation };
479 # $self->{u_notation}->{ $notation } = $id;
480 }
481 }
482 7 ▶ $concept->{notation} = $notation unless $notation eq "";
Branch: 1 branch
BranchTrueFalseTotal
unless $notation eq "" 6 1 7
483  
484 # add label properties
485 7 foreach my $type ( keys %LABEL_TYPES ) {
486 28 my $label = delete $arg->{$type};
487 28 ▶ next unless defined $label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label 27 1 28
488 # TODO: add to existing labels, check for unqiueness, mandatory etc.
489 1 $concept->{$type} = $label;
490 }
491  
492 # add documentation/note properties
493 7 foreach my $name ( keys %NOTE_PROPERTIES ) {
494 49 my @values = _values( delete $arg->{$name} );
495 49 ▶ if ( @values ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 0 49 49
496 0 push @{ $concept->{$name} }, @values;
497 }
498 # TODO: entail skos:note
499 }
500  
501 # additional constraint of SKOS::Simple
502 croak "concept <$id> cannot have broader if it is top concept!"
503 7 ▶ if ( @broader && $self->{top}->{$id} );
Branch: 1 branch
BranchTrueFalseTotal
if @broader and $self->{"top"}{$id} 0 7 7
Condition66%2/3
@broader and $self->{"top"}{$id}
!ll&&!rl&&r
520
MC/DC0%0/2
@broader and $self->{"top"}{$id}
@broader $self->{"top"}{$id}
Truth table66%2/3
A and B
A= @broaderB= $self->{"top"}{$id}
ABresult
0X0
100
111
504  
505 # add inverse relations
506 7 foreach my $i (@related) {
507 0 ▶ next if $concept->{related}->{$i};
Branch: 1 branch
BranchTrueFalseTotal
if $concept->{"related"}{$i} 0 0 0
508 0 ▶ $self->addConcept( related => $id, _nocheck => 1, id => $i )
Branch: 1 branch
BranchTrueFalseTotal
unless $nocheck 0 0 0
509 unless $nocheck;
510 0 $concept->{related}->{$i} = 1;
511 }
512 7 foreach my $i (@broader) {
513 2 ▶ next if $concept->{broader}->{$i};
Branch: 1 branch
BranchTrueFalseTotal
if $concept->{"broader"}{$i} 0 2 2
514 2 ▶ if ( $self->{hierarchy} eq 'tree' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"hierarchy"} eq "tree") 0 2 2
515 croak "tree property violated by <$id> skos:broader <$i>"
516 0 ▶ if %{$concept->{broader}};
Branch: 1 branch
BranchTrueFalseTotal
if %{$concept->{"broader"};} 0 0 0
517 } # TODO: if 'multi[tree]': detect loops
518
519 2 ▶ $self->addConcept( narrower => $id, _nocheck => 1, id => $i )
Branch: 1 branch
BranchTrueFalseTotal
unless $nocheck 2 0 2
520 unless $nocheck;
521 2 $concept->{broader}->{$i} = 1;
522 }
523 7 foreach my $i (@narrower) {
524 2 ▶ next if $concept->{narrower}->{$i};
Branch: 1 branch
BranchTrueFalseTotal
if $concept->{"narrower"}{$i} 0 2 2
525 2 ▶ $self->addConcept( broader => $id, _nocheck => 1, id => $i )
Branch: 1 branch
BranchTrueFalseTotal
unless $nocheck 0 2 2
526 unless $nocheck;
527 2 $concept->{narrower}->{$i} = 1;
528 }
529  
530 7 return $id;
531 }
532  
533 =head2 topConcepts ( [ @ids ] )
534  
535 Marks one or more concepts as top concepts. The given concepts must
536 already exist and must not have any broader concepts. Without parameters,
537 this methods returns a list of all top concept identifiers. Unless you
538 explicitly specify top concepts, a list of I<all> concepts without broader
539 concepts is returned. As soon as you explicitly set some top concepts,
540 they will be the I<only> top concepts. You can reset the top concepts
541 to all concepts without broader concepts, provide C<undef> as only argument.
542  
543 =cut
544  
545 13 ▶sub topConcepts {
Subroutine topConcepts: called CC=9 SCAR=36.0
Pod documented
546 13 my $self = shift;
547 13 ▶ unless ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 13 0 13
548 0 ▶ return keys %{ $self->{top} } if %{ $self->{top} };
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->{"top"};} 0 13 13
549 return grep {
550 5 not %{$self->{concepts}->{$_}->{broader}}
551 13 } keys %{$self->{concepts}};
552 }
553 0 ▶ if ( @_ == 1 && not defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not defined $_[0]) 0 0 0
Condition0%0/3
@_ == 1 and not defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and not defined $_[0]
@_ == 1 not defined $_[0]
Truth table0%0/3
A and not(B)
A= @_ == 1B= not defined $_[0]
ABresult
0X0
100
111
554 0 $self->{top} = { };
555 0 return;
556 }
557 0 foreach my $id ( @_ ) {
558 0 ▶ croak "Unknown concept <$id>" unless $self->{concepts}->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"concepts"}{$id} 0 0 0
559 0 ▶ next if $self->{top}->{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"top"}{$id} 0 0 0
560 croak "Concept <$id> must not have broader to be top concept"
561 0 ▶ if keys %{ $self->{concepts}->{broader} };
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"concepts"}{"broader"};} 0 0 0
562 0 $self->{top}->{$id} = 1;
563 }
564 }
565  
566 =head2 concept ( %properties )
567  
568 Returns the identifier of a concept with given notation and/or label.
569 It is not checked whether the given concept exists in this scheme.
570 Returns either a string or an empty string if no identifier is given.
571  
572 =cut
573  
574 5 ▶sub concept {
Subroutine concept: called CC=1 SCAR=0.0
Pod documented
575 5 my $id = "";
576 5 eval {
577 5 my $arg = shift->conceptentification( @_ );
578 4 $id = $arg->{id};
579 };
580 5 return $id;
581 }
582  
583 =head2 hasConcept ( $id | %properties )
584  
585 Returns whether there is a concept of a given id. Instead of providing
586 a specific concept C<id> you can also use unique properties (C<notation>,
587 C<label>, C<identifier>) depending on the scheme's settings.
588  
589 =cut
590  
591 3 ▶sub hasConcept {
Subroutine hasConcept: called CC=4 SCAR=15.6
Pod documented
592 3 my $self = shift;
593 3 ▶ my $id = (@_ == 1) ? $_[0] : $self->concept( @_ );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 2 3
594 3 ▶ return exists $self->{concepts}->{$id}
Branch: 1 branch
BranchTrueFalseTotal
if defined $id and $id ne "" 3 0 3
Condition33%1/3
defined $id and $id ne ""
!ll&&!rl&&r
003
MC/DC0%0/2
defined $id and $id ne ""
defined $id $id ne ""
Truth table33%1/3
A and B
A= defined $idB= $id ne ""
ABresult
0X0
100
111
595 if defined $id and $id ne "";
596  
597 0 return 0;
598 }
599  
600 =head2 concepts
601  
602 Returns a list of all concept's ids.
603  
604 =cut
605  
606 0 ▶sub concepts {
Subroutine concepts: not called CC=1 SCAR=6.9
Pod documented
607 0 my $self = shift;
608 0 return keys %{ $self->{concepts} };
609 }
610  
611 =head2 size
612  
613 Returns the total number of concepts.
614  
615 =cut
616  
617 3 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
618 3 my $self = shift;
619 3 return scalar keys %{ $self->{concepts} };
620 }
621  
622 =head2 conceptentification ( %properties )
623  
624 Checks and possibly expands some given concept properties to ensure that
625 there is a valid concept id. Returns a hashref that contains the concept
626 id (field C<id>) or croaks. Depending on the identification settings of
627 this scheme you must pass at least one of C<notation>, C<label>,
628 C<identifier>, C<id>.
629  
630 =cut
631  
632 12 ▶sub conceptentification {
Subroutine conceptentification: called CC=25 SCAR=39.8
Pod documented
633 12 my ($self, %arg) = @_;
634  
635 12 my $idtype = $self->{identity};
636 12 my $labeltype = $self->{label};
637 12 my $lang = $self->{language};
638  
639 12 ▶ $arg{label} = '' unless defined $arg{label};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{"label"} 9 3 12
640  
641 # rename 'label' parameter if given
642 12 ▶ if ( $arg{label} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"label"} ne "") 3 9 12
643 # TODO: what if arg{labeltype} is also given? better croak
644 3 $arg{$labeltype} = $arg{label};
645 }
646  
647 # set a label's default language if only a string was given
648 # expand label property to be a hashref on undef
649 12 foreach my $type ( 'label', keys %LABEL_TYPES ) {
650 60 ▶ next unless defined $arg{$type} and $arg{$type} ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{$type} and $arg{$type} ne "" 54 6 60
Condition100%3/3
defined $arg{$type} and $arg{$type} ne ""
!ll&&!rl&&r
4596
MC/DC100%2/2
defined $arg{$type} and $arg{$type} ne ""
defined $arg{$type} $arg{$type} ne ""
Truth table100%3/3
A and B
A= defined $arg{$type}B= $arg{$type} ne ""
ABresult
0X0
100
111
651 6 ▶ unless ( ref($arg{$type}) ) { # HASH
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $arg{$type}) 6 0 6
652 6 ▶ if ( $arg{$type} eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{$type} eq '') { } 0 6 6
653 0 delete $arg{$type};
654 } else {
655 6 $arg{$type} = { $lang => $arg{$type} };
656 }
657 }
658 }
659  
660 12 ▶ $arg{id} = "" unless defined $arg{id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{"id"} 9 3 12
661 12 ▶ $arg{$idtype} = "" unless defined $arg{$idtype};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{$idtype} 3 9 12
662  
663 12 ▶ if ( $arg{id} ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{'id'} ne '') { } 3 9 12
664 # derive property from id
665 3 ▶ if ( $idtype eq 'notation' or $idtype eq 'identifier' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idtype eq 'notation' or $idtype eq 'identifier') { } 3 0 3
Condition33%1/3
$idtype eq "notation" or $idtype eq "identifier"
l!l&&r!l&&!r
300
MC/DC0%0/2
$idtype eq "notation" or $idtype eq "identifier"
$idtype eq "notation" $idtype eq "identifier"
Truth table33%1/3
A or B
A= $idtype eq "notation"B= $idtype eq "identifier"
ABresult
1X1
011
000
666 3 ▶ if ( $arg{$idtype} ne "" and $arg{$idtype} ne $arg{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{$idtype} ne "" and $arg{$idtype} ne $arg{"id"}) 0 3 3
Condition33%1/3
$arg{$idtype} ne "" and $arg{$idtype} ne $arg{"id"}
!ll&&!rl&&r
300
MC/DC0%0/2
$arg{$idtype} ne "" and $arg{$idtype} ne $arg{"id"}
$arg{$idtype} ne "" $arg{$idtype} ne $arg{"id"}
Truth table33%1/3
A and B
A= $arg{$idtype} ne ""B= $arg{$idtype} ne $arg{"id"}
ABresult
0X0
100
111
667 croak "concept id cannot be both '" . $arg{id}
668 0 . "' and '" . $arg{$idtype} . "'";
669 }
670 3 $arg{$idtype} = $arg{id};
671 } else { # $idtype eq 'label'
672 0 ▶ $arg{label} = { } if $arg{label} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"label"} eq "" 0 0 0
673 0 my $l = $arg{label}->{$lang};
674 croak "concept id cannot be both '" . $arg{id} . "' and label '$l'"
675 0 ▶ if ( defined $l and $l ne "" and $l ne $arg{id} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $l and $l ne "" and $l ne $arg{"id"} 0 0 0
Condition0%0/6
defined $l and $l ne "" and $l ne $arg{"id"}
!ll&&!rl&&r
000
defined $l and $l ne ""
!ll&&!rl&&r
000
MC/DC0%0/3
defined $l and $l ne "" and $l ne $arg{"id"}
defined $l $l ne "" $l ne $arg{"id"}
Truth table0%0/4
A and B and C
A= defined $lB= $l ne ""C= $l ne $arg{"id"}
ABCresult
0XX0
10X0
1100
1111
676  
677 0 $arg{label}->{$lang} = $arg{id};
678 }
679 } else {
680 # derive id from property (TODO: url-encode if needed)
681 9 ▶ if ( $idtype eq 'notation' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($idtype eq 'notation') { } 5 4 9
elsif ($idtype eq 'identifier') { } 0 4 4
682 5 $arg{id} = $arg{notation};
683 } elsif ( $idtype eq 'identifier' ) {
684 0 $arg{id} = $arg{identifier};
685 } else { # $idtype eq 'label'
686 4 ▶ $arg{id} = $arg{$labeltype}->{ $lang } if $arg{$labeltype};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{$labeltype} 3 1 4
687 }
688 }
689  
690 12 $arg{id} = $self->{idescape}->( $arg{id} );
691  
692 12 ▶ if ( $self->{idregexp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"idregexp"}) 0 12 12
693 croak "identifier " . $arg{id} . " does not fit to pattern"
694 0 ▶ unless $arg{id} =~ $self->{idregexp};
Branch: 1 branch
BranchTrueFalseTotal
unless $arg{"id"} =~ /$self->{"idregexp"}/ 0 0 0
695 }
696  
697 12 ▶ croak "Missing $idtype as concept identifier" if $arg{id} eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"id"} eq "" 1 11 12
698  
699 # TODO: check $arg{id} for well-formedness as URI part
700  
701 11 return \%arg;
702 }
703  
704 =head2 addHashref ( $hashref )
705  
706 experimental.
707  
708 =cut
709  
710 0 ▶sub addHashref {
Subroutine addHashref: not called CC=13 SCAR=52.0
Pod documented
711 0 my ($self, $hash) = @_;
712  
713 0 my $base = $self->{base}; # may be ""
714  
715 # ignores all but the following predicates
716 my %predicates = (
717 $NAMESPACES{rdf}.'type' => 'a',
718 0 $NAMESPACES{dc}.'identifier' => 'id'
719 );
720  
721 0 my @pnames = (
722 keys %NOTE_PROPERTIES,
723 keys %RELATION_PROPERTIES,
724 keys %LABEL_PROPERTIES, 'notation' );
725  
726 0 foreach my $p (@pnames) {
727 0 $predicates{ $NAMESPACES{skos}.$p } = $p;
728 }
729  
730 0 foreach my $subject ( keys %$hash ) {
731 0 my $subj = $subject;
732 0 ▶ next unless ($subj =~ s/^$base//);
Branch: 1 branch
BranchTrueFalseTotal
unless $subj =~ s/^$base// 0 0 0
733  
734 # TODO: $self->{scheme} as subject
735  
736 0 my %concept = (); # => $subj );
737 0 $concept{notation} = $subj; # TODO: remove
738 0 my $is_concept = 0; # TODO: check
739  
740 0 foreach my $predicate ( keys %{$hash->{$subject}} ) {
741 0 ▶ my $p = $predicates{ $predicate } || next;
Condition0%0/2
$predicates{$predicate} || (next)
l!l
00
MC/DC0%0/1
$predicates{$predicate} || (next)
$predicates{$predicate}
Truth table0%0/2
A
A= $predicates{$predicate}
Aresult
11
00
742  
743 0 foreach my $object ( @{ $hash->{$subject}->{$predicate} } ) {
744 0 my $obj;
745
746 0 ▶ if ( $p =~ /^(narrower|broader)$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($p =~ /^(narrower|broader)$/) { } 0 0 0
elsif ($p =~ /^(prefLabel)/) { } 0 0 0
elsif ($p eq 'definition') { } 0 0 0
747 0 ▶ next unless $object->{type} eq 'uri';
Branch: 1 branch
BranchTrueFalseTotal
unless $object->{"type"} eq "uri" 0 0 0
748 0 $obj = $object->{value};
749 #print "$obj\n";
750 0 ▶ next unless ($obj =~ s/^$base//);
Branch: 1 branch
BranchTrueFalseTotal
unless $obj =~ s/^$base// 0 0 0
751 0 push @{$concept{$p}}, $obj;
752 } elsif ( $p =~ /^(prefLabel)/ ) {
753 0 $obj = $object->{value}; # TODO: language
754 0 $concept{label} = $obj; # TODO: unique
755 } elsif ( $p eq 'definition' ) {
756 }
757  
758 # TODO: map
759 #print "$subj $p\n";
760 }
761 }
762 0 ▶ if ( %concept ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%concept) 0 0 0
763 0 $self->addConcept( %concept );
764 }
765 }
766 }
767  
768 =head1 SERIALIZATION METHODS
769  
770 The following methods serialize the concept scheme or parts of it in
771 L<Terse RDF Triple language|http://www.w3.org/TeamSubmission/turtle/>
772 (RDF/Turtle). A valid serialization must start with some namespace
773 declarations, and a base declaration. Both are only included by the
774 C<turtle> method, but they can also be requested independently.
775 All return values end with a newline unless it is the empty string.
776  
777 A later version may also support 'hashref' format for serializing.
778  
779 =head2 turtle ( [ %options ] )
780  
781 Returns a full Turtle serialization of this concept scheme.
782 The return value is equivalent to calling C<turtleNamespaces>,
783 C<turtleBase>, C<turtleScheme>, and C<turtleConcepts>.
784  
785 The parameter C<< lean => 1 >> enables a lean serialization, which
786 does not include infereable RDF statements. Other parameters are
787 passed to C<turtleScheme> and C<turtleConcepts> as well.
788  
789 =cut
790  
791 8 ▶sub turtle {
Subroutine turtle: called CC=1 SCAR=0.0
Pod documented
792 8 my ($self,%opt) = @_;
793  
794 8 return join( "\n",
795 $self->turtleNamespaces,
796 $self->turtleBase ,
797 $self->turtleScheme( %opt ) ,
798 $self->turtleConcepts( %opt ) );
799 }
800  
801 =head2 turtleNamespaces
802  
803 Returns Turtle syntax namespace declarations for this scheme.
804  
805 =cut
806  
807 8 ▶sub turtleNamespaces {
Subroutine turtleNamespaces: called CC=4 SCAR=15.0
Pod documented
808 8 my $self = shift;
809  
810 8 ▶ if ( $self->{uses_dc} and not $self->{namespaces}->{dc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"uses_dc"} and not $self->{"namespaces"}{"dc"}) 0 8 8
Condition33%1/3
$self->{"uses_dc"} and not $self->{"namespaces"}{"dc"}
!ll&&!rl&&r
800
MC/DC0%0/2
$self->{"uses_dc"} and not $self->{"namespaces"}{"dc"}
$self->{"uses_dc"} not $self->{"namespaces"}{"dc"}
Truth table33%1/3
A and not(B)
A= $self->{"uses_dc"}B= not $self->{"namespaces"}{"dc"}
ABresult
0X0
100
111
811 0 $self->{namespaces}->{dc} = $NAMESPACES{dc};
812 }
813  
814 8 my @lines;
815 8 foreach my $name (keys %{$self->{namespaces}}) {
816 16 push @lines, "\@prefix $name: <" . $self->{namespaces}->{$name} . "> .";
817 }
818  
819 8 return join("\n", @lines) . "\n";
820 }
821  
822 =head2 turtleBase
823  
824 Returns a Turtle URI base declaration for this scheme.
825 An empty string is returned if no URI base is set.
826  
827 =cut
828  
829 8 ▶sub turtleBase {
Subroutine turtleBase: called CC=2 SCAR=8.1
Pod documented
830 8 my $self = shift;
831  
832 8 ▶ return "" if $self->{base} eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"base"} eq "" 8 0 8
833 0 return "\@base <" . $self->{base} . "> .\n"
834 }
835  
836  
837 =head2 turtleScheme ( [ top => 0 ] )
838  
839 Returns RDF statements about the concept scheme in Turtle syntax.
840 Details about concepts or namespace/base declarations are not included.
841 The option C<< top => 0 >> (enabled by default) can be used to supress
842 serializing the C<skos:hasTopConcept> property.
843  
844 =cut
845  
846 14 ▶sub turtleScheme {
Subroutine turtleScheme: called CC=3 SCAR=11.0
Pod documented
847 14 my ($self,%opt) = @_;
848 14 ▶ $opt{top} = 1 unless exists $opt{top};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt{"top"} 10 4 14
849  
850 # note that lean => 1 does not imply top => 0 !
851 14 ▶ if ( $opt{top} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'top'}) { } 10 4 14
852 10 my @top = $self->topConcepts();
853 10 $self->{prop}->{'skos:hasTopConcept'} = [ map { "<$_>" } @top ];
854 } else {
855 4 delete $self->{prop}->{'skos:hasTopConcept'}
856 }
857  
858 14 return turtle_statement( "<" . $self->{scheme} . ">", %{$self->{prop}} );
859 }
860  
861 =head2 turtleConcept ( $id [, %options ] )
862  
863 Returns a concept in Turtle syntax. With option C<< top => 0 >> you can disable
864 serializing the C<skos:topConceptOf> property. By default, each concept is
865 connected to its concept scheme with either C<skos:topConceptOf>, or with
866 C<skos:inScheme>. With option C<< scheme => 0 >> you can disable serializing
867 the latter property. With C<< scheme => 2 >> the property C<skos:inScheme>
868 is also included in the serialization if C<skos:topConceptOf> is given,
869 although the former can be derived as super-property of the latter.
870  
871 =cut
872  
873 8 ▶sub turtleConcept {
Subroutine turtleConcept: called CC=17 SCAR=29.2
Pod documented
874 8 my ($self,$id,%opt) = @_;
875 8 ▶ $opt{top} = 1 unless exists $opt{top};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt{"top"} 5 3 8
876 8 ▶ $opt{top} = 0 if $opt{lean};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"lean"} 1 7 8
877 8 ▶ $opt{scheme} = 1 unless exists $opt{scheme};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt{"scheme"} 4 4 8
878  
879 8 my $c = $self->{concepts}->{$id};
880  
881 8 my %stm = ( 'a' => 'skos:Concept' );
882  
883 # TODO: Support multiple notations
884 8 ▶ if ( $c->{notation} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"notation"} ne "") 8 0 8
885 8 $stm{'skos:notation'} = turtle_literal( $c->{notation} );
886 }
887  
888 # Concept Labels: pref, alt, hidden, title
889 8 ▶ while ( my ($type,$property) = each %LABEL_TYPES ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %SKOS::Simple::LABEL_TYPES) 32 8 40
890 32 ▶ next unless $c->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->{$type} 32 0 32
891 0 my $label = turtle_literal_list( $c->{$type} );
892 0 $stm{$property} = $label;
893 # TODO: entail rdfs:label if requested (pref, alt, hidden)
894 }
895  
896  
897 8 foreach my $rel (qw(broader narrower related)) {
898 24 ▶ next unless %{$c->{$rel}};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$c->{$rel};} 22 2 24
899 2 $stm{"skos:$rel"} = [ map { "<$_>" } keys %{$c->{$rel}} ];
900 }
901  
902 8 my $is_top = 0;
903 8 ▶ if ( $opt{top} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"top"}) 4 4 8
904 4 ▶ if ( (keys %{ $self->{top} }) ? $self->{top}->{$id} : not %{$c->{broader}} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %{$$self{"top"};} ? $self->{"top"}{$id} : !%{$c->{"broader"};}) 3 1 4
keys %{$$self{'top'};} ? : 0 4 4
905 3 $stm{"skos:topConceptOf"} = "<" . $self->{scheme} . ">";
906 3 $is_top = 1;
907 }
908 }
909  
910 # [S7] skos:topConceptOf is a sub-property of skos:inScheme
911 8 ▶ if ( $opt{scheme} - $is_top > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"scheme"} - $is_top > 0) 5 3 8
912 # TODO: with 'lean' => 1 and not top => 0
913 # this is implied if the concept is topConcept
914 5 $stm{'skos:inScheme'} = '<' . $self->{scheme} . '>';
915 }
916  
917 8 ▶ if ( defined $c->{identifier} and $c->{identifier} ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->{"identifier"} and $c->{"identifier"} ne "") 0 8 8
Condition33%1/3
defined $c->{"identifier"} and $c->{"identifier"} ne ""
!ll&&!rl&&r
800
MC/DC0%0/2
defined $c->{"identifier"} and $c->{"identifier"} ne ""
defined $c->{"identifier"} $c->{"identifier"} ne ""
Truth table33%1/3
A and B
A= defined $c->{"identifier"}B= $c->{"identifier"} ne ""
ABresult
0X0
100
111
918 0 $stm{'dc:identifier'} = turtle_literal( $c->{identifier} );
919 }
920  
921 # TODO: S17 - infer skos:note only if requested
922 8 foreach my $name ( keys %NOTE_PROPERTIES ) {
923 56 ▶ next unless $c->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->{$name} 56 0 56
924 0 $stm{"skos:$name"} = turtle_literal_list( $c->{$name} );
925 }
926  
927 8 return turtle_statement( "<$id>", %stm );
928 }
929  
930 =head2 turtleConcepts ( [ %options ] )
931  
932 Returns all concepts in Turtle syntax.
933  
934 =cut
935  
936 15 ▶sub turtleConcepts {
Subroutine turtleConcepts: called CC=1 SCAR=0.0
Pod documented
937 15 my ($self,%opt) = @_;
938 15 delete $opt{id};
939  
940 return join( "\n",
941 8 map { $self->turtleConcept( $_, %opt ) }
942 15 keys %{ $self->{concepts} } );
943 }
944  
945 =head1 EXPORTABLE FUNCTIONS
946  
947 =head2 skos
948  
949 This is just a shortcut for C<< SKOS::Simple->new >>.
950  
951 =cut
952  
953 1 ▶sub skos {
Subroutine skos: called CC=1 SCAR=0.0
Pod documented
954 1 SKOS::Simple->new(@_)
955 }
956  
957 =head2 is_uri ( $uri )
958  
959 Copied from L<Data::Validate::URI>, originally by Richard Sonnen.
960  
961 =cut
962  
963 7 ▶sub is_uri{
Subroutine is_uri: called CC=21 SCAR=40.8
Pod documented
964 7 ▶ my $self = shift if ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 7 7
965 7 my $value = shift;
966
967 7 ▶ return unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 7 7
968
969 # check for illegal characters
970 7 ▶ return if $value =~ /[^a-z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\.\-\_\~\%]/i;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ m<[^a-z0-9\:/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\.\-\_\~\%]>i 0 7 7
971
972 # check for hex escapes that aren't complete
973 7 ▶ return if $value =~ /%[^0-9a-f]/i;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /%[^0-9a-f]/i 0 7 7
974 7 ▶ return if $value =~ /%[0-9a-f](:?[^0-9a-f]|$)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /%[0-9a-f](:?[^0-9a-f]|$)/i 0 7 7
975
976 # from RFC 3986
977 7 my($scheme, $authority, $path, $query, $fragment) =
978 ($value =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|);
979
980 # scheme and path are required, though the path can be empty
981 7 ▶ return unless (defined($scheme) && length($scheme) && defined($path));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scheme and length $scheme and defined $path 0 7 7
Condition33%2/6
defined $scheme and length $scheme and defined $path
!ll&&!rl&&r
007
defined $scheme and length $scheme
!ll&&!rl&&r
007
MC/DC0%0/3
defined $scheme and length $scheme and defined $path
defined $scheme length $scheme defined $path
Truth table25%1/4
A and B and C
A= defined $schemeB= length $schemeC= defined $path
ABCresult
0XX0
10X0
1100
1111
982
983 # if authority is present, the path must be empty or begin with a /
984 7 ▶ if(defined($authority) && length($authority)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $authority and length $authority) { } 7 0 7
Condition33%1/3
defined $authority and length $authority
!ll&&!rl&&r
007
MC/DC0%0/2
defined $authority and length $authority
defined $authority length $authority
Truth table33%1/3
A and B
A= defined $authorityB= length $authority
ABresult
0X0
100
111
985 7 ▶ return unless(length($path) == 0 || $path =~ m!^/!);
Branch: 1 branch
BranchTrueFalseTotal
unless length $path == 0 or $path =~ m[^/] 0 7 7
Condition66%2/3
length $path == 0 or $path =~ m[^/]
l!l&&r!l&&!r
610
MC/DC0%0/2
length $path == 0 or $path =~ m[^/]
length $path == 0 $path =~ m[^/]
Truth table66%2/3
A or B
A= length $path == 0B= $path =~ m[^/]
ABresult
1X1
011
000
986
987 } else {
988 # if authority is not present, the path must not start with //
989 0 ▶ return if $path =~ m!^//!;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^//] 0 0 0
990 }
991
992 # scheme must begin with a letter, then consist of letters, digits, +, ., or -
993 7 ▶ return unless lc($scheme) =~ m!^[a-z][a-z0-9\+\-\.]*$!;
Branch: 1 branch
BranchTrueFalseTotal
unless lc($scheme) =~ /^[a-z][a-z0-9\+\-\.]*$/ 0 7 7
994
995 # re-assemble the URL per section 5.3 in RFC 3986
996 7 my $out = $scheme . ':';
997 7 ▶ if(defined $authority && length($authority)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $authority and length $authority) 7 0 7
Condition33%1/3
defined $authority and length $authority
!ll&&!rl&&r
007
MC/DC0%0/2
defined $authority and length $authority
defined $authority length $authority
Truth table33%1/3
A and B
A= defined $authorityB= length $authority
ABresult
0X0
100
111
998 7 $out .= '//' . $authority;
999 }
1000 7 $out .= $path;
1001 7 ▶ if(defined $query && length($query)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query and length $query) 0 7 7
Condition33%1/3
defined $query and length $query
!ll&&!rl&&r
700
MC/DC0%0/2
defined $query and length $query
defined $query length $query
Truth table33%1/3
A and B
A= defined $queryB= length $query
ABresult
0X0
100
111
1002 0 $out .= '?' . $query;
1003 }
1004 7 ▶ if(defined $fragment && length($fragment)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fragment and length $fragment) 0 7 7
Condition33%1/3
defined $fragment and length $fragment
!ll&&!rl&&r
700
MC/DC0%0/2
defined $fragment and length $fragment
defined $fragment length $fragment
Truth table33%1/3
A and B
A= defined $fragmentB= length $fragment
ABresult
0X0
100
111
1005 0 $out .= '#' . $fragment;
1006 }
1007
1008 7 return $out;
1009 }
1010  
1011 =head2 uri_or_empty ( $uri )
1012  
1013 Checks whether the passed parameter is either an URI or undefined.
1014  
1015 =cut
1016  
1017 30 ▶sub uri_or_empty {
Subroutine uri_or_empty: called CC=3 SCAR=11.7
Pod documented
1018 30 ▶ return (not defined $_[0] or $_[0] eq '' or is_uri($_[0]));
Condition66%4/6
!defined($_[0]) || $_[0] eq '' || is_uri($_[0])
l!l&&r!l&&!r
2370
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
0237
MC/DC0%0/3
!defined($_[0]) || $_[0] eq '' || is_uri($_[0])
not defined $_[0] $_[0] eq "" is_uri($_[0])
Truth table50%2/4
not(A) or B || C
A= not defined $_[0]B= $_[0] eq ""C= is_uri($_[0])
ABCresult
1XX1
01X1
0011
0000
1019 }
1020  
1021 6 1;
1022  
1023 =head1 SEE ALSO
1024  
1025 The SKOS ontology and its semantics is defined in
1026 L<http://www.w3.org/TR/skos-primer> and
1027 L<http://www.w3.org/TR/skos-reference>. Turtle format
1028 is specified at http://www.w3.org/TeamSubmission/turtle/.
1029  
1030 See also L<Biblio::Thesaurus> for a similar module.
1031  
1032 =cut
↑ Summary t1.pl »