JESS Java Expert System Shall Defining functions Artificial

  • Slides: 51
Download presentation
JESS (Java Expert System Shall) Defining functions Artificial Intelligence

JESS (Java Expert System Shall) Defining functions Artificial Intelligence

List n The basic unit of structure in Jess n A list is (…)

List n The basic unit of structure in Jess n A list is (…) enclosing ≥ 0 symbols, numbers, strings, or other lists n No commas between elements (+ 3 2) (a b c) ("Hello, World") () (deftemplate foo (slot bar))

Calling Function n n All code in Jess is in the form of a

Calling Function n n All code in Jess is in the form of a function call Function calls are lists using prefix notation n The head of the list is the name of the function being called, and the other items in the list are the arguments of the function. (+ 2 3 ) n The arguments of a function call can be numbers, symbols, strings, or other function calls (+ (+ 2 3) (* 3 3)) Every Jess function has a result : there are no void functions!

Defining function n n You can write your own functions in the Jess language

Defining function n n You can write your own functions in the Jess language using the deffunction construct. (deffunction <name> (<parameter>*) [<comment>] <expression>*) (bind ? x (- ? X 1 ? X 2)) (bind ? y (- ? Y 1 ? Y 2)) (bind ? z (- ? Z 1 ? Z 2)) (bind ? distance (sqrt (+ (* ? x) (* ? y) (* ? z)))) Jess> (deffunction distance (? X 1 ? Y 1 ? Z 1 ? X 2 ? Y 2 ? Z 2) "Compute the distance between two points in 3 D space" (bind ? x (- ? X 1 ? X 2)) (bind ? y (- ? Y 1 ? Y 2)) (bind ? z (- ? Z 1 ? Z 2)) (bind ? distance (sqrt (+ (* ? x) (* ? y) (* ? z)))) (return ? distance)) TRUE

