blib/lib/PMLTQ/Suggest/Utils.pm untested

filter: Statementstmt 0.0% 0 / 187 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 38 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 335 CC 42 SCAR 50.6
CC
42
Cov
0%
apply_file_suffix
420.0
_find_tree_no
72.0
parse_file_suffix
30.0
CRAP 157.9
1 package PMLTQ::Suggest::Utils;
2 our $AUTHORITY = 'cpan:MATY';
3 $PMLTQ::Suggest::Utils::VERSION = '0.0.3';
4 use strict;
5 use warnings;
6  
7 use Treex::PML::Document;
8 use List::MoreUtils 'uniq';
9 use File::Basename 'basename';
10 use UNIVERSAL::DOES;
11  
12 use Encode ();
13 use Treex::PML::Schema::CDATA;
14 use Treex::PML::Factory;
15 use UNIVERSAL;
16  
17 #######################################################################################
18 # Usage : first(\&sub, @list)
19 # Purpose : Return the first element of list for which the sub returns true
20 # (no arguments are passed to the sub, it has to use $_);
21 # Return undef otherwise (or empty list in list context)
22 # Returns : see Purpose
23 # Parameters : anonymous_sub \&sub -- subroutine that does not take any arguments and
24 # returns values which can be evaluated to true or false
25 # list @list -- first element from the @list, which is accepted by \&sub is then returned
26 # Throws : no exceptions
27 # Comments : Prototyped function
28 sub first (&@) {
29 my $code = shift;
30  
31 foreach (@_) {
32 return $_ if &{$code}();
33 }
34  
35 return;
36 }
37 #######################################################################################
38 ### from TrEd::Utils
39 # Usage : apply_file_suffix($win, $goto)
40 # Purpose : Set current tree and node positions to positions described by
41 # $goto suffix in file displayed in $win window
42 # Returns : 1 if the new position was found and set, 0 otherwise
43 # Parameters : TrEd::Window $win -- reference to TrEd::Window object
44 # string $goto -- suffix of the file (or a position in the file)
45 # Throws : no exceptions
46 # Comments : Possible suffix formats:
47 # ##123.2 -- tree number 123 (if counting from 1) and its second node
48 # #123.3 -- tree whose $root->{form} equals to #123 and its third node
49 # (only hint found in Treex/PML/Backend/CSTS/Csts2fs.pm)
50 # #a123 -- finds node with id #a123 and the tree it belongs to
51 # The node's id can also be placed after the '.', e.g. ##123.#a123, in
52 # which case the sub searches for node with id #a123 inside tree no 123
53 #
54 # Sets $win->{treeNo} and $win->{currentNode} if appropriate.
55 # See Also : parse_file_suffix()
56 sub apply_file_suffix {
57 my ( $win, $goto ) = @_;
58 return if ( !defined $win );
59 my $fsfile = $win->{FSFile};
60 return if !( defined $fsfile && defined $goto && $goto ne ''); # $EMPTY_STR );
61  
62 if ( $goto =~ m/^##([0-9]+)/ ) {
63  
64 # handle cases like '##123'
65 my $no = int( $1 - 1 );
66 $win->{treeNo} = min( max( 0, $no ), $fsfile->lastTreeNo() );
67 return 0 if $win->{treeNo} != $no;
68 }
69 elsif ( $goto =~ /^#([0-9]+)/ ) {
70  
71 # handle cases like '#123'
72 # this is PDT 1.0-specific code, sorry
73 my $no;
74 for ( my $i = 0; $i <= $fsfile->lastTreeNo(); $i++ ) {
75 if ( $fsfile->treeList()->[$i]->{form} eq "#$1" ) {
76 $no = $i;
77 last;
78 }
79 }
80 return 0 if ( !defined $no );
81 $win->{treeNo} = $no;
82 }
83 elsif ( $goto =~ /^#([^#]+)$/ ) {
84  
85 # handle cases like '#a123'
86 my $id = $1;
87 if ( Treex::PML::Schema::CDATA->check_string_format( $id, 'ID' ) ) {
88 my $id_hash = $fsfile->appData('id-hash');
89 if ( UNIVERSAL::isa( $id_hash, 'HASH' )
90 && exists $id_hash->{$id} )
91 {
92 my $node = $id_hash->{$id};
93  
94 # we would like to use Treex::PML::Index() here, but can't
95 # and why we can not?
96 my $list = $fsfile->treeList();
97 my $root = UNIVERSAL::can( $node, 'root' ) && $node->root();
98 my $n = defined($root) && first {
99 $list->[$_] == $root;
100 }
101 0 .. $#$list;
102  
103 if ( defined $root and !defined($n) ) {
104 $n = _find_tree_no( $fsfile, $root, $list );
105  
106 # exit from _find_tree_no() function
107 if ( !defined $n || $n == -1 ) {
108 return 0;
109 }
110 }
111 if ( defined($n) ) {
112 $win->{treeNo} = $n;
113 $win->{currentNode} = $node;
114 return 1;
115 }
116 else {
117 return 0;
118 }
119 }
120 }
121 }
122  
123 # new: we're the dot in .[0-9]+ (TM)
124 if ( $goto =~ /\.([0-9]+)$/ ) {
125 my $root = get_node_by_no( $win, $1 );
126 if ($root) {
127 $win->{currentNode} = $root;
128 return 1;
129 }
130 else {
131 return 0;
132 }
133 }
134 elsif ( $goto =~ /\.([^0-9#][^#]*)$/ ) {
135 my $id = $1;
136 if ( Treex::PML::Schema::CDATA->check_string_format( $id, 'ID' ) ) {
137 my $id_hash = $fsfile->appData('id-hash');
138 if ( UNIVERSAL::isa( $id_hash, 'HASH' )
139 && exists( $id_hash->{$id} ) )
140 {
141 return 1
142 if ( $win->{currentNode} = $id_hash->{$id} ); # assignment
143 }
144 else {
145 return 0;
146 }
147 }
148 }
149 return 1;
150  
151 # hey, caller, you should redraw after this!
152 }
153  
154 #TODO: document & test this unclear function
155 sub _find_tree_no {
156 my ( $fsfile, $root, $list ) = @_;
157 my $n = undef;
158  
159 # hm, we have a node, but don't know to which tree
160 # it belongs
161 my $trees_type = $fsfile->metaData('pml_trees_type');
162 my $root_type = $root->type();
163  
164 #TODO: empty? or defined???
165 if ( $trees_type and $root_type ) {
166 my $trees_type_is = $trees_type->get_decl_type();
167 my %paths;
168 my $is_sequence;
169 my $found;
170 my @elements;
171 if ( $trees_type_is == Treex::PML::Schema::PML_LIST_DECL() ) {
172 @elements = [ 'LM', $trees_type->get_content_decl() ];
173 }
174 elsif ( $trees_type_is == Treex::PML::Schema::PML_SEQUENCE_DECL() ) {
175  
176 # Treex::PML::Schema::Element::get_name(),
177 # ::Schema::Decl::get_content_decl()
178 @elements = map { [ $_->get_name(), $_->get_content_decl() ] }
179 $trees_type->get_elements();
180 $is_sequence = 1;
181 }
182 else {
183 return -1;
184 }
185  
186 for my $el (@elements) {
187 $paths{ $el->[0] } = [
188 $trees_type->get_schema->find_decl(
189 sub {
190 $_[0] == $root_type;
191 },
192 $el->[1],
193 {}
194 )
195 ];
196 if ( @{ $paths{ $el->[0] } } ) {
197 $found = 1;
198 }
199 }
200 return -1 if !$found;
201 TREE:
202 for my $i ( 0 .. $#$list ) {
203 my $tree = $list->[$i];
204 my $paths
205 = $is_sequence
206 ? $paths{ $tree->{'#name'} }
207 : $paths{LM};
208 for my $p ( @{ $paths || [] } ) {
209 for my $value ( $tree->all($p) ) {
210 if ( $value == $root ) {
211 $n = $i;
212 last TREE;
213 }
214 }
215 }
216 }
217 }
218 return $n;
219 }
220  
221 #######################################################################################
222 ### from TrEd::Utils
223 # Usage : parse_file_suffix($filename)
224 # Purpose : Split file name into file name itself and its suffix
225 # Returns : List which contains file name and its suffix, if there is no suffix,
226 # second list element is undef
227 # Parameters : scalar $filename -- name of the file
228 # Throws : no exceptions
229 # Comments : File suffix can be of the following forms:
230 # a) 1 or 2 #-signs, upper-case characters or numbers, and optionally followed by
231 # optional dash, full stop and at least one number
232 # b) 2 #-signs, at least one number, full stop, followed by
233 # one non-numeric not-# character and any number of not-# chars
234 # c) 1 #-sign followed by any number of not-# characters
235 # See Also :
236 sub parse_file_suffix {
237 my ($filename) = @_;
238 #
239 return if ( !defined $filename );
240 if ( $filename =~ s/(##?[0-9A-Z]+(?:-?\.[0-9]+)?)$// ) {
241 return ( $filename, $1 );
242 }
243 elsif (
244 $filename =~ m{^
245 (.*) # file name with any characters followed by
246 (\#\#[0-9]+\.) # 2x#, at least one number and full stop
247 ([^0-9\#][^\#]*) # followed by one non-numeric not-# character and any number of not-# chars
248 $
249 }x
250 and Treex::PML::Schema::CDATA->check_string_format( $3, 'ID' )
251 )
252 {
253 return ( $1, $2 . $3 );
254 }
255 elsif (
256 $filename =~ m{^
257 (.*) # file name with any characters followed by
258 \# # one hash followed by
259 ([^\#]+) # any number of not-# characters
260 $
261 }x
262 and Treex::PML::Schema::CDATA->check_string_format( $2, 'ID' )
263 )
264 {
265 return ( $1, '#' . $2 );
266 }
267 else {
268 return ( $filename, undef );
269 }
270 }
271  
272 ######################################
273  
274  
275  
276 # open a data file and related files on lower layers
277 sub open_file {
278 my $filename = shift;
279 # TODO fsfile caching and closing !!!
280 my $fsfile = Treex::PML::Factory->createDocumentFromFile($filename);
281 if ($Treex::PML::FSError) {
282 die "Error loading file $filename: $Treex::PML::FSError ($!)\n";
283 }
284 my $requires = $fsfile->metaData('fs-require');
285 if ($requires) {
286 for my $req (@$requires) {
287 my $req_filename = $req->[1]->abs( $fsfile->URL );
288 warn("REQUIRES $req_filename");
289 my $secondary = $fsfile->appData('ref');
290 unless ($secondary) {
291 $secondary = {};
292 $fsfile->changeAppData( 'ref', $secondary );
293 }
294 my $sf = open_file($req_filename);
295 $secondary->{ $req->[0] } = $sf;
296 }
297 }
298 return $fsfile;
299 }
300 #############################################
301  
302  
303 sub GetSecondaryFiles {
304 my ($fsfile) = @_;
305 # is probably the same as Treex::PML::Document->relatedDocuments()
306 # a reference to a list of pairs (id, URL)
307 my $requires = $fsfile->metaData('fs-require');
308 my @secondary;
309 if ($requires) {
310 foreach my $req (@$requires) {
311 my $id = $req->[0];
312 my $req_fs
313 = ref( $fsfile->appData('ref') )
314 ? $fsfile->appData('ref')->{$id}
315 : undef;
316 if ( UNIVERSAL::DOES::does( $req_fs, 'Treex::PML::Document' ) ) {
317 push( @secondary, $req_fs );
318 }
319 }
320 }
321 return uniq(@secondary);
322 }
323  
324  
325 sub OpenSecondaryFiles {
326 my ( $fsfile ) = @_;
327 my $win = undef;
328 my $status = 1;
329 return $status if $fsfile->appData('fs-require-loaded');
330 $fsfile->changeAppData( 'fs-require-loaded', 1 );
331 my $requires = $fsfile->metaData('fs-require'); #$fsfile->relatedDocuments()
332 if (defined $requires) {
333 for my $req (@$requires) {
334 next if ref( $fsfile->appData('ref')->{ $req->[0] } );
335 my $req_filename
336 = Treex::PML::ResolvePath( $fsfile->filename, $req->[1] );
337 print STDERR "Pre-loading dependent $req_filename ($req->[1]) as appData('ref')->{$req->[0]}\n";
338 my ( $req_fs, $status2 ) = open_file( # TODO simplify Tred::File::open_file() subrutine
339 $win, $req_filename,
340 -preload => 1,
341 -norecent => 1
342 );
343 _merge_status( $status, $status2 );
344 if ( !$status2->{ok} ) {
345 close_file( $win, -fsfile => $req_fs, -no_update => 1 );
346 return $status2;
347 }
348 else { #zaznac do zavisleho, ze je zavisly na nadradenom
349 push @{ $req_fs->appData('fs-part-of') },
350 $fsfile; # is this a good idea?
351 main::__debug("Setting appData('ref')->{$req->[0]} to $req_fs");
352 $fsfile->appData('ref')->{ $req->[0] } = $req_fs;
353 }
354 }
355 }
356 return $status;
357 }
358  
359 sub ThisAddress {
360 my ($node, $fsfile) = @_;
361 my $type = $node->type;
362 my ($id_attr) = $type && $type->find_members_by_role('#ID');
363  
364 return basename($fsfile->filename) . '#' . $node->{ $id_attr->get_name }
365 }
366  
367 sub GetNodeIndex {
368 my $node = shift;
369 my $i = -1;
370 while ($node) {
371 $node = $node->previous();
372 $i++;
373 }
374 return $i;
375 }
376  
377  
378 1;