AI-ExpertSystem-Advanced

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
        my $ai = AI::ExpertSystem::Advanced->new(
                viewer_class => 'terminal',
                knowledge_db => $yaml_kdb,
                initial_facts => ['I'],
                verbose => 1);
        $ai->mixed();
        $ai->summary();
 
Attributes
    initial_facts
        A list/set of initial facts the algorithms start using.
 
        During the forward algorithm the task is to find a list of goals
        caused by these initial facts (the only data we have in that
        moment).
 
        Lets imagine your knowledge database is about symptoms and diseases.
        You need to find what diseases are caused by the symptoms of a
        patient, these first symptons are the initial facts.
 
        Initial facts as also asked and inference facts can be negative or

README  view on Meta::CPAN

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    not enough information).
 
goals_to_check_dict
    Very similar to goals_to_check (and indeed of initial_facts_dict).
    We want to make the job easier.
 
    It will be a dictionary made of the data of goals_to_check.
 
inference_facts
    Inference facts are basically the core of an expert system. These
    are facts that are found and copied when a set of facts (initial,
    inference or asked) match with the causes of a goal.
 
    inference_facts is a AI::ExpertSystem::Advanced::Dictionary, it will
    store the name of the fact, the rule that caused these facts to be
    copied to this dictionary, the sign and the algorithm that triggered
    it.
 
knowledge_db
    The object reference of the knowledge database
    AI::ExpertSystem::Advanced is using.

README  view on Meta::CPAN

313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
              });
 
      my $ai = AI::ExpertSystem::Advanced->new(
              viewer_class => 'terminal',
              knowledge_db => $yaml_kdb,
              initial_facts => ['F', 'J']);
      $ai->forward();
      $ai->summary();
 
  The forward chaining algorithm is one of the main methods used in Expert
  Systems. It starts with a set of variables (known as initial facts) and
  reads the available rules.
 
  It will be reading rule by rule and for each one it will compare its
  causes with the initial, inference and asked facts. If all of these
  causes are in the facts then the rule will be shoot and all of its goals
  will be copied/converted to inference facts and will restart reading
  from the first rule.
 
backward()

README  view on Meta::CPAN

338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
            filename => 'examples/knowledge_db_one.yaml'
            });
 
    my $ai = AI::ExpertSystem::Advanced->new(
            viewer_class => 'terminal',
            knowledge_db => $yaml_kdb,
            goals_to_check => ['J']);
    $ai->backward();
    $ai->summary();
 
The backward algorithm starts with a set of *assumed* goals (facts). It
will start reading goal by goal. For each goal it will check if it
exists in the initial, inference and asked facts (see
is_goal_in_our_facts()) for more information).
 
*   If the goal exist then it will be removed from the dictionary, it
    will also verify if there are more visited rules to shoot.
 
    If there are still more visited rules to shoot then it will check
    from what rule the goal comes from, if it was copied from a rule
    then this data will exist. With this information then it will see

README  view on Meta::CPAN

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
  just one is found then the algorithm ends.
 
  At the end (if there are still no positive inference facts) it will run
  the forward() algorithm and restart (by looking again for any positive
  inference fact).
 
  A good example to understand how this algorithm is useful is: imagine
  you are a doctor and know some of the symptoms of a patient. Probably
  with the first symptoms you have you can get to a positive conclusion
  (eg that a patient has *X* disease). However in case there's still no
  clue, then a set of questions (done by the call of backward()) of
  symptons related to the initial symptoms will be asked to the user. For
  example, we know that that the patient has a headache but that doesn't
  give us any positive answer, what if the patient has flu or another
  disease? Then a set of these *related* symptons will be asked to the
  user.
 
summary($return)
  The main purpose of any expert system is the ability to explain: what is
  happening, how it got to a result, what assumption(s) it required to
  make, the fatcs that were excluded and the ones that were used.
 
  This method will use the viewer (or return the result) in YAML format of
  all the rules that were shot. It will explain how it got to each one of
  the causes so a better explanation can be done by the viewer.

inc/Module/Install.pm  view on Meta::CPAN

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
#line 1
 
# For any maintainers:
# The load order for Module::Install is a bit magic.
# It goes something like this...
#
# IF ( host has Module::Install installed, creating author mode ) {
#     1. Makefile.PL calls "use inc::Module::Install"
#     2. $INC{inc/Module/Install.pm} set to installed version of inc::Module::Install
#     3. The installed version of inc::Module::Install loads
#     4. inc::Module::Install calls "require Module::Install"
#     5. The ./inc/ version of Module::Install loads
# } ELSE {
#     1. Makefile.PL calls "use inc::Module::Install"
#     2. $INC{inc/Module/Install.pm} set to ./inc/ version of Module::Install
#     3. The ./inc/ version of Module::Install loads
# }
 
use 5.005;
use strict 'vars';
 
use vars qw{$VERSION $MAIN};
BEGIN {
        # All Module::Install core packages now require synchronised versions.
        # This will be used to ensure we don't accidentally load old or

inc/Module/Install.pm  view on Meta::CPAN

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        *inc::Module::Install::VERSION = *VERSION;
        @inc::Module::Install::ISA     = __PACKAGE__;
 
}
 
 
 
 
 
# Whether or not inc::Module::Install is actually loaded, the
# $INC{inc/Module/Install.pm} is what will still get set as long as
# the caller loaded module this in the documented manner.
# If not set, the caller may NOT have loaded the bundled version, and thus
# they may not have a MI version that works with the Makefile.PL. This would
# result in false errors or unexpected behaviour. And we don't want that.
my $file = join( '/', 'inc', split /::/, __PACKAGE__ ) . '.pm';
unless ( $INC{$file} ) { die <<"END_DIE" }
 
