-
Notifications
You must be signed in to change notification settings - Fork 0
/
InferenceMotor.pm
executable file
·201 lines (164 loc) · 6.04 KB
/
InferenceMotor.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/perl
=head1 Lincense
/* -*- Mode: Perl */
/*
* InferenceMotor.pm
* Copyright (C) 2014 Barajas D. Paul <[email protected]>
*
* RegExpert-System is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RegExpert-System is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.";
*/
=head1 NAME
InferenceEngine.pm
=head1 VERSION
VERSION 0.001
=head1 DESCRIPTION
The inference engine applied logical rules to the knowledge base and deduced new knowledge. This process would iterate as each new fact in the knowledge base could trigger additional rules in the inference engine.
=cut
use warnings;
no warnings 'experimental::smartmatch';
package InferenceMotor;
use Conclusion;
use Data::Dumper;
use Compiler;
use Common_definitions;
require Exporter;
use feature qw/switch/;
our @EXPORT = qw(
Interface_rule
antecedent_analyse
validateRules
validateHypothesis
ValidatelastElementConclusion
get_AoA
);
our @ISA = qw(Exporter);
our $ConlusionBC;
our @AoA=[];
=head1 FUNCTION
Interface_rule
=head1 DESCRIPTION
Interface Rule is used to validate user decisions.
=head1 BUGS
Not found
=cut
sub Interface_rule(){
($atom,$Antecedents)=@_;
my ($value_to_process,$entry_value);
$atom=~ s/\!//g if ($atom =~ /\!/);
if(&Concluded($atom)){
next;
}
($cpatoms)=$atom;
push @AntecendentsBased, $cpatoms;
print "$atom --> \n$Antecedents->{$atom}";
chomp ($entry_Value = <STDIN>);
if($entry_Value =~ /[yes|y|ye]/i){
$value_to_process=$atom;
}else{
$value_to_process="!".$atom;
}
&validateRules($value_to_process);
}
=head1 FUNCTION
validateRules
=head1 DESCRIPTION
validateRules used method of reasoning called "forward chaining"
Forward chaining starts with the available data and uses inference rules to extract more data (from Interface_rule fuction) until a goal is reached (Conclusion.pm). An inferenceEngine.pm using forward chaining searches the inference rules until it finds one where the antecedent (If clause) is known to be true. When such a rule is found, the engine can conclude, or infer, the consequent (Then clause), resulting in the addition of new information to its data.
=head1 BUGS
varible $+<negation>, in some case avoid to match the negation '!'.
=cut
sub validateRules(){
($atom_check)=shift;
print "hola $atom_check\n";
my $row=0;
my $status=IMPLICATION_ATOM;
my $conjuntion_arg=0;
foreach my $check_rule (@contentRules){
#print "------------------------------------------>$check_rule\n";
$status=IMPLICATION_ATOM;
#print "###################$row\n";
if($check_rule =~ /(${atom_match})\s?\)$/){
if ($1 eq $atom_check){
($inc)=&AddConclusion($atom_check);
#print "---------------------$atom_check\n";
$row++ and next unless $inc ne 1;
}
}
while(my ($opt,$pattern)= each %implication_atom){
local $count=0;
if($check_rule =~ /$pattern(${atom_check})|(?<negation>(?:\!|))(${atom_check})$pattern/){
if(defined($+{negation}) and ($+{negation} eq "!")){
next;
}
foreach my $preposition (&get_groups_matching($check_rule)){
if(&exact_match_atom($atom_check,$preposition)){
$conjuntion_arg=$count;
}
$count++;
}
$conjuntion_arg++;
push @{$AoA[$row]->[$conjuntion_arg]},$atom_check;
&VerifyConclusion(\@AoA,$row);
$status=CONJUNTION_ATOM;
$row++;
}
}
next if $status eq CONJUNTION_ATOM;
if(&exact_match_atom($atom_check,$check_rule)){
push @{$AoA[$row]},$atom_check;
&VerifyConclusion(\@AoA,$row);
}
$row++;
$status=IMPLICATION_ATOM;
}
}
=head1 FUNCTION
validateHypothesis
=head1 DESCRIPTION
validateHypothesis used method of reasoning called "backward chaining"
Backward chaining starts with a list of goals ($IntConclusionHash and $FinalConclusions) and works backwards from the consequent to the antecedent to see if there is data available that will support any of these consequents. An inference engine using backward chaining would search the inference rules until it finds one which has a consequent (Then clause) that matches a desired goal. If the antecedent (If clause) of that rule is not known to be true, then it is added to the list of goals (in order for one's goal to be confirmed one must also provide data that confirms this new rule).
=head1 BUGS
Not found
=cut
sub validateHypothesis(){
my $value=shift;
my $ant=shift;
$ConlusionBC=$value;
&AddConclusion($ConlusionBC);
print Dumper($Conclusion);
my $index=0;
my $value_to_process;
my @ArrayConsequents;
&ValidatelastElementConclusion('AddRule',$value);
print "Test: ".Dumper(@ArrayHypotesis);
#print "Test: ".Dumper(@CorrectHypotesys);
foreach(@ArrayHypotesis){
print "test: ".Dumper($_);
my $array=$$_;
my @data=@$array;
pop @data;
foreach(@data){
my $aux=$_;
my $aux2=$aux;
my $aux3=$aux;
next if ($aux ~~ @ArrayConsequents);
#next if (exists $IntConclusionHash{$aux});
push @ArrayConsequents,$aux2;
#print $AntecedentValues{$aux};
&Interface_rule($aux,$ant);
}
}
}
sub get_AoA{return \@AoA;}
1;