File Coverage

blib/lib/NNexus/Job.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             # /=====================================================================\ #
2             # | NNexus Autolinker | #
3             # | Job Request Module | #
4             # |=====================================================================| #
5             # | Part of the Planetary project: http://trac.mathweb.org/planetary | #
6             # | Research software, produced as part of work done by: | #
7             # | the KWARC group at Jacobs University | #
8             # | Copyright (c) 2012 | #
9             # | Released under the MIT License (MIT) | #
10             # |---------------------------------------------------------------------| #
11             # | Adapted from the original NNexus code by | #
12             # | James Gardner and Aaron Krowne | #
13             # |---------------------------------------------------------------------| #
14             # | Deyan Ginev #_# | #
15             # | http://kwarc.info/people/dginev (o o) | #
16             # \=========================================================ooo==U==ooo=/ #
17             package NNexus::Job;
18 5     5   434220 use strict;
  5         9  
  5         201  
19 5     5   24 use warnings;
  5         8  
  5         161  
20 5     5   24 use feature qw(say);
  5         12  
  5         437  
21              
22 5     5   1783 use Mojo::DOM;
  5         140876  
  5         195  
23 5     5   2043 use NNexus::Discover qw(mine_candidates);
  0            
  0            
24             use NNexus::Annotate qw(serialize_concepts);
25             use NNexus::Classification qw(disambiguate);
26             use NNexus::Morphology qw(canonicalize_url);
27              
28             sub new {
29             my ($class,%opts) = @_;
30             $opts{format} = lc($opts{format}||'html');
31             $opts{result} = {};
32             $opts{url} = canonicalize_url($opts{url}) if $opts{url};
33             bless \%opts, $class; }
34              
35             sub execute {
36             my ($self) = @_;
37             my $function = $self->{function};
38             if ($function eq 'linkentry') {$self->_link_entry;}
39             elsif ($function eq 'indexentry') {$self->_index_entry;}
40             else {$self->_fail_with("Invalid action, aborting!"); } }
41              
42             sub _fail_with {
43             my ($self,$message)=@_;
44             # TODO: Spec this out, maybe similar to LaTeXML?
45             my $result = {payload=>q{},message=>$message,status=>'Failed!'};
46             $self->{result} = $result; }
47              
48             sub _ok_with {
49             my ($self,$payload,$message)=@_;
50             # TODO: Spec this out, maybe similar to LaTeXML?
51             my $result = {payload=>$payload,message=>$message,status=>'OK'};
52             $self->{result} = $result; }
53              
54             sub response { $_[0]->{result};}
55             sub result { $_[0]->{result}->{payload}; }
56             sub message { $_[0]->{result}->{message}; }
57             sub status { $_[0]->{result}->{status}; }
58              
59             sub _link_entry {
60             my ($self) = @_;
61             # Process in 2 Steps:
62             # I. Concept Discovery
63             my ($concepts_mined,$text_length) =
64             NNexus::Discover::mine_candidates(
65             db=>$self->{db},
66             body=>$self->{body},
67             url=>$self->{url},
68             domain=>$self->{domain},
69             format=>$self->{format},
70             verbosity=>$self->{verbosity});
71             # II. Disambiguation
72             my $concepts_refined = NNexus::Classification::disambiguate(
73             $concepts_mined,
74             text_length=>$text_length,
75             include_all=>$self->{include_all},
76             verbosity=>$self->{verbosity});
77             # III. Annotation
78             $self->{annotation} //= 'html';
79             $self->{embed} //= 1;
80             my $serialized_result =
81             NNexus::Annotate::serialize_concepts(
82             body=>$self->{body},
83             concepts=>$concepts_refined,
84             annotation=>$self->{annotation},
85             embed=>$self->{embed},
86             domain=>$self->{domain},
87             verbosity=>$self->{verbosity});
88             $self->{result}={payload=>$serialized_result,message=>'No obvious problems.', status=>'OK'};
89             $serialized_result; }
90              
91             sub _index_entry {
92             my ($self)=@_;
93             my $domain = $self->{domain} || 'all';
94             my $url = $self->{url}||$self->{body};
95             my $dom = $self->{dom};
96             if ($dom && (! ref $dom)) { # Text:
97             $dom = Mojo::DOM->new($dom);
98             }
99             my $should_update = $self->{should_update} // 1;
100             require NNexus::Index::Dispatcher;
101             my $dispatcher = NNexus::Index::Dispatcher->new(db=>$self->{db},domain=>$domain,
102             verbosity=>$self->{verbosity},should_update=>$should_update,
103             start=>$url,dom=>$dom);
104             my @invalidation_suggestions;
105             while (my $payload = $dispatcher->index_step) {
106             push @invalidation_suggestions, @{$payload}; }
107             my $report_url = ($url ne 'default') ? "http://$url" : 'the default domain root';
108             $self->_ok_with(\@invalidation_suggestions,"IndexConcepts succeeded in domain $domain, on $report_url"); }
109              
110             1;
111              
112             __END__