This file is indexed.

/usr/share/perl5/TM/DM.pm is in libtm-perl 1.56-7.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
package TM::DM::TopicMap;

use TM;
use Data::Dumper;

sub new {
    my $class = shift;
    return bless { @_ }, $class;
}

sub id {
    my $self  = shift;
    my $map   = $self->{tmdm}->{map};
    return $map->baseuri;
}

sub topics {
    my $self  = shift;
    my $map   = $self->{tmdm}->{map};

    return map { 
	         TM::DM::Topic->new (
				     tmdm  => $self->{tmdm},
				     sad   =>      $_->[TM->ADDRESS],
				     sids  => [ @{ $_->[TM->INDICATORS] } ],
				     mid   => $_->[TM->LID]
				     )
		 } $map->toplets (@_);
}

sub associations {
    my $self = shift;
    my $map  = $self->{tmdm}->{map};
    my %search = @_;
    foreach my $k (keys %search) {
	$search{$k} = $map->tids ($search{$k}) if $k =~ /role|type|player/;
    }

    return map { TM::DM::Association->new (lid  => $_->[TM->LID],
					   tmdm => $self->{tmdm}) }
           grep ($_->[TM->KIND] == TM->ASSOC,
                 $map->match (TM->FORALL, %search)
		 );
}

sub reifier {
    my $self  = shift;
    my $map   = $self->{tmdm}->{map};

    my ($mid) = $map->is_reified ($map->baseuri)             # find a topic which has as subject address the baseuri
	or return undef;
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $mid);
}

sub topic {
    my $self = shift;
    my $id   = shift;
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $self->{tmdm}->{map}->tids ($id));
}

1;

package TM::DM::Topic;

sub new {
    my $class = shift;
    bless { @_ }, $class;
}

sub _mk_topic {
    my $tmdm   = shift,
    my $mid    = shift;
    my $map    = $tmdm->{map};
    my $midlet = $map->midlet ($mid);

    return TM::DM::Topic->new (tmdm => $tmdm,
                               mid  => $mid,
                               sad  =>     $midlet->[TM->ADDRESS],
                               sids => [ @{$midlet->[TM->INDICATORS]} ]);
}

sub subjectLocators {
    my $self = shift;
    return ($self->{sad});
}

sub subjectIdentifiers {
    my $self = shift;
    return @{ $self->{sids} };
}

sub id {
    my $self = shift;
    return $self->{mid};
}

sub parent {
    my $self = shift;
    return TM::DM::TopicMap->new (tmdm => $self->{tmdm});
}

sub names {
    my $self = shift;
    my $map  = $self->{tmdm}->{map};

    return
	map { TM::DM::Name->new (
                                 tmdm  => $self->{tmdm},
				 lid   => $_->[TM->LID],
				 ) }
             grep ($_->[TM->KIND] == TM->NAME,
                   $map->match (TM->FORALL, char    => 1,
                                            topic   => $self->{mid} ));
}

sub occurrences {
    my $self = shift;
    my $map  = $self->{tmdm}->{map};

    return
	map { TM::DM::Occurrence->new (
                                       tmdm  => $self->{tmdm},
				       lid   => $_->[TM->LID],
				       ) }
             grep ($_->[TM->KIND] == TM->OCC,
                 $map->match (TM->FORALL, char    => 1,
                                          topic   => $self->{mid} ));

}

sub roles {
    my $self = shift;
    my $map  = $self->{tmdm}->{map};
    my $mid  = $self->{mid};

    my @rs;
    foreach my $a (   grep ($_->[TM->KIND] == TM->ASSOC,
			    $map->match (TM->FORALL, iplayer => $self->{mid} ))) {
	push @rs, map {
	               TM::DM::Role->new (
					  tmdm   => $self->{tmdm},
					  lid    => $a->[TM->LID],
					  player => $mid,
					  type   => $_
					  )
		       } $map->get_roles ($a, $mid);
    }
    return @rs;
}

1;

package TM::DM::Association;

sub new {
    my $class = shift;
    bless { @_ }, $class;
}

sub id {
    my $self   = shift;
    return $self->{lid};
}

sub type {
    my $self   = shift;
    my $map    = $self->{tmdm}->{map};
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $map->retrieve ($self->{lid})->[TM->TYPE]);
}

sub scope {
    my $self   = shift;
    my $map    = $self->{tmdm}->{map};
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $map->retrieve ($self->{lid})->[TM->SCOPE]);
}

