1 package Text::Tradition::Directory;
7 use KiokuDB::TypeMap::Entry::Naive;
9 extends 'KiokuX::Model';
13 Text::Tradition::Directory - a KiokuDB interface for storing and retrieving traditions
17 use Text::Tradition::Directory;
18 my $d = Text::Tradition::Directory->new(
19 'dsn' => 'dbi:SQLite:mytraditions.db',
20 'extra_args' => { 'create' => 1 },
23 my $tradition = Text::Tradition->new( @args );
24 $d->save_tradition( $tradition );
25 my $stemma = Text::Tradition::Stemma->new(
26 'dot' => $dotfile, 'collation' => $tradition->collation );
27 $d->save_stemma( $stemma );
29 foreach my $id ( $d->traditions ) {
30 print $d->tradition( $id )->name;
31 print $d->stemma( $id )->as_svg;
36 Text::Tradition::Directory is an interface for storing and retrieving text traditions and all their data, including an associated stemma hypothesis. It is an instantiation of a KiokuDB::Model, storing traditions and associated stemmas by UUID.
42 Returns a Directory object. Apart from those documented in L<KiokuX::Model>,
47 =item * preload - Load all traditions and stemmata into memory upon instantiation. Defaults to true. (TODO manage on-demand loading)
53 Returns the ID of all traditions in the database.
55 =head2 tradition( $id )
57 Returns the Text::Tradition object of the given ID.
61 Returns the Text::Tradition::Stemma object associated with the given tradition ID.
63 =head2 save_tradition( $tradition )
65 Writes the given tradition to the database, returning its UUID.
67 =head2 save_stemma( $stemma )
69 Writes the given stemma to the database, returning its UUID.
75 use Text::Tradition::Stemma;
76 use_ok 'Text::Tradition::Directory';
78 my $fh = File::Temp->new();
79 my $file = $fh->filename;
81 my $dsn = "dbi:SQLite:dbname=$file";
83 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
84 'extra_args' => { 'create' => 1 } );
85 is( ref $d, 'Text::Tradition::Directory', "Got directory object" );
87 my $t = Text::Tradition->new(
90 'file' => 't/data/simple.txt',
92 my $uuid = $d->save_tradition( $t );
93 ok( $uuid, "Saved test tradition" );
95 my $s = Text::Tradition::Stemma->new(
96 'collation' => $t->collation,
97 'dotfile' => 't/data/simple.dot' );
98 my $sid = $d->save_stemma( $s );
99 ok( $sid, "Saved test stemma" );
101 is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
102 is( $d->stemma( $uuid ), $s, "Correct stemma returned for id" );
103 is( scalar $d->tradition_ids, 1, "Only one tradition in DB" );
105 # Connect to a new instance
106 my $e = Text::Tradition::Directory->new( 'dsn' => $dsn );
107 is( scalar $e->tradition_ids, 1, "One tradition preloaded from DB" );
108 my $te = $e->tradition( $uuid );
109 is( $te->name, $t->name, "New instance returns correct tradition" );
110 my $se = $e->stemma( $uuid );
111 is( $se->graph, $s->graph, "New instance returns correct stemma" );
112 is( $e->tradition( 'NOT-A-UUID' ), undef, "Undef returned for non-tradition" );
113 is( $e->stemma( 'NOT-A-UUID' ), undef, "Undef returned for non-stemma" );
114 $te->name( "Changed name" );
115 my $new_id = $e->save_tradition( $te );
116 is( $new_id, $uuid, "Updated tradition ID did not change" );
118 my $f = Text::Tradition::Directory->new( 'dsn' => $dsn, 'preload' => 0 );
119 is( scalar $f->tradition_ids, 0, "No traditions preloaded from DB" );
120 ### TODO This doesn't work, as I cannot get an object scope in the
121 ### 'tradition' wrapper.
122 # my $tf = $f->tradition( $uuid );
123 # is( $tf->name, $t->name, "Next instance returns correct tradition" );
124 # is( $tf->name, "Changed name", "Change to tradition carried through" );
132 default => sub { {} },
136 add_tradition => 'set',
138 tradition_ids => 'keys',
144 isa => 'KiokuDB::TypeMap',
146 KiokuDB::TypeMap->new(
148 "Graph" => KiokuDB::TypeMap::Entry::Naive->new,
149 "Graph::AdjacencyMap" => KiokuDB::TypeMap::Entry::Naive->new,
161 around 'tradition' => sub {
162 my( $orig, $self, @arg ) = @_;
163 my $data = $self->$orig( @arg );
165 # Connect to the DB and fetch the thing.
168 my $trad = $self->lookup( $id );
169 if( ref( $trad ) eq 'Text::Tradition' ) {
170 $self->add_tradition( $id => $trad );
173 # If we got this far...
176 return $data->{'object'};
179 around 'stemma' => sub {
180 my( $orig, $self, @arg ) = @_;
181 my $data = $self->$orig( @arg );
183 # Connect to the DB and fetch the thing.
186 my $trad = $self->lookup( $id );
187 if( ref( $trad ) eq 'Text::Tradition' ) {
189 $self->add_tradition( $id => $trad );
190 # Find the stemma whose collation belongs to $trad
191 my $ret = $self->grep( sub { $_->collation eq $trad->collation } );
193 until ( $ret->is_done ) {
194 foreach my $st ( $ret->items ) {
195 warn "Found two saved stemmas for tradition $id" if $stemma;
200 $self->add_stemma( $stemma );
204 # If we got this far...
207 return $data->{'stemma'};
210 around 'add_tradition' => sub {
211 my( $orig, $self, $id, $obj ) = @_;
212 $self->$orig( $id => { 'object' => $obj } );
215 around 'add_stemma' => sub {
216 my( $orig, $self, $id, $obj ) = @_;
217 $self->{data_hash}->{$id}->{'stemma'} = $obj;
220 # Load all the relevant data from the DSN we were passed.
226 $self->fetch_all if( $self->dsn && $self->preload );
229 # Connect to self, get the traditions and stemmas, and save them
233 my $scope = $self->new_scope;
234 my $stream = $self->root_set;
236 until( $stream->is_done ) {
237 foreach my $obj ( $stream->items ) {
238 my $uuid = $self->object_to_id( $obj );
239 if( ref( $obj ) eq 'Text::Tradition' ) {
240 $self->add_tradition( $uuid => $obj );
241 } elsif( ref( $obj ) eq 'Text::Tradition::Stemma' ) {
242 $stemmata{$obj->collation} = $obj;
244 warn "Found root object in DB that is neither tradition nor stemma: $obj";
248 # Now match the stemmata to their traditions.
249 foreach my $id ( $self->tradition_ids ) {
250 my $c = $self->tradition( $id )->collation;
251 if( exists $stemmata{$c} ) {
252 $self->add_stemma( $id => $stemmata{$c} );
259 my( $self, $tradition ) = @_;
260 # Write the thing to the db and return its ID.
261 unless( ref( $tradition ) eq 'Text::Tradition' ) {
262 warn "Object $tradition is not a Text::Tradition";
265 my $scope = $self->new_scope;
266 my $uuid = $self->store( $tradition );
267 $self->add_tradition( $uuid => $tradition );
272 my( $self, $stemma ) = @_;
273 unless( ref( $stemma ) eq 'Text::Tradition::Stemma' ) {
274 warn "Object $stemma is not a Text::Tradition::Stemma";
277 my $scope = $self->new_scope;
278 # Get the tradition to which this stemma belongs.
279 my $tradition = $stemma->collation->tradition;
280 # Make sure the tradition is in the DB.
281 my $tid = $self->save_tradition( $tradition );
283 warn "Could not access this stemma's tradition; aborting";
286 my $sid = $self->store( $stemma );
287 $self->add_stemma( $tid => $stemma );