Treex/Core/Run.pm

filter: Statementstmt 45.0% 104 / 231 Branchbran 13.3% 8 / 60 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 55.8% 19 / 34 Podpod 33.3% 1 / 3 totaltotal 36.7% 132 / 359 CC 40 SCAR 41.1
CC
40
Cov
36%
_execute
182.0
_init_scenario
110.0
treex
90.0
CRAP 61.2
« Treex/Block/Util/DefinedAttr.pm ↑ Summary Treex/Block/Util/Eval.pm »
1 package Treex::Core::Run;
2 2 $Treex::Core::Run::VERSION = '2.20151210';
3 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Treex::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use MooseX::SemiAffordanceAccessor 0.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 with 'MooseX::Getopt';
10  
11  
12 # TODO some of these modules might not be needed, check this
13 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use List::MoreUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use IO::Interactive;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 ▶use File::Glob 'bsd_glob';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 our @EXPORT_OK = q(treex);
29  
30 2 has 'save' => (
31 traits => ['Getopt'],
32 cmd_aliases => 's',
33 is => 'rw',
34 isa => 'Bool',
35 default => 0,
36 documentation => 'save all documents',
37 );
38  
39 has 'quiet' => (
40 traits => ['Getopt'],
41 cmd_aliases => 'q',
42 is => 'rw',
43 isa => 'Bool',
44 default => 0,
45 0 ▶ trigger => sub { Treex::Core::Log::log_set_error_level('FATAL'); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 2 documentation => q{Warning, info and debug messages are suppressed. Only fatal errors are reported.},
47 );
48  
49 2 has 'cleanup' => (
50 traits => ['Getopt'],
51 is => 'rw', isa => 'Bool', default => 0,
52 documentation => q{Delete all temporary files.},
53 );
54  
55 has 'error_level' => (
56 traits => ['Getopt'],
57 cmd_aliases => 'e',
58 is => 'rw', isa => 'ErrorLevel', default => 'INFO',
59 0 ▶ trigger => sub { Treex::Core::Log::log_set_error_level( $_[1] ); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 2 documentation => q{Possible values: ALL, DEBUG, INFO, WARN, FATAL},
61 );
62  
63 2 has 'lang' => (
64 traits => ['Getopt'],
65 cmd_aliases => [ 'language', 'L' ],
66 is => 'rw', isa => 'Str',
67 documentation => q{shortcut for adding "Util::SetGlobal language=xy" at the beginning of the scenario},
68 );
69  
70 2 has 'selector' => (
71 traits => ['Getopt'],
72 cmd_aliases => 'S',
73 is => 'rw', isa => 'Str',
74 documentation => q{shortcut for adding "Util::SetGlobal selector=xy" at the beginning of the scenario},
75 );
76  
77 2 has 'tokenize' => (
78 traits => ['Getopt'],
79 cmd_aliases => 't',
80 is => 'rw', isa => 'Bool',
81 documentation => q{shortcut for adding "Read::Sentences W2A::Tokenize" at the beginning of the scenario (or W2A::XY::Tokenize if used with --lang=xy)},
82 );
83  
84  
85 # treex -h should not print "Unknown option: h" before the usage.
86 #has 'help' => (
87 # traits => ['Getopt'],
88 # cmd_aliases => 'h',
89 # is => 'ro', isa => 'Bool', default => 0,
90 # documentation => q{Print usage info},
91 #);
92  
93 2 has 'filenames' => (
94 traits => ['NoGetopt'],
95 is => 'rw',
96 isa => 'ArrayRef[Str]',
97 documentation => 'treex file names',
98 );
99  
100 2 has 'scenario' => (
101 traits => ['NoGetopt'],
102 is => 'rw',
103 isa => 'Treex::Core::Scenario',
104 predicate => '_has_scenario',
105 documentation => 'scenario object',
106 );
107  
108  
109 2 has 'watch' => (
110 traits => ['Getopt'],
111 is => 'ro',
112 isa => 'Str',
113 documentation => 're-run when the given file is changed TODO better doc',
114 );
115  
116 2 has 'dump_scenario' => (
117 traits => ['Getopt'],
118 cmd_aliases => 'd',
119 is => 'rw',
120 isa => 'Bool',
121 default => 0,
122 documentation => 'Just dump (print to STDOUT) the given scenario and exit.',
123 );
124  
125 2 has 'dump_required_files' => (
126 traits => ['Getopt'],
127 is => 'rw',
128 isa => 'Bool',
129 default => 0,
130 documentation => 'Just dump (print to STDOUT) files required by the given scenario and exit.',
131 );
132  
133 2 has 'cache' => (
134 traits => ['Getopt'],
135 is => 'rw',
136 isa => 'Str',
137 default => "",
138 documentation => 'Use cache. Required memory is specified in format memcached,loading. Numbers are in GB.',
139 );
140  
141 has version => (
142 traits => ['Getopt'],
143 is => 'ro',
144 isa => 'Bool',
145 default => 0,
146 cmd_aliases => 'v',
147 documentation => q(Print treex and perl version),
148 trigger => sub {
149 0 ▶ print get_version();
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 exit();
151 },
152 2 );
153  
154 #
155 # Parallel head execution options
156 # TODO move them to Treex::Core::Parallel::Head
157  
158 2 has 'forward_error_level' => (
159 traits => ['Getopt'],
160 cmd_aliases => 'E',
161 is => 'rw', isa => 'ErrorLevel', default => 'WARN',
162 documentation => q{messages with this level or higher will be forwarded from the distributed jobs to the main STDERR},
163 );
164  
165  
166 2 has 'parallel' => (
167 traits => ['Getopt'],
168 cmd_aliases => 'p',
169 is => 'rw',
170 isa => 'Bool',
171 default => 0,
172 documentation => 'Parallelize the task on SGE cluster (using qsub).',
173 );
174  
175 2 has 'jobs' => (
176 traits => ['Getopt'],
177 cmd_aliases => 'j',
178 is => 'ro',
179 isa => 'Int',
180 default => 10,
181 documentation => 'Number of jobs for parallelization, default 10. Requires -p.',
182 );
183  
184  
185 2 has 'local' => (
186 traits => ['Getopt'],
187 is => 'ro',
188 isa => 'Bool',
189 documentation => 'Run jobs locally (might help with multi-core machines). Requires -p.',
190 );
191  
192 2 has 'priority' => (
193 traits => ['Getopt'],
194 is => 'ro',
195 isa => 'Int',
196 default => -100,
197 documentation => 'Priority for qsub, an integer in the range -1023 to 0 (or 1024 for admins), default=-100. Requires -p.',
198 );
199  
200 2 has 'mem' => (
201 traits => ['Getopt'],
202 cmd_aliases => [ 'm', 'memory' ],
203 is => 'ro',
204 isa => 'Str',
205 default => '2G',
206 documentation => 'How much memory should be allocated for cluster jobs, default=2G. Requires -p. '
207 . 'Translates to "qsub -hard -l mem_free=$mem -l h_vmem=2*$mem -l act_mem_free=$mem". '
208 . 'Use --mem=0 and --qsub to set your own SGE settings (e.g. if act_mem_free is not available).',
209 );
210  
211 2 has 'name' => (
212 traits => ['Getopt'],
213 is => 'ro',
214 isa => 'Str',
215 default => '',
216 documentation => 'Prefix of submitted jobs. Requires -p. '
217 . 'Translates to "qsub -N $name-jobname".',
218 );
219  
220 2 has 'qsub' => (
221 traits => ['Getopt'],
222 is => 'ro',
223 isa => 'Str',
224 default => '',
225 documentation => 'Additional parameters passed to qsub. Requires -p. '
226 . 'See --priority and --mem. You can use e.g. --qsub="-q *@p*,*@s*" to use just machines p* and s*. '
227 . 'Or e.g. --qsub="-q *@!(twi*|pan*)" to skip twi* and pan* machines.',
228 );
229  
230 2 has 'workdir' => (
231 is => 'rw',
232 traits => ['Getopt'],
233 isa => 'Str',
234 default => './{NNN}-cluster-run-{XXXXX}',
235 documentation => 'working directory for temporary files in parallelized processing; ' .
236 'one can create automatic directories by using patterns: ' .
237 '{NNN} is replaced by an ordinal number with so many leading zeros to have length of the number of Ns, ' .
238 '{XXXX} is replaced by a random string, whose length is the same as the number of Xs (min. 4). ' .
239 'If not specified, directories such as 001-cluster-run, 002-cluster-run etc. are created',
240 );
241  
242 has 'sge_job_numbers' => (
243 is => 'rw',
244 traits => ['NoGetopt'],
245 documentation => 'list of numbers of jobs executed on sge',
246 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
247 2 );
248  
249 2 has 'survive' => (
250 traits => ['Getopt'],
251 is => 'rw',
252 isa => 'Bool',
253 default => 0,
254 documentation => 'Continue collecting jobs\' outputs even if some of them crashed (risky, use with care!).',
255 );
256  
257  
258 #
259 # Parallel node/worker execution options
260 # TODO move them to Treex::Core::Parallel::Node
261  
262 2 has 'jobindex' => (
263 traits => ['Getopt'],
264 is => 'ro',
265 isa => 'Int',
266 documentation => 'Not to be used manually. If number of jobs is set to J and modulo set to M, only I-th files fulfilling I mod J == M are processed.',
267 );
268  
269 2 has 'outdir' => (
270 traits => ['Getopt'],
271 is => 'ro',
272 isa => 'Str',
273 documentation => 'Not to be used manually. Dictory for collecting standard and error outputs in parallelized processing.',
274 );
275  
276 2 has 'server' => (
277 traits => ['Getopt'],
278 is => 'ro',
279 isa => 'Str',
280 default => '',
281 documentation => 'Not to be used manually. Used to point parallel jobs to the head.',
282 );
283  
284 #
285 #
286 #
287  
288 sub _usage_format {
289 0 ▶ return "usage: %c %o scenario [-- treex_files]\nscenario is a sequence of blocks or *.scen files\noptions:";
Subroutine _usage_format: not called CC=1 SCAR=6.9
290 }
291  
292 #gets info about version of treex and perl
293 sub get_version {
294 0 ▶ my $perl_v = $^V;
Subroutine get_version: not called CC=4 SCAR=30.0
Pod undocumented
295 0 my $perl_x = $^X;
296 0 ▶ my $treex_v = $Treex::Core::Run::VERSION || 'DEV';
Condition0%0/2
$Treex::Core::Run::VERSION || 'DEV'
l!l
00
MC/DC0%0/1
$Treex::Core::Run::VERSION || 'DEV'
$Treex::Core::Run::VERSION
Truth table0%0/2
A
A= $Treex::Core::Run::VERSION
Aresult
11
00
297 0 my $treex_x = which('treex');
298  
299 # File::Which::which sometimes fails to found treex.
300 0 ▶ if ( !defined $treex_x ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $treex_x) 0 0 0
301 0 chomp( $treex_x = `which treex 2> /dev/null` );
302 0 ▶ $treex_x ||= '<treex not found in $PATH>';
Condition0%0/2
$treex_x ||= "<treex not found in \$PATH>"
l!l
00
MC/DC0%0/1
$treex_x ||= "<treex not found in \$PATH>"
$treex_x
Truth table0%0/2
A
A= $treex_x
Aresult
11
00
303 }
304 0 my $version_string = <<"VERSIONS";
305 Treex version: $treex_v from $treex_x
306 Perl version: $perl_v from $perl_x
307 VERSIONS
308 0 return $version_string;
309 }
310  
311 sub BUILD {
312  
313 # more complicated tests on consistency of options will be place here
314 0 ▶ my ($self) = @_;
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
315  
316 0 return;
317 }
318  
319  
320 sub _execute {
321 0 ▶ my ($self) = @_;
Subroutine _execute: not called CC=13 SCAR=52.0
322  
323 0 ▶ if ( $self->dump_scenario || $self->dump_required_files ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dump_scenario or $self->dump_required_files) 0 0 0
Condition0%0/3
$self->dump_scenario or $self->dump_required_files
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->dump_scenario or $self->dump_required_files
$self->dump_scenario $self->dump_required_files
Truth table0%0/3
A or B
A= $self->dump_scenarioB= $self->dump_required_files
ABresult
1X1
011
000
324  
325 # If someone wants to run treex -d My::Block my_scen.scen
326 0 my $scen_str = $self->_construct_scenario_string_with_quoted_whitespace();
327 0 $self->set_scenario( Treex::Core::Scenario->new( scenario_string => $scen_str, runner => $self ) );
328  
329 # TODO: Do it properly - perhaps, add a Scenario option to not load all the blocks.
330 # We cannot create the real scenario instance without loading all the blocks
331 # However, since r6307 some Scenario's functions were changed to methods, so we must create a dummy instance.
332  
333 #my @block_items = Treex::Core::Scenario::parse_scenario_string($scen_str);
334 #my @block_items = $dummy_scenario->parse_scenario_string($scen_str);
335  
336 0 ▶ if ( $self->dump_scenario ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dump_scenario) 0 0 0
337 0 print "# Full Scenario generated by 'treex --dump_scenario' on " . localtime() . "\n";
338 0 print $self->scenario->construct_scenario_string( multiline => 1 ), "\n";
339 }
340  
341 0 ▶ if ( $self->dump_required_files ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dump_required_files) 0 0 0
342 0 print "# Required files generated by 'treex --dump_required_files' on " . localtime() . "\n";
343 0 print join "\n", $self->scenario->get_required_files(), "\n";
344 }
345 0 exit;
346 }
347  
348 0 ▶ if ( $self->dump_required_files ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dump_required_files) 0 0 0
349  
350 0 my $scen_str = join ' ', @{ $self->extra_argv };
351 0 $self->set_scenario( Treex::Core::Scenario->new( scenario_string => $scen_str, runner => $self ) );
352  
353 0 print "# Required files generated by 'treex --dump_required_files' on " . localtime() . "\n";
354 0 print join "\n", $self->scenario->get_required_files(), "\n";
355 0 exit;
356 }
357 0 my $done = 0;
358 0 my $time;
359 0 my $watch = $self->watch;
360  
361 0 ▶ if ( defined $watch ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $watch) 0 0 0
362 0 ▶ log_fatal "Watch file '$watch' does not exists" if !-f $watch;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $watch 0 0 0
363 0 $time = ( stat $watch )[9];
364 }
365  
366 0 ▶ while ( !$done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 0 0
367
368 0 $self->_execute_scenario();
369  
370 0 $done = 1;
371 0 my $info_written = 0;
372 WATCH_CHANGE:
373 0 ▶ while ( defined $watch && -f $watch ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $watch and -f $watch) 0 0 0
Condition0%0/3
defined $watch and -f $watch
!ll&&!rl&&r
000
MC/DC0%0/2
defined $watch and -f $watch
defined $watch -f $watch
Truth table0%0/3
A and B
A= defined $watchB= -f $watch
ABresult
0X0
100
111
374 0 my $new_time = ( stat $watch )[9];
375 0 ▶ if ( $new_time > $time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_time > $time) 0 0 0
376 0 $time = $new_time;
377 0 $done = 0;
378 0 last WATCH_CHANGE;
379 }
380 0 ▶ if ( !$info_written ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info_written) 0 0 0
381 0 log_info "Watching '$watch' file. Touch it to re-run, delete to quit.";
382 0 $info_written = 1;
383 }
384 0 sleep 1;
385 }
386 }
387 0 return;
388 }
389  
390 2 my %READER_FOR = (
391 'treex' => 'Treex',
392 'treex.gz' => 'Treex',
393 'txt' => 'Text',
394 'txt.gz' => 'Text',
395 'streex' => 'Treex',
396 'mrg' => 'PennMrg',
397 'mrg.gz' => 'PennMrg',
398 'tag' => 'CdtTag',
399  
400 # TODO:
401 # conll => 'Conll',
402 # plsgz => 'Plsgz',
403 # tmt
404 );
405  
406 sub _get_reader_name_for {
407 13 ▶ my $self = shift;
Subroutine _get_reader_name_for: called CC=6 SCAR=17.9
408 13 my @names = @_;
409 13 my $base_re = join( '|', keys %READER_FOR );
410 13 my $re = qr{\.($base_re)$};
411 13 my @extensions;
412 my $first;
413  
414 13 foreach my $name (@names) {
415 21 ▶ if ( $name =~ /$re/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /$re/) { } 17 4 21
416 17 my $current = $1;
417 17 $current =~ s/\.gz$//;
418 17 ▶ if ( !defined $first ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $first) 9 8 17
419 9 $first = $current;
420 }
421 17 ▶ if ( $current ne $first ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current ne $first) 1 16 17
422 1 log_fatal 'All files (' . join( ',', @names ) . ') must have the same extension' . "\n" .
423 " current = $current\n" .
424 " first = $first\n" .
425 " curname = $name";
426 }
427 16 push @extensions, $current;
428 }
429 else {
430 4 log_fatal 'Files (' . join( ',', @names ) . ') must have extensions';
431 }
432 }
433 8 my $r = $READER_FOR{$first};
434 8 ▶ log_fatal "There is no DocumentReader implemented for extension '$first'" if !$r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 1 7 8
435 7 return "Read::$r";
436 }
437  
438 # This is where the main work is done. It is overridden in parallel execution.
439 sub _execute_scenario {
440 0 ▶ my ($self) = @_;
Subroutine _execute_scenario: not called CC=1 SCAR=6.9
441
442 0 log_info "Local (single-process) execution.";
443  
444 0 $self->_init_scenario();
445  
446 0 my $scenario = $self->scenario;
447  
448 0 my $runnin_started = time;
449 0 $scenario->run();
450  
451 0 log_info "Running the scenario took " . ( time - $runnin_started ) . " seconds";
452  
453 0 return;
454 }
455  
456 # Parameters can contain whitespaces that should be preserved
457 sub _construct_scenario_string_with_quoted_whitespace {
458 0 ▶ my ($self) = @_;
Subroutine _construct_scenario_string_with_quoted_whitespace: not called CC=3 SCAR=24.8
459 0 my @arguments;
460 0 foreach my $arg ( @{ $self->extra_argv } ) {
461 0 ▶ if ( $arg =~ /(\S+)=(.*\s.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /(\S+)=(.*\s.*)$/) { } 0 0 0
462 0 my ( $name, $value ) = ( $1, $2 );
463 0 $value =~ s/'/\\'/g;
464 0 push @arguments, qq($name='$value');
465 }
466 else {
467 0 push @arguments, $arg;
468 }
469 }
470 0 return join ' ', @arguments;
471 }
472  
473 sub _init_scenario {
474 0 ▶ my ($self) = @_;
Subroutine _init_scenario: not called CC=10 SCAR=47.0
475  
476 0 my $scen_str = $self->_construct_scenario_string_with_quoted_whitespace();
477  
478 # some command line options are just shortcuts for blocks; the blocks are added to the scenario now
479 0 ▶ if ( $self->filenames ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filenames) 0 0 0
480 0 my $reader = $self->_get_reader_name_for( @{ $self->filenames } );
481 0 log_info "Block $reader added to the beginning of the scenario.";
482 0 $scen_str = "$reader from=" . join( ',', @{ $self->filenames } ) . " $scen_str";
483 }
484  
485 0 ▶ if ( $self->save ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->save) 0 0 0
486 0 log_info "Block Write::Treex added to the end of the scenario.";
487 0 $scen_str .= ' Write::Treex';
488 }
489  
490 0 ▶ if ( $self->tokenize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tokenize) 0 0 0
491 0 my $tokenizer = 'W2A::Tokenize';
492 0 my $lang = $self->lang;
493 0 ▶ if ($lang && $lang ne 'all'){
Branch: 1 branch
BranchTrueFalseTotal
if ($lang and $lang ne "all") 0 0 0
Condition0%0/3
$lang and $lang ne "all"
!ll&&!rl&&r
000
MC/DC0%0/2
$lang and $lang ne "all"
$lang $lang ne "all"
Truth table0%0/3
A and B
A= $langB= $lang ne "all"
ABresult
0X0
100
111
494 0 my $module = 'Treex::Block::W2A::' . uc($lang) . '::Tokenize';
495 0 ▶ if (eval "use $module;1"){
Branch: 1 branch
BranchTrueFalseTotal
if (eval "use $module;1") 0 0 0
496 0 $tokenizer = 'W2A::' . uc($lang) . '::Tokenize';
497 }
498 }
499 0 $scen_str = "Read::Sentences $tokenizer $scen_str";
500 }
501  
502 0 ▶ if ( $self->lang ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lang) 0 0 0
503 0 $scen_str = 'Util::SetGlobal language=' . $self->lang . " $scen_str";
504 }
505  
506 0 ▶ if ( $self->selector ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->selector) 0 0 0
507 0 $scen_str = 'Util::SetGlobal selector=' . $self->selector . " $scen_str";
508 }
509  
510 0 my $loading_started = time;
511 0 ▶ if ( $self->_has_scenario ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_scenario) { } 0 0 0
512 0 $self->scenario->restart();
513 }
514 else {
515 0 $self->set_scenario( Treex::Core::Scenario->new( from_string => $scen_str, runner => $self ) );
516 0 $self->scenario->load_blocks;
517 }
518  
519 0 my $loading_ended = time;
520 0 log_info "Loading the scenario took " . ( $loading_ended - $loading_started ) . " seconds";
521  
522 0 return;
523 }
524  
525  
526 # A factory subroutine, creating the right Treex object for the job.
527 # (local single-process: Treex::Core::Run, parallel processing head: Treex::Parallel::Head,
528 # parallel processing worker node: Treex::Parallel::Node)
529 sub treex {
530  
531 # ref to array of arguments, or a string containing all arguments as on the command line
532 0 ▶ my $arguments = shift;
Subroutine treex: not called CC=9 SCAR=45.0
Pod documented
533  
534 0 ▶ if ( ref($arguments) eq 'ARRAY' && scalar @$arguments > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arguments eq 'ARRAY' and scalar @$arguments > 0) { } 0 0 0
elsif (defined $arguments and ref $arguments ne 'ARRAY') { } 0 0 0
Condition0%0/3
ref $arguments eq "ARRAY" and scalar @$arguments > 0
!ll&&!rl&&r
000
MC/DC0%0/2
ref $arguments eq "ARRAY" and scalar @$arguments > 0
ref $arguments eq "ARRAY" scalar @$arguments > 0
Truth table0%0/3
A and B
A= ref $arguments eq "ARRAY"B= scalar @$arguments > 0
ABresult
0X0
100
111
535 0 ▶ my $idx = first_index { $_ eq '--' } @$arguments;
Subroutine __ANON__: not called CC=1 SCAR=6.9
536 0 my %args = ( argv => $arguments );
537 0 ▶ if ( $idx != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx != -1) 0 0 0
538 0 $args{filenames} = [ splice @$arguments, $idx + 1 ];
539 0 pop @$arguments; # delete "--"
540 }
541 0 my $runner;
542
543 0 ▶ if (any { $_ =~ /^--jobindex/ } @$arguments){
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
if (&any(sub { $_ =~ /^--jobindex/; } , @$arguments)) { } 0 0 0
elsif (&any(sub { $_ =~ /^(--parallel|-p|-pj\d+)$/; } , @$arguments)) { } 0 0 0
544 0 require Treex::Core::Parallel::Node;
545 0 $runner = Treex::Core::Parallel::Node->new_with_options( \%args );
546 }
547 0 ▶ elsif (any { $_ =~ /^(--parallel|-p|-pj\d+)$/ } @$arguments){
Subroutine __ANON__: not called CC=1 SCAR=6.9
548 0 require Treex::Core::Parallel::Head;
549 0 $runner = Treex::Core::Parallel::Head->new_with_options( \%args );
550 }
551 else {
552 0 $runner = Treex::Core::Run->new_with_options( \%args );
553 }
554 0 $runner->_execute();
555  
556 }
557  
558 ▶ elsif ( defined $arguments && ref($arguments) ne 'ARRAY' ) {
Condition0%0/3
defined $arguments and ref $arguments ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $arguments and ref $arguments ne "ARRAY"
defined $arguments ref $arguments ne "ARRAY"
Truth table0%0/3
A and B
A= defined $argumentsB= ref $arguments ne "ARRAY"
ABresult
0X0
100
111
559 0 ▶ treex( [ grep { defined $_ && $_ ne '' } split( /\s/, $arguments ) ] );
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_ ne "" 0 0 0
560 }
561  
562 else {
563 0 treex('--help');
564  
565 #log_fatal 'Unspecified arguments for running treex.';
566 }
567 0 return;
568 }
569  
570 2 1;
571  
572 __END__
573  
574 =head2 --watch option
575  
576 SYNOPSIS:
577 touch timestamp.file
578 treex --watch=timestamp.file my.scen & # or without & and open another terminal
579 # after all documents are processed, treex is still running, watching timestamp.file
580 # you can modify any modules/blocks and then
581 touch timestamp.file
582 # All modified modules will be reloaded (the number of reloaded modules is printed).
583 # The document reader is restarted, so it starts reading the first file again.
584 # To exit this "watching loop" either rm timestamp.file or press Ctrl^C.
585  
586 BENEFITS:
587 * much faster development cycles (e.g. most time of en-cs translation is spent on loading)
588 * Now I have some non-deterministic problems with loading NER::Stanford
589 - using --watch I get it loaded on all jobs once and then I don't have to reload it.
590  
591 TODO:
592 * modules are just reloaded, no constructors are called yet
593  
594  
595 =for Pod::Coverage BUILD get_version
596  
597 =encoding utf-8
598  
599 =head1 NAME
600  
601 Treex::Core::Run + treex - applying Treex blocks and/or scenarios on data
602  
603 =head1 VERSION
604  
605 version 2.20151210
606  
607 =head1 SYNOPSIS
608  
609 In bash:
610  
611 > treex myscenario.scen -- data/*.treex
612 > treex My::Block1 My::Block2 -- data/*.treex
613  
614 In Perl:
615  
616 use Treex::Core::Run q(treex);
617 treex([qw(myscenario.scen -- data/*.treex)]);
618 treex([qw(My::Block1 My::Block2 -- data/*.treex)]);
619  
620 =head1 DESCRIPTION
621  
622 C<Treex::Core::Run> allows to apply a block, a scenario, or their mixture on a
623 set of data files. It is designed to be used primarily from bash command line,
624 using a thin front-end script called C<treex>. However, the same list of
625 arguments can be passed by an array reference to the function C<treex()>
626 imported from C<Treex::Core::Run>.
627  
628 Note that this module supports distributed processing (Linux-only!), simply by
629 adding the switch C<-p>. The C<treex> method then creates a
630 C<Treex::Core::Parallel::Head> object, which extends C<Treex::Core::Run>
631 by providing parallel processing functionality.
632  
633 Then there are two ways to process the data in a parallel fashion. By
634 default, SGE cluster\'s C<qsub> is expected to be available. If you have no
635 cluster but want to make the computation parallelized at least on a multicore
636 machine, add the C<--local> switch.
637  
638 =head1 SUBROUTINES
639  
640 =over 4
641  
642 =item treex
643  
644 create new runner and runs scenario given in parameters
645  
646 =back
647  
648 =head1 USAGE
649  
650 usage: treex [-?dEehjLmpqSstv] [long options...] scenario [-- treex_files]
651 scenario is a sequence of blocks or *.scen files
652 options:
653 -h -? --usage --help Prints this usage information.
654 -s --save save all documents
655 -q --quiet Warning, info and debug messages are
656 suppressed. Only fatal errors are
657 reported.
658 --cleanup Delete all temporary files.
659 -e --error_level Possible values: ALL, DEBUG, INFO, WARN,
660 FATAL
661 -L --language --lang shortcut for adding "Util::SetGlobal
662 language=xy" at the beginning of the
663 scenario
664 -S --selector shortcut for adding "Util::SetGlobal
665 selector=xy" at the beginning of the
666 scenario
667 -t --tokenize shortcut for adding "Read::Sentences
668 W2A::Tokenize" at the beginning of the
669 scenario (or W2A::XY::Tokenize if used
670 with --lang=xy)
671 --watch re-run when the given file is changed
672 TODO better doc
673 -d --dump_scenario Just dump (print to STDOUT) the given
674 scenario and exit.
675 --dump_required_files Just dump (print to STDOUT) files
676 required by the given scenario and exit.
677 --cache Use cache. Required memory is specified
678 in format memcached,loading. Numbers are
679 in GB.
680 -v --version Print treex and perl version
681 -E --forward_error_level messages with this level or higher will
682 be forwarded from the distributed jobs
683 to the main STDERR
684 -p --parallel Parallelize the task on SGE cluster
685 (using qsub).
686 -j --jobs Number of jobs for parallelization,
687 default 10. Requires -p.
688 --local Run jobs locally (might help with
689 multi-core machines). Requires -p.
690 --priority Priority for qsub, an integer in the
691 range -1023 to 0 (or 1024 for admins),
692 default=-100. Requires -p.
693 --memory -m --mem How much memory should be allocated for
694 cluster jobs, default=2G. Requires -p.
695 Translates to "qsub -hard -l
696 mem_free=$mem -l h_vmem=2*$mem -l
697 act_mem_free=$mem". Use --mem=0 and
698 --qsub to set your own SGE settings
699 (e.g. if act_mem_free is not available).
700 --name Prefix of submitted jobs. Requires -p.
701 Translates to "qsub -N $name-jobname".
702 --qsub Additional parameters passed to qsub.
703 Requires -p. See --priority and --mem.
704 You can use e.g. --qsub="-q *@p*,*@s*"
705 to use just machines p* and s*. Or e.g.
706 --qsub="-q *@!(twi*|pan*)" to skip twi*
707 and pan* machines.
708 --workdir working directory for temporary files in
709 parallelized processing; one can create
710 automatic directories by using patterns:
711 {NNN} is replaced by an ordinal number
712 with so many leading zeros to have
713 length of the number of Ns, {XXXX} is
714 replaced by a random string, whose
715 length is the same as the number of Xs
716 (min. 4). If not specified, directories
717 such as 001-cluster-run, 002-cluster-run
718 etc. are created
719 --survive Continue collecting jobs' outputs even
720 if some of them crashed (risky, use with
721 care!).
722 --jobindex Not to be used manually. If number of
723 jobs is set to J and modulo set to M,
724 only I-th files fulfilling I mod J == M
725 are processed.
726 --outdir Not to be used manually. Dictory for
727 collecting standard and error outputs in
728 parallelized processing.
729 --server Not to be used manually. Used to point
730 parallel jobs to the head.
731  
732 =head1 AUTHORS
733  
734 Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz>
735  
736 Martin Popel <popel@ufal.mff.cuni.cz>
737  
738 Martin Majliš
739  
740 Ondřej Dušek <odusek@ufal.mff.cuni.cz>
741  
742 =head1 COPYRIGHT AND LICENSE
743  
744 Copyright © 2011-2014 by Institute of Formal and Applied Linguistics, Charles University in Prague
745  
746 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Treex/Block/Util/DefinedAttr.pm ↑ Summary Treex/Block/Util/Eval.pm »