File Coverage

blib/lib/Wikidata/Reconcilation.pm
Criterion Covered Total %
statement 39 61 63.9
branch 3 12 25.0
condition 0 6 0.0
subroutine 10 11 90.9
pod 2 2 100.0
total 54 92 58.7


line stmt bran cond sub pod time code
1             package Wikidata::Reconcilation;
2              
3 4     4   565166 use strict;
  4         11  
  4         176  
4 4     4   25 use warnings;
  4         9  
  4         2430  
5              
6 4     4   2202 use Class::Utils qw(set_params);
  4         41118  
  4         93  
7 4     4   334 use Error::Pure qw(err);
  4         9  
  4         167  
8 4     4   3602 use LWP::UserAgent;
  4         260852  
  4         207  
9 4     4   2419 use WQS::SPARQL;
  4         35117  
  4         172  
10 4     4   1822 use WQS::SPARQL::Result;
  4         2338  
  4         2495  
11              
12             our $VERSION = 0.04;
13              
14             # Constructor.
15             sub new {
16 3     3 1 422321 my ($class, @params) = @_;
17              
18             # Create object.
19 3         8 my $self = bless {}, $class;
20              
21             # User agent.
22 3         59 $self->{'agent'} = __PACKAGE__." ($VERSION)";
23              
24             # First match mode.
25 3         11 $self->{'first_match'} = 0;
26              
27             # Language.
28 3         8 $self->{'language'} = 'en';
29              
30             # LWP::UserAgent object.
31 3         9 $self->{'lwp_user_agent'} = undef;
32              
33             # Verbose mode.
34 3         9 $self->{'verbose'} = 0;
35              
36             # Process parameters.
37 3         18 set_params($self, @params);
38              
39 3 100       38 if (! defined $self->{'lwp_user_agent'}) {
40             $self->{'lwp_user_agent'} = LWP::UserAgent->new(
41 2         50 'agent' => $self->{'agent'},
42             );
43             } else {
44 1 50       7 if (! $self->{'lwp_user_agent'}->isa('LWP::UserAgent')) {
45 1         51 err "Parameter 'lwp_user_agent' must be a 'LWP::UserAgent' instance.";
46             }
47             }
48              
49             $self->{'_q'} = WQS::SPARQL->new(
50             'lwp_user_agent' => $self->{'lwp_user_agent'},
51 2         6960 'verbose' => $self->{'verbose'},
52             );
53              
54 2         178 return $self;
55             }
56              
57             sub reconcile {
58 1     1 1 8 my ($self, $reconcilation_rules_hr) = @_;
59              
60 1         6 my @sparql = $self->_reconcile($reconcilation_rules_hr);
61              
62 0         0 my $ret_hr;
63             my %qids;
64 0 0       0 if ($self->{'verbose'}) {
65 0         0 print "SPARQL queries:\n";
66             }
67 0         0 foreach my $sparql (@sparql) {
68 0         0 $ret_hr = $self->{'_q'}->query($sparql);
69 0         0 my @ret = map { $_->{'item'} } WQS::SPARQL::Result->new(
70 0         0 'verbose' => $self->{'verbose'},
71             )->result($ret_hr);
72 0         0 foreach my $ret (@ret) {
73 0         0 $qids{$ret}++;
74             }
75 0 0 0     0 if (@ret && $self->{'first_match'}) {
76 0         0 last;
77             }
78             }
79 0 0       0 if ($self->{'verbose'}) {
80 0         0 print "Results:\n";
81 0         0 foreach my $item (sort keys %qids) {
82 0         0 print '- '.$item.': '.$qids{$item}."\n";
83             }
84             }
85              
86 0         0 return sort keys %qids;
87             }
88              
89             sub _reconcile {
90 1     1   3 my ($self, $reconcilation_rules_hr) = @_;
91              
92 1         6 err "This is abstract class. You need to implement _reconcile() method.";
93 0           my @sparql;
94              
95 0           return @sparql;
96             }
97              
98             sub _exists_id {
99 0     0     my ($self, $reconcilation_rules_hr, $id) = @_;
100              
101 0 0 0       if (exists $reconcilation_rules_hr->{'identifiers'}->{$id}
102             && defined $reconcilation_rules_hr->{'identifiers'}->{$id}) {
103              
104 0           return 1;
105             } else {
106 0           return 0;
107             }
108             }
109              
110             1;
111              
112             __END__