sub roles {
    my $self = shift;
    my $map  = $self->{tmdm}->{map};
    my $a    = $map->retrieve ($self->{lid});

    my ($ps, $rs) = ($a->[TM->PLAYERS], $a->[TM->ROLES]);

    return map { TM::DM::Role->new (                         # make a role from it
				    tmdm   => $self->{tmdm},
				    lid    => $a->[TM->LID],
				    player => $_->[1],
				    type   => $_->[0]
				    ) }
           map { [ $rs->[$_], $ps->[$_] ] }                  # get the role and the player, $_ is index
           (0 .. $#$ps)                                      # get all indices for all roles
	       ;
}

sub parent {
    my $self = shift;
    return TM::DM::TopicMap->new (tmdm => $self->{tmdm});
}

sub reifier {
    my $self  = shift;
    my $map   = $self->{tmdm}->{map};
    my $a    = $map->retrieve ($self->{lid});

    my ($mid) = $map->is_reified ($a)
	or return undef;
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $mid);
}

1;

package TM::DM::Occurrence;

#### datatype INSIDE the value


sub new {
    my $class = shift;
    return bless { @_ }, $class;
}

sub id {
    my $self   = shift;
    return $self->{lid};
}

# non-TMDM: bring back value + data type

sub value {
    my $self = shift;
    my $map  = $self->{tmdm}->{map};
    my $a    = $map->retrieve ($self->{lid});
    return ref ($a->[TM->PLAYERS]->[0]) ? # it can only be one of them
	        $a->[TM->PLAYERS]->[0] :
		$a->[TM->PLAYERS]->[1];   # we keep the datatype
}

sub type {
    my $self = shift;
    my $map  = $self->{tmdm}->{map};
    my $a    = $map->retrieve ($self->{lid});
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $a->[TM->TYPE]);
}

# only one scope!!!

sub scope {
    my $self   = shift;
    my $map  = $self->{tmdm}->{map};
    my $a    = $map->retrieve ($self->{lid});
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $a->[TM->SCOPE]);
}

sub reifier {
    my $self  = shift;
    my $map   = $self->{tmdm}->{map};
    my $a     = $map->retrieve ($self->{lid});

    my ($mid) = $map->is_reified ($a)
	or return undef;
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $mid);
}

sub parent {
    my $self = shift;
    my $map  = $self->{tmdm}->{map};
    my $a    = $map->retrieve ($self->{lid});
    my $mid  = ref ($a->[TM->PLAYERS]->[0]) ? 
	            $a->[TM->PLAYERS]->[1]  :
		    $a->[TM->PLAYERS]->[0];
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $mid);
}

1;

package TM::DM::Name;

sub new {
    my $class = shift;
    return bless { @_ }, $class;
}

sub id {
    my $self   = shift;
    return $self->{lid};
}

sub value {
    my $self = shift;
    my $map  = $self->{tmdm}->{map};
    my $a    = $map->retrieve ($self->{lid});
    my $v    = ref ($a->[TM->PLAYERS]->[0]) ? # it can only be one of them
	            $a->[TM->PLAYERS]->[0] :
		    $a->[TM->PLAYERS]->[1];
    return $v->[0]; # the data type is always a string, boring
}

sub type {
    my $self = shift;
    my $map  = $self->{tmdm}->{map};
    my $a    = $map->retrieve ($self->{lid});
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $a->[TM->TYPE]);
}

sub scope {
    my $self   = shift;
    my $map  = $self->{tmdm}->{map};
    my $a    = $map->retrieve ($self->{lid});
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $a->[TM->SCOPE]);
}

sub reifier {
    my $self  = shift;
    my $map   = $self->{tmdm}->{map};
    my $a     = $map->retrieve ($self->{lid});
    my ($mid) = $map->is_reified ($a)
	or return undef;
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $mid);
}

sub parent {
    my $self = shift;
    my $map  = $self->{tmdm}->{map};
    my $a    = $map->retrieve ($self->{lid});
    my $mid  = ref ($a->[TM->PLAYERS]->[0]) ? 
	            $a->[TM->PLAYERS]->[1]  :
		    $a->[TM->PLAYERS]->[0];
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $mid);
}

1;

package TM::DM::Role;

sub new {
    my $class = shift;
    return bless { @_ }, $class;
}



sub player {
    my $self = shift;
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $self->{player});
}

sub type {
    my $self = shift;
    return TM::DM::Topic::_mk_topic ($self->{tmdm}, $self->{type});
}

sub parent {
    my $self = shift;
    return TM::DM::Association->new (tmdm => $self->{tmdm}, lid => $self->{lid});
}