Defining function n Example – Jess> (deffunction min ($? args) "Compute the smallest of

Defining function n Example – Jess> (deffunction min ($? args) "Compute the smallest of a list of positive numbers" (bind ? minval (nth$ 1 ? args)) (foreach ? n ? args (if (< ? n ? minval) then (bind ? minval ? n))) (return ? minval)) nth$ TRUE Takes and integer index and a list, Jess> (min 10 100 77 6 43) returns the element at that index 6 Jess list indices start at 1 (base-1) Jess> (nth$ 2 ? list 1) 2

JESS (Java Expert System Shall) Representing facts in Jess Artificial Intelligence

JESS (Java Expert System Shall) Representing facts in Jess Artificial Intelligence

Jess’s working memory n Manipulating the working memory n assert — Adds facts to

Jess’s working memory n Manipulating the working memory n assert — Adds facts to working memory n clear — Clears all of Jess n deffacts — Defines the initial contents of working memory n facts — Displays the contents of working memory n reset — Initializes the working memory n retract — Removes facts from working memory n watch — Tells Jess to print diagnostics when interesting things happen

Jess’s working memory n The watch function n user can use the watch function

Jess’s working memory n The watch function n user can use the watch function to tell Jess to print messages when various interesting things happen. n If you type the expression (watch facts), then you’ll see a message whenever any facts are added or removed. n Example – Jess> (watch facts) TRUE Jess> (reset) ==> f-0 (MAIN: : initial-fact) TRUE Jess> (unwatch facts) TRUE

Jess’s working memory n The facts function n n You can see a list

Jess’s working memory n The facts function n n You can see a list of all the facts in the working memory using the facts function. Example – Jess> (facts) f-0 (MAIN: : initial-fact) For a total of 1 facts.

Jess’s working memory n Creating facts with assert n n New facts are added

Jess’s working memory n Creating facts with assert n n New facts are added to the working memory using the assert function Example – Jess> (reset) TRUE Jess> (assert (groceries milk eggs bread)) <Fact-1> Jess> (facts) f-0 (MAIN: : initial-fact) f-1 (MAIN: : groceries milk eggs bread) For a total of 2 facts.

Jess’s working memory n Removing facts with retract n n You can remove individual

Jess’s working memory n Removing facts with retract n n You can remove individual facts from the working memory using the retract function. Arguments for retract can be numeric fact-ids.

Jess’s working memory n Example – Jess> (facts) f-0 (MAIN: : initial-fact) f-1 (MAIN:

Jess’s working memory n Example – Jess> (facts) f-0 (MAIN: : initial-fact) f-1 (MAIN: : groceries milk eggs bread) For a total of 2 facts. Jess> (retract 1) TRUE Jess> (facts) f-0 (MAIN: : initial-fact) For a total of 1 facts. Jess> (bind ? f (fact-id 0)) <Fact-0> Jess> (retract ? f) TRUE Jess> (facts) For a total of 0 facts.

The deffacts construct n n Jess includes the deffacts construct. The facts in all

The deffacts construct n n Jess includes the deffacts construct. The facts in all existing deffacts are asserted into the working memory whenever the reset function is issued.

The deffacts construct n Example –

The deffacts construct n Example –

ordered facts are unordered facts with a single multislot. ■ ppdeftemplate—Displays a pretty-printed deftemplate

ordered facts are unordered facts with a single multislot. ■ ppdeftemplate—Displays a pretty-printed deftemplate ■ show-deftemplates—Lists all the deftemplates currently defined

ordered facts n Example -

ordered facts n Example -

Unordered facts n Unordered facts are working memory elements that behave like rows in

Unordered facts n Unordered facts are working memory elements that behave like rows in a database table. (person (name "Bob Smith") (age 34) (gender Male)) (automobile (make Ford) (model Explorer) (year 1999)) (box (location kitchen) (contents spatula)) n you have to specify their structure using the deftemplate construct.

Unordered facts n The deftemplate construct n n you have to use the deftemplate

Unordered facts n The deftemplate construct n n you have to use the deftemplate construct to define the slots that kind of fact. Example –

Unordered facts n Default slot values n n you can specify the slots in

Unordered facts n Default slot values n n you can specify the slots in any order, and you don’t have to include every slot. If you omit any slots when you assert an unordered fact, they’re filled in using default values.

Unordered facts n Default slot values n Example -

Unordered facts n Default slot values n Example -

Unordered facts n Default slot values n n You can specify your own default

Unordered facts n Default slot values n n You can specify your own default value by using a slot qualifie Example-

Unordered facts n Multislots n n Sometimes, though, it’s handy to keep a list

Unordered facts n Multislots n n Sometimes, though, it’s handy to keep a list of things in a slot. For example, if you wanted to keep track of a person’s hobbies in a hobbies slot. Example -

Unordered facts n Changing slot values with modify n n You can change the

Unordered facts n Changing slot values with modify n n You can change the values in the slots of an unordered fact using the modify function. Example -

JESS (Java Expert System Shall) Writing rules in jess Artificial Intelligence

JESS (Java Expert System Shall) Writing rules in jess Artificial Intelligence

Forward-chaining rules n n n defrule—Defines a new rule ppdefrule—Pretty-prints a rule run—Begins firing

Forward-chaining rules n n n defrule—Defines a new rule ppdefrule—Pretty-prints a rule run—Begins firing activated rules from the agenda undefrule—Deletes a rule watch rules—Prints a diagnostic when a rule fires watch activations—Prints a diagnostic when a rule is activated

Forward-chaining rules n Defrule n n Rules are uniquely identified by their name. Example

Forward-chaining rules n Defrule n n Rules are uniquely identified by their name. Example -

Forward-chaining rules n Watch n Example-

Forward-chaining rules n Watch n Example-

Forward-chaining rules n ppdefrule n Example –

Forward-chaining rules n ppdefrule n Example –

Forward-chaining rules n Wrong-rule

Forward-chaining rules n Wrong-rule

Forward-chaining rules

Forward-chaining rules

Constraining slot data n Literal constraints n n A pattern including a literal value

Constraining slot data n Literal constraints n n A pattern including a literal value matches only facts that include that value. Example -

Constraining slot data n Variables as constraints n n You can specify a variable

Constraining slot data n Variables as constraints n n You can specify a variable instead of a literal value for any part of the slot data in a pattern. Example -

Constraining slot data n Multifields n add Regular variables match exactly one value. Multifields

Constraining slot data n Multifields n add Regular variables match exactly one value. Multifields can match any number of values

Constraining slot data n Connective constraints n n n Any single constraint preceded by

Constraining slot data n Connective constraints n n n Any single constraint preceded by a tilde (~) matches the opposite of what the constraint would originally have matched. Ampersands (&) represent logical and, and pipes (|) represent logical or Example – (client (city ~Bangor)) (client (items-purchased ? x ~? x)) (client (city Boston|Hartford)) (client (city ? c&~Bangor)) (client (city ~Bangor&Portland))

Constraining slot data n predicate functions n n Literal constraints, variables, and connectives suffice

Constraining slot data n predicate functions n n Literal constraints, variables, and connectives suffice for many situations, but there are some things they can’t express. Example -

Constraining slot data n Pattern bindings n n To use retract, modify, or duplicate

Constraining slot data n Pattern bindings n n To use retract, modify, or duplicate on a fact matched by the LHS of a rule, you need to pass a handle to the fact to the RHS of the rule. To do this, you use a pattern-binding variable Example -

Constraining slot data n The test conditional element n n A pattern with test

Constraining slot data n The test conditional element n n A pattern with test as the head is special; the body consists not of a pattern to match against the working memory but of a Boolean function. Example -

JESS (Java Expert System Shall) Scripting Java with Jess Artificial Intelligence

JESS (Java Expert System Shall) Scripting Java with Jess Artificial Intelligence

Creating Java objects n n n lists are useful, they are not as powerful

Creating Java objects n n n lists are useful, they are not as powerful as the Map and Set containers in Java’s Collections API. Jess’s new function lets you create instances of Java classes. Example – Jess> (bind ? prices (new java. util. Hash. Map)) Jess has an import function you can use to do the same thing Example – Jess> (import java. util. *) TRUE Jess> (bind ? prices (new Hash. Map))

Creating Java objects n n n So far, user used Hash. Map’s default constructor.

Creating Java objects n n n So far, user used Hash. Map’s default constructor. Of course, user can create objects using a class’s other constructors as well. Example. Jess> (bind ? prices (new Hash. Map 20 0. 5)) When you call a Java method, Jess converts the arguments from Jess data types to Java types,

Creating Java objects

Creating Java objects

Creating Java objects

Creating Java objects

Calling Java methods n n you can invoke any of that object’s methods using

Calling Java methods n n you can invoke any of that object’s methods using the call function. Example – Jess> (call ? prices put bread 0. 99) Jess> (call ? prices put peas 1. 99) Jess> (call ? prices put beans 1. 79) Jess> (call ? prices get peas) 1. 99

Calling Java methods n Nesting function calls, and shortcut n compact code can be

Calling Java methods n Nesting function calls, and shortcut n compact code can be readable and efficient. Hashmap = new Hashmap(); Map. put(“bread” new Double(o. 99)); (bind ? prices (new Hash. Map)) (call ? prices put bread 0. 99) ((bind ? prices (new Hash. Map)) put bread 0. 99)

Calling Java methods n Calling static methods n n In both Java and Jess

Calling Java methods n Calling static methods n n In both Java and Jess code, you can use just the name of a Java class to invoke any of its static methods. Example – Jess> (call Thread sleep 1000) (pause for one second) Jess>

Calling Java methods n Calling set and get methods n n Special Java objects

Calling Java methods n Calling set and get methods n n Special Java objects called Java. Beans play an important role in Jess. One of JB is a pair of methods to simplify accessing their data. n public String get. Name() { return name; } public void set. Name(String n) { name = n; } n They are often called accessors and mutators, or getters and setters.

Calling Java methods n Calling set and get methods n Example – Jess> (bind

Calling Java methods n Calling set and get methods n Example – Jess> (bind ? b (new javax. swing. JButton)) <External-Address: javax. swing. JButton> Jess> (? b set. Text "Press Me") ; ; or. . . Jess> (set ? b text "Press Me") Jess> (? b get. Text ) ; ; or. . . "Press Me" Jess> (get ? b text) "Press Me"

Calling Java methods n How Jess chooses among overloaded method n n n When

Calling Java methods n How Jess chooses among overloaded method n n n When you call an overloaded method in Java code, the Java compiler chooses an overload based on the exact compile-time types of the parameters. Jess is much more relaxed about choosing Example – void println() void println(boolean x) void println(char[] x) void println(double x) void println(float x) void println(int x) void println(long x) void println(Object x) void println(String x)

Accessing Java member data n n Some Java classes have public variables Instance variables

Accessing Java member data n n Some Java classes have public variables Instance variables are members of a class that belong to individual objects Jess can access public instance variables of Java objects using the get-member and set-member functions. Example – Jess> (bind ? pt (new java. awt. Point)) <External-Address: java. awt. Point> Jess> (set-member ? pt x 37) 37 Jess> (set-member ? pt y 42) 42 Jess> (get-member ? pt x) 37

Working with exception n Java methods can signal an error by throwing an exception.

Working with exception n Java methods can signal an error by throwing an exception. Jess signals errors in your Jess code and failures in its own functions using exceptions, too. user can do this using the try function.

Working with exception n Example – Jess> (deffunction parse. Int (? string) (try (bind

Working with exception n Example – Jess> (deffunction parse. Int (? string) (try (bind ? i (call Integer parse. Int ? string)) (printout t "The answer is " ? i crlf) catch (printout t "Invalid argument" crlf))) TRUE Jess> (parse. Int "10") The answer is 10 Number. Format. Exception Jess> (parse. Int "l 0") Invalid argument