| 1 | package TM::View; | ||
| 2 | # $Id: View.pm,v 1.9 2009-11-27 01:32:55 az Exp $ | ||
| 3 | |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | $VERSION = qw(('$Revision: 1.9 $'))[1]; | ||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | require AutoLoader; | ||
| 10 | |||
| 11 | 1 | ▶ | use TM 1.43; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use TM::Literal; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use base qw(TM); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | ▶ | use XML::Writer; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 1 | ▶ | use IO::String; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 1 | ▶ | use UNIVERSAL qw(isa); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | |||
| 19 | =pod | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | TM::View - Topic Maps, Views and Listlets | ||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | use TM; | ||
| 28 | use TM::View; | ||
| 29 | |||
| 30 | my $map; | ||
| 31 | # map is created/synced somehow | ||
| 32 | |||
| 33 | # do not change the map afterwards or pass the constructor | ||
| 34 | # a deep copy (e.g. using dclone from Storable). | ||
| 35 | my $view=TM::View->new($map); | ||
| 36 | |||
| 37 | $view->sequence_add("tm://sometopic"); # added with default style info | ||
| 38 | $view->sequence_add("tm://othertopic",0); # add at the beginning | ||
| 39 | my $length=$view->sequence_length; | ||
| 40 | my $whogoesthere=$view->who(0); # which topic is shown first? | ||
| 41 | |||
| 42 | # retrieve the style describing the topic's midlet by location | ||
| 43 | my ($who,%style)=$view->style(1,0); | ||
| 44 | ($who,%style)=$view->style("tm://sometopic",0); # or by topic | ||
| 45 | $style{bullet}=1; # modify it | ||
| 46 | $style{emphasize}=0; | ||
| 47 | $style{custom_attrib}="what you want!"; | ||
| 48 | $view->style("tm://sometopic",0,%style); # and write it back | ||
| 49 | |||
| 50 | my $xml=$view->make_listlet; | ||
| 51 | |||
| 52 | =head1 DESCRIPTION | ||
| 53 | |||
| 54 | This package provides sequencing and styling mechanisms for Topic Map slide shows or other | ||
| 55 | serialized presentations. | ||
| 56 | |||
| 57 | =head1 CONCEPTS | ||
| 58 | |||
| 59 | A view consists of an extract of a map together with | ||
| 60 | rendering information which presents a linear sequence of topics and their | ||
| 61 | interesting aspects. The main use of views is for using topicmaps as source | ||
| 62 | for slides or similar linear presentation mechanisms. | ||
| 63 | |||
| 64 | A view contains information about which topics are shown in what sequence, and what | ||
| 65 | of the available information related to a topic is presented and how. | ||
| 66 | TM::View manages this information in data structures called I<styles>. | ||
| 67 | |||
| 68 | Every topic in a map can have sundry attributes associated with it, e.g. basenames, | ||
| 69 | occurrences and class/instance information. None of these have any implicit ordering. | ||
| 70 | From a view perspective, these | ||
| 71 | attributes belong to the topic and their display is controlled by the topic's | ||
| 72 | style. A topic can also be involved in associations as member, role being played or | ||
| 73 | type of the association. Associations are thus not directly associated with a single | ||
| 74 | topic but instead are deemed to be interesting for every involved topic. | ||
| 75 | A view thus includes information about a particular association multiple times | ||
| 76 | in each involved topic's style. | ||
| 77 | |||
| 78 | In TM::View a style element consists of two components: an identifier of the topicmap object | ||
| 79 | being controlled, and a reference to a hash of attribute/value pairs. The attributes | ||
| 80 | describe the formatting of the object but do not include the object content. | ||
| 81 | Besides certain attributes with predefined meanings (see topic_as_listlet below), there are no | ||
| 82 | restrictions on the attributes. | ||
| 83 | |||
| 84 | TM::View uses the data structures and identification concepts described in L<TM(3)>, which separate | ||
| 85 | the world into Midlets (without assertions) and Assertions (with midlets). | ||
| 86 | (Everything in a map is a midlet. "Real" topics are only present as midlets, whereas | ||
| 87 | implicit topics have a midlet and an assertion with the same identifier.) | ||
| 88 | Midlets contain only topic id and reification information, | ||
| 89 | whereas assertions carry everything else: topic attributes and associations. | ||
| 90 | |||
| 91 | A single style element controls the display of a single assertion. In practice that means | ||
| 92 | that a basename has its style element, separate from an occurrence even if they both belong | ||
| 93 | to the same topic. | ||
| 94 | |||
| 95 | The style elements for all displayable aspects of a single topic are collected in a list: | ||
| 96 | this is called the I<styles of a topic>. Position in this list controls display position, | ||
| 97 | and this style list contains all information to create a slide or page about this topic. | ||
| 98 | Note that the style of a topic is always I<complete> and contains I<all> the topic's aspects: | ||
| 99 | Aspects that should not be displayed are flagged thus by a specific | ||
| 100 | attribute (see topic_as_listlet). | ||
| 101 | |||
| 102 | Any midlet in a map can have such a style list (but of course not all midlets are worth displaying: | ||
| 103 | for example, a topic that was implicitely defined by specifying a scope for some other attribute | ||
| 104 | hardly contains interesting information). | ||
| 105 | |||
| 106 | Multiple topic styles describe a linear journey through the map, and this is represented in | ||
| 107 | TM::View as the I<sequence>. A view contains exactly one sequence, which lists the topics that | ||
| 108 | are to be displayed (and their order). The sequence usually is only a subset of all available | ||
| 109 | topics (or more precisely midlets). | ||
| 110 | |||
| 111 | =head1 RELATIONSHIP BETWEEN MAP AND VIEW | ||
| 112 | |||
| 113 | A TM::View object embeds a topicmap upon its creation, after which this map object must not be | ||
| 114 | modified anymore. (To be precise, adding elements to the map might be mostly safe, but | ||
| 115 | modification or removal of existing map content is definitely unsafe.) | ||
| 116 | It is suggested that the programmer use dclone (see L<Storable(3)>) or something similar to create | ||
| 117 | a deep copy of the map object for the constructor, if modification of the map object is expected | ||
| 118 | at a later stage. | ||
| 119 | |||
| 120 | As maps change over time, one-shot discardable views would be of little use for serious | ||
| 121 | knowledge management: specifying a display sequence and extracting the appropriate information from | ||
| 122 | a map is time-consuming. One would have to recreate the display sequence with a new view that applies to | ||
| 123 | the new map, and manually copy over all transferrable attributes. Obviously this is tedious and | ||
| 124 | inefficient, and TM::View provides automation for as much of this process as possible. | ||
| 125 | |||
| 126 | A TM::View object can be "applied" to a modified version of the respective map using | ||
| 127 | the method I<reconcile>. | ||
| 128 | |||
| 129 | I<reconcile> resolves the differences between the old (embedded) and updated map and | ||
| 130 | migrates the view information into the context of the updated map. This is done minimizing the loss | ||
| 131 | of style information: | ||
| 132 | the styles for unmodified elements are copied over, styles applying to removed elements are removed | ||
| 133 | and new elements are added where appropriate | ||
| 134 | (e.g. a new occurrence is added to the styles of the topic it applies to, with default display attributes). | ||
| 135 | Modified (and renamed) topics and associations are identified and their styles are transported over, | ||
| 136 | but style information for modified assertions is lost (the assertion will show up as new). | ||
| 137 | Topics and assertions affected by these changes are flagged and upon completion, the map embedded | ||
| 138 | in the old view is discarded and the new map is snapshotted in. | ||
| 139 | |||
| 140 | =head1 INTERFACE | ||
| 141 | |||
| 142 | The methods provided by TM::View fall into three categories: managing the overall sequence of topics, | ||
| 143 | managing the styles (of assertions) of a particular topic and creating output. | ||
| 144 | |||
| 145 | The methods commonly use two different identifiers: a I<topic identifier> and | ||
| 146 | an I<assertion identifier>, both of which are I<internal identifiers> as described in L<TM(3)>. | ||
| 147 | The I<tid> applies to the sequence, whereas the I<aid> | ||
| 148 | applies to the aspects related to a particular topic. The sequence-related methods obviously | ||
| 149 | do not require aid parameters. | ||
| 150 | |||
| 151 | Most methods allow to specify topics by either I<tid> or position in the sequence. | ||
| 152 | Similar mechanisms apply to selecting assertions by their I<aid> or by position in the sequence | ||
| 153 | of styles. | ||
| 154 | |||
| 155 | =head2 Constructor | ||
| 156 | |||
| 157 | $view=TM::View->new($tmobj); | ||
| 158 | |||
| 159 | The constructor requires a TM map object as sole argument, which is the map the view | ||
| 160 | applies to. The map object is attached to the view and must not be modified afterwards. | ||
| 161 | It is highly suggested to use dclone (see L<Storable(3)>) or similar to create a deep copy | ||
| 162 | of the map for the view: | ||
| 163 | |||
| 164 | $safeview=TM::View->new(dclone($tmobj)); | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | # makes a new blank view | ||
| 169 | # arguments: a TM map | ||
| 170 | sub new | ||
| 171 | { | ||
| 172 | my ($class,$map,%opts)=@_; | ||
| 173 | die "map MUST be provided\n" if (!$map); | ||
| 174 | |||
| 175 | my $self= | ||
| 176 | { | ||
| 177 | tm=>$map, | ||
| 178 | sequence=>[], | ||
| 179 | }; | ||
| 180 | return bless $self, $class; # reblessing | ||
| 181 | } | ||
| 182 | |||
| 183 | =pod | ||
| 184 | |||
| 185 | =head2 Methods | ||
| 186 | |||
| 187 | =over | ||
| 188 | |||
| 189 | =item B<map:> | ||
| 190 | |||
| 191 | $tmobj=$view->map; | ||
| 192 | |||
| 193 | This method returns the map object associated with the view. The map object can be used | ||
| 194 | for retrieval purposes but must not be modified. | ||
| 195 | |||
| 196 | =cut | ||
| 197 | |||
| 198 | sub map | ||
| 199 | { | ||
| 200 | my ($self)=@_; | ||
| 201 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 202 | |||
| 203 | return $self->{tm}; | ||
| 204 | } | ||
| 205 | |||
| 206 | # want basenames first, occurrences next, then assocs but assoc kind is zero. | ||
| 207 | # occurrences and assocs sorted by their types | ||
| 208 | sub _sort_elems | ||
| 209 | { | ||
| 210 | my $ak=($a->[TM->KIND]||3).$a->[TM->TYPE]; | ||
| 211 | my $bk=($b->[TM->KIND]||3).$b->[TM->TYPE]; | ||
| 212 | return $ak cmp $bk; | ||
| 213 | } | ||
| 214 | |||
| 215 | =pod | ||
| 216 | |||
| 217 | =item B<sequence_add:> | ||
| 218 | |||
| 219 | $length=$view->sequence_add(I<tid> [,I<position>]); | ||
| 220 | |||
| 221 | This method adds the topic I<tid> to the view. If no position argument | ||
| 222 | is given (or if it is invalid), the topic is added at the end of the sequence. | ||
| 223 | The position argument is a number, with zero being the start of the sequence. | ||
| 224 | The sequence can not have holes. | ||
| 225 | |||
| 226 | The style information for the topic is a built-in default, which can be modified | ||
| 227 | using the B<style> method. | ||
| 228 | |||
| 229 | The new length of the sequence is returned on success, or undef if the | ||
| 230 | topic is already sequenced or non-existent. | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | # adds a topic at index | ||
| 235 | # parameters: topic (midlet) id, position (end if not present) | ||
| 236 | # returns new length of the sequence, undef if topic already present | ||
| 237 | sub sequence_add | ||
| 238 | { | ||
| 239 | my ($self,$tid,$location)=@_; | ||
| 240 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 241 | my $lastindex=$#{$self->{sequence}}; | ||
| 242 | |||
| 243 | return undef if (grep($_->[0]->[0] eq $tid,@{$self->{sequence}})); | ||
| 244 | return undef if (!$self->{tm}->midlet($tid)); | ||
| 245 | |||
| 246 | my $compat=(exists($self->{tm}->{usual_suspects})?$self->{tm}->{baseuri}:""); | ||
| 247 | |||
| 248 | # find the right spot to put things and make space | ||
| 249 | if (!defined($location) || $location<0 || $location>$lastindex) | ||
| 250 | { | ||
| 251 | $location=$lastindex+1; | ||
| 252 | } | ||
| 253 | else | ||
| 254 | { | ||
| 255 | my @newa; | ||
| 256 | @newa=(@{$self->{sequence}}[0..$location-1],undef, | ||
| 257 | @{$self->{sequence}}[$location..$lastindex]); | ||
| 258 | $self->{sequence}=\@newa if ($location<=$lastindex); | ||
| 259 | } | ||
| 260 | |||
| 261 | # now collect the displayed elements for this topic | ||
| 262 | # and create the default order | ||
| 263 | # first the topic itself; then all the other stuff | ||
| 264 | my @how=([$tid,$self->_default_style($tid,$tid)]); | ||
| 265 | |||
| 266 | # anyid is too extensive: returns occs,bns,ins of scope=this or of type=this, too! | ||
| 267 | # but those are not interesting for this topic... | ||
| 268 | my @allelem=$self->{tm}->match(TM->FORALL,anyid=>$tid); | ||
| 269 | my @interesting=grep { $_->[TM->KIND]==TM->ASSOC # assocs are fine | ||
| 270 | || $self->{tm}->is_x_player($_,$tid,$compat."thing") # as are names/occs where we are directly involved | ||
| 271 | } (@allelem); | ||
| 272 | map { push @how,[$_->[TM->LID], | ||
| 273 | $self->_default_style($tid,$_->[TM->LID])] } (sort { _sort_elems() } @interesting); | ||
| 274 | $self->{sequence}->[$location]=\@how; | ||
| 275 | return $lastindex+2; | ||
| 276 | } | ||
| 277 | |||
| 278 | =pod | ||
| 279 | |||
| 280 | =item B<sequence_remove:> | ||
| 281 | |||
| 282 | $length=$view->sequence_remove(I<tid or index>); | ||
| 283 | |||
| 284 | $length=$view->sequence_remove("tm://sometopic"); | ||
| 285 | $length=$view->sequence_remove(12); | ||
| 286 | |||
| 287 | This method removes a topic from the sequence. The topic can be identified either | ||
| 288 | by its topic id or by its position in the sequence. | ||
| 289 | |||
| 290 | The method returns the new length of the sequence or undef on an unsequenced or | ||
| 291 | nonexistent topic. | ||
| 292 | |||
| 293 | =cut | ||
| 294 | |||
| 295 | # removes topic with id or whatever at some index | ||
| 296 | # returns new length of sequence or undef if bad index/no such topic | ||
| 297 | sub sequence_remove | ||
| 298 | { | ||
| 299 | my ($self,$tidorindex)=@_; | ||
| 300 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 301 | |||
| 302 | my $loc=$self->_find_tidindex($tidorindex,$self->{sequence}); | ||
| 303 | return undef if (!defined $loc); | ||
| 304 | |||
| 305 | splice(@{$self->{sequence}},$loc,1); | ||
| 306 | return scalar @{$self->{sequence}}; | ||
| 307 | } | ||
| 308 | |||
| 309 | =pod | ||
| 310 | |||
| 311 | =item B<sequence_length:> | ||
| 312 | |||
| 313 | $howmany=$view->sequence_length; | ||
| 314 | |||
| 315 | The method returns the number of currently sequenced topics. | ||
| 316 | |||
| 317 | =cut | ||
| 318 | |||
| 319 | # how many topics in the sequence? | ||
| 320 | sub sequence_length | ||
| 321 | { | ||
| 322 | my ($self)=@_; | ||
| 323 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 324 | return scalar @{$self->{sequence}}; | ||
| 325 | } | ||
| 326 | |||
| 327 | =pod | ||
| 328 | |||
| 329 | =item B<clear:> | ||
| 330 | |||
| 331 | $view->clear; | ||
| 332 | |||
| 333 | This method clears the list of sequenced topics and returns nothing. | ||
| 334 | |||
| 335 | =cut | ||
| 336 | |||
| 337 | # nuke the sequence, effectively clearing the whole profile | ||
| 338 | sub clear | ||
| 339 | { | ||
| 340 | my ($self)=@_; | ||
| 341 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 342 | $self->{sequence}=[]; | ||
| 343 | } | ||
| 344 | |||
| 345 | =pod | ||
| 346 | |||
| 347 | =item B<sequence_move:> | ||
| 348 | |||
| 349 | $newpos=$view->sequence_move(I<tid or index>,I<delta>); | ||
| 350 | |||
| 351 | $newpos=$view->sequence_move(10,-1); # up one | ||
| 352 | $newpos=$view->sequence_move('tm://sometopic',2); # down two slots | ||
| 353 | |||
| 354 | This method moves a sequenced topic to a different slot in the sequence. The topic | ||
| 355 | can be identified by its id or its position. The movement is controlled by the | ||
| 356 | I<delta> argument which indicates how many slots the topic is to move. | ||
| 357 | |||
| 358 | The method returns the effective new position or undef on bad arguments. | ||
| 359 | |||
| 360 | =cut | ||
| 361 | |||
| 362 | # lookup topic x (or index) and move it by delta slots, | ||
| 363 | # shifting the sequence around as necessary. | ||
| 364 | # returns the effective new position of the moved element or undef on bad args | ||
| 365 | sub sequence_move | ||
| 366 | { | ||
| 367 | my ($self,$tidorindex,$delta)=@_; | ||
| 368 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 369 | my $oldindex=$self->_find_tidindex($tidorindex,$self->{sequence}); | ||
| 370 | return undef if (!defined $oldindex); | ||
| 371 | |||
| 372 | my $newseq=_move($oldindex,$delta,@{$self->{sequence}}); | ||
| 373 | return undef if (!defined $newseq); | ||
| 374 | $self->{sequence}=$newseq; | ||
| 375 | return $oldindex+$delta; | ||
| 376 | } | ||
| 377 | |||
| 378 | =pod | ||
| 379 | |||
| 380 | =item B<where:> | ||
| 381 | |||
| 382 | ($topicindex,$aidindex)=$view->where(I<tid or index> [,I<aid or index>]); | ||
| 383 | |||
| 384 | print "happy!" if ($view->where("tm://joy")); # is sequenced | ||
| 385 | ($shakespeare,$bnidx)=$view->where("tm://frailty","tm://woman"); | ||
| 386 | |||
| 387 | This method looks up a topic (or one of its attached assertions) in the sequence and | ||
| 388 | returns the position in the sequence (and within this topic's information) or undef | ||
| 389 | if no match was identified. | ||
| 390 | |||
| 391 | If no aid argument is given, the topic is looked up and aidindex=0 is returned. | ||
| 392 | With an aid, first the topic is looked up and then the given aid is looked up in | ||
| 393 | the style list for this topic. If either lookup fails, undef is returned. | ||
| 394 | |||
| 395 | =cut | ||
| 396 | |||
| 397 | # lookup where a topic is sequenced | ||
| 398 | # returns index,mindex or undef | ||
| 399 | sub where | ||
| 400 | { | ||
| 401 | my ($self,$tidorindex,$midorindex)=@_; | ||
| 402 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 403 | my $index=$self->_find_tidindex($tidorindex,$self->{sequence}); | ||
| 404 | return undef if (!defined $index); | ||
| 405 | my $mindex=0; | ||
| 406 | if (defined $midorindex) | ||
| 407 | { | ||
| 408 | $mindex=$self->_find_tidindex($midorindex,$self->{sequence}->[$index]); | ||
| 409 | return undef if (!defined $mindex); | ||
| 410 | } | ||
| 411 | return ($index,$mindex); | ||
| 412 | } | ||
| 413 | |||
| 414 | =pod | ||
| 415 | |||
| 416 | =item B<who:> | ||
| 417 | |||
| 418 | $tid=$view->who(I<topic index> [,I<assertion index>]); | ||
| 419 | |||
| 420 | This method performs the inverse of who: given an index into the sequence, | ||
| 421 | it returns the I<tid> of the topic in that slot. | ||
| 422 | With the optional assertion index, the I<aid> of the assertion in that place | ||
| 423 | (within the styles of the topic) is returned. | ||
| 424 | |||
| 425 | If either index is invalid, undef is returned. | ||
| 426 | |||
| 427 | =cut | ||
| 428 | |||
| 429 | # lookup who is at sequence loc and optionally style location | ||
| 430 | # if midorindex given: returns assertion id, otherwise returns the topic id, | ||
| 431 | # returns undef if garbled args | ||
| 432 | sub who | ||
| 433 | { | ||
| 434 | my ($self,$tidorindex,$midorindex)=@_; | ||
| 435 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 436 | my $index=$self->_find_tidindex($tidorindex,$self->{sequence}); | ||
| 437 | return undef if (!defined $index); | ||
| 438 | $midorindex||=0; # the topic itself | ||
| 439 | my $mindex=$self->_find_tidindex($midorindex,$self->{sequence}->[$index]); | ||
| 440 | return undef if (!defined $mindex); | ||
| 441 | return $self->{sequence}->[$index]->[$mindex]->[0]; # the id | ||
| 442 | } | ||
| 443 | |||
| 444 | |||
| 445 | =pod | ||
| 446 | |||
| 447 | =item B<style_length> | ||
| 448 | |||
| 449 | $nr_aspects=$view->style_length(I<tid or index>); | ||
| 450 | |||
| 451 | Returns the number of assertions that the style of this topic contains. | ||
| 452 | The topic is identified by its tid or index in the sequence, and if the | ||
| 453 | argument is invalid, undef is returned. | ||
| 454 | |||
| 455 | =cut | ||
| 456 | |||
| 457 | # returns the number of style elements for this topic | ||
| 458 | # undef if bad index/tid | ||
| 459 | sub style_length | ||
| 460 | { | ||
| 461 | my ($self,$tidorindex)=@_; | ||
| 462 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 463 | my $index=$self->_find_tidindex($tidorindex,$self->{sequence}); | ||
| 464 | return undef if (!defined $index); | ||
| 465 | |||
| 466 | return scalar @{$self->{sequence}->[$index]}; | ||
| 467 | } | ||
| 468 | |||
| 469 | =pod | ||
| 470 | |||
| 471 | =item B<style:> | ||
| 472 | |||
| 473 | ($id,%stylehash)=$view->style(I<tid or index>,I<aid or index> [,%newstyle]); | ||
| 474 | |||
| 475 | This method gets (and optionally sets) a style. The sequence is searched for | ||
| 476 | a topic matching the first argument, and within the list of styles for this topic, | ||
| 477 | the requested assertion is looked up. If either lookup fails, undef is returned. | ||
| 478 | |||
| 479 | On success, the aid of the found assertion and a copy of the display attributes hash | ||
| 480 | is returned. If the optional newstyle argument is given, then the display attributes | ||
| 481 | hash is replaced with a copy of newstyle. Note that when setting newstyle the I<previous> | ||
| 482 | display attributes are returned. | ||
| 483 | |||
| 484 | The contents of the stylehash are unrestricted, but certain attributes have specific | ||
| 485 | meanings for TM::View's output methods which are discussed below, with topic_as_listlet. | ||
| 486 | |||
| 487 | The style at index 0 is a dummy style which contains no display attributes (except the _is_changed | ||
| 488 | flag) and only identifies the topic in question. The dummy style cannot be modified. | ||
| 489 | |||
| 490 | =cut | ||
| 491 | |||
| 492 | |||
| 493 | # returns the node id and style hash that was identified, and sets a new style if given | ||
| 494 | # both topic and element id can be given as indices or full base-prefixed identifiers | ||
| 495 | # if a new style is given, then it is set but the the old one is returned. | ||
| 496 | # returns undef if tid/mid not found | ||
| 497 | # style() returns ($id,%style), with the data being a full copy of the style | ||
| 498 | sub style | ||
| 499 | { | ||
| 500 | my ($self,$tidorindex,$midorindex,%newstyle)=@_; | ||
| 501 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 502 | my $index=$self->_find_tidindex($tidorindex,$self->{sequence}); | ||
| 503 | return undef if (!defined $index); | ||
| 504 | my $mindex=$self->_find_tidindex($midorindex,$self->{sequence}->[$index]); | ||
| 505 | return undef if (!defined $mindex); | ||
| 506 | |||
| 507 | my @oldinfo=($self->{sequence}->[$index]->[$mindex]->[0], | ||
| 508 | %{$self->{sequence}->[$index]->[$mindex]->[1]}); | ||
| 509 | $self->{sequence}->[$index]->[$mindex]->[1]={%newstyle} | ||
| 510 | if (%newstyle && $mindex>0); | ||
| 511 | return @oldinfo; | ||
| 512 | } | ||
| 513 | |||
| 514 | =pod | ||
| 515 | |||
| 516 | =item B<style_move:> | ||
| 517 | |||
| 518 | $newpos=$view->style_move(I<tid or index>,I<aid or index>,I<delta>); | ||
| 519 | |||
| 520 | Identifies the style for the given assertion in the context of the given topic | ||
| 521 | and moves it by delta slots. The dummy style at index 0 cannot be moved, nor | ||
| 522 | can any other assertion be moved into slot 0. | ||
| 523 | |||
| 524 | On success, the new position is returned. If either topic or assertion lookup fails, | ||
| 525 | or if the delta argument results in an invalid position, undef is returned. | ||
| 526 | |||
| 527 | =cut | ||
| 528 | |||
| 529 | # moves a style entry by +/-delta in the list of styles | ||
| 530 | # returns new position or undef if args broken | ||
| 531 | sub style_move | ||
| 532 | { | ||
| 533 | my ($self,$tidorindex,$midorindex,$delta)=@_; | ||
| 534 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 535 | |||
| 536 | my $index=$self->_find_tidindex($tidorindex,$self->{sequence}); | ||
| 537 | return undef if (!defined $index); | ||
| 538 | |||
| 539 | my $mindex=$self->_find_tidindex($midorindex,$self->{sequence}->[$index]); | ||
| 540 | return undef if (!defined $mindex); | ||
| 541 | |||
| 542 | # the first style elem (the topic midlet itself) can't be moved around! | ||
| 543 | return undef if ($mindex==0 || $mindex+$delta==0); | ||
| 544 | |||
| 545 | my $newstyleseq=_move($mindex,$delta,@{$self->{sequence}->[$index]}); | ||
| 546 | return undef if (!defined $newstyleseq); | ||
| 547 | $self->{sequence}->[$index]=$newstyleseq; | ||
| 548 | |||
| 549 | return $mindex+$delta; | ||
| 550 | } | ||
| 551 | |||
| 552 | # move element in list from oldspot to +/-delta | ||
| 553 | # returns undef if the args are bad or ref of new list | ||
| 554 | sub _move | ||
| 555 | { | ||
| 556 | my ($oldindex,$delta,@list)=@_; | ||
| 557 | my $lastindex=$#list; | ||
| 558 | my $newindex=$oldindex+$delta; | ||
| 559 | |||
| 560 | return undef if (!$delta || $newindex<0 || $newindex>$lastindex); | ||
| 561 | |||
| 562 | my ($which)=splice(@list,$oldindex,1); | ||
| 563 | my @newseq=(@list[0..$newindex-1], | ||
| 564 | $which,@list[$newindex..$lastindex-1]); | ||
| 565 | die "_move broken\n" if ($#newseq != $lastindex); | ||
| 566 | return \@newseq; | ||
| 567 | } | ||
| 568 | |||
| 569 | # find a topic or index in an array of (tid,something) | ||
| 570 | # needs ref of array, returns index or undef | ||
| 571 | sub _find_tidindex | ||
| 572 | { | ||
| 573 | my ($self,$tidorindex,$where)=@_; | ||
| 574 | my $index; | ||
| 575 | |||
| 576 | # topic or index? SOME tids start with baseuri, but all indices are numeric | ||
| 577 | if ($tidorindex!~/^[0-9]+$/) | ||
| 578 | { | ||
| 579 | # tid | ||
| 580 | for (0..$#{$where}) | ||
| 581 | { | ||
| 582 | # are we searching in the sequence or the style list of a sequenced topic? | ||
| 583 | if ($where==$self->{sequence}) | ||
| 584 | { | ||
| 585 | # searching in the sequence list, look at the first style node's first elem | ||
| 586 | # (which is the tid) | ||
| 587 | ($index=$_,last) if ($where->[$_]->[0]->[0] eq $tidorindex); | ||
| 588 | } | ||
| 589 | else | ||
| 590 | { | ||
| 591 | # otherwise: one level less indirection | ||
| 592 | ($index=$_,last) if ($where->[$_]->[0] eq $tidorindex); | ||
| 593 | } | ||
| 594 | } | ||
| 595 | } | ||
| 596 | else | ||
| 597 | { | ||
| 598 | $index=$tidorindex; | ||
| 599 | } | ||
| 600 | return undef if (!defined $index || $index<0 || $index>$#{$where}); | ||
| 601 | return $index; | ||
| 602 | } | ||
| 603 | |||
| 604 | # returns a list of [tindex,mindex] pairs where the listed tid was present | ||
| 605 | sub _find_tid_anywhere | ||
| 606 | { | ||
| 607 | my ($self,$tid)=@_; | ||
| 608 | my @where; | ||
| 609 | for my $ti (0..$#{$self->{sequence}}) | ||
| 610 | { | ||
| 611 | for my $mi (0..$#{$self->{sequence}->[$ti]}) | ||
| 612 | { | ||
| 613 | push @where,[$ti,$mi] if ($self->{sequence}->[$ti]->[$mi]->[0] eq $tid); | ||
| 614 | } | ||
| 615 | } | ||
| 616 | return @where; | ||
| 617 | } | ||
| 618 | |||
| 619 | # returns list of *sequenced* topics (their indices) that | ||
| 620 | # a given assertion applies to | ||
| 621 | # applies to: basename, oc -> type and topic in question; isa -> both topics | ||
| 622 | # association -> all role, type and player topics | ||
| 623 | # uses whichmap to control where to get the assertion info from | ||
| 624 | # returns a list of sequence indices or an empty list | ||
| 625 | sub _find_applicable | ||
| 626 | { | ||
| 627 | my ($self,$map,$aid)=@_; | ||
| 628 | |||
| 629 | my @found=(); | ||
| 630 | my @lookfor; | ||
| 631 | |||
| 632 | my $ass=$map->retrieve($aid); | ||
| 633 | return @found if (!$ass); # this is just a new midlet, not an assoc; uninteresting | ||
| 634 | |||
| 635 | my $compat=(exists($map->{usual_suspects})?$map->{baseuri}:""); | ||
| 636 | my ($lid,$scope,$kind,$type,$players,$roles)= | ||
| 637 | @{$ass}[TM->LID,TM->SCOPE,TM->KIND,TM->TYPE,TM->PLAYERS,TM->ROLES]; | ||
| 638 | |||
| 639 | if ($kind == TM->NAME || $kind == TM->OCC) | ||
| 640 | { | ||
| 641 | push @lookfor,$type,$map->get_x_players($ass,$compat."thing"); | ||
| 642 | } | ||
| 643 | else | ||
| 644 | { | ||
| 645 | # assocs: all players, all roles and the type topic are always involved | ||
| 646 | push @lookfor, @{$players},@{$roles},$type; | ||
| 647 | } | ||
| 648 | my $where; | ||
| 649 | map { defined($where=$self->_find_tidindex($_,$self->{sequence})) | ||
| 650 | && push @found,$where; } (@lookfor); | ||
| 651 | return @found; | ||
| 652 | } | ||
| 653 | |||
| 654 | =pod | ||
| 655 | |||
| 656 | =item B<reconcile:> | ||
| 657 | |||
| 658 | $mapdiff=$view->reconcile(I<map object>); | ||
| 659 | |||
| 660 | The reconcile method transfers style information from the current, snapshotted map | ||
| 661 | over to the (newer) map given as argument. On completion, the new map replaces the | ||
| 662 | old map. The new map must not be modified after the reconcile operation, and it is highly | ||
| 663 | suggested to pass a deep copy (e.g. using dclone from L<Storable(3)>) of the map object. | ||
| 664 | |||
| 665 | The method returns the output of TM::diff() for convenience. | ||
| 666 | |||
| 667 | Reconcile identifies unchanged or renamed-but-identical topics and migrates their | ||
| 668 | style settings over. For changed topics or changed aspects of topics, all I<precisely> | ||
| 669 | identifyable information is migrated: identical occurrence data, associations | ||
| 670 | whose membership has not changed and so on. | ||
| 671 | |||
| 672 | Where this identification is not possible (because the topic/aspect is gone/was added in the new map), | ||
| 673 | reconcile will flag the change. Every sequenced topic whose aspects were modified receives the attribute | ||
| 674 | I<_is_changed> with a true value. This attribute is also set for aspects of a topic that have changed. | ||
| 675 | This attribute can be cleared using the method clear_changed. | ||
| 676 | |||
| 677 | After reconcile has completed, the view is consistent with the argument map. | ||
| 678 | |||
| 679 | =cut | ||
| 680 | |||
| 681 | # takes a newer version of the map and merges it into the current view/profile, | ||
| 682 | # while retaining all the applicable style information from the profile. | ||
| 683 | # changed and new elements get an extra style element _is_changed=>1. | ||
| 684 | # note that the view is modified! | ||
| 685 | # returns the actual map diff | ||
| 686 | sub reconcile | ||
| 687 | { | ||
| 688 | my ($self,$newmap)=@_; | ||
| 689 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 690 | |||
| 691 | # gimme the diff but don't link in the topics: i have both maps a/v | ||
| 692 | my $diff=TM::diff($newmap,$self->{tm}, | ||
| 693 | {consistency=>[TM->Subject_based_Merging,TM->Indicator_based_Merging], | ||
| 694 | include_changes=>0}); | ||
| 695 | my $oldmap=$self->{tm}; | ||
| 696 | |||
| 697 | # deleted stuff: topics/midlets we nuke if they were sequenced. | ||
| 698 | # for assertions we remove all styles where mentioned | ||
| 699 | my @cand= map { @$_ } (values %{$diff->{minus}}); # keys are topics, values are array of mids where involved | ||
| 700 | map { push @cand,@{$diff->{modified}->{$_}->{minus}} if ($diff->{modified}->{$_}->{minus}) } | ||
| 701 | (keys %{$diff->{modified}}); | ||
| 702 | for my $goner (@cand) | ||
| 703 | { | ||
| 704 | for ($self->_find_tid_anywhere($goner)) | ||
| 705 | { | ||
| 706 | my ($t,$m)=@$_; | ||
| 707 | if ($m==0) | ||
| 708 | { | ||
| 709 | # topic itself sequenced, remove the whole sequenced thing | ||
| 710 | $self->sequence_remove($t); | ||
| 711 | } | ||
| 712 | else | ||
| 713 | { | ||
| 714 | # remove this style element | ||
| 715 | splice(@{$self->{sequence}->[$t]},$m,1); | ||
| 716 | # record the change in the toplevel style | ||
| 717 | $self->{sequence}->[$t]->[0]->[1]->{_is_changed}=1; | ||
| 718 | } | ||
| 719 | } | ||
| 720 | } | ||
| 721 | |||
| 722 | # next step is to rename stuff where topics changed names. update the names | ||
| 723 | # everywhere in our view: topic midlet ids themselves, but also association assertions | ||
| 724 | # this must happen before the stage of adding new/updated stuff in, | ||
| 725 | # because that requires finding affected players for assocs, and the map diff | ||
| 726 | # returns new assocs from the new map (hence using its namespace). | ||
| 727 | for my $oldname (keys %{$diff->{identities}}) | ||
| 728 | { | ||
| 729 | my $newname=$diff->{identities}->{$oldname}; | ||
| 730 | |||
| 731 | for my $t (@{$self->{sequence}}) | ||
| 732 | { | ||
| 733 | map { $_->[0]=$newname if ($_->[0] eq $oldname) } (@{$t}); | ||
| 734 | } | ||
| 735 | } | ||
| 736 | |||
| 737 | # new stuff: from a view perspective, new topics are uninteresting as | ||
| 738 | # they can't be present in the old view, so there's nothing to do. | ||
| 739 | # new associations and assertions must be attached to previously sequenced topics | ||
| 740 | # which are involved (player/role/type/topic with name,occ,scope...) | ||
| 741 | @cand=map { @$_ } (values %{$diff->{plus}}); # diff plus key is topic, value is array of mids..which we need | ||
| 742 | map { push @cand,@{$diff->{modified}->{$_}->{plus}} if ($diff->{modified}->{$_}->{plus}) } | ||
| 743 | (keys %{$diff->{modified}}); | ||
| 744 | |||
| 745 | for my $newbie (@cand) | ||
| 746 | { | ||
| 747 | my @where=$self->_find_applicable($newmap,$newbie); | ||
| 748 | for my $loc (@where) | ||
| 749 | { | ||
| 750 | my $style=$self->_default_style($self->{sequence}->[$loc]->[0]->[0], # the id of the context topic | ||
| 751 | $newbie,$newmap); # the new assertion, but info from the new map | ||
| 752 | $style->{_is_changed}=1; | ||
| 753 | |||
| 754 | push @{$self->{sequence}->[$loc]},[$newbie,$style]; | ||
| 755 | # remember the changed state for both the topic as well as the element | ||
| 756 | $self->{sequence}->[$loc]->[0]->[1]->{_is_changed}=1; | ||
| 757 | } | ||
| 758 | } | ||
| 759 | |||
| 760 | # finally, save the new map with the view | ||
| 761 | $self->{tm}=$newmap; | ||
| 762 | return $diff; | ||
| 763 | } | ||
| 764 | |||
| 765 | =pod | ||
| 766 | |||
| 767 | =item B<clear_changed:> | ||
| 768 | |||
| 769 | $view->clear_changed([I<tid or index>]); | ||
| 770 | |||
| 771 | This method removes the _is_changed flag wholesale from all topics in the sequence | ||
| 772 | if no argument is given. Alternatively, it can clear the flag from a specific sequenced | ||
| 773 | topic only. Returns nothing. | ||
| 774 | |||
| 775 | =cut | ||
| 776 | |||
| 777 | # removes the _changed flag from one selected topic or all elements of the view | ||
| 778 | # returns nothing | ||
| 779 | sub clear_changed | ||
| 780 | { | ||
| 781 | my ($self,$tidorindex)=@_; | ||
| 782 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 783 | my $tindex; | ||
| 784 | $tindex=$self->_find_tidindex($tidorindex,$self->{sequence}) if (defined $tidorindex); | ||
| 785 | |||
| 786 | for my $t (defined($tindex)?$self->{sequence}->[$tindex]:@{$self->{sequence}}) | ||
| 787 | { | ||
| 788 | for my $s (@{$t}) | ||
| 789 | { | ||
| 790 | # s is id+style, second element is the style | ||
| 791 | delete $s->[1]->{_is_changed}; | ||
| 792 | } | ||
| 793 | } | ||
| 794 | } | ||
| 795 | |||
| 796 | =pod | ||
| 797 | |||
| 798 | =item B<topic_as_listlet:> | ||
| 799 | |||
| 800 | $xml=$view->topic_as_listlet(I<tid or index>, [I<xml::writer obj>,I<io::string obj>]); | ||
| 801 | |||
| 802 | This method produces a Listlet for a particular topic. Without the optional arguments, | ||
| 803 | the method creates and uses a temporary XML::Writer object for creating the output; | ||
| 804 | With writer and ios arguments given, these will be used instead. | ||
| 805 | |||
| 806 | A Listlet is a very simple XML representation of a topic's slide/page as described by | ||
| 807 | the topic's style. Basic listlets conform to the following DTD: | ||
| 808 | |||
| 809 | <!ELEMENT listlet (PCDATA? listlet*)> | ||
| 810 | <!ATTLIST listlet title CDATA #IMPLIED | ||
| 811 | url URL #IMPLIED> | ||
| 812 | |||
| 813 | Every listlet has a header with the slide title, and recursively embedded further listlets for | ||
| 814 | the topic's displayed aspects. The textual content is either in the title attribute or in the | ||
| 815 | text data. For listlets dealing with addressable resources, a url attribute can be present as well. | ||
| 816 | |||
| 817 | The title attribute of a listlet is chosen using find_nicename (see below) to provide context-sensitive | ||
| 818 | titles. | ||
| 819 | |||
| 820 | However, topic_as_listlet does not limit the user to said (minimal) DTD: any scalar-valued style attribute whose | ||
| 821 | name does not start with an underscore will be transformed into an XML attribute of the same name in | ||
| 822 | the listlet output. (This obviously imposes XML attribute naming restrictions to style attributes.) | ||
| 823 | |||
| 824 | Attributes with a leading underscore in their name are considered internal and are not printed at all: | ||
| 825 | such attributes may be useful if one needs to store hashes or arrays in a style but still uses topic_as_listlet. | ||
| 826 | |||
| 827 | Certain style attributes have special meaning for topic_as_listlet and are not printed | ||
| 828 | directly as well: | ||
| 829 | |||
| 830 | =over | ||
| 831 | |||
| 832 | =item _on: | ||
| 833 | |||
| 834 | Controls whether this aspect is shown or not. | ||
| 835 | |||
| 836 | =item _type_on: | ||
| 837 | |||
| 838 | Controls whether a typed occurrence should be shown with an I<enclosing> listlet | ||
| 839 | that displays the occurrence's type. With this option, the AsTMa fragment | ||
| 840 | |||
| 841 | reference | ||
| 842 | bn: reference text: | ||
| 843 | |||
| 844 | in(reference): some book | ||
| 845 | |||
| 846 | is rendered as | ||
| 847 | |||
| 848 | <listlet title="reference text:"> | ||
| 849 | <listlet>some book</listlet> | ||
| 850 | </listlet> | ||
| 851 | |||
| 852 | Without the attribute, only | ||
| 853 | |||
| 854 | <listlet>some book</listlet> | ||
| 855 | |||
| 856 | would be output. | ||
| 857 | |||
| 858 | =item _player_order | ||
| 859 | |||
| 860 | Controls the display ordering of players for I<this> particular display of an association. This is an array reference, | ||
| 861 | the values correspond to the TM-native ordering of players as present in the assertion and the position | ||
| 862 | describes the intended display position. | ||
| 863 | |||
| 864 | _player_order and _player_style are present wherever an association can be shown (= in the styles | ||
| 865 | of all involved topics). Each of these displays has separate _player_style and _player_order attributes. | ||
| 866 | _player_order must be kept consistent (ie. list all players exactly once). | ||
| 867 | |||
| 868 | =item _player_styles | ||
| 869 | |||
| 870 | This controls the display of individual players within an association. This is an array reference, | ||
| 871 | with position corresponding to the TM-native ordering of players in the assertion. Each array cell | ||
| 872 | is a hashref with style attributes applicable to this particular player. | ||
| 873 | |||
| 874 | Within these individual player styles, two attributes are special: | ||
| 875 | |||
| 876 | =over | ||
| 877 | |||
| 878 | =item _on | ||
| 879 | |||
| 880 | controls whether this player is shown. | ||
| 881 | |||
| 882 | =item _role_on | ||
| 883 | |||
| 884 | controls whether the role the player is playing should be shown parenthesized after the player. | ||
| 885 | |||
| 886 | =back | ||
| 887 | |||
| 888 | For every player a (possibly empty) _player_styles element must always be present. | ||
| 889 | |||
| 890 | =back | ||
| 891 | |||
| 892 | =cut | ||
| 893 | |||
| 894 | # returns the unadorned listlet for a single sequenced topic | ||
| 895 | # returns undef if no matching topic is found | ||
| 896 | # if writer and ios are given, they will be used to generate the output | ||
| 897 | # if not, a new writer is created an an appropriate xml decl is produced. | ||
| 898 | sub topic_as_listlet | ||
| 899 | { | ||
| 900 | my ($self,$tidorindex,$writer,$ios)=@_; | ||
| 901 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 902 | |||
| 903 | my $index=$self->_find_tidindex($tidorindex,$self->{sequence}); | ||
| 904 | return undef if (!defined $index); | ||
| 905 | |||
| 906 | my $compat=(exists($self->{tm}->{usual_suspects})?$self->{tm}->{baseuri}:""); | ||
| 907 | |||
| 908 | if (!$ios || !$writer) | ||
| 909 | { | ||
| 910 | $ios = IO::String->new; | ||
| 911 | $writer = new XML::Writer(OUTPUT=>$ios); | ||
| 912 | $writer->xmlDecl("iso-8859-1"); | ||
| 913 | } | ||
| 914 | |||
| 915 | my $t=$self->{sequence}->[$index]; | ||
| 916 | # first is the midlet, for which we'll use the first active basename as title | ||
| 917 | # ...or the topic id without the base | ||
| 918 | my $tid=$t->[0]->[0]; | ||
| 919 | my ($tindex,$title)=$self->find_nicename($tid,$t,undef); | ||
| 920 | $writer->startTag("listlet","title"=>$title); | ||
| 921 | |||
| 922 | my $map=$self->{tm}; | ||
| 923 | |||
| 924 | my ($typed_oc,$typed_ass,$typed_bn)=("","",""); | ||
| 925 | # rest are bns, ocs, sundry associations | ||
| 926 | for my $i (1..$#$t) | ||
| 927 | { | ||
| 928 | my ($id,$style)=@{$t->[$i]}; | ||
| 929 | next if (!$style->{_on} || $i==$tindex); # uninteresting or already consumed | ||
| 930 | |||
| 931 | # clean styles of the internal _xxx keys | ||
| 932 | my @cls = map { ($_,$style->{$_}) } (grep(!/^_/ && defined $style->{$_}, keys %{$style})); | ||
| 933 | my $a=$map->retrieve($id); | ||
| 934 | my $type=$a->[TM->TYPE]; | ||
| 935 | |||
| 936 | # deal with typed occurrence combining: if typed_oc is set and this one is | ||
| 937 | # not an occurrence or one with a different type or one where no type info is desired | ||
| 938 | # then close the dangling grouping listlet. | ||
| 939 | if ($typed_oc && ( $a->[TM->KIND]!=TM->OCC || $type ne $typed_oc || !$style->{_type_on})) | ||
| 940 | { | ||
| 941 | $typed_oc=""; | ||
| 942 | $writer->endTag("listlet"); | ||
| 943 | } | ||
| 944 | # ditto assoc | ||
| 945 | if ($typed_ass && ( $a->[TM->KIND]!=TM->ASSOC || $typed_ass ne join(' ',$type,sort $map->get_roles($a,$tid)))) | ||
| 946 | { | ||
| 947 | $typed_ass=""; | ||
| 948 | $writer->endTag("listlet"); | ||
| 949 | } | ||
| 950 | # ditto basename | ||
| 951 | if ($typed_bn && ( $a->[TM->KIND]!=TM->NAME || $type ne $typed_oc || !$style->{_type_on})) | ||
| 952 | { | ||
| 953 | $typed_bn=""; | ||
| 954 | $writer->endTag("listlet"); | ||
| 955 | } | ||
| 956 | |||
| 957 | # what is this thing? | ||
| 958 | if ($a->[TM->KIND]==TM->NAME) | ||
| 959 | { | ||
| 960 | # (extra) basename: type-prefixed listlet entry | ||
| 961 | if ($type ne $compat."name" && !$typed_bn && $style->{_type_on}) | ||
| 962 | { | ||
| 963 | $writer->startTag("listlet", | ||
| 964 | title=>($self->find_nicename($type,undef,undef))[1], | ||
| 965 | ("bullet"=>1)); | ||
| 966 | $typed_bn=$type; | ||
| 967 | } | ||
| 968 | $writer->dataElement("listlet","", | ||
| 969 | title=>($map->get_x_players($a,$compat."value"))[0]->[0],@cls); | ||
| 970 | } | ||
| 971 | elsif ($a->[TM->KIND]==TM->OCC) | ||
| 972 | { | ||
| 973 | # occurrence: inline text or hyperlink, with extra type-wrapper if nontrivially typed | ||
| 974 | # and if wanted | ||
| 975 | my $extra; | ||
| 976 | if ($type ne $compat."occurrence" && !$typed_oc && $style->{_type_on}) | ||
| 977 | { | ||
| 978 | # extra indentation/wrapping using the type's basename | ||
| 979 | # which we leave open and dangling... | ||
| 980 | $writer->startTag("listlet", | ||
| 981 | title=>($self->find_nicename($type,undef,undef))[1], | ||
| 982 | # style of the wrapper: only bullet | ||
| 983 | # fixme: i have not real good answer as to what style such | ||
| 984 | # an extra element should have: none? bulleted? common of children? | ||
| 985 | # first child? all ugly... | ||
| 986 | ("bullet"=>1)); | ||
| 987 | # ...because we remember this type for subsequent occs of the same type | ||
| 988 | # where we don't make indentation/wrapping listlets | ||
| 989 | $typed_oc=$type; | ||
| 990 | } | ||
| 991 | my $value=($map->get_x_players($a,$compat."value"))[0]; | ||
| 992 | if ($value->[1] eq TM::Literal->URI) | ||
| 993 | { | ||
| 994 | $writer->dataElement('listlet','','url'=>$value->[0],@cls); | ||
| 995 | } | ||
| 996 | else | ||
| 997 | { | ||
| 998 | $writer->dataElement('listlet',$value->[0],@cls); | ||
| 999 | } | ||
| 1000 | } | ||
| 1001 | else | ||
| 1002 | { | ||
| 1003 | # general association: walk the sequenced role/player combos | ||
| 1004 | # collate assocs of the same type, IFF our roles are the same | ||
| 1005 | # FIXME: good idea for non-isa/instances? dunno | ||
| 1006 | |||
| 1007 | # find the name of the role the current topic plays in this assoc | ||
| 1008 | # (if any!) and find a name that is scoped with the played role if possible | ||
| 1009 | my @roles=$map->get_roles($a,$tid); | ||
| 1010 | $writer->startTag("listlet", | ||
| 1011 | title=>($self->find_nicename($type,undef,$roles[0]))[1], # undef fine if not player | ||
| 1012 | @cls) if ($typed_ass ne join(' ',$type,sort @roles)); | ||
| 1013 | $typed_ass=join(' ',$type,sort @roles); | ||
| 1014 | |||
| 1015 | # walk through the player-role elements of this assoc | ||
| 1016 | for my $pi (@{$style->{_player_order}}) | ||
| 1017 | { | ||
| 1018 | my $thisstyle=$style->{_player_styles}->[$pi]; | ||
| 1019 | next if (!$thisstyle || !$thisstyle->{_on}); # disabled p/r combo | ||
| 1020 | |||
| 1021 | my $player=$a->[TM->PLAYERS]->[$pi]; | ||
| 1022 | my $role=$a->[TM->ROLES]->[$pi]; | ||
| 1023 | my @thiscls=map { ($_,$thisstyle->{$_}) } (grep(!/^_/,keys %{$thisstyle})); | ||
| 1024 | $writer->dataElement("listlet","", | ||
| 1025 | title=>(($self->find_nicename($player,undef,undef))[1] | ||
| 1026 | .($thisstyle->{_role_on}? | ||
| 1027 | (" [".$self->find_nicename($role,undef,undef)."]"):"")), | ||
| 1028 | @thiscls); | ||
| 1029 | } | ||
| 1030 | } | ||
| 1031 | } | ||
| 1032 | # if a typed occurrence was the last scheduled element, then we need | ||
| 1033 | # to close the dangling container | ||
| 1034 | $writer->endTag("listlet") if ($typed_oc || $typed_ass || $typed_bn); | ||
| 1035 | $writer->endTag("listlet"); # the "page" listlet | ||
| 1036 | return ${$ios->string_ref}; | ||
| 1037 | } | ||
| 1038 | |||
| 1039 | |||
| 1040 | =pod | ||
| 1041 | |||
| 1042 | =item B<make_listlet:> | ||
| 1043 | |||
| 1044 | $xml=$view->make_listlet([I<metadata hash>]); | ||
| 1045 | |||
| 1046 | This method runs topic_as_listlet on all sequenced topics, and optionally adds a | ||
| 1047 | metadata node to the toplevel listlet: | ||
| 1048 | |||
| 1049 | <!ELEMENT metadata title author*> | ||
| 1050 | <!ELEMENT title PCDATA> | ||
| 1051 | <!ELEMENT author PCDATA email? affiliation?> | ||
| 1052 | <!ELEMENT email PCDATA> | ||
| 1053 | <!ELEMENT affiliation PCDATA> | ||
| 1054 | <!ATTLIST affiliation url URL> | ||
| 1055 | |||
| 1056 | The metadata argument is optional, and the hash keys directly correspond to the attributes | ||
| 1057 | of the resulting metadata node - with a possible exception for authorship: | ||
| 1058 | |||
| 1059 | If the author value is scalar, then email, affiliation and url are expected to be scalar, too, | ||
| 1060 | and a single author node is created. If author is an array-ref, then all other attributes (except title) | ||
| 1061 | are expected to be array-refs as well and matching array values will be used to construct multiple author | ||
| 1062 | nodes. | ||
| 1063 | |||
| 1064 | make_listlet returns the listlet xml or undef if the arguments are inconsistent. | ||
| 1065 | |||
| 1066 | =cut | ||
| 1067 | |||
| 1068 | # options: known keys are title, author, email, affiliation, url | ||
| 1069 | # if author is a scalar, then all others are expected to be scalars, too | ||
| 1070 | # if author is ref to array, then so must the others. | ||
| 1071 | # returns undef if the arguments make no sense or the listlet xml | ||
| 1072 | |||
| 1073 | # title and author are empty if not given; affiliation, email and url | ||
| 1074 | # are not present in this case. | ||
| 1075 | sub make_listlet | ||
| 1076 | { | ||
| 1077 | my ($self,%opt)=@_; | ||
| 1078 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 1079 | |||
| 1080 | my $listletns = "http://topicmaps.bond.edu.au/listlet/1.1/"; | ||
| 1081 | |||
| 1082 | my $ios = IO::String->new; | ||
| 1083 | my $writer = new XML::Writer(OUTPUT=>$ios); | ||
| 1084 | $writer->xmlDecl("iso-8859-1"); | ||
| 1085 | $writer->startTag("listlet","xmlns" => $listletns); | ||
| 1086 | |||
| 1087 | $writer->startTag("metadata"); | ||
| 1088 | $opt{title} && $writer->dataElement("title",$opt{title}); | ||
| 1089 | |||
| 1090 | my $bad=0; | ||
| 1091 | $bad++ if UNIVERSAL::isa($opt{author},"ARRAY"); | ||
| 1092 | $bad++ if UNIVERSAL::isa($opt{email},"ARRAY"); | ||
| 1093 | $bad++ if UNIVERSAL::isa($opt{affiliation},"ARRAY"); | ||
| 1094 | $bad++ if UNIVERSAL::isa($opt{url},"ARRAY"); | ||
| 1095 | return undef if ($bad!=0 && $bad!=4); # all or none must be arrays | ||
| 1096 | |||
| 1097 | # serialise the metadata and print it out | ||
| 1098 | my @md; | ||
| 1099 | if (!ref($opt{author})) | ||
| 1100 | { | ||
| 1101 | push @md,[@opt{"author","email","affiliation","url"}]; | ||
| 1102 | } | ||
| 1103 | else | ||
| 1104 | { | ||
| 1105 | map { push @md,[$opt{"author"}->[$_],$opt{"email"}->[$_], | ||
| 1106 | $opt{"affiliation"}->[$_],$opt{"url"}->[$_] ] } (0..@{$opt{"author"}}); | ||
| 1107 | } | ||
| 1108 | for (@md) | ||
| 1109 | { | ||
| 1110 | my ($a,$e,$af,$url)=@$_; | ||
| 1111 | |||
| 1112 | next if (!$a); | ||
| 1113 | $writer->startTag("author");$writer->characters($a); | ||
| 1114 | |||
| 1115 | $writer->dataElement("email",$e) if ($e); | ||
| 1116 | $writer->dataElement("affiliation",$af,$url?("url"=>$url):()) if ($af); | ||
| 1117 | $writer->endTag("author"); | ||
| 1118 | } | ||
| 1119 | $writer->endTag("metadata"); | ||
| 1120 | |||
| 1121 | |||
| 1122 | # main part: walk through sequence and print every active style element | ||
| 1123 | my $map=$self->{tm}; | ||
| 1124 | |||
| 1125 | for my $i (0..$#{$self->{sequence}}) | ||
| 1126 | { | ||
| 1127 | $self->topic_as_listlet($i,$writer,$ios); | ||
| 1128 | } | ||
| 1129 | |||
| 1130 | $writer->endTag("listlet"); # the top listlet | ||
| 1131 | $writer->end(); | ||
| 1132 | return ${$ios->string_ref}; | ||
| 1133 | } | ||
| 1134 | |||
| 1135 | # returns a default style appropriate for the element | ||
| 1136 | # this depends on the thing (assoc/occ/bn) and on the context (the topic in the sequence) | ||
| 1137 | # may need to run with a temp. new map (during reconcile), map otherwise taken from self | ||
| 1138 | # returns ref of style hash | ||
| 1139 | sub _default_style | ||
| 1140 | { | ||
| 1141 | my ($self,$context,$thing,$map)=@_; | ||
| 1142 | my %style=(_on=>1); | ||
| 1143 | $map||=$self->{tm}; | ||
| 1144 | |||
| 1145 | # a topic itself -> on and be done with it. | ||
| 1146 | return \%style if ($context eq $thing); | ||
| 1147 | |||
| 1148 | my $compat=(exists($self->{tm}->{usual_suspects})?$self->{tm}->{baseuri}:""); | ||
| 1149 | |||
| 1150 | # what's the nature of this thing? | ||
| 1151 | my $a=$map->retrieve($thing); | ||
| 1152 | die "$thing is not an assertion!\n" if (!$a); | ||
| 1153 | |||
| 1154 | # occs and basenames: type display | ||
| 1155 | if ($a->[TM->KIND]==TM->OCC) | ||
| 1156 | { | ||
| 1157 | # non-trivially typed -> _type_on | ||
| 1158 | $style{_type_on}=1 if ($a->[TM->TYPE] ne $compat."occurrence"); | ||
| 1159 | } | ||
| 1160 | elsif ($a->[TM->KIND]==TM->NAME) | ||
| 1161 | { | ||
| 1162 | # non-trivially typed -> _type_on | ||
| 1163 | $style{_type_on}=1 if ($a->[TM->TYPE] ne $compat."name"); | ||
| 1164 | } | ||
| 1165 | elsif ($a->[TM->KIND]==TM->ASSOC) | ||
| 1166 | { | ||
| 1167 | # if this is isa/class/instance, then no _role_on please! | ||
| 1168 | my $roleon=$a->[TM->TYPE] eq $compat."isa"?0:1; | ||
| 1169 | # prime the _player_order and _player_styles | ||
| 1170 | my @players=@{$a->[TM->PLAYERS]}; | ||
| 1171 | $style{_player_order}=[0..$#players]; | ||
| 1172 | $style{_player_styles}=[ map { {_on=>1,_role_on=>$roleon,bullet=>1} } (@players)]; | ||
| 1173 | |||
| 1174 | # but disable the roles which the context topic plays (if any, it could be the role instead) | ||
| 1175 | map { delete $style{_player_styles}->[$_]->{_on}; } grep($context eq $players[$_], 0..$#players); | ||
| 1176 | } | ||
| 1177 | |||
| 1178 | # everthing is bulleted by default | ||
| 1179 | $style{bullet}=1; | ||
| 1180 | |||
| 1181 | return \%style; | ||
| 1182 | } | ||
| 1183 | |||
| 1184 | =pod | ||
| 1185 | |||
| 1186 | =item B<find_nicename:> | ||
| 1187 | |||
| 1188 | ($source,$displayname)=$view->find_nicename(I<tid or index> [,$ownstyles,$context]); | ||
| 1189 | |||
| 1190 | This method adds some context-sensitivity to choosing names for the display of | ||
| 1191 | a topic. If only the tid/index argument is given, then the first basename of this topic | ||
| 1192 | with source=0 is returned. | ||
| 1193 | If the ownstyles restriction is set, then the first I<displayed> basename of this topic | ||
| 1194 | and its style list index is returned. If the restriction is not set, but context is given, | ||
| 1195 | then the basename that is scoped with the context is returned, or the first unscoped basename. | ||
| 1196 | |||
| 1197 | In any case, if nothing matching was found, the tid without baseuri is returned. | ||
| 1198 | |||
| 1199 | As an example, consider this AsTMa map fragment: | ||
| 1200 | vienna (city) | ||
| 1201 | bn: Vindobona | ||
| 1202 | bn@austria: Wien | ||
| 1203 | bn@oepest: A-1xxx | ||
| 1204 | in: Wien ist anders. | ||
| 1205 | |||
| 1206 | find_nicename("tm://vienna") will return any of the three basenames, depending on how the TM backend orders | ||
| 1207 | assertions. find_nicename("tm://vienna",0,"tm://murkin") will return (0,"Vindobona") because there is | ||
| 1208 | no basename with the requested scope. find_nicename("tm://vienna",0,"tm://oepest") will return (0,"A-1xxx"). | ||
| 1209 | |||
| 1210 | This comes especially handy for associations, where the best name often depends on | ||
| 1211 | the perspective; for this find_nicename works best when given some help like in the following map fragment: | ||
| 1212 | |||
| 1213 | (is-part-of) | ||
| 1214 | whole: a_thing | ||
| 1215 | part: some_other_thing | ||
| 1216 | |||
| 1217 | is-part-of | ||
| 1218 | bn@whole: has components: | ||
| 1219 | bn@part: is a component of: | ||
| 1220 | |||
| 1221 | =cut | ||
| 1222 | |||
| 1223 | # returns (index,text) of a suitable basename to display | ||
| 1224 | # if no restriction given or scope is requested: returns (0,basename-with-scope) from the whole map | ||
| 1225 | # otherwise: only checks the listed styles for the first active basename | ||
| 1226 | # returns (0,tid-without-base) if nothing suitable found | ||
| 1227 | sub find_nicename | ||
| 1228 | { | ||
| 1229 | my ($self,$tid,$ownstyle,$scope)=@_; | ||
| 1230 | die "argument is not a TM::View object\n" if (!$self || ref($self) ne __PACKAGE__); | ||
| 1231 | |||
| 1232 | my $map=$self->{tm}; | ||
| 1233 | my $nice; | ||
| 1234 | |||
| 1235 | my $compat=(exists($self->{tm}->{usual_suspects})?$self->{tm}->{baseuri}:""); | ||
| 1236 | |||
| 1237 | if (!$ownstyle || $scope) | ||
| 1238 | { | ||
| 1239 | # a minor hack for class/instance relationships: if the map user hasn't given | ||
| 1240 | # a nice setup, we use "instances:" and "is a:", rsp. | ||
| 1241 | if ($tid eq $compat."isa") | ||
| 1242 | { | ||
| 1243 | return (0,$scope eq $compat."class"?"instances:":"is a:"); | ||
| 1244 | } | ||
| 1245 | |||
| 1246 | # this is for association titles and typed-occurrence titling | ||
| 1247 | # we look in the map for a basename assertion for this | ||
| 1248 | # topic with the proper scope (if given) | ||
| 1249 | my @bns=$map->match(TM->FORALL,topic=>$tid,char=>1,type=>$compat."name"); | ||
| 1250 | |||
| 1251 | # if not scoped: return first bn or tid without base, | ||
| 1252 | # if scoped: first bn with matching scope, unscoped basename, or tid without base | ||
| 1253 | my ($firstbn,$usbn); | ||
| 1254 | for my $a (@bns) | ||
| 1255 | { | ||
| 1256 | my $this=($map->get_x_players($a,$compat."value"))[0]->[0]; | ||
| 1257 | $firstbn||=$this; | ||
| 1258 | return (0,$this) if (!$scope || $a->[TM->SCOPE] eq $scope); | ||
| 1259 | $usbn=$this if ($a->[TM->SCOPE] eq $compat."us"); | ||
| 1260 | } | ||
| 1261 | return (0,$usbn) if ($usbn && $scope); | ||
| 1262 | return (0,$firstbn) if ($firstbn && !$scope); | ||
| 1263 | } | ||
| 1264 | else | ||
| 1265 | { | ||
| 1266 | # walk through our own styles and check each assertion for basename and active | ||
| 1267 | # zeroth style is irrelevant: midlet itself | ||
| 1268 | |||
| 1269 | my $index=$self->_find_tidindex($tid,$self->{sequence}); | ||
| 1270 | if (defined $index) | ||
| 1271 | { | ||
| 1272 | my $styles=$self->{sequence}->[$index]; | ||
| 1273 | for my $i (1..$#{$styles}) | ||
| 1274 | { | ||
| 1275 | next if (!$styles->[$i]->[1]->{_on}); | ||
| 1276 | my $a=$map->retrieve($styles->[$i]->[0]); | ||
| 1277 | next if ($a->[TM->KIND]!=TM->NAME); | ||
| 1278 | return ($i,($map->get_x_players($a,$compat."value"))[0]->[0]); | ||
| 1279 | } | ||
| 1280 | } | ||
| 1281 | } | ||
| 1282 | |||
| 1283 | # nothing found? tid without basename, then | ||
| 1284 | $nice=$tid; | ||
| 1285 | my $base=$self->{tm}->{baseuri}; | ||
| 1286 | $nice=~s/^$base//; | ||
| 1287 | return (0,$nice); | ||
| 1288 | } | ||
| 1289 | |||
| 1290 | =pod | ||
| 1291 | |||
| 1292 | =back | ||
| 1293 | |||
| 1294 | =head1 SEE ALSO | ||
| 1295 | |||
| 1296 | L<TM(3)> | ||
| 1297 | |||
| 1298 | =head1 AUTHOR | ||
| 1299 | |||
| 1300 | Alexander Zangerl, <alphazulu@cpan.org> | ||
| 1301 | |||
| 1302 | =head1 COPYRIGHT AND LICENSE | ||
| 1303 | |||
| 1304 | Copyright 2007, 2008 Alexander Zangerl | ||
| 1305 | |||
| 1306 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl | ||
| 1307 | itself. | ||
| 1308 | |||
| 1309 | =cut | ||
| 1310 | |||
| 1311 | 1; | ||
| 1312 | |||
| 1313 | __END__ | ||