1;

package TM::DM;

require Exporter;
use base qw(Exporter);

use Data::Dumper;

=pod

=head1 NAME

TM::DM - Topic Maps, read-only TMDM layer

=head1 SYNOPSIS

   # somehow acquire a map (see TM and its subclasses)
   my $tm = ....

   # put a TMDM layer on top of it
   use TM::DM;
   my $tmdm = new TM::DM (map => $tm);

   # get the TMDM topic map item
   my $topicmap = $tmdm->topicmap;

   # ask for all topics
   my @topics = $topicmap->topics;
   # for all associations
   my @assocs = $topicmap->associations;

   # get a particular topic
   my $adam = $topicmap->topic ('adam');

   # get some of its properties
   $adam->id;
   $adam->subjectLocators;
   $adam->subjectIdentifiers;
   $adam->parent;
   my @ns = $adam->names;
   my @os = $adam->occurrences;

   # similar for assocs
   my @as = $topicmap->associations (iplayer => 'adam');
   $as[0]->type;
   $as[0]->parent;
   my @rs = $as[0]->roles;

=head1 ABSTRACT

This package provides a TMDM-ish (read-only) view on an existing topic map.

=head1 DESCRIPTION

TMDM, the Topic Maps Data Model

   http://www.isotopicmaps.org/sam/sam-model/

is the ISO standard for the I<high-level> model for Topic Maps. 


=head2 TMDM Concepts

TMDM's main concepts are the

=over

=item I<topic map item>

containing any number of topic and association items

=item I<topic item>

containing any number of names, occurrence items, subject
locators and subject identifiers

=item I<association item>

containing a type, a scope and any number of role items

=item I<name item>

containing a string, a type and a scope

=item I<occurrence item>

containing a data value (together with its data type), a type
and a scope

=item I<role item>

containing a type and a player

=back

All items have an I<item id> and all (except the map) have a parent
which links back to where the item belongs.

This package implements for each of the above a class and access methods to retrieve actual
structure and values from an existing map. Nota bene, there are some deviations from TMDM:

=over

=item

only ONE identifier per item is supported

=item

at most ONE subject locator per topic is supported

=item

no variants are supported (might be added at some stage, poke me)

=item

a scope consists only of a single topic

=item

role items do not have an identity, so they also cannot be reified

=back

=head2 Modus Operandi

Before you can use the TMDM layer, you need TM information in the form of a L<TM> object. Any
subclass should do, materialized and non-materialized maps should both be fine. Only with such
a map you can instantiate a TMDM layer:

  use TM::Materialized::AsTMa;
  my $tm = new TM::Materialized::AsTMa (file => 'test.atm');

  use TM::DM;
  my $tmdm = new TM::DM (map => $tm);

Probably the first thing you need to do is to get a handle on the whole topic map:

  my $topicmap = $tmdm->topicmap;

That is delivered as an instance of TM::DM::TopicMap as described below. From there you start to
extract topics and associations and that way you then further drill down.

=head2 Implementation Notes

This implementation only supports B<reading> map information, not changing it or modifying the
structure of the map. Not that it is impossible to do, but many applications get their map content
from elsewhere and a read/write interface is an overkill in these cases.

All objects generated here are B<ephemeral>, i.e. they are only instantiated because you wanted the
map information embedded into them. This implies that if you ask for one and the same topic twice,
you are getting two copies of the topic information. The following will not work as expected:

   my $t0 = $topicmap->topic ('adam');
   my $t1 = $topicmap->topic ('adam');

   warn "have the same topic!" if $t0 == $t1;

This will work:

   warn "have the same topic!" if $t0->id eq $t1->id;

=head1 INTERFACES

=head2 TM::DM

The TM::DM class itself does not offer much functionality itself. It only keeps the connection to the
background map.

=head3 Constructor

The constructor expects exactly one parameter, namely the background map.

I<$tmdm> = new TM::DM (map => I<$tm>)

=cut

sub new {
    my $class   = shift;
    my %options = @_;
    $TM::log->logdie (scalar __PACKAGE__ .": map parameter is not really a TM instance")     unless ref ($options{map}) && $options{map}->isa ('TM');
    
    return bless { %options }, $class;
}

=pod

=head3 Methods

=over

=item B<topicmap>

I<$topicmap> = I<$tmdm>->topicmap

This method generates a Topic Map item. See L<TM::DM::TopicMap|/TM::DM::TopicMap> .

=cut

sub topicmap {
    my $self = shift;
    return new TM::DM::TopicMap (tmdm => $self);
}


