Commit f44f2c8c authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+6 −0
Changes for 010201_1353/Examples/PP2E/Ai/ExpertSystem/Todo.txt: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
-- use new dict.copy to copy bindings
-- use class constructors (added after holmes coded)
-- cnv script could use PyTools/fixeoln_all.py
-- text/gui modes
-- uses classes for rules, etc, not dicts
-- there's too much whitespace in these files
+3 −0
Changes for 010201_1353/Examples/PP2E/Ai/ExpertSystem/__init__.py: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line

#empty
+300 KiB

File added.

No diff preview for this file type.

+203 −0
Changes for 010201_1353/Examples/PP2E/Ai/ExpertSystem/holmes/README.1ST: 203 added lines, 0 removed lines.
Original line number Diff line number Diff line
System:       Holmes
Description:  An expert-system shell coded in Python
Author:       Mark Lutz


DESCRIPTION: 
------------
A Python-coded expert system shell, submitted as an 
example of the power of the Python language as a 
general systems development language.  A number of
smaller examples (including an enhanced shell) are
also included (in the 'others' directory).

PURPOSE: 
--------
This system does not demonstrate all of Python's qualities 
(embedding, etc.), but should serve as proof of the 
utility of the language in sophisticated applications.
There are certainly better examples of Python's power (for
example, browse the library code), but expert systems employ 
particularly interesting algorithms.

FUNCTIONALITY: 
--------------
The shell supports both forward and backward chaining inference 
over a set of logical implication rules.  The backward chainer 
is roughly similar to the Prolog language, and rule selection 
uses general (but constrained) unification.  Rules may contain 
logical (quantified) variables, and conjunctions of goals, and
are entered in a text file, or interactively.  Backward and 
forward chaining queries are entered interactively in the
shell; facilities for reading queries from files also exist.
Log files in the /examples directory demonstrate the sort
of bahaviour and interaction implemented in holmes.

LIMITATIONS: 
------------
The rule matcher does not allow nested terms, and there are no 
extra-logical predicates.  Therefore, Holmes is not really 
suitable for general logic programming, and is more of a tool 
for exploring symbolic knowledge bases.  Holmes also handles 
only certain information (there are no certainty factors (yet)).




DISCLAIMER.
----------------------------------------------------------------------

	*THIS IS NOT PROFESSIONAL-QUALITY SOFTWARE*

Holmes was developed as part of a language evaluation process 
conducted at XVT Software, under the guise of an R&D project myself 
and Amrit Prem were involved in.  It was meant as benchmark, and was
never intended to be used or seen externally.  

Consequently, I make no claims about the quality of the code or the 
documentation; it's research software.  I cut corners to save time
in alot of places;  the parser, for example, uses simple field
scanning, and does not detect syntax errors (if you're lucky, it
will abort on an error).

You are free to use, distribute, or modify the source code in any way 
you see fit.  But I assume no responsibility for its quality.  If you 
do want to use this system in a real application (I wouldn't recommend
it), you should expect to have to modify it substantially, to make it
more robust.  As is, it's not much more than a tool for experimenting 
with expert systems concepts.




IMPLEMENTATION NOTES.
----------------------------------------------------------------------
Having said all that, I do think this is a good example of the type
of thing Python excels at.  This system contains 2 versions of the 
shell, each of which is a multi-file system, with multiple forward- 
and backward-chaining inference engines, a pattern matcher (simplified
unification), a rule file parser, an interactive shell, explanation
facilities, index trees, etc.  Each of the versions contains roughly 
2000 lines of text.

Despite this complexity, I was able to develop this system in roughly
1 month.  Perhaps more importantly, I was able to develop the system
incrementally (witness the multiple backward chainers), and rapidly 
modify the code over and over.  In effect, the entire development 
process was an exercise in rapid prototyping.  Python lends itself 
to this form of experimental, interactive program development.
The end result was a readable, usable, and maintainable system.

My experiences demonstrated, in my opinion, that Python is ideal
for rapid development.  It's orders of magnitude faster to use than C
or C++: 

   * The lack of compile/link time provides immediate feedback for 
     program changes.

   * The simpler syntax makes for less to write and read, fewer
     syntax errors to correct, easier program modifications, etc.

   * The inherent semantic power of the language makes most tasks
     simpler, leads to fewer runtime errors, etc.

Python provides very fast turnaround, even for major program changes.
Further, the run-time performance of the system, once coded, was
very acceptable for such a computationally-intensive task.  Recoding
in C would not gain enough performance to justify such a tedious task.

Of equal significance, Python proved far superior to other popular
dynamic scripting language (Perl, TCL, etc.): 

    * I was able to learn the language, and use its syntax almost
      immediately

    * I was actually able to read and re-use the code I had written, 
      long after I had written it.

This simplicity and readability of Python are, in my opinion, the most
important advantages it has over these other tools.  Python, being 
more like a traditional programming language, is a better choice for 
writing large, complex software systems fast, whose code must be 
re-used, maintained by others, etc.  To be blunt, I would never have
attempted a system of this level of complexity in a Perl, or TCL.

To summarize, Python combines the best features of:

        Scripting languages (TCL, Perl):
              - embedability
              - access to system resources and external API's

        Dynamic languages (Lisp, Smalltalk, Prolog):
              - interactive
              - dynamic typing
              - very-high-level objects
              - no compile, link steps
              - incremental development/testing

        Traditional languages (C, C++, Modula, Pascal):
              - procedural syntax
              - modules, classes, exceptions
              - straightforward semantics

