blib/lib/PMLTQ/Command/init.pm

filter: Statementstmt 27.8% 29 / 104 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 50.0% 8 / 16 Podpod 0.0% 0 / 3 totaltotal 23.5% 37 / 157 CC 23 SCAR 38.2
CC
23
Cov
21%
run
90.0
try_resolve_references
90.0
__ANON__
12.0
CRAP 45.8
« blib/lib/PMLTQ/Relation/DescendantIteratorWithBoundedDepth.pm ↑ Summary blib/lib/PMLTQ/Relation/TreexFileIterator.pm »
1 package PMLTQ::Command::init;
2 1 our $AUTHORITY = 'cpan:MATY';
3 1 $PMLTQ::Command::init::VERSION = '1.5.0';
4 # ABSTRACT: Initialize empty database
5  
6 1 ▶use PMLTQ::Base 'PMLTQ::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use PMLTQ::PML2BASE;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use PMLTQ::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Treex::PML::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::Util qw/none first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶has usage => sub { shift->extract_usage };
Subroutine __ANON__: not called CC=1 SCAR=1.2
16  
17 1 my %known_relations = (
18 'a-align-links/counterpart.rf' => 'a-node',
19 'a-node/p_terminal.rf' => 'p-terminal',
20 'a-root/ptree.rf' => 'p-nonterminal',
21 'apreds/LM/target.rf' => 'node',
22 'local_event/author' => 'user',
23 'n-node/a.rf' => 'a-node',
24 'nonterminal/coref.rf' => 'nonterminal',
25 'nonterminal/coindex.rf' => 'nonterminal',
26 'nonterminal/gapping.rf' => 'nonterminal',
27 'problem/author' => 'user',
28 'secedge/idref' => 'nonterminal',
29 'st-node/tnode.rfs' => 't-node',
30 't-a/aux.rf' => 'a-node',
31 't-a/lex.rf' => 'a-node',
32 't-align-links/counterpart.rf' => 't-node',
33 't-bridging-link/target_node.rf' => 't-node',
34 't-coref_text-link/target_node.rf' => 't-node',
35 't-discourse-link/a-connectors.rf' => 'a-node',
36 't-discourse-link/all_a-connectors.rf' => 'a-node',
37 't-discourse-link/t-connectors.rf' => 't-node',
38 't-discourse-link/target_node.rf' => 't-node',
39 't-node/compl.rf' => 't-node',
40 't-node/coref_gram.rf' => 't-node',
41 't-node/coref_text.rf' => 't-node',
42 't-node/original_parent.rf' => 't-node',
43 't-node/src_tnode.rf' => 't-node',
44 't-node/val_frame.rf' => 'v-frame',
45 't-root/atree.rf' => 'a-root',
46 't-root/src_tnode.rf' => 't-node',
47 'terminal/coindex.rf' => 'nonterminal',
48 'valency_lexicon/owner' => 'user',
49 );
50  
51 sub run {
52 0 ▶ my $self = shift;
Subroutine run: not called CC=9 SCAR=45.0
Pod undocumented
53  
54 0 my $config = $self->config;
55  
56 0 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
57 0 say 'Specify at least one schema file';
58 0 return 0;
59 }
60  
61 0 my ( @layers, @schemas );
62  
63 0 for my $schema_file (@_) {
64 0 ▶ unless ( -f $schema_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $schema_file) 0 0 0
65 0 my $in_resources = File::Spec->catfile( $config->{resources}, $schema_file );
66 0 ▶ unless ( -f $in_resources ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $in_resources) 0 0 0
67 0 say "Schema file '$schema_file' not found";
68 0 return 0;
69 }
70 0 $schema_file = $in_resources;
71 }
72  
73 0 push @schemas, my $schema = Treex::PML::Factory->createPMLSchema( { filename => $schema_file } );
74 0 PMLTQ::PML2BASE::init();
75 0 my $refs = PMLTQ::PML2BASE::complete_schema_pmlref_list($schema);
76 0 PMLTQ::PML2BASE::destroy();
77  
78 0 push @layers, {
79 name => $schema->get_root_name,
80 data => '<change this to point to files relative to the data_dir>',
81 COMMENT_path => '<change this to point to files relative to the data directory on server>',
82 references => {%$refs}
83 };
84 }
85  
86 my $resources_dir =
87 @schemas > 1
88 0 ▶ ? $self->common_prefix( map { dirname( File::Spec->canonpath( $_->get_url ) ) } @schemas )
Branch: 1 branch
BranchTrueFalseTotal
@schemas > 1 ? : 0 0 0
89 : dirname( $schemas[0]->get_url );
90  
91 0 $_->set_url( File::Spec->abs2rel( $_->get_url, $resources_dir ) ) for (@schemas);
92  
93 # resolve refs
94 0 $self->try_resolve_references( \@layers, \@schemas );
95  
96 0 my $treebank_title = $self->prompt_str( 'Full treebank title', { default => 'Prague Dependency Treebank 3.0' } );
97  
98 0 my $treebank_id = join '', grep {/[a-z0-9]/} map { substr $_, 0, 1 } split /[^a-z0-9]+/, lc $treebank_title;
99 $treebank_id = $self->prompt_str(
100 'Treebank ID (can only contain lowercase letters, numbers and underscores)', {
101 default => $treebank_id,
102 0 ▶ check => sub { defined $_[0] and length $_[0] and $_[0] =~ m/^[a-z0-9_]+$/ }
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and length $_[0] and $_[0] =~ /^[a-z0-9_]+$/ 0 0 0
Condition0%0/3
defined $_[0] and length $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and length $_[0]
defined $_[0] length $_[0]
Truth table0%0/3
A and B
A= defined $_[0]B= length $_[0]
ABresult
0X0
100
111
103 }
104 0 );
105  
106 0 my $yaml = YAML::Tiny->new(
107 {
108 treebank_id => $treebank_id,
109 COMMENT_data_dir => '',
110 resources => $resources_dir,
111 COMMENT_output_dir => '',
112 db =>
113 {
114 name => $treebank_id,
115 COMMENT_host => 'localhost',
116 COMMENT_port => 5432,
117 COMMENT_user => '',
118 COMMENT_password => ''
119 },
120 COMMENT_sys_db => 'postgres',
121 layers => \@layers,
122 title => $treebank_title,
123 COMMENT_homepage => '',
124 COMMENT_description => '',
125 COMMENT_isFree => 'false',
126 COMMENT_isAllLogged => 'false',
127 COMMENT_isPublic => 'false',
128 COMMENT_isFeatured => 'false',
129 COMMENT_web_api =>
130 {
131 COMMENT_dbserver => '',
132 COMMENT_url => '',
133 COMMENT_user => '',
134 COMMENT_password => ''
135 },
136 COMMENT_manuals =>
137 [
138 {
139 COMMENT_title => '',
140 COMMENT_url => ''
141 }
142 ],
143 COMMENT_tags =>
144 [
145 'COMMENT_mytag'
146 ],
147 COMMENT_language => 'lang code',
148 COMMENT_test_query =>
149 {
150 COMMENT_result_dir => 'webverify_query_results',
151 COMMENT_queries =>
152 [
153 {
154 COMMENT_filename => '01.svg',
155 COMMENT_query => 'a-node [];'
156 }
157 ]
158 }
159 }
160 );
161  
162 0 my $result = $yaml->write_string;
163  
164 0 $result =~ s/\n( *[-]? *)COMMENT_/\n#$1/g;
165 0 $result =~ s/\n#(.*?)\n( *-)/\n#$1\n#$2/g;
166
167 0 say $result;
168  
169 0 my $save = $self->prompt_yn( 'Save?', { default => 0 } );
170  
171 0 ▶ if ($save) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save) 0 0 0
172 0 my $filename = $self->prompt_str(
173 'Save as', {
174 default => 'pmltq.yml'
175 }
176 );
177 #$yaml->write($filename);
178 0 ▶ open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or die "Could not open file '${filename}' $!" 0 0 0
179 0 print $fh $result;
180 0 close $fh;
181 }
182 }
183  
184 sub common_prefix {
185 0 ▶ my $self = shift;
Subroutine common_prefix: not called CC=3 SCAR=24.8
Pod undocumented
186 0 my @paths = @_;
187 0 my %prefixes;
188  
189 0 for (@_) {
190 0 my @parts = File::Spec->splitdir($_);
191 0 my $prefix = shift @parts;
192 0 ++$prefixes{$prefix};
193 0 ++$prefixes{ $prefix = File::Spec->catdir( $prefix, $_ ) } for (@parts);
194 }
195 0 ▶ return first { $prefixes{$_} == @paths } reverse sort keys %prefixes;
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 }
197  
198 sub try_resolve_references {
199 0 ▶ my ( $self, $layers, $schemas ) = @_;
Subroutine try_resolve_references: not called CC=9 SCAR=45.0
Pod undocumented
200  
201 0 my %type_map;
202  
203 0 for my $schema (@$schemas) {
204 $schema->for_each_decl(
205 sub {
206 0 ▶ my $decl = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
207 0 ▶ my $is_node = ( $decl->get_role || '' ) eq '#NODE';
Condition0%0/2
$decl->get_role || ''
l!l
00
MC/DC0%0/1
$decl->get_role || ''
$decl->get_role
Truth table0%0/2
A
A= $decl->get_role
Aresult
11
00
208 0 ▶ return unless $is_node;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_node 0 0 0
209  
210 0 my $type = PMLTQ::Common::DeclPathToQueryType( $decl->get_decl_path );
211 0 $type_map{$type} = $schema;
212 }
213 0 );
214 }
215  
216 0 for my $layer (@$layers) {
217 0 my $refs = $layer->{references};
218 0 for my $path ( grep { $known_relations{$_} } keys %$refs ) {
219 0 my $type = $known_relations{$path};
220 0 my $schema = $type_map{$type};
221 0 ▶ next unless $schema; # check if type exists at all
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 0 0
222  
223 0 my $is_other_schema = $schema->get_root_name ne $layer->{name};
224 0 ▶ $refs->{$path} = ( $is_other_schema ? $schema->get_root_name . ':' : '' ) . $type;
Branch: 1 branch
BranchTrueFalseTotal
$is_other_schema ? : 0 0 0
225  
226 0 ▶ if ($is_other_schema) { # add to related schemas
Branch: 1 branch
BranchTrueFalseTotal
if ($is_other_schema) 0 0 0
227 0 ▶ $layer->{'related-schema'} = [] unless $layer->{'related-schema'};
Branch: 1 branch
BranchTrueFalseTotal
unless $layer->{"related-schema"} 0 0 0
228 0 my $schema_url = $schema->get_url->as_string;
229 0 push @{ $layer->{'related-schema'} }, $schema_url
230 0 ▶ if none { $schema_url eq $_ } @{ $layer->{'related-schema'} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $schema_url eq $_; } , @{$$layer{"related-schema"};}) 0 0 0
231 }
232 }
233 }
234 }
235  
236  
237 1 1;
238  
239 __END__
« blib/lib/PMLTQ/Relation/DescendantIteratorWithBoundedDepth.pm ↑ Summary blib/lib/PMLTQ/Relation/TreexFileIterator.pm »