=pod

=back

=head2 TM::DM::TopicMap

This class provides access to all TMDM properties:

=over

=item B<id>

This returns the item identifier.

=item B<topics>

I<@topics> = I<$topicmap>->topics (I<@list-of-ids>)

I<@topics> = I<$topicmap>->topics

I<@topics> = I<$topicmap>->topics (I<$selection-spec>)

This method expects a list containing topic valid identifiers and returns for each of the topics a
C<TM::DM::Topic> reference. If any of the input identifiers do not denote a valid topic in the map,
undef will be returned in its place. If the parameter list is empty, B<all> topics will be
returned. Have fun, I mean, use with care.

Examples:

    # round tripping topic ids
    print map { $_->id } $topicmap->topics ('abel', 'cain' );

    print "he again" if $topicmap->topics ('god');

If a selection is specified then the same language as in L<TM> (method C<toplets>) can be used.

=item B<associations>

I<@as> = I<$topicmap>->associations

I<@as> = I<$topicmap>->associations (I<%search_spec>);

This method retrieves the list of ALL associations when it is invoked without a search
specification. See L<TM> for that.

=item B<reifier>

This returns the topic item which reifies the association. C<undef> is returned if there is none.

=item B<topic>

This returns a topic item with that id. This method will die if the id is invalid. Note that
always new copies are made.

=back

=head2 TM::DM::Topic

=over

=item B<subjectLocators>

Returns the (only) subject locator (URI string) in the topic item or C<undef> if there is none.

=item B<subjectIdentifiers>

Returns a list of URI strings. Might be empty.

=item B<id>

Returns the item id.

=item B<parent>

Returns a TM::DM::TopicMap item in which this topic is embedded.

=item B<names>

I<@names> = I<$topic>->names

Returns a list of TMDM name items.

=item B<occurrences>

I<@occurrences> = I<$topic>->occurrences

Returns a list of TMDM occurrences items.

=item B<roles>

I<@roles> = I<$topic>->roles

Returns a list of TM::DM::Role items where this topic plays any role.

=back

=head2 TM::DM::Association

=over

=item B<id>

Returns the item id.

=item B<type>

Returns a TM::DM::Topic item which is the type of the association.
This is always defined.

=item B<scope>

Returns the scope of the association in the form of a single TM::DM::Topic item. This is always
defined as for the I<unconstrained scope> the topic C<us> is used (see L<TM::PSI>).

=item B<roles>

I<@roles> = I<$assoc>->roles

Returns a list of roles of the association. Each role is a TM::DM::Role item.

=item B<parent>

Returns a TM::DM::TopicMap item for the embedding map.

=item B<reifier>

Returns a TM::DM::Topic item if this association is reified. C<undef> otherwise.

=back

=head2 TM::DM::Occurrence

=over

=item B<id>

Returns the item id.

=item B<value>

Returns the value (together with the data type) in the form of a L<TM::Literal>
object.

=item B<type>

Returns a TM::DM::Topic item which is the type of the occurrence.  This is always defined.

=item B<scope>

Returns the scope of the occurrence in the form of a single TM::DM::Topic
item. This is always defined.

=item B<reifier>

Returns a TM::DM::Topic item if this occurrence is reified. C<undef> otherwise.

=item B<parent>

Returns the TM::DM::Topic item of the topic where this occurrence is part of.

=back

=head2 TM::DM::Name

=over

=item B<id>

Returns the item id.

=item B<value>

Returns the string value of the name.

=item B<type>

Returns a TM::DM::Topic item which is the type of the name.
This is always defined.

=item B<scope>

Returns the scope of the name in the form of a single TM::DM::Topic
item. This is always defined.

=item B<reifier>

Returns a TM::DM::Topic item if this name is reified. C<undef> otherwise.

=item B<parent>

Returns the TM::DM::Topic item of the topic where this name is part of.

=back

=head2 TM::DM::Role

=over

=item B<player>

Returns a TM::DM::Topic item for the topic which is the player in this role.

=item B<type>

Returns a TM::DM::Topic item for the topic which is the type of this role.

=item B<parent>

Returns a TM::DM::Association item of the association where this role is in.

=back

=head1 SEE ALSO

L<TM>, L<TM::Easy>

=head1 COPYRIGHT AND LICENSE

Copyright 200[68] by Robert Barta, E<lt>drrho@cpan.orgE<gt>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl
itself.

=cut

our $VERSION  = '0.04';
our $REVISION = '$Id$';

1;

__END__