====== CLIPS ===== Manual for Clips is available at [[http://clipsrules.sourceforge.net/|Clips homepage]] ===== Introduction ===== ====Expert systems==== // An intelligent computer program that uses knowledge and inference procedures to solve problems that are difficult enough to require significant human expertise for their solutions. // --Prof. Edward Feigenbauma, Stanford University In particular, expers systems can be compared to a program that simulates work of an human expert in decision making tasks. An Expert is a person who has very specialist knowledge, unavailable for majority of people. An Expert can solve a problem that most of people would not be able to solve, or would not do it effectively enough. ====CLIPS==== Experts systems using Clips can be created in two ways: * in console (extremally inconvinient) * in a file, which is later loaded into the Clips interpreter with //load// command. An exerise is aimed at creating simple calculator for creditworthiness. **Rules presented below are fictional** Creditworthiness will be defined as an ability of a borrower to pay current debt in a timely manner. ===Tips & Tricks=== * CLIPS inherited partially syntax from LISP, hence all commands have to be ebraced wtih parentheses. * To start Clips console, type in the Unix console clips * To exit Clips type (exit) * To **load a file** with rules/facts type (load filename) (reset) Command //(reset)// loads facts defined with **deffacts** (See sectopn[[#Facts]]). * To **clear knowledge base** type (clear) * To **run inference** process type (run) * To launch //help//, type (help) * **Comments** in Clips are denoted with ; (semicolon) * It is common that Clips files have **.clp** extension. ==== Facts ==== - Facts are defined with **deffacts** command. For example to add several facts that would describe person name, last name, age, etc. following code should be placed in a file or typed in the Clips console: (deffacts my_facts "comment" (name jan) (surname kowalski) (age 24)) To add defined facts the //(reset))// comman has to be invoked. **Attention** Facts within **deffacts** are not connected with each other in any way. In an example from above to a fact base three independent facts would be added: //(name jan)//, //(surname kowalski)// and //(age 24)//. - Facts can be added to a knowledge base with //assert// command: (assert (age 23)) The //assert// command would add a fact immediatelly, howewer after invoking //(reset)// command, only facts defined with //deffact// would remain and those added with //assert// will be lost. - Facts can consist of one or more parts: (deffacts myFacts (year 2010) (person jan kowalski 45) (informatyka stosowana)) - Removal and modification of facts is available with **retract** and **modify** commands - To list all facts from knowledge base, following command should be used:(facts) ===Exercise 1=== Using **deffacts**, define facts that would represent persoal data such as name, surname, age, salary. Load created file into the Clips, and list all files. ==== Rules ==== - To define rules **defrule** keyword is used. To add arule which will print //Hello world// message, if there is //name jan// fact in fact base, following code should be used:(defrule rule-1 "Comment" (name jan) => (printout t "Hello World!" crlf)) - If there is more than one fact in a knowledge base that starts with //name// we may want to print a name of the person to whom the salutation is addressed. In this case, the rule form example above should look like that:(defrule rule-1 "Comment" (name ?value) => (printout t "Hello World!" ?value crlf)) - To check if a value is equal to, less than, or greater than the other value a //test// functino is used. For exampel to check if the age of a person is less than 18 following rule should be defined:(defrule rule-1 "Comment" (name ?imie) (age ?age) (test (< ?age 18)) => (printout t "Hi " ?imie ". Your not supposed to be here!" crlf)) ===Exercise 2=== Define two rules that would describe weather a person can be a potential debtor. If an age of a peerson is less than 18 it can not be, if it is geater and the person's salary exceedes 1500$ - a credit can be granted. After the inference process is run, "Credit granted" or "Credit not granted" message should be printed in the screen. ==== Templates ==== A fat can contain any number of elements. Both following facts are valid: (assert (name szymon)) (assert (person with a name szymon)) Aobove notation is not convinient for facts that sould represent more complex data. for more complex facts, **deftemplate** lub **defclass** constructs are used:(deftemplate person (slot name) (slot surname) (slot age)) It is possible to acces some of the slots ommiting other:(assert (person (name jan) (surname kowalski) (age 50)) (defrule rule "" (person (age ?value)) (test (>= 18 ?value)) => (printout t "Hello!" crlf)) ===Exercise 3=== Define a temaplate //person// which would store data defined in previous exercises. Modify defined rules to work with template data. ==== Modules ==== Modules in Clips are used fro several reasons - They help to manage large knowledge bases by groupping rules into smaller sets - They improve efficiency (for each module, a separate RETE network is created) - They allow for simple inference control (//focus// command) ===defmodule=== A module is defined with a **defmodule** construct: (defmodule myModule) To define that some rule belongs to some module a**::** operator is used (it is simiellar to known from C++ scope resolution operator):(defrule myModule::rule "" => (printout t "---- Rule in myModule ----" crlf) ===import and export=== While defining modules it can be defined if facts added by rules/templates/classes that belongs to the module should be visible outside its scope:(defmodule myModule (export ?ALL)) For exported data to be visible inside other module, it has to be specified which data and from which module should be **imported**::(defmodule myModule (export ?ALL) (defmodule myOtherModule (import myModule ?ALL)) All data not explicetely assign to any module by defauld belongs to a MAIN module. It is required sometimes to redefine MAIN module to export all data, so it can be imported in other modules:(defmodule MAIN (export ?ALL)) ===focus=== it is possible to define order in which defined modules will be processed by an inference engine. - An arbitrary order can be defined:(defrule MAIN::startrule "" => (printout t "---- STARTING ----" crlf) (focus salutation greeting userMessage) ; inference control ) - Modules can be switched in Right Hand Side (RHS) of rule:(defrule MAIN::reugla "" (osoba (wiek ?value)) (test (>= 18 ?value)) => (printout t "I'm switching from MAIN to adoult" crlf) (focus adoult)) ===Exercise 4=== Define three modules: //PrivateCapability// //FinancialCapability// and //FinalCapability//. Add facts //privCap, finCap, endCap// that would be set in aforementioned modules. ==Module PrivateCapability== //privCap// should be calculated based on table below: ^Age ^ Number of famili members ^ privCap ^ | <0 ; 17> |ANY | 0 | | <18 ; 24> |<0; 2> | 3 | | <18 ; 24> |<3 ; 4> | 2| | <18 ; 24> |<4 ; inf> | 0| | <25 ; 65> |<0; 2> | 5| | <25 ; 65> |<3 ; 4> | 3| | <25 ; 65> |<4 ; oo> | 1| | <66 ; 75> |<0; 2> | 4| | <66 ; 75> |<3 ; 4> | 3| | <66 ; 75> |<4 ; inf> | 2| | <75 ; oo> |ANY | 0 | ==Module FinancialCapability== //finCap// should be calculated based on table below: ^Salary ^ Type of contract ^ Other credits^ finCap ^ |<0; 1000>| ANY | ANY | 0| |<1001; 2000> | oprace | nie | 2| |<1001; 2000> | oprace | tak | 1| |<1001; 2000> | inne | ANY | 0| |<2001; 6000> | oprace | nie | 4| |<2001; 6000> | oprace | tak | 2| |<2001; 6000> | inne | ANY | 1| |<6001; 30000> | oprace | nie | 6| |<6001; 30000> | oprace | tak | 4| |<6001; 30000> | inne | ANY | 2| |<30001; oo> | ANY | ANY | 6| ==Module FinalCapability== //endCap// should be calculated according to the following equation: (zdpriv*2 + zdfin*5)*1000. Modules should be fired in following order (use //focus//): - PrivateCapability - FinancialCapability - FinalCapability ==== User interface === To read data from the jeyboard a **(read)** command is used: defrule regula "" => (printout t "Podaj swoj wiek: ") (bind ?wiek (read)) (printout t "Podaj swoje imie: ") (bind ?imie (read)) (printout t "Podaj swoje nazwisko: ") (bind ?nazwisko (read)) (assert(osoba(imie ?imie)(nazwisko ?nazwisko)(wiek ?wiek)))) Reading data from the user can be done only in RHS of the rule. ===Exercise 5=== Modify program from previous exercise by providing user interface that would allow user to type his famili status, age, salary. After that an inference process should be fired and information about the amount of credid allowd for the user printed. ===== UWAGI ===== * kilka słów wprowadzenia teoretycznego * więcej przykładów "za ršczkę" * w materiałach wykłšd w pdf do pobrania ===== Komentarze ===== Z braku lepszego miejsca tutaj studenci wpisujš komentarze natury ogólnej do tego lab. 8-)