1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
|
package XTM::topic;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;
require AutoLoader;
use Data::Dumper;
use XTM::generic;
use XTM::instanceOf;
use XTM::baseName;
use XTM::scope;
use XTM::PSI;
use XTM::subjectIdentity;
use XTM::occurrence;
use XTM::topicRef;
use XTM::resourceRef;
use XTM::subjectIndicatorRef;
use XTM::variant;
use XTM::variantName;
use XTM::parameters;
use XTM::resourceData;
use XTM::baseNameString;
use XTM::Log ('elog');
use URI;
@ISA = qw(Exporter AutoLoader XTM::generic);
@EXPORT = qw( );
$VERSION = '0.14';
=pod
=head1 NAME
XTM::topic - Topic Map management, Topic
=head1 SYNOPSIS
use XTM::topic;
my $t = new XTM::topic;
...
print join (",", @{$t->occurrences()});
print "bliss and happiness" if $t->has_instanceOf ('t-billionair');
# now this time I am providing the id myself
my $t2 = new XTM::topic (id => '1234');
# there is also a cheap way to populate the topic with a default
my $t3 = new XTM::topic (id => '1234', populate => \&XTM::topic::default_populate);
# see XTM::generic for more methods
=head1 DESCRIPTION
This package provides the primitive class Topic for Topic Maps.
=head1 INTERFACE
=head2 Constructor
The constructor expects a hash with following (optional) fields:
=over
=item C<id>:
a topic id (unique in the map in use), if not given, it will be generated
=item C<populate>:
a code reference to a subroutine populating the topic. There are the following B<predefined>
subroutines:
=over
=item I<default_populate>
This routine expects as first parameter a C<XTM::topic>-blessed object and fills in
the PSI topic as 'instanceOf', global scope where appropriate. It also generates in a fairly
ad-hoc manner a 'baseName' from the topic id (substituting '-' by ' ').
=back
=back
Example:
$t = new XTM::topic ('id' => 'x123');
=cut
our $topic_cntr = 0;
sub new {
my $class = shift;
my %options = @_;
if ($options{id}) {
unless ($options{id} =~ /^[\w_:][\w\d\-\.]*/ && $options{id} !~ /^xml:/) { # Professional XML, page 33
elog ($class, 1, "topic ID '$options{id}' might make problems with XML processors");
}
} else {
$options{id} = sprintf ("t-%10.10d", $topic_cntr++);
}
elog ($class, 5, "new '$options{id}'");
my $self = bless { id => $options{id},
instanceOfs => [],
baseNames => [],
occurrences => []
}, $class;
$self->{ids} = [ $self->{id} ]; # eventually this will contain id and all aliases of this topic
&{$options{populate}} ($self)
if (defined $options{populate} && ref ($options{populate}) eq 'CODE');
return $self;
}
#sub ids {
# my $self = shift;
# return @{$self->{ids}};
#}
# this is a simple routine (NO method) to fill a basename with a default scope
sub default_populate {
my $t = shift;
my $name = $t->{id}; # default base name
$name =~ s/-/ /g;
my $b = new XTM::baseName ();
$b->add_baseNameString (new XTM::baseNameString (string => $name));
$b->add_scope (new XTM::scope());
$b->scope->add_reference_s (new XTM::topicRef (href => $XTM::PSI::xtm{universal_scope}) );
$t->add__s ($b);
my $i = new XTM::instanceOf (reference => new XTM::topicRef (href => $XTM::PSI::xtm{topic}));
$t->add__s ($i);
}
=pod
=head2 Methods
Following accessor methods are available via L<XTM::generic> (see that package how
to set/add values):
=over
=item I<baseNames>:
@{ I<$t>->baseNames}
returns list reference of L<XTM::baseName> nodes.
=item I<instanceOfs>:
@{ I<$t>->instanceOfs}
returns list reference of L<XTM::instanceOf> nodes.
=item I<subjectIdentity>:
I<$t>->subjectIdentity
returns a L<XTM::subjectIdentity> node
=back
Other methods are:
=over
=item I<id>:
print I<$t>->id
I<$t>->id ("x123");
returns the id of the topic. If provided with a non-empty scalar parameter, this value will be
used to change the id.
=cut
sub id {
my $self = shift;
my $id = shift;
if ($id) {
$self->{ids} = [ grep ($_ ne $self->{id}, @{$self->{ids}}), $id ];
$self->{id} = $id;
}
return $self->{id};
}
=pod
=item I<occurrences>:
@{ I<$t>->occurrences }
I<$t>->occurrences ( I<$coderef> )
I<$t>->occurrences ( I<$arrayref> )
returns the occurrences of the topic as a list reference. If provided with a CODEREF parameter,
this subroutine will be used to filter the occurrences. If provided with a ARRAYREF
parameter, this will be used as new value.
=cut
sub occurrences {
my $self = shift;
my $sub = shift;
elog ('XTM::topic', 5, "occurrences", $sub);
return ref ($sub) eq 'CODE' ?
[ grep (&$sub ($_), @{$self->{occurrences}}) ] :
ref ($sub) eq 'ARRAY' ?
$self->{occurrences} = $sub :
$self->{occurrences};
}
=pod
=item I<map>:
I<$t>->map
I<$t>->map ( I<$newmap> )
is an accessor method for the C<map> component.
=cut
sub map {
my $self = shift;
$_[0] ? $self->{map} = shift : $self->{map};
}
=pod
=item I<has_instanceOf>:
I<$t>->has_instanceOf ( I<$type_topic_id> )
returns true if the topic is a B<direct> subtype of a topic specified as C<tid> for
the only parameter.
Example:
print "bliss and happiness" if $t->has_instanceOf ('t-billionair');
=cut
our %callers;
sub has_instanceOf {
my $self = shift;
my $ioid = shift;
$callers{$ioid}->{join ("",caller)}++;
# my $u = new URI ($ioid); # too expensive
# if ($u->scheme) { # absolute
# TODO: depth parameter
$ioid = "#$ioid" unless ($ioid =~ /[^\#]\w*:.+/); #relative, good approximation of URI?
return 1 if grep ($_->{reference}->{href} eq $ioid, @{$self->{instanceOfs}});
}
=pod
=item I<add_defaults>
I<$t>->add_defaults
This methods add default values according to the XTM standard. Specifically,
it assures that
=over
=item The topic has at least one C<instanceOf> component and there at least
one entry. If not, one will be generated (XTM clause 3.6.1).
=item Every occurrence must have a type (XTM clause 3.9.1).
=item If a scope exists (in an occurrence or a baseName), then at
least one reference must be there (XTM clause 3.3.1).
=back
=cut
sub add_defaults {
my $self = shift;
# 3.6.1
unless ($self->{instanceOfs} && @{$self->{instanceOfs}}) {
$self->add__s (new XTM::instanceOf ( reference => new XTM::topicRef (href => $XTM::PSI::xtm{topic})));
}
foreach my $o (@{$self->{occurrences}}) {
# 3.3.1
unless ($o->{scope}) {
$o->add (new XTM::scope ( references => [ new XTM::topicRef (href => $XTM::PSI::xtm{universal_scope}) ]));
}
# 3.9.1
unless ($o->{instanceOf}) {
$o->add (new XTM::instanceOf ( reference => new XTM::topicRef (href => $XTM::PSI::xtm{occurrence})));
}
}
foreach my $b (@{$self->{baseNames}}) {
unless ($b->{scope}) {
$b->add (new XTM::scope ( references => [ new XTM::topicRef (href => $XTM::PSI::xtm{universal_scope}) ]));
}
}
}
=pod
=item I<canonicalize>
I<$t>->canonicalize
This method simplifies the topic by eliminating duplicates as prescribed in F.6.
=cut
sub __make_uniq_string {
#warn " !! make _uniq called";
my $list = shift;
my %found;
return [ grep ( ! $found{$_}++ , @$list ) ];
}
sub __make_uniq_ref {
my $eval = shift;
my $list = shift;
my %found;
for (my $i = 0, my $l; defined ($l = $list->[$i]); $i++) {
my $f = &$eval ($l);;
#warn "f is ".$f;
if ($found{$f}++) {
# warn "found duplicate $f";
#warn "------before splice ".Dumper $list;
splice (@$list, $i, 1);
#warn "------after splice ".Dumper $list;
}
}
}
use Digest::MD5;
sub canonicalize {
my $self = shift;
# warn " canon : ".$self->{id};
# warn "making uniq".Dumper $self->{ids};
$self->{ids} = __make_uniq_string ($self->{ids})
if @{$self->{ids}} > 1;
# warn " after making uniq".Dumper $self->{ids};
# warn "making uniq2".Dumper $self->{instanceOfs};
__make_uniq_ref (sub { $_[0]->{reference}->{href}}, $self->{instanceOfs})
if @{$self->{instanceOfs}} > 1;
# warn " after making uniq2".Dumper $self->{instanceOfs};
my $md5 = Digest::MD5->new;
foreach my $bn ($self->{baseNames} ? @{$self->{baseNames}} : ()) {
next if $bn->{fingerprint};
$md5->add($bn->{baseNameString}->{string});
my @hrefs = map { $_->{href} } @{$bn->{scope}->{references}};
foreach my $h (sort { $a cmp $b } @hrefs) { # they have to be sorted!
$md5->add($h);
}
$bn->add_fingerprint ($md5->hexdigest); # automatically resets md5 object!
# warn "bn: ".$bn->{baseNameString}->{string}." has ".$bn->{fingerprint};
}
__make_uniq_ref (sub { $_[0]->{fingerprint} }, $self->{baseNames})
if $self->{baseNames} && @{$self->{baseNames}} > 1;
# die "XTM::topic: topic has more than one addressable resource (violates XTM clause 3.6.2)"
# if ($self->{subjectIdentity} &&
# $self->{subjectIdentity}->{references} &&
# @{$self->{subjectIdentity}->{references}} > 1 &&
# grep (isa($_, 'XTM::resourceRef'), @{$self->{subjectIdentity}->{references}}) > 1);
__make_uniq_ref (sub { $_[0]->{href} }, $self->{subjectIdentity}->{references})
if ($self->{subjectIdentity} &&
$self->{subjectIdentity}->{references} &&
@{$self->{subjectIdentity}->{references}} > 1)
}
=pod
=item I<connected>:
@{ I<$t>->connected }
returns a list reference of all topic references mentioned in this topic. These
references might be 'internal' or 'external' ones.
Example:
foreach (@{$t->connected}) {
print "$t->id mentions $_\n";
}
=cut
sub connected {
my $self = shift;
my @connected = ();
foreach my $i (@{$self->instanceOfs}) {
push @connected, $i->reference->href;
}
foreach my $b (@{$self->baseNames}) {
foreach my $r (@{$b->scope->references}) {
push @connected, $r->href;
}
}
foreach my $o (@{$self->occurrences}) {
foreach my $r (@{$o->scope->references}) {
push @connected, $r->href;
}
push @connected, $o->instanceOf->reference->href;
}
if ($self->subjectIdentity) {
push @connected, $self->subjectIdentity->resourceRef->href if $self->subjectIdentity->resourceRef;
}
# TOBEDONE
# variants
return \@connected;
}
=pod
=item I<xml>:
I<$t>->xml ( I<$xmlwriter> )
returns an XML representation of the topic.
Example:
$xmlwriter = new XML::Writer ...
...
$t->xml($xmlwriter); # outputs all onto $xmlwriter
=cut
sub xml {
my $self = shift;
my $writer = shift;
$writer->startTag ('topic', id => $self->{id});
foreach my $t (@{$self->instanceOfs}) {
$t->xml ($writer);
}
foreach my $b (@{$self->baseNames}) {
$b->xml ($writer);
}
foreach my $o (@{$self->occurrences}) {
$o->xml ($writer);
}
$self->subjectIdentity->xml ($writer) if $self->subjectIdentity;
$writer->endTag ('topic');
# variants still missing, better wait until DB solution
}
=pod
=back
=head1 SEE ALSO
L<XTM>, L<XTM::generic>
=head1 AUTHOR INFORMATION
Copyright 200[1-2], Robert Barta <rho@telecoma.net>, All rights reserved.
This library is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
http://www.perl.com/perl/misc/Artistic.html
=cut
1;
__END__
if (0 && $self->occurrences) {
die "I am not here";
my $p = 0;
sub occult { # computes a value which allows to 'compare' to occurrences
my $oc = shift;
my $v;
$v .= ref ($oc); # resourceRef or resourceData
if (ref($oc) eq 'XTM::resourceRef') {
$v .= $oc->resource->data;
} else {
$v .= $oc->resource->href;
}
$v .= join "", map { $_->href } @{$oc->scope->references};
$v .= $oc->instanceOf->reference->href;
return $v;
}
$self->occurrences ([ grep (occult($_) ne $p && ($p = occult ($_)),
sort {occult ($a) cmp occult ($b) } @{$self->occurrences})]);
foreach my $oc (@{$self->occurrences}) {
$oc->scope->references ( [ sort {ref ($a) cmp ref ($b) || $a->href cmp $b->href } @{$oc->scope->references} ]);
}
}
|