File Coverage

blib/lib/NNexus/Job.pm
Criterion Covered Total %
statement 62 68 91.1
branch 8 10 80.0
condition 10 16 62.5
subroutine 15 18 83.3
pod 6 6 100.0
total 101 118 85.5


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   796424 use strict;
  5         11  
  5         251  
19 5     5   29 use warnings;
  5         10  
  5         210  
20 5     5   29 use feature qw(say);
  5         15  
  5         488  
21              
22 5     5   2210 use Mojo::DOM;
  5         162770  
  5         160  
23 5     5   2404 use NNexus::Discover qw(mine_candidates);
  5         14  
  5         555  
24 5     5   2465 use NNexus::Annotate qw(serialize_concepts);
  5         9  
  5         262  
25 5     5   2638 use NNexus::Classification qw(disambiguate);
  5         61  
  5         816  
26 5     5   64 use NNexus::Morphology qw(canonicalize_url);
  5         8  
  5         3817  
27              
28             sub new {
29 14     14 1 450071 my ($class,%opts) = @_;
30 14   100     111 $opts{format} = lc($opts{format}||'html');
31 14         46 $opts{result} = {};
32 14 100       72 $opts{url} = canonicalize_url($opts{url}) if $opts{url};
33 14         72 bless \%opts, $class; }
34              
35             sub execute {
36 13     13 1 66 my ($self) = @_;
37 13         55 my $function = $self->{function};
38 13 100       55 if ($function eq 'linkentry') {$self->_link_entry;}
  8 50       29  
  5         22  
39 0         0 elsif ($function eq 'indexentry') {$self->_index_entry;}
40             else {$self->_fail_with("Invalid action, aborting!"); } }
41              
42             sub _fail_with {
43 0     0   0 my ($self,$message)=@_;
44             # TODO: Spec this out, maybe similar to LaTeXML?
45 0         0 my $result = {payload=>q{},message=>$message,status=>'Failed!'};
46 0         0 $self->{result} = $result; }
47              
48             sub _ok_with {
49 5     5   15 my ($self,$payload,$message)=@_;
50             # TODO: Spec this out, maybe similar to LaTeXML?
51 5         28 my $result = {payload=>$payload,message=>$message,status=>'OK'};
52 5         134 $self->{result} = $result; }
53              
54 10     10 1 866 sub response { $_[0]->{result};}
55 3     3 1 90 sub result { $_[0]->{result}->{payload}; }
56 0     0 1 0 sub message { $_[0]->{result}->{message}; }
57 0     0 1 0 sub status { $_[0]->{result}->{status}; }
58              
59             sub _link_entry {
60 8     8   11 my ($self) = @_;
61             # Process in 2 Steps:
62             # I. Concept Discovery
63 8         67 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 8         76 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 8   100     40 $self->{annotation} //= 'html';
79 8   50     21 $self->{embed} //= 1;
80 8         68 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 8         48 $self->{result}={payload=>$serialized_result,message=>'No obvious problems.', status=>'OK'};
89 8         44 $serialized_result; }
90              
91             sub _index_entry {
92 5     5   10 my ($self)=@_;
93 5   50     18 my $domain = $self->{domain} || 'all';
94 5   33     18 my $url = $self->{url}||$self->{body};
95 5         13 my $dom = $self->{dom};
96 5 100 66     38 if ($dom && (! ref $dom)) { # Text:
97 2         20 $dom = Mojo::DOM->new($dom);
98             }
99 5   50     549 my $should_update = $self->{should_update} // 1;
100 5         1704 require NNexus::Index::Dispatcher;
101 5         67 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 5         13 my @invalidation_suggestions;
105 5         22 while (my $payload = $dispatcher->index_step) {
106 5         7 push @invalidation_suggestions, @{$payload}; }
  5         22  
107 5 50       24 my $report_url = ($url ne 'default') ? "http://$url" : 'the default domain root';
108 5         39 $self->_ok_with(\@invalidation_suggestions,"IndexConcepts succeeded in domain $domain, on $report_url"); }
109              
110             1;
111              
112             __END__