TM::Graph - Topic Maps, trait for graph-like operations
use TM::Materialized::AsTMa; my $tm = new TM::Materialized::AsTMa (file => 'old_testament.atm'); $tm->sync_in; Class::Trait->apply ( $tm => 'TM::Graph' ); # find groups of topics connected print Dumper $tm->clusters; # use association types to compute a hull print "friends of Mr. Cairo: ". Dumper [ $tm->frontier ([ $tm_>tid ('mr-cairo') ], [ [ $tm->tids ('foaf') ] ]) ]; # see whether there is a link (direct print "I always knew it" if $tm->is_path ( [ 'gw-bush' ], # there could be more (bless [ [ 'foaf' ] ], '*'), 'osama-bin-laden');
Obviously a topic map is also a graph, the topics being the nodes, and the associations forming the edges, albeit these connections connect not always only two nodes, but, ok, you should know TMs by now.
This package provides some functions which focus more on the graph-like nature of Topic Maps.
This trait provides the following methods:
$hashref = clusters ($tm)
computes the islands of topics. It figures out which topics are connected via associations and - in case they are - will collate them into clusters. The result is a hash reference to a hash containing list references of topic ids organized in a cluster.
In default mode, this function only regards topics to be in the same cluster if topics play roles in one and the same maplet. The role topics themselves or the type or the scope are ignored.
You can change this behaviour by passing in options like
use_scope => 1 use_roles => 1 use_type => 1
use_scope => 1 you will let a lot of topics collapse into one cluster as most maplets usually are in the unconstrained scope.
NOTE: This is yet a somewhat expensive operation.
@hull = $tm->frontier (\@start_lids, $path_spec)
This method computes a qualified hull, i.e. a list of all topics which are reachable from @start_lids via a path specified by $path_spec. The path specification is a (recursive) data structure, describing sequences, alternatives and repetition (the
* operator), all encoded as lists of lists. The topics in that path specification are interpreted as assertion types.
Example (reformatting for better reading):
# a single step: start knows ... [ ] # outer level: sequence (there is only one) [ 'knows' ] # inner level: alternatives (there is only one) # two subsequent steps: start knows ... isa ... [ ] # outer level: two entries [ 'knows' ], [ 'isa' ] # inner level, one entry each # repetition: start knows ... knows ... knows ... ad infinitum bless [ ], '*' # outer level: one entry, but blessed [ 'knows' ] # inner level # alternatives: start knows | hates ... [ ] # outer level: one entry [ 'knows', 'hates' ] # inner level: alternatives # nesting: first follow an 'eats', then any number of 'begets' [ ] [ 'eats' ], [ ] bless [ ], '*' [ 'begets' ]
NOTE: All tids have to be made map-absolute with
NOTE: Cycles are detected.
NOTE: I am not sure how this performs at rather large graphs, uhm, maps.
$bool = $tm->is_path (\@start_lids, $path_spec, $end_lid)
This method returns
1 if there is a path from start_lids to end_lid via the path specification. See frontier for that one.
@neighbors = $tm->neighborhood ($MAXDEPTH, \@start_lids)
This method returns a list of neighbors for the given start LIDs. In that it follows paths with the maximal length given as first parameter. In any case the path with length
0 is returned, which includes any of the starting nodes.
Each neighbor is represented by a hash (reference) with the
path and the
end LID. The path is a list (reference) holding the LIDs of the association types visited along the path.
Copyright 200 by Robert Barta, <firstname.lastname@example.org>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.