Please invoke ${\__PACKAGE__} with:
 
        use inc::${\__PACKAGE__};
 
not:

inc/Module/Install/Metadata.pm  view on Meta::CPAN

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
        }
 
        return 1;
}
 
sub all_from {
        my ( $self, $file ) = @_;
 
        unless ( defined($file) ) {
                my $name = $self->name or die(
                        "all_from called with no args without setting name() first"
                );
                $file = join('/', 'lib', split(/-/, $name)) . '.pm';
                $file =~ s{.*/}{} unless -e $file;
                unless ( -e $file ) {
                        die("all_from cannot find $file from $name");
                }
        }
        unless ( -f $file ) {
                die("The path '$file' does not exist, or is not a file");
        }

inc/Module/Install/Metadata.pm  view on Meta::CPAN

318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
        return $self->{values}->{no_index};
}
 
sub read {
        my $self = shift;
        $self->include_deps( 'YAML::Tiny', 0 );
 
        require YAML::Tiny;
        my $data = YAML::Tiny::LoadFile('META.yml');
 
        # Call methods explicitly in case user has already set some values.
        while ( my ( $key, $value ) = each %$data ) {
                next unless $self->can($key);
                if ( ref $value eq 'HASH' ) {
                        while ( my ( $module, $version ) = each %$value ) {
                                $self->can($key)->($self, $module => $version );
                        }
                } else {
                        $self->can($key)->($self, $value);
                }
        }

lib/AI/ExpertSystem/Advanced.pm  view on Meta::CPAN

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use YAML::Syck qw(Dump);
 
our $VERSION = '0.03';
 
=head1 Attributes
 
=over 4
 
=item B<initial_facts>
 
A list/set of initial facts the algorithms start using.
 
During the forward algorithm the task is to find a list of goals caused
by these initial facts (the only data we have in that moment).
 
Lets imagine your knowledge database is about symptoms and diseases. You need
to find what diseases are caused by the symptoms of a patient, these first
symptons are the initial facts.
 
Initial facts as also asked and inference facts can be negative or positive. By
default the initial facts are positive.

lib/AI/ExpertSystem/Advanced.pm  view on Meta::CPAN

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
It will be a dictionary made of the data of L<goals_to_check>.
 
=cut
has 'goals_to_check_dict' => (
        is => 'ro',
        isa => 'AI::ExpertSystem::Advanced::Dictionary');
 
=item B<inference_facts>
 
Inference facts are basically the core of an expert system. These are facts
that are found and copied when a set of facts (initial, inference or asked)
match with the causes of a goal.
 
L<inference_facts> is a L<AI::ExpertSystem::Advanced::Dictionary>, it will
store the name of the fact, the rule that caused these facts to be copied to
this dictionary, the sign and the algorithm that triggered it.
 
=cut
has 'inference_facts' => (
        is => 'ro',
        isa => 'AI::ExpertSystem::Advanced::Dictionary');

lib/AI/ExpertSystem/Advanced.pm  view on Meta::CPAN

719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
            });
 
    my $ai = AI::ExpertSystem::Advanced->new(
            viewer_class => 'terminal',
            knowledge_db => $yaml_kdb,
            initial_facts => ['F', 'J']);
    $ai->forward();
    $ai->summary();
 
The forward chaining algorithm is one of the main methods used in Expert
Systems. It starts with a set of variables (known as initial facts) and reads
the available rules.
 
It will be reading rule by rule and for each one it will compare its causes
with the initial, inference and asked facts. If all of these causes are in the
facts then the rule will be shoot and all of its goals will be copied/converted
to inference facts and will restart reading from the first rule.
 
=cut
sub forward {
    my ($self) = @_;

lib/AI/ExpertSystem/Advanced.pm  view on Meta::CPAN

790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
            filename => 'examples/knowledge_db_one.yaml'
            });
 
    my $ai = AI::ExpertSystem::Advanced->new(
            viewer_class => 'terminal',
            knowledge_db => $yaml_kdb,
            goals_to_check => ['J']);
    $ai->backward();
    $ai->summary();
 
The backward algorithm starts with a set of I<assumed> goals (facts). It will
start reading goal by goal. For each goal it will check if it exists in the
initial, inference and asked facts (see L<is_goal_in_our_facts()>) for more
information).
 
=over 4
 
=item *
 
If the goal exist then it will be removed from the dictionary, it will also
verify if there are more visited rules to shoot.

lib/AI/ExpertSystem/Advanced.pm  view on Meta::CPAN

977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
verify for any positive inference fact, if just one is found then the algorithm
ends.
 
At the end (if there are still no positive inference facts) it will run the
L<forward()> algorithm and restart (by looking again for any positive inference
fact).
 
A good example to understand how this algorithm is useful is: imagine you are
a doctor and know some of the symptoms of a patient. Probably with the first
symptoms you have you can get to a positive conclusion (eg that a patient has
I<X> disease). However in case there's still no clue, then a set of questions
(done by the call of L<backward()>) of symptons related to the initial symptoms
will be asked to the user. For example, we know that that the patient has a
headache but that doesn't give us any positive answer, what if the patient has
flu or another disease? Then a set of these I<related> symptons will be asked
to the user.
 
=cut
sub mixed {
    my ($self) = @_;
 
    if (!$self->forward()) {
        $self->{'viewer'}->print_error("The first execution of forward failed");
        return 0;
    }



( run in 1.386 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )