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