blib/lib/AI/Logic/AnswerSet.pm

filter: Statementstmt 8.5% 22 / 256 Branchbran 0.8% 1 / 116 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 20.0% 4 / 20 Podpod 100% 15 / 15 totaltotal 10.0% 42 / 417 CC 68 SCAR 48.4
CC
68
Cov
6%
mapAS
272.0
statistics
240.0
_evaluate
182.0
CRAP 126.8
↑ Summary
1 package AI::Logic::AnswerSet;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Exporter;
8  
9 1 our @ISA = qw(Exporter);
10  
11 1 our %EXPORT_TAGS = ( 'all' => [ qw(
12
13 ) ] );
14  
15 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
16  
17 1 our @EXPORT = qw(
18
19 );
20  
21 1 our $VERSION = '0.02';
22  
23 0 ▶sub executeFromFileAndSave { #Executes DLV with a file as input and saves the output in another file
Subroutine executeFromFileAndSave: not called CC=4 SCAR=30.0
Pod documented
24  
25 0 open DLVW, ">>", "$_[1]";
26 0 print DLVW $_[2];
27 0 close DLVW;
28  
29 0 ▶ open(SAVESTDOUT, ">&STDOUT") or die "Can't save STDOUT: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open SAVESTDOUT, ">&STDOUT" or die "Can't save STDOUT: $!\n" 0 0 0
30 0 ▶ open(STDOUT, ">$_[0]") or die "Can't open STDOUT to $_[0]", "$!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">$_[0]" or die "Can't open STDOUT to $_[0]", "$!\n" 0 0 0
31  
32  
33 0 my @args = ("./dlv", "$_[1]");
34 0 ▶ system(@args) == 0
Branch: 1 branch
BranchTrueFalseTotal
system(@args) == 0 or die "system @args failed: $?" 0 0 0
35 or die "system @args failed: $?";
36  
37 0 open(STDOUT,">&SAVESTDOUT"); #close file and restore STDOUT
38 0 close OUTPUT;
39  
40 }
41  
42 0 ▶sub executeAndSave { #Executes DLV and saves the output of the program written by the user in a file
Subroutine executeAndSave: not called CC=4 SCAR=30.0
Pod documented
43  
44 0 ▶ open(SAVESTDOUT, ">&STDOUT") or die "Can't save STDOUT: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open SAVESTDOUT, ">&STDOUT" or die "Can't save STDOUT: $!\n" 0 0 0
45 0 ▶ open(STDOUT, ">$_[0]") or die "Can't open STDOUT to $_[0]", "$!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">$_[0]" or die "Can't open STDOUT to $_[0]", "$!\n" 0 0 0
46  
47 0 my @args = ("./dlv --");
48 0 ▶ system(@args) == 0 or die "system @args failed: $?";
Branch: 1 branch
BranchTrueFalseTotal
system(@args) == 0 or die "system @args failed: $?" 0 0 0
49  
50 0 open(STDOUT,">&SAVESTDOUT"); #close file and restore STDOUT
51 0 close OUTPUT;
52  
53  
54 }
55  
56  
57 0 ▶sub iterativeExec { # Executes an input program with several instances and stores them in a bidimensional array
Subroutine iterativeExec: not called CC=4 SCAR=30.0
Pod documented
58  
59 0 my @input = @_;
60  
61 0 my @returned_value;
62  
63 0 ▶ if(@input) {
Branch: 1 branch
BranchTrueFalseTotal
if (@input) { } 0 0 0
64
65 0 my $option = $input[$#input];
66  
67 0 ▶ if($option =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option =~ /^-/) { } 0 0 0
68 0 pop(@input);
69 }
70 else {
71 0 $option = "";
72 }
73  
74 0 my $dir = pop(@input);
75 0 my @files = qx(ls $dir);
76
77 0 my $size = @files;
78  
79 0 ▶ for(my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 0 0 0
80  
81 0 my $elem = $files[$i];
82 0 chomp $elem;
83 0 my @args = ("./dlv", "@input", "$dir$elem", "$option");
84 0 my (@out) = `@args`;
85 0 push @{$returned_value[$i]}, @out;
86 }
87
88 }
89  
90 else {
91 0 print "INPUT ERROR\n";
92 }
93  
94 0 return @returned_value;
95  
96 }
97  
98 0 ▶sub singleExec { # Executes a single input program or opens the DLV terminal and stores it in an array
Subroutine singleExec: not called CC=2 SCAR=17.9
Pod documented
99  
100 0 my @input = @_;
101 0 my @returned_value;
102  
103 0 ▶ if(@input) {
Branch: 1 branch
BranchTrueFalseTotal
if (@input) { } 0 0 0
104  
105  
106 0 my @args = ("./dlv", "@input");
107 0 (@returned_value) = `@args`;
108
109 }
110  
111 else {
112 0 my $command = "./dlv --";
113 0 (@returned_value) = `$command`;
114 }
115  
116 0 return @returned_value;
117 }
118  
119 0 ▶sub selectOutput { # Select one of the outputs returned by the iterative execution of more input programs
Subroutine selectOutput: not called CC=1 SCAR=6.9
Pod documented
120  
121 0 my @stdoutput = @{$_[0]};
122 0 my $n = $_[1];
123  
124 0 return @{$stdoutput[$n]};
125
126 }
127  
128 0 ▶sub getFacts { # Return the facts of the input program
Subroutine getFacts: not called CC=6 SCAR=37.4
Pod documented
129  
130 0 my $input = shift;
131  
132 0 my @isAFile = stat($input);
133  
134 0 my @facts;
135  
136 0 ▶ if(@isAFile) {
Branch: 1 branch
BranchTrueFalseTotal
if (@isAFile) { } 0 0 0
137  
138 0 open INPUT, "<", "$input";
139 0 my @rows = <INPUT>;
140 0 foreach my $row (@rows) {
141 0 ▶ if($row =~ /^(\w+)(\(((\w|\d|\.)+,?)*\))?\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row =~ /^(\w+)(\(((\w|\d|\.)+,?)*\))?\./) 0 0 0
142 0 push @facts, $row;
143 }
144 }
145 0 close INPUT;
146  
147 }
148 else {
149 0 my @str = split /\. /,$input;
150 0 foreach my $elem (@str) {
151  
152 0 ▶ if($elem =~ /^(\w+)(\(((\w|\d|\.)+,?)*\))?\.?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem =~ /^(\w+)(\(((\w|\d|\.)+,?)*\))?\.?$/) 0 0 0
153 0 push @facts, $elem;
154 }
155 }
156 }
157 0 return @facts;
158
159 }
160  
161 1 ▶sub addCode { #Adds code to input
Subroutine addCode: called CC=2 SCAR=7.8
Pod documented
162  
163 1 my $program = $_[0];
164 1 my $code = $_[1];
165 1 my @isAFile = stat($program);
166  
167 1 ▶ if(@isAFile) {
Branch: 1 branch
BranchTrueFalseTotal
if (@isAFile) { } 0 1 1
168 0 open PROGRAM, ">>", $program;
169 0 print PROGRAM "$code\n";
170 0 close PROGRAM;
171 }
172  
173 else {
174 1 $program = \($_[0]);
175 1 $$program = "$$program $code";
176 }
177
178 }
179  
180 0 ▶sub getASFromFile { #Gets the Answer Set from the file where the output was saved
Subroutine getASFromFile: not called CC=4 SCAR=30.0
Pod documented
181  
182 0 ▶ open RESULT, "<", "$_[0]" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open RESULT, "<", "$_[0]" or die $! 0 0 0
183 0 my @result = <RESULT>;
184 0 my @arr;
185 0 foreach my $line (@result) {
186  
187 0 ▶ if($line =~ /\{\w*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\{\w*/) 0 0 0
188 0 $line =~ s/(\{|\})//g;
189 #$line =~ s/\n//g; # delete \n from $line
190 0 my @tmp = split(', ', $line);
191 0 push @arr, @tmp;
192 }
193  
194 }
195  
196 0 close RESULT;
197 0 return @arr;
198 }
199  
200 0 ▶sub getAS { #Returns the Answer Sets from the array where the output was saved
Subroutine getAS: not called CC=3 SCAR=24.8
Pod documented
201  
202 0 my @result = @_;
203 0 my @arr;
204  
205 0 foreach my $line (@result) {
206  
207  
208 0 ▶ if($line =~ /\{\w*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\{\w*/) 0 0 0
209 0 $line =~ s/(\{|\})//g;
210 0 $line =~ s/(Best model:)//g;
211 0 my @tmp = split(', ', $line);
212 0 push @arr, @tmp;
213 }
214  
215 }
216  
217 0 return @arr;
218 }
219  
220 0 ▶sub statistics { # Return an array of hashes in which the statistics of every predicate of every answerSets are stored
Subroutine statistics: not called CC=15 SCAR=54.8
Pod documented
221 # If a condition of comparison is specified(number of predicates) it returns the answer sets that satisfy
222 # that condition
223  
224 0 my @as = @{$_[0]};
225 0 my @pred = @{$_[1]};
226 0 my @num = @{$_[2]};
227 0 my @operators = @{$_[3]};
228  
229 0 my @sets;
230 my @ans;
231
232 0 my $countAS = 0;
233 0 my @stat;
234  
235 my $countPred;
236  
237 0 foreach my $elem (@as) {
238  
239 0 ▶ if($elem =~ /(\w+).*\n/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem =~ /(\w+).*\n/) { } 0 0 0
elsif ($elem =~ /(\w+).*/) { } 0 0 0
240 0 push @{$sets[$countAS]}, $elem;
241 0 ▶ if(_existsPred($1,\@pred)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_existsPred($1, \@pred)) 0 0 0
242 0 $stat[$countAS]{$1} += 1;
243 0 $countAS += 1;
244 }
245 }
246  
247 elsif($elem =~ /(\w+).*/) {
248 0 push @{$sets[$countAS]}, $elem;
249 0 ▶ if(_existsPred($1,\@pred)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_existsPred($1, \@pred)) 0 0 0
250 0 $stat[$countAS]{$1} += 1;
251 }
252 }
253 }
254  
255 0 my $comparison = 0;
256 0 ▶ if(@num and @operators) {
Branch: 2 branches
BranchTrueFalseTotal
if (@num and @operators) { } 0 0 0
elsif (@num and not @operators) { } 0 0 0
Condition0%0/3
@num and @operators
!ll&&!rl&&r
000
MC/DC0%0/2
@num and @operators
@num @operators
Truth table0%0/3
A and B
A= @numB= @operators
ABresult
0X0
100
111
257 0 $comparison = 1;
258 }
259 ▶ elsif(@num and !@operators) {
Condition0%0/3
@num and not @operators
!ll&&!rl&&r
000
MC/DC0%0/2
@num and not @operators
@num not @operators
Truth table0%0/3
A and not(B)
A= @numB= not @operators
ABresult
0X0
100
111
260 0 print "Error: comparison element missing";
261 0 return @ans;
262 }
263
264
265  
266 0 ▶ if($comparison) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comparison) 0 0 0
267 0 my $size = @pred;
268 0 my $statSize = @stat;
269  
270 0 ▶ for(my $j = 0; $j < $statSize; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $statSize) 0 0 0
271 0 ▶ for(my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 0 0 0
272  
273 0 my $t = $stat[$j]{$pred[$i]};
274  
275 0 ▶ if(_evaluate($t,$num[$i],$operators[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_evaluate($t, $num[$i], $operators[$i])) { } 0 0 0
276 0 $countPred++;
277 }
278 else {
279 0 $countPred = 0;
280 0 break;
281 }
282 }
283  
284 0 ▶ if($countPred == $size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($countPred == $size) 0 0 0
285 0 push @ans , $sets[$j];
286 }
287 0 $countPred = 0;
288 }
289 0 return @ans;
290  
291 }
292  
293 0 return @stat;
294 }
295  
296 0 ▶sub _evaluate { #private use only
Subroutine _evaluate: not called CC=13 SCAR=52.0
297  
298 0 my $value = shift;
299 0 my $num = shift;
300 0 my $operator = shift;
301  
302 0 ▶ if($operator eq "==") {
Branch: 6 branches
BranchTrueFalseTotal
if ($operator eq '==') { } 0 0 0
elsif ($operator eq '!=') { } 0 0 0
elsif ($operator eq '>') { } 0 0 0
elsif ($operator eq '>=') { } 0 0 0
elsif ($operator eq '<') { } 0 0 0
elsif ($operator eq '<=') { } 0 0 0
303 0 ▶ if($value == $num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value == $num) 0 0 0
304 0 return 1;
305 }
306 0 return 0;
307 }
308 elsif($operator eq "!=") {
309 0 ▶ if($value != $num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value != $num) 0 0 0
310 0 return 1;
311 }
312 0 return 0;
313 }
314 elsif($operator eq ">") {
315 0 ▶ if($value > $num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > $num) 0 0 0
316 0 return 1;
317 }
318 0 return 0;
319 }
320 elsif($operator eq ">=") {
321 0 ▶ if($value >= $num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value >= $num) 0 0 0
322 0 return 1;
323 }
324 0 return 0;
325 }
326 elsif($operator eq "<") {
327 0 ▶ if($value < $num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < $num) 0 0 0
328 0 return 1;
329 }
330 0 return 0;
331 }
332 elsif($operator eq "<=") {
333 0 ▶ if($value <= $num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value <= $num) 0 0 0
334 0 return 1;
335 }
336 0 return 0;
337 }
338 0 return 0;
339 }
340  
341 0 ▶sub mapAS { #Mapping of the Answer Sets in an array of hashes
Subroutine mapAS: not called CC=16 SCAR=56.1
Pod documented
342  
343 0 my $countAS = 0;
344  
345 0 my @answerSets = @{$_[0]};
346  
347 0 my @second;
348 0 ▶ if($_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) 0 0 0
349 0 @second = @{$_[1]};
350 }
351  
352 0 my @third;
353 0 ▶ if($_[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) 0 0 0
354 0 @third = @{$_[2]};
355 }
356  
357 0 my @selectedAS;
358
359 my @predList;
360  
361 0 my @pred;
362  
363 0 ▶ if(@second) {
Branch: 1 branch
BranchTrueFalseTotal
if (@second) 0 0 0
364 0 ▶ if($second[0] =~ /\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($second[0] =~ /\d+/) { } 0 0 0
365  
366 0 @selectedAS = @second;
367 0 ▶ if(@third) {
Branch: 1 branch
BranchTrueFalseTotal
if (@third) 0 0 0
368 0 @predList = @third;
369 }
370  
371 }
372  
373 else {
374 0 @predList = @second;
375 0 ▶ if(@third) {
Branch: 1 branch
BranchTrueFalseTotal
if (@third) 0 0 0
376 0 @selectedAS = @third;
377 }
378 }
379 }
380  
381  
382 0 foreach my $elem (@answerSets) {
383  
384  
385 0 ▶ if($elem =~ /(\w+).*\n/){
Branch: 2 branches
BranchTrueFalseTotal
if ($elem =~ /(\w+).*\n/) { } 0 0 0
elsif ($elem =~ /(\w+).*/) { } 0 0 0
386 0 ▶ if(@predList) {
Branch: 1 branch
BranchTrueFalseTotal
if (@predList) { } 0 0 0
387 0 ▶ if(_existsPred($1,\@predList)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_existsPred($1, \@predList)) 0 0 0
388 0 push @{$pred[$countAS]{$1}}, $elem;
389 }
390 }
391 else {
392 0 push @{$pred[$countAS]{$1}}, $elem;
393 }
394 0 $countAS = $countAS + 1;
395
396 }
397  
398 elsif($elem =~ /(\w+).*/) {
399 0 ▶ if(@predList) {
Branch: 1 branch
BranchTrueFalseTotal
if (@predList) { } 0 0 0
400 0 ▶ if(_existsPred($1,\@predList)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_existsPred($1, \@predList)) 0 0 0
401 0 push @{$pred[$countAS]{$1}}, $elem;
402 }
403 }
404 else {
405 0 push @{$pred[$countAS]{$1}}, $elem;
406 }
407 }
408
409 }
410  
411 0 ▶ if(@selectedAS) {
Branch: 1 branch
BranchTrueFalseTotal
if (@selectedAS) 0 0 0
412
413 0 my $size = @selectedAS;
414  
415 0 my @selectedPred;
416  
417  
418 0 ▶ for(my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 0 0 0
419 0 my $as = $selectedAS[$i];
420 0 push @selectedPred, $pred[$as];
421 }
422  
423 0 return @selectedPred;
424 }
425 0 return @pred;
426  
427 }
428  
429 0 ▶sub _existsPred { #Verifies the existence of a predicate (private use only)
Subroutine _existsPred: not called CC=3 SCAR=24.8
430  
431 0 my $pred = $_[0];
432 0 my @predList = @{$_[1]};
433  
434 0 my $size = @predList;
435  
436 0 ▶ for(my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 0 0 0
437 0 ▶ if($pred eq $predList[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pred eq $predList[$i]) 0 0 0
438 0 return 1;
439 }
440 }
441 0 return 0;
442
443 }
444  
445 0 ▶sub getPred { #Returns the predicates from the array of hashes
Subroutine getPred: not called CC=1 SCAR=6.9
Pod documented
446  
447 0 my @pr = @{$_[0]};
448 0 return @{$pr[$_[1]]{$_[2]}};
449 }
450  
451 0 ▶sub getProjection { #Returns the values selected by the user
Subroutine getProjection: not called CC=3 SCAR=24.8
Pod documented
452  
453 0 my @pr = @{$_[0]};
454 0 my @projection;
455  
456 0 my @res = @{$pr[$_[1]]{$_[2]}};
457
458 0 my $size = @res;
459 0 my $fieldsStr;
460  
461 0 ▶ for(my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 0 0 0
462 0 my $pred = @{$pr[$_[1]]{$_[2]}}[$i];
463 0 ▶ if($pred =~ /(\w+)\((.+)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pred =~ /(\w+)\((.+)\)/) 0 0 0
464 0 $fieldsStr = $2;
465 }
466 0 my @fields = split(',',$fieldsStr);
467 0 push @projection , $fields[$_[3]-1];
468
469 }
470  
471 0 return @projection;
472 }
473  
474 0 ▶sub createNewFile {
Subroutine createNewFile: not called CC=1 SCAR=6.9
Pod documented
475  
476 0 my $file = $_[0];
477 0 my $code = $_[1];
478  
479 0 open FILE, ">", $file;
480 0 print FILE "$code\n";
481 0 close FILE;
482  
483 }
484  
485 0 ▶sub addFacts {
Subroutine addFacts: not called CC=2 SCAR=17.9
Pod documented
486  
487 0 my $name = $_[0];
488 0 my @facts = @{$_[1]};
489 0 my $append = $_[2];
490 0 my $filename = $_[3];
491
492 0 open FILE, $append, $filename;
493  
494 0 foreach my $f (@facts) {
495 0 print FILE "$name($f).\n";
496 }
497 0 close FILE;
498 }
499  
500  
501 1 1;
502 __END__
↑ Summary