script updates
[scpubgit/stemmatology.git] / script / save_to_db.pl
1 #!/usr/bin/env perl
2
3 use lib 'lib';
4 use strict;
5 use warnings;
6 use File::Basename;
7 use KiokuDB;
8 use KiokuDB::TypeMap::Entry::Naive;
9 use Text::Tradition;
10 use Text::Tradition::Stemma;
11
12 # Make a KiokuDB store from the traditions data we have.
13
14 my $kdb = KiokuDB->connect( "dbi:SQLite:dbname=db/traditions.db", 
15         create => 1,
16         typemap => KiokuDB::TypeMap->new(
17             isa_entries => {
18                 "Graph" => KiokuDB::TypeMap::Entry::Naive->new,
19                 "Graph::AdjacencyMap" => KiokuDB::TypeMap::Entry::Naive->new,
20             },
21         ),
22     );
23     
24 my %stemma_map = (
25         'florilegium.xml' => 'stemma_a.dot',
26         'besoin.xml' => 'stemma_b.dot',
27         'heinrichi.xml' => 'stemma_h.dot',
28         'parzival.xml' => 'stemma_p.dot',
29         's158.xml' => 'stemma_s.dot',
30         );
31
32 my $dir = $ARGV[0];
33 if( $dir ) {
34         $dir =~ s/\/$//;
35         opendir( DIR, $dir ) or die "Could not open directory $dir";
36         while( readdir DIR ) {
37                 next unless /\.xml$/;
38                 print STDERR "Looking at $_\n";
39                 my $tradition = Text::Tradition->new( 
40                         'input' => 'Self',
41                         'file' => "$dir/$_",
42                         'linear' => 1,
43                         );
44                 my $stemma;
45                 if( exists $stemma_map{$_} ) {
46                         my $stemmafile = "$dir/" . $stemma_map{$_};     
47                         open my $stemma_fh, '<', $stemmafile or die "Could not read stemma file $stemmafile";           
48                         $stemma = Text::Tradition::Stemma->new(
49                                 'collation' => $tradition->collation,
50                                 'dot' => $stemma_fh,
51                                 );
52                 }
53                         
54                 my $scope = $kdb->new_scope;
55                 my $tid = $kdb->store( $tradition );
56                 my $sid = $kdb->store( $stemma ) if $stemma;
57                 print STDERR "Stored tradition for " . $tradition->name . " at $tid\n";
58                 print STDERR "\tand stemma at $sid\n" if $stemma;
59         }
60 }
61
62 # Now try reading the objects from the DB.
63
64 my $scope = $kdb->new_scope;
65
66 my $stream = $kdb->root_set;
67 until( $stream->is_done ) {
68         foreach my $t ( $stream->items ) {
69                 print STDERR "*** Object " . $kdb->object_to_id( $t ) . " ***\n";
70                 if( ref( $t ) eq 'Text::Tradition' ) {
71                         print STDERR "Got tradition " . $t->name . " out of the database\n";
72                         my @wits = map { $_->sigil } $t->witnesses;
73                         print STDERR "...with witnesses @wits\n";
74                         my $c = $t->collation;
75                         print STDERR "Collation has " . scalar( $c->readings ) . " readings\n";
76                         print STDERR "Collation has " . scalar( $c->paths ) . " paths\n";
77                         print STDERR "Collation has " . scalar( $c->relationships ) . " relationship links\n";
78                 } elsif( ref( $t ) eq 'Text::Tradition::Stemma' ) {
79                         print STDERR "Got stemma for tradition " . $t->collation->tradition->name 
80                                 . " out of the database\n";
81                         print STDERR "Stemma graph is " . $t->graph . "\n";
82                 } else {
83                         print STDERR "Got unexpected object of type " . ref( $t ) 
84                                 . " out of the database\n";
85                 }
86         }
87 }