File Coverage

blib/lib/Lingua/YaTeA/OptionSet.pm
Criterion Covered Total %
statement 96 139 69.0
branch 15 44 34.0
condition 6 12 50.0
subroutine 28 34 82.3
pod 30 31 96.7
total 175 260 67.3


line stmt bran cond sub pod time code
1             package Lingua::YaTeA::OptionSet;
2 5     5   28 use strict;
  5         9  
  5         120  
3 5     5   22 use warnings;
  5         6  
  5         100  
4              
5 5     5   21 use Data::Dumper;
  5         8  
  5         8064  
6              
7              
8             our $VERSION=$Lingua::YaTeA::VERSION;
9              
10             sub new
11             {
12 4     4 1 9 my ($class) = @_;
13 4         11 my %default = ("suffix"=>"default");
14 4         8 my $this = {};
15 4         8 bless ($this,$class);
16 4         15 $this->{OPTIONS} = ();
17 4         13 $this->addOptionSet(\%default);
18 4         12 return $this;
19             }
20              
21              
22             sub addOptionSet
23             {
24 12     12 1 26 my ($this,$options_set_h,$message_set,$display_language) = @_;
25 12         18 my $name;
26             my $val;
27 12         46 while (my ($opt,$val) = each (%$options_set_h))
28             {
29 105         153 $this->addOption($opt,$val,$message_set,$display_language);
30             }
31             }
32              
33             sub addOption
34             {
35 112     112 1 172 my ($this,$name,$value,$message_set,$display_language) = @_;
36              
37 112 100       155 if(! $this->optionExists($name))
38             {
39 108         237 $this->{OPTIONS}->{$name} = Lingua::YaTeA::Option->new($name,$value);
40             }
41             else
42             {
43 4         13 $this->getOption($name)->update($value,$message_set,$display_language);
44             }
45              
46             }
47              
48              
49             sub checkCompulsory
50             {
51 4     4 1 9 my ($this,$comp_list) = @_;
52 4         13 my @compulsory_options = split (",",$comp_list);
53 4         9 my $compulsory;
54 4         10 foreach $compulsory (@compulsory_options)
55             {
56 4 50       9 if(!defined $this->optionExists($compulsory))
57             {
58 0         0 die "You must define option \"" .$compulsory . "\"\n";
59             }
60             }
61             }
62              
63             sub is_disable
64             {
65 0     0 1 0 my ($this,$name) = @_;
66 0         0 my $option;
67              
68 0 0       0 if (defined $this->{OPTIONS}->{$name})
69             {
70 0 0       0 if ($this->{OPTIONS}->{$name} == 0) {
71 0         0 return(1);
72             }
73             }
74 0         0 return (0);
75             }
76              
77             sub is_enable
78             {
79 8     8 1 13 my ($this,$name) = @_;
80 8         11 my $option;
81              
82 8 50       18 if (defined $this->{OPTIONS}->{$name})
83             {
84 8 50       19 if ($this->{OPTIONS}->{$name} == 1) {
85 0         0 return(1);
86             }
87             }
88 8         29 return (0);
89             }
90              
91              
92             sub optionExists
93             {
94 1386     1386 1 1934 my ($this,$name) = @_;
95 1386         1616 my $option;
96             # foreach $option (@{$this->{OPTIONS}})
97             # {
98             # if($option->getName eq $name)
99             # {
100             # return $option;
101             # }
102             # }
103 1386 100       2615 if(defined $this->{OPTIONS}->{$name})
104             {
105 1203         3409 return $this->{OPTIONS}->{$name};
106             }
107 183         422 return(undef);
108             }
109              
110             sub getOption
111             {
112 1097     1097 1 1650 my ($this,$name) = @_;
113            
114 1097         1718 return ($this->optionExists($name));
115              
116             # if(! return $this->optionExists($name))
117             # {
118             # die "Option ". $name . " not defined\n";
119             # }
120              
121             }
122              
123             sub getOptions
124             {
125 0     0 1 0 my ($this) = @_;
126 0         0 return $this->{OPTIONS};
127             }
128              
129              
130             sub getLanguage
131             {
132 14     14 1 25 my ($this) = @_;
133 14         26 return $this->getOption("language")->getValue;
134             }
135              
136             sub getChainedLinks
137             {
138 0     0 1 0 my ($this) = @_;
139 0 0       0 if(defined $this->getOption("chained-links"))
140             {
141 0         0 return 1;
142             }
143 0         0 return 0;
144             }
145              
146             sub getSentenceBoundary
147             {
148 2     2 1 4 my ($this) = @_;
149 2         5 return $this->getOption("SENTENCE_BOUNDARY")->getValue;
150             }
151              
152             sub getDocumentBoundary
153             {
154 2     2 1 4 my ($this) = @_;
155 2         5 return $this->getOption("DOCUMENT_BOUNDARY")->getValue;
156             }
157              
158             sub getParsingDirection
159             {
160 80     80 1 105 my ($this) = @_;
161 80         135 return $this->getOption("PARSING_DIRECTION")->getValue;
162             }
163              
164             sub MatchTypeValue
165             {
166 4     4 1 8 my ($this) = @_;
167 4 100 66     9 if ((defined $this->getOption("match-type")) && ($this->getOption("match-type")->getValue() ne "")) {
168 2         4 return $this->getOption("match-type")->getValue;
169             }
170 2         6 return 0;
171             }
172              
173             sub readFromFile
174             {
175 4     4 1 8 my ($this,$file) = @_;
176 4         20 my $conf = Config::General->new($file->getPath);
177 4         4247 $this->addOptionSet($conf->{'DefaultConfig'});
178 4         15 $this->checkMaxLength;
179 4         56 $conf = undef;
180             }
181              
182             sub checkMaxLength
183             {
184 4     4 1 8 my ($this) = @_;
185 4 50       9 if(!$this->optionExists('PHRASE_MAXIMUM_LENGTH'))
186             {
187 0         0 $this->addOption('PHRASE_MAXIMUM_LENGTH',12);
188             }
189 4 50       13 if(!$this->optionExists('CANDIDATE_MAXIMUM_LENGTH'))
190             {
191 0         0 $this->addOption('CANDIDATE_MAXIMUM_LENGTH',12);
192             }
193             }
194              
195             sub getMaxLength
196             {
197 74     74 1 110 my ($this) = @_;
198 74         119 return $this->getOption("PHRASE_MAXIMUM_LENGTH")->getValue;
199              
200             }
201              
202             sub getCompulsory
203             {
204 250     250 1 353 my ($this) = @_;
205 250         415 return $this->getOption("COMPULSORY_ITEM")->getValue;
206              
207             }
208              
209             sub getSuffix
210             {
211 3     3 1 13 my ($this) = @_;
212 3         7 return $this->getOption("suffix")->getValue;
213              
214             }
215              
216             sub getDisplayLanguage
217             {
218 42     42 1 78 my ($this) = @_;
219 42         91 return $this->getOption("MESSAGE_DISPLAY")->getValue;
220             }
221              
222             sub getDefaultOutput
223             {
224 2     2 1 5 my ($this) = @_;
225 2         6 return $this->getOption("default_output")->getValue;
226             }
227              
228             sub setMatchType
229             {
230 0     0 1 0 my ($this,$match_type) = @_;
231            
232 0         0 $this->addOption('match-type',$match_type);
233             }
234              
235             sub getTermListStyle
236             {
237 2     2 1 4 my ($this) = @_;
238 2         7 return $this->getOption("termList")->getValue;
239             }
240              
241             sub getTTGStyle
242             {
243 2     2 1 6 my ($this) = @_;
244 2         6 return $this->getOption("TTG-style-term-candidates")->getValue;
245             }
246              
247             sub getOutputPath
248             {
249 3     3 1 9 my ($this) = @_;
250 3         11 return $this->getOption("output-path")->getValue;
251             }
252              
253             sub setDefaultOutputPath
254             {
255 4     4 1 10 my ($this) = @_;
256              
257 4 50 33     10 if((!defined $this->getOption("output-path")) ||($this->getOption("output-path")->getValue =~ /^\s*$/))
258             {
259 4         11 $this->addOption("output-path",".");
260             }
261             }
262              
263             sub getTCMaxLength
264             {
265 2     2 0 4 my ($this) = @_;
266 2         6 return $this->getOption("CANDIDATE_MAXIMUM_LENGTH")->getValue;
267             }
268              
269              
270             sub disable
271             {
272 0     0 1 0 my ($this,$option_name,$message_set,$display_language) = @_;
273            
274 0 0       0 if($this->optionExists($option_name))
275             {
276 0         0 delete($this->{OPTIONS}->{$option_name});
277             }
278 0         0 print STDERR "WARNING: " . $message_set->getMessage('DISABLE_OPTION')->getContent($display_language) . $option_name ." \n";
279             }
280              
281             sub enable
282             {
283 0     0 1 0 my ($this,$option_name,$option_value,$message_set,$display_language) = @_;
284            
285 0 0       0 if(! $this->optionExists($option_name))
286             {
287 0         0 $this->addOption($option_name,$option_value,$message_set,$display_language);
288 0         0 print STDERR "WARNING: \"" . $option_name ."\"" . $message_set->getMessage('ENABLE_OPTION')->getContent($display_language);
289 0 0       0 if(defined $option_value)
290             {
291 0         0 print STDERR "." . $message_set->getMessage('OPTION_VALUE')->getContent($display_language) . "\"". $this->getOption($option_name)->getValue . "\"";
292             }
293 0         0 print STDERR "\n";
294             }
295            
296             }
297              
298              
299             sub handleOptionDependencies
300             {
301 4     4 1 10 my ($this,$message_set) = @_;
302 4         16 my %incompatibilities = ('annotate-only' => ["TC-for-BioLG", "debug"]);
303 4         17 my %necessarily_linked = ('TT-for-BioLG' => ["termino", "match-type:if"],
304             'bootstrap' => ["termList"]);
305 4         19 my $incompatible_option;
306             my $necessary_option;
307 4         0 my @necessary_option;
308 4         0 my $option;
309 4         0 my $value;
310            
311              
312              
313 4         16 foreach $option (keys %incompatibilities)
314             {
315 4 50       11 if ($this->optionExists($option))
316             {
317 4         6 foreach $incompatible_option (@{$incompatibilities{$option}})
  4         12  
318             {
319 8 50 33     12 if (($this->optionExists($incompatible_option)) && ($this->is_enable($incompatible_option)))
320             {
321 0         0 print STDERR "WARNING: \"" . $incompatible_option . "\" & \"" . $option . "\"" . $message_set->getMessage('INCOMPATIBLE_OPTIONS')->getContent($this->getDisplayLanguage) . " \n";
322 0         0 $this->disable($option,$message_set,$this->getDisplayLanguage);
323 0         0 last;
324             }
325             }
326             }
327             }
328              
329 4         13 foreach $option (keys %necessarily_linked)
330             {
331 8 50 66     17 if (($this->optionExists($option)) && ($this->getOption('TT-for-BioLG')->getValue() == 1))
332             {
333 0           foreach $necessary_option (@{$necessarily_linked{$option}})
  0            
334             {
335 0           @necessary_option = split (/:/,$necessary_option);
336 0 0         if(!$this->optionExists($necessary_option[0]))
337             {
338 0 0         if($necessary_option[0] eq "termino")
339             {
340 0           print STDERR $option . ": " . $message_set->getMessage('TERMINO_NEEDED')->getContent($this->getDisplayLanguage) . " \n";
341 0           die "\n";
342             }
343             else
344             {
345 0           $this->enable($necessary_option[0],$necessary_option[1],$message_set,$this->getDisplayLanguage);
346             }
347             }
348             else
349             {
350 0 0         if(defined $necessary_option[1])
351             {
352 0 0         if($this->getOption($necessary_option[0])->getValue ne $necessary_option[1])
353             {
354 0           $this->getOption($necessary_option[0])->update($necessary_option[1],$message_set,$this->getDisplayLanguage);
355             }
356             }
357             }
358             }
359             }
360             }
361              
362             }
363              
364             1;
365              
366             __END__