Of course, Python is not a panacea for all the ills of the software
industry.  For instance, its speed makes it weak as a number-cruncher.
But it's an ideal rapid development/prototyping language, and does 
well as a scripting/integration language.  Both of these tasks usually 
have modest performance constraints, as do many application areas 
(GUI design, and database programming, for example).  Python's 
performance is more than adequate for these cases.  More importantly, 
speed-of-development often outweighs speed-of-code in these (and 
many other) areas; Python wins this test easily.

Although Python is well-suited for self-contained rapid development 
(as in Holmes), I suspect that Python's real contribution will be 
in the area of mixed-language applications.  Guido has called this 
a 'hybrid' approach: using Python for modules where performance 
isn't critical (user-interface, etc.), and C or C++ for modules 
where it is.  Alternatively, one may initially develop all modules 
in Python for speed-of-development, and later move selected 
time-critical modules to C as needed.

By now, we should have learned that no language can simultaneously 
satisfy all development goals.  But a mixed approach leverages the 
best of all available tools.  Here again, Python excels: it's clean 
call-in and call-out intefaces to C make it ideal as an embedded
dynamic language, in large applications.  Python's can also function 
as the 'glue' for external API's (data-bases, GUI's, etc.), providing 
a programmatic interface.  Further, it's clear syntax and semantics 
put it far ahead other languages with similar goals, such as TCL 
(which are usually just extended 'shell' languages).

These are all very subjective comments.  I'd encourage you to 
browse the source code, experiment with the system, and draw 
your own conclusions.  You may very well become as evangelical 
about Python as I did :-).




WHERE TO GO FROM HERE.
----------------------------------------------------------------------
Read the 'README.2ND' file for information on package contents, loading
and running.  Also see the 'holmes.doc' files in both versions (holmes
and holmes2) for implementation details.  Draw your own conclusions. 
Have fun.  Feel free to email me with questions or comments.

If you are interested in more information about expert systems, you
might try consulting a good AI text book.  Books on Lisp and Prolog
also cover these sorts of things.  My favorites are:
	-- The Art of Prolog, by Sterling and Shapiro
	-- Lisp, by Winston and Horn
	-- The Elements of Artificial Intelligence, by Tanimoto

I hope to develop a tool for automating Python/C integration in
the near future.  Details will be posted as they emerge.



Mark Lutz
lutz@kapre.com
Boulder, Colorado
Feb. 1994
+116 −0
Changes for 010201_1353/Examples/PP2E/Ai/ExpertSystem/holmes/README.2ND: 116 added lines, 0 removed lines.
Original line number Diff line number Diff line
CONTENTS:
---------
These directories contain the holmes inference system in Python:

    holmes/
        the original (naive) verion of holmes

    holmes2/
        the optimized, index tree version of holmes

    kbases/
        some example knowledge base files (fixit, zoo, eats, etc.)

holmes/ and holmes2/ both contain a 'holmes.doc' information 
file, with implementation details.  Both holmes versions contain 
a number of backward chainer variants, demonstrating different 
codings/algorithms.  Both versions also contain 2 forward
chainer varients, which use different semantics for logical 
negation ('not'). 

Other directories:

    examples/
        logs showing example interaction/use of holmes, and the
        psh command-line shell

    holmes3/ 
        an incomplete start at adding uncertainties to holmes   
        (I don't have time to finish this version right now)

    others/
        other Python examples that have nothing to do with Holmes
	    psh.py    -- an enhanced Python interactive command shell
 	    sets.py   -- sets and relational algebra in Python
	    sets.res  -- sets.py expected results
 	    roman.py  -- a Python take-off of an Icon program 


RUNNING:
--------
One way (perhaps not the best) to run the system:
    goto python/lib
    cp ../../holmes/* or ../../holmes2/* to .
    python holmes.py

Better, set up your python load path environment variable to point 
to the directory you put the holmes modules in.  You only need to 
import 'holmes'; the holmes module imports the rest of the system 
itself.  You must have Python installed to use Holmes; I wrote it 
under Python 0.9.8 (hence the lack of class constructors/destructors), 
but it should work with 1.0 (I've run it under 1.0 briefly-- email 
me if you have troubles).

When you import holmes (or run 'python holmes.py'), an interactive
shell starts up.  Here, you are able to 

	-- load external rule files,
	-- enter rules interactively, 
	-- submit forward and backward chaining requests, 
	-- get how/why explanations, 
	-- enter Python commands, 
	-- browse the current rule base, 
	-- specify which backward chaining varient you want to use
	-- etc.  

Typing 'help' at the 'holmes>' prompt shows you all available commands.
Normally, you will type yur rules into a text file with an editor,
start holmes, load the rules file, and run queries inside holmes.

Some of the more important shell commands:
	To load a rule file:
    		holmes> @= a:\kbases\fixit.py

	To submit a backward chaining query:
    		holmes> ?- fixit tv ?prob ?soln

	To forward chain, from a set of initial facts:
    		holmes> +- class mammal, is intelligent

	To add a new rule:
		holmes> += rule 1 if human ?X then mortal ?x

	To list all rules in the knowledge base:
		holmes> @@

	To get help:
    		holmes> help


An example session:
	python holmes.py
	holmes> += rule 1 if human ?x then mortal ?x
	holmes> ?- mortal guido

	is this true: "human guido" ? <yes>

	yes: (no variables)

	show proof? <no>
        more solutions? <no>

	holmes> +- human amrit

	I deduced these facts...
	    mortal amrit
	I started with these facts...
	    human amrit

	show proofs? <no>

	holmes> stop

See the examples/* log files for more detailed examples of interaction
under holmes and psh.  These logs serve as good documentation of use
of the system.
Loading