To build a knowledge base, Jess must read input from keyboard / files to execute commands and load...
-
date post
21-Dec-2015 -
Category
Documents
-
view
217 -
download
0
Transcript of To build a knowledge base, Jess must read input from keyboard / files to execute commands and load...
![Page 1: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/1.jpg)
Introduction to Jess
![Page 2: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/2.jpg)
To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs.
During the execution process, Jess groups symbols together into tokens – groups of characters that have the same meaning.
A field is a special type of token of which there are 8 types.
Expert Systems: Principles and Programming, Fourth Edition 2
Fields
![Page 3: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/3.jpg)
The floats and integers make up the numeric fields – simply numbers.
Integers have only a sign and digits.
Floats have a decimal and possibly “e” for scientific notation.
Expert Systems: Principles and Programming, Fourth Edition 3
Numeric Fields
![Page 4: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/4.jpg)
Symbols begin with printable ASCII characters followed by zero or more characters, followed by a delimiter.
Jess is case sensitive.
Expert Systems: Principles and Programming, Fourth Edition 4
Symbol Fields
![Page 5: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/5.jpg)
Strings must begin and end with double quotation marks.
Spaces w/in the string are significant.
The actual delimiter symbols can be included in a string by preceding the character with a backslash.
Expert Systems: Principles and Programming, Fourth Edition 5
String Fields
![Page 6: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/6.jpg)
External addresses represent the address of an external data structure returned by a user-defined function.
Fact address fields are used to refer to a specific fact.
Instance Name / Address field – instances are similar to facts addresses but refer to the instance rather than a fact.
Expert Systems: Principles and Programming, Fourth Edition 6
Address Fields
![Page 7: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/7.jpg)
The Jess prompt is: Jess>
This is the type-level mode where commands can be entered.
To exit Jess, one types: Jess> (exit)
Jess will accept input from the user / evaluate it / return an appropriate response:Jess> (+ 3 4) value 7 would be returned.
Expert Systems: Principles and Programming, Fourth Edition 7
Entering / Exiting Jess
![Page 8: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/8.jpg)
Expert Systems: Principles and Programming, Fourth Edition 8
Basic CLIPS Commands (1)
![Page 9: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/9.jpg)
Expert Systems: Principles and Programming, Fourth Edition 9
Basic CLIPS Commands (2)
![Page 10: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/10.jpg)
To solve a problem, Jess must have data or information with which to reason.
Each chunk of information is called a fact.
Facts consist of:
◦ Relation name (symbolic field)◦ Zero or more slots w/associated values
Expert Systems: Principles and Programming, Fourth Edition 10
Facts and Jess
![Page 11: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/11.jpg)
Expert Systems: Principles and Programming, Fourth Edition 11
Example Fact in Jess
![Page 12: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/12.jpg)
Before facts can be constructed, Jess must be informed of the list of valid slots for a given relation name.
A deftemplate is used to describe groups of facts sharing the same relation name and contain common information.
Expert Systems: Principles and Programming, Fourth Edition 12
Deftemplate
![Page 13: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/13.jpg)
Expert Systems: Principles and Programming, Fourth Edition 13
Deftemplate General Format
![Page 14: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/14.jpg)
Facts with a relation name defined using deftemplate are called deftemplate facts.
Facts with a relation name that does not have a corresponding deftemplate are called ordered facts – have a single implied multifield slot for storing all the values of the relation name.
Expert Systems: Principles and Programming, Fourth Edition 14
Deftemplate vs. Ordered Facts
![Page 15: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/15.jpg)
Adding Facts
Jess store all facts known to it in a fact list. To add a fact to the list, we use the assert command.
Expert Systems: Principles and Programming, Fourth Edition 15
![Page 16: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/16.jpg)
Displaying Facts
Jess> (facts)
Expert Systems: Principles and Programming, Fourth Edition 16
![Page 17: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/17.jpg)
Just as facts can be added, they can also be removed.
Removing facts results in gaps in the fact identifier list.
To remove a fact:
Jess> (retract 2)
Expert Systems: Principles and Programming, Fourth Edition 17
Removing Facts
![Page 18: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/18.jpg)
Modifying Facts
Slot values of deftemplate facts can be modified using the modify command:
Expert Systems: Principles and Programming, Fourth Edition 18
![Page 19: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/19.jpg)
A new fact index is generated because when a fact is modified:◦ The original fact is retracted◦ The modified fact is asserted
The duplicate command is similar to the modify command, except it does not retract the original fact.
Expert Systems: Principles and Programming, Fourth Edition 19
Results of Modification
![Page 20: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/20.jpg)
The watch command is useful for debugging purposes.
If facts are “watched”, Jess will automatically print a message indicating an update has been made to the fact list whenever either of the following has been made:◦ Assertion◦ Retraction
Expert Systems: Principles and Programming, Fourth Edition 20
Watch Command
![Page 21: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/21.jpg)
The deffacts construct can be used to assert a group of facts.
Groups of facts representing knowledge can be defined as follows:
(deffacts <deffacts name> [<optional] comment]<facts> * )
The reset command is used to assert the facts in a deffacts statement.
Expert Systems: Principles and Programming, Fourth Edition 21
Deffacts Construct
![Page 22: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/22.jpg)
To accomplish work, an expert system must have rules as well as facts.
Rules can be typed into Jess (or loaded from a file).
Consider the pseudocode for a possible rule:
IF the emergency is a fireTHEN the response is to activate the sprinkler system
Expert Systems: Principles and Programming, Fourth Edition 22
The Components of a Rule
![Page 23: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/23.jpg)
First, we need to create the deftemplate for the types of facts:
(deftemplate emergency (slot type))-- type would be fire, flood, etc.
Similarly, we must create the deftemplate for the types of responses:
(deftemplate response (slot action))-- action would be “activate the sprinkler”
Expert Systems: Principles and Programming, Fourth Edition 23
Rule Components
![Page 24: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/24.jpg)
The rule would be shown as follows:
(defrule fire-emergency “An example rule”(emergency (type fire))=>(assert (response
(action activate-sprinkler-system))))
Expert Systems: Principles and Programming, Fourth Edition 24
Rule Components
![Page 25: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/25.jpg)
The header of the rule consists of three parts:1. Keyword defrule2. Name of the rule – fire-emergency3. Optional comment string – “An example rule”
Expert Systems: Principles and Programming, Fourth Edition 25
Analysis of the Rule
![Page 26: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/26.jpg)
If all the patterns of a rule match facts, the rule is activated and put on the agenda.
The agenda is a collection of activated rules.
The arrow => represents the beginning of the THEN part of the IF-THEN rule.
The last part of the rule is the list of actions that will execute when the rule fires.
Expert Systems: Principles and Programming, Fourth Edition 26
Analysis of Rule
![Page 27: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/27.jpg)
To run the Jess program, use the run command:
Jess> (run [<limit>])
-- the optional argument <limit> is the maximum number of rules to be fired – if omitted, rules will fire until the agenda is empty.
Expert Systems: Principles and Programming, Fourth Edition 27
The Agenda and Execution
![Page 28: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/28.jpg)
When the program runs, the rule with the highest salience on the agenda is fired.
Rules become activated whenever all the patterns of the rule are matched by facts.
The reset command is the key method for starting or restarting .
Facts asserted by a reset satisfy the patterns of one or more rules and place activation of these rules on the agenda.
Expert Systems: Principles and Programming, Fourth Edition 28
Execution
![Page 29: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/29.jpg)
To display the rules on the agenda, use the agenda command:
Jess> (agenda)
Refraction is the property that rules will not fire more than once for a specific set of facts.
The refresh command can be used to make a rule fire again by placing all activations that have already fired for a rule back on the agenda.
Expert Systems: Principles and Programming, Fourth Edition 29
What is on the Agenda?
![Page 30: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/30.jpg)
The list-deftemplates displays the current list of deftemplates.
(rules)/(facts) display the current list of rules/facts accordingly.
The ppdefrule, ppdeftemplate and ppdeffacts commands display the text representations of a defrule, deftemplate, and a deffact, respectively.
Expert Systems: Principles and Programming, Fourth Edition 30
Command for Manipulating Constructs
![Page 31: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/31.jpg)
The undefrule, undeftemplate, and undeffacts commands are used to delete a defrule, a deftemplate, and a deffact, respectively.
The clear command clears the Jess environment and adds the initialfact-defacts to the Jess environment.
The printout command can also be used to print information.
Expert Systems: Principles and Programming, Fourth Edition 31
Commands
![Page 32: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/32.jpg)
Comments – provide a good way to document programs to explain what constructs are doing.
Variables – store values, syntax requires preceding with a question mark (i.e: ?example)
Expert Systems: Principles and Programming, Fourth Edition 32
Commenting and Variables
![Page 33: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/33.jpg)
A variable can be bound to a fact address of a fact matching a particular pattern on the LHS of a rule by using the pattern binding operator “<-”.
Single-field wildcards can be used in place of variables when the field to be matched against can be anything and its value is not needed later in the LHS or RHS of the rule (?).
Multifield variables and wildcards allow matching against more than one field in a pattern ($?)
Expert Systems: Principles and Programming, Fourth Edition 33
Fact Addresses, Single-Field Wildcards, and Multifield Variables
![Page 34: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/34.jpg)
In addition to pattern matching capabilities and variable bindings, Jess has more powerful pattern matching operators.
Consider writing a rule for all people who do not have brown hair:◦ We could write a rule for every type of hair color that is
not brown.◦ This involves testing the condition in a roundabout
manner – tedious, but effective.
Expert Systems: Principles and Programming, Fourth Edition 34
Field Constraints
![Page 35: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/35.jpg)
The technique for writing a rule for all non-brown hair colors implies that we have the ability to supply all hair colors – virtually impossible.
An alternative is to use a field constraint to restrict the values a field may have on the LHS – the THEN part of the rule.
Expert Systems: Principles and Programming, Fourth Edition 35
Field Constraints
![Page 36: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/36.jpg)
Connective constraints are used to connect variables and other constraints.
Not connective – the ~ acts on the one constraint or variable that immediately follows it.
Or constraint – the symbol | is used to allow one or more possible values to match a field or a pattern.
And constraint – the symbol & is useful with binding instances of variables and on conjunction with the not constraint.
Expert Systems: Principles and Programming, Fourth Edition 36
Connective Constraints
![Page 37: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/37.jpg)
Field constraints can be used together with variables and other literals to provide powerful pattern matching capabilities.
Example #1: ?eyes1&blue|green◦ This constraint binds the person’s eye color to the
variable, ?eyes1 if the eye color of the fact being matched is either blue or green.
Example #2: ?hair1&~black◦ This constraint binds the variable ?hair1 if the hair
color of the fact being matched is not black.
Expert Systems: Principles and Programming, Fourth Edition 37
Combining Field Constraints
![Page 38: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/38.jpg)
Functions and Expressions
Jess has the capability to perform calculations.
The math functions in Jess are primarily used for modifying numbers that are used to make inferences by the application program.
Expert Systems: Principles and Programming, Fourth Edition 38
![Page 39: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/39.jpg)
Numeric expressions are written in Jess in LISP-style – using prefix form – the operator symbol goes before the operands to which it pertains.
Example #1:5 + 8 (infix form) + 5 8 (prefix form)
Example #2:(infix) (y2 – y1) / (x2 – x1) > 0(prefix) (> ( / ( - y2 y1 ) (- x2 x1 ) ) 0)
Expert Systems: Principles and Programming, Fourth Edition 39
Numeric Expressions in Jess
![Page 40: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/40.jpg)
Most functions (addition) have a return value that can be an integer, float, symbol, string, or multivalued value.
Some functions (facts, agenda commands) have no return values – just side effects.
Division results are usually rounded off. Return values for +, -, and * will be integer if all
arguments are integer, but if at least one value is floating point, the value returned will be float.
Expert Systems: Principles and Programming, Fourth Edition 40
Return Values
![Page 41: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/41.jpg)
Many Jess functions accept variable numbers of arguments.
Example:Jess> (- 3 5 7) returns 3 - 5 =-2 - 7 = -9
There is no built-in arithmetic precedence in Jess – everything is evaluated from left to right.
To compensate for this, precedence must be explicitly written.
Expert Systems: Principles and Programming, Fourth Edition 41
Variable Numbers of Arguments
![Page 42: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/42.jpg)
Embedding Expressions
Expressions may be freely embedded within other expressions:
Expert Systems: Principles and Programming, Fourth Edition 42
![Page 43: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/43.jpg)
Suppose you wanted to sum the areas of a group of rectangles.◦ The heights and widths of the rectangles can be
specified using the deftemplate:
(deftemplate rectangle (slot height) (slot width))
The sum of the rectangle areas could be specified using an ordered fact such as:
(sum 20)
Expert Systems: Principles and Programming, Fourth Edition 43
Summing Values Using Rules
![Page 44: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/44.jpg)
A deffacts containing sample information is:
(deffacts initial-information(rectangle (height 10) (width 6))(rectangle (height 7) (width 5)(rectangle (height 6) (width 8))(rectangle (height 2) (width 5))(sum 0))
Expert Systems: Principles and Programming, Fourth Edition 44
Summing Values
![Page 45: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/45.jpg)
Expert Systems: Principles and Programming, Fourth Edition 45
Summing Values
![Page 46: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/46.jpg)
Sometimes it is advantageous to store a value in a temporary variable to avoid recalculation.
The bind function can be used to bind the value of a variable to the value of an expression using the following syntax:
(bind <variable> <value>)
Expert Systems: Principles and Programming, Fourth Edition 46
The Bind Function
![Page 47: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/47.jpg)
I/O Functions
When a Jess program requires input from the user of a program, a read function can be used to provide input from the keyboard:
Expert Systems: Principles and Programming, Fourth Edition 47
![Page 48: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/48.jpg)
The read function can only input a single field at a time.
Characters entered after the first field up to the are discarded.
To input, say a first and last name, they must be delimited with quotes, “xxx xxx”.
Data must be followed by a carriage return to be read.
Expert Systems: Principles and Programming, Fourth Edition 48
Read Function from Keyboard
![Page 49: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/49.jpg)
Input can also come from external files. Output can be directed to external files. Before a file can be accessed, it must be opened
using the open function:
Example:(open “mydata.dat” data “r”)
mydata.dat – is the name of the file (path can also be provided)
Expert Systems: Principles and Programming, Fourth Edition 49
I/O from/to Files
![Page 50: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/50.jpg)
data – is the logical name that Jess associates with the file
“r” – represents the mode – how the file will be used – here read access
The open function acts as a predicate function
◦ Returns true if the file was successfully opened◦ Returns false otherwise
Expert Systems: Principles and Programming, Fourth Edition 50
I/O from/to Files
![Page 51: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/51.jpg)
Expert Systems: Principles and Programming, Fourth Edition 51
Table 8.2 File Access Modes
![Page 52: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/52.jpg)
Once access to the file is no longer needed, it should be closed.
Failure to close a file may result in loss of information.
General format of the close function:(close [<file-ID>])
(close data) example
Expert Systems: Principles and Programming, Fourth Edition 52
Close Function
![Page 53: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/53.jpg)
Reading / Writing to a File
Which logical name used, depends on where information will be written – logical name t refers to the terminal (standard output device).
Expert Systems: Principles and Programming, Fourth Edition 53
![Page 54: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/54.jpg)
A predicate function is defined to be any function that returns:◦ TRUE◦ FALSE
Any value other than FALSE is considered TRUE.
We say the predicate function returns a Boolean value.
Expert Systems: Principles and Programming, Fourth Edition 54
Predicate Functions
![Page 55: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/55.jpg)
Processing of information often requires a loop. Sometimes a loop needs to terminate
automatically as the result of an arbitrary expression.
The test condition provides a powerful way to evaluate expressions on the LHS of a rule.
Rather than pattern matching against a fact in a fact list, the test CE evaluates an expression – outermost function must be a predicate function.
Expert Systems: Principles and Programming, Fourth Edition 55
The Test Conditional Element
![Page 56: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/56.jpg)
In JESS you can drop “test” term from your test conditions.
A rule will be triggered only if all its test CEs are satisfied along with other patterns.
(<predicate-function>)
Example:
(> ?value 1)
Expert Systems: Principles and Programming, Fourth Edition 56
Test Condition
![Page 57: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/57.jpg)
The OR Conditional Element
Consider the two rules:
Expert Systems: Principles and Programming, Fourth Edition 57
![Page 58: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/58.jpg)
OR Conditional Element
These two rules can be combined into one rule – or CE requires only one CE be satisfied:
Expert Systems: Principles and Programming, Fourth Edition 58
![Page 59: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/59.jpg)
The And Conditional Element
The and CE is opposite in concept to the or CE – requiring all the CEs be satisfied:
Expert Systems: Principles and Programming, Fourth Edition 59
![Page 60: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/60.jpg)
Not Conditional Element
When it is advantageous to activate a rule based on the absence of a particular fact in the list, Jess allows the specification of the absence of the fact in the LHS of a rule using the not conditional element:
Expert Systems: Principles and Programming, Fourth Edition 60
![Page 61: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/61.jpg)
Not Conditional
We can implement this as follows:
Expert Systems: Principles and Programming, Fourth Edition 61
![Page 62: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/62.jpg)
Jess provides slot attributes which can be specified when deftemplate slots are defined.
Slot attributes provide strong typing and constraint checking.
One can define the allowed types that can be stored in a slot, range of numeric values.
Multislots can specify min / max numbers of fields they can contain.
Default attributes can be provided for slots not specified in an assert command.
Expert Systems: Principles and Programming, Fourth Edition 62
Deftemplate Attributes
![Page 63: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/63.jpg)
Defines the data types can be placed in a slot
Example:
(deftemplate person(multislot name (type SYMBOL))(SLOT AGE (TYPE integer)))
Once defined, Jess will enforce these restrictions on the slot attributes
name – must store symbolsage – must store integers
Expert Systems: Principles and Programming, Fourth Edition 63
Type Attribute
![Page 64: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/64.jpg)
Allowed Value Attributes
Jess allows one to specify a list of allowed values for a specific type – 8 are provided:
Symbols Strings
Lexemes Integers
Floats Numbers
Instance-names Values
Expert Systems: Principles and Programming, Fourth Edition 64
![Page 65: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/65.jpg)
This attribute allows the specification of minimum and maximum numeric values.
Example:(deftemplate person(multislot name (type SYMBOL))(slot age (type INTEGER) (range 0 ?VARIABLE)))
Expert Systems: Principles and Programming, Fourth Edition 65
Range Attributes
![Page 66: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/66.jpg)
Jess provides two explicit techniques for controlling the execution of rules:◦ Salience◦ Modules
Salience allows the priority of rules to be explicitly specified.
The agenda acts like a stack (LIFO) – most recent activation placed on the agenda being first to fire.
Expert Systems: Principles and Programming, Fourth Edition 66
Salience
![Page 67: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/67.jpg)
Salience allows more important rules to stay at the top of the agenda, regardless of when they were added.
Lower salience rules are pushed lower on the agenda; higher salience rules are higher.
Salience is set using numeric values in the range -10,000 +10,000 – zero is intermediate priority.
Salience can be used to force rules to fire in a sequential fashion.
Expert Systems: Principles and Programming, Fourth Edition 67
Salience
![Page 68: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/68.jpg)
Rules of equal salience, activated by different patterns are prioritized based on the stack order of facts.
If 2+ rules with same salience are activated by the same fact, no guarantee about the order in which they will be place on the agenda.
Expert Systems: Principles and Programming, Fourth Edition 68
Salience
![Page 69: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/69.jpg)
Expert Systems: Principles and Programming, Fourth Edition 69
Phases and Control Facts
![Page 70: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/70.jpg)
Expert Systems: Principles and Programming, Fourth Edition 70
Figure 9.2 Assignment of Salience for Different Phases
![Page 71: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/71.jpg)
Approaches:1. Embed the control knowledge directly into the rules.
Example:
Detection rules would include rules indicating when the isolation phase should be entered. Each group of rules would be given a pattern indicating in which phase it would be applicable.
Expert Systems: Principles and Programming, Fourth Edition 71
Implementation of System
![Page 72: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/72.jpg)
2. Use salience to organize the rules.
3. Separate the control knowledge from the domain knowledge. Each rule is given a control pattern that indicates its applicable phase. Control rules are then written to transfer control between the different phases.
Expert Systems: Principles and Programming, Fourth Edition 72
Implementation
![Page 73: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/73.jpg)
Salience Hierarchy
Salience hierarchy is a description of the salience values used by an expert system.
Each level corresponds to a specific set of rules whose members are all given the same salience.
When rules for detection / isolation / recovery are zero, salience hierarchy is:
Expert Systems: Principles and Programming, Fourth Edition 73
![Page 74: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/74.jpg)
Because salience is such a powerful tool, allowing explicit control over execution, there is potential for misuse.
Well-designed rule-based programs should allow inference engine to control firings in an optimal manner.
Salience should be used to determine the order when rules fire, not for selecting a single rule from a group of rules when patterns can control criteria for selection.
Expert Systems: Principles and Programming, Fourth Edition 74
Misuse of Salience
![Page 75: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/75.jpg)
No more than seven salience values should ever be required for coding an expert system – bested limited to 3 – 4.
For large expert systems, programmers should use modules to control the flow of execution – limited to 2 – 3 salience values.
75
Rule of Thumb
![Page 76: To build a knowledge base, Jess must read input from keyboard / files to execute commands and load programs. During the execution process, Jess groups.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d615503460f94a43bbf/html5/thumbnails/76.jpg)
For information on modules, please refer to the “Jess Tutorial” handout.
Expert Systems: Principles and Programming, Fourth Edition 76
Modules