大三知識工程chapter_第1頁
大三知識工程chapter_第2頁
大三知識工程chapter_第3頁
大三知識工程chapter_第4頁
大三知識工程chapter_第5頁
已閱讀5頁,還剩46頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Chapter 7:Introduction to CLIPSExpert Systems: Principles and Programming, Fourth EditionExpert Systems: Principles and Programming, Fourth Edition2ObjectivesLearn what type of language CLIPS isStudy the notation (syntax語法) used by CLIPSLearn the meaning of a field and what types exitLearn how to la

2、unch and exit from CLIPSLearn how to represent, add增加, remove刪除, modified修改, and duplicated復制 in CLIPSExpert Systems: Principles and Programming, Fourth Edition3ObjectivesLearn how to debug programs using the watch commandLearn how to use the deffacts construct to define a group of facts 自定義事實結構Lear

3、n how to use the agenda command and execute CLIPS programsLearn about commands that can manipulate constructs 結構Expert Systems: Principles and Programming, Fourth Edition4ObjectivesLearn how to use the printout commandLearn how to use multiple rulesLearn how to use the set-break command 設置斷點命令Learn

4、how to use the load and save constructsLearn how to use variables變量, single and multifield wildcards, and comment constructs 單字段通配符和塊世界Expert Systems: Principles and Programming, Fourth Edition5What is CLIPS?CLIPS is a multi-paradigm多范例 programming language that provides support for:Rule-basedObject

5、-orientedProcedural programming它支持基于規(guī)則的、面向對象的和面向過程的編程Syntactically, CLIPS resembles:EclipseCLIPS/R2JESS在語法上,CLIPS與Eclipse、CLIPS/R2、JESS語言規(guī)則極為相似。Expert Systems: Principles and Programming, Fourth Edition6CLIPS CharacteristicsCLIPS is an acronym首字母的縮寫 for C Language Integrated Production System.CLIPS

6、was designed using the C language at the NASA/Johnson Space Center.美國航空航天局/約翰遜太空中心CLIPS is portable PC CRAY.Expert Systems: Principles and Programming, Fourth Edition7CLIPS Notation記號Symbols other than those delimited by , , or should be typed exactly as shown. mean the contents are optional 可選 and

7、mean that a replacement 內容規(guī)定的一類值要替換掉 is to be made.* following a description means that the description can be replaced by zero or more occurrences of the specified value.表示語句可以用規(guī)定的值替換0次或更多次。Expert Systems: Principles and Programming, Fourth Edition8CLIPS NotationDescriptions followed by + mean that

8、 one or more values specified by description should be used in place of the syntax description.該描述所規(guī)定的一個或多個值應該被用來代替這個語句描述+ 等價于 *A vertical bar | indicates a choice among one or more of the items separated by the bars.表示由此豎線分開的一個或多個選項中選擇一項。all | none |some 可替換為all或none或someExpert Systems: Principles

9、and Programming, Fourth Edition9Fields 字段To build a knowledge base, CLIPS must read input from keyboard / files to execute commands and load programs.During the execution process, CLIPS groups symbols together into tokens 把字符組合起來形成標記 groups of characters that have the same meaning.A field is a speci

10、al type of token of which there are 8 types.(浮點型、整型、符號型、字符串型、外部地址、事實地址、實例名和實力地址)Expert Systems: Principles and Programming, Fourth Edition10Numeric Fields 數(shù)字字段The floats and integers make up the numeric fields simply numbers.Integers have only a sign and digits 數(shù)字.Floats have a decimal 小數(shù) and possib

11、ly “e” for scientific notation.Expert Systems: Principles and Programming, Fourth Edition11Symbol Fields 符號字段Symbols begin with printable ASCII characters followed by zero or more characters, followed by a delimiter 分隔符 .符號是一種字段,它以可打印的ASCII字符開頭、后接零個或多個字符。符號以分界符為結尾,分界符包括任何非打印ASCII字符(包括空格、tab、回車和換行,et

12、c)CLIPS is case sensitive. 大小寫敏感e.g. Space-Station 345bExpert Systems: Principles and Programming, Fourth Edition12String Fields 字符串字段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 precedin

13、g前 the character with a backslash反斜杠符號. 要在一個字符串里包含雙引號,可以用反斜杠“ “ single-token” ”“ “ single-token”“ single-token ”Expert Systems: Principles and Programming, Fourth Edition13Address FieldsExternal addresses represent the address of an external data structure returned by a user-defined function. 外部地址代表

14、由用戶自定義函數(shù)返回的外部數(shù)據(jù)結構的地址Fact address fields are used to refer to a specific fact. 事實地址用來指向一個特定的事實Expert Systems: Principles and Programming, Fourth Edition14Entering / Exiting CLIPSThe CLIPS prompt is: CLIPSThis is the type-level mode where commands can be entered.To exit CLIPS, one types: CLIPS (exit)

15、CLIPS will accept input from the user / evaluate it / return an appropriate response:CLIPS (+ 3 4) value 7 would be returned.Expert Systems: Principles and Programming, Fourth Edition15Facts and CLIPSTo solve a problem, CLIPS must have data or information with which to reason 以便推理.Each chunk of info

16、rmation is called a fact.每個信息塊被稱為事實。Facts consist of: 事實由關系名+若干個槽Relation name (symbolic field) Zero or more slots w/associated valuesExpert Systems: Principles and Programming, Fourth Edition16Example Fact in CLIPSExpert Systems: Principles and Programming, Fourth Edition17Deftemplate 自定義模板結構Before

17、 facts can be constructed, CLIPS must be informed of the list of valid slots for a given relation name.事實被創(chuàng)建之前,必須告知CLIPS一個給定關系名的合法槽的列表。A deftemplate is used to describe groups of facts sharing the same relation name and contain common information.共享相同的關系名和包含共同信息的幾組事實可以利用自定義模板結構來描述。Expert Systems: Pr

18、inciples and Programming, Fourth Edition18Deftemplate General FormatExpert Systems: Principles and Programming, Fourth Edition19Deftemplate vs. Ordered Facts有序事實Facts with a relation name defined using deftemplate are called deftemplate facts.有關系名且有一相應自定義模板的事實稱為自定義模板事實Facts with a relation name that

19、 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 Edition20隱式自定義模板顯式自定義模板結構自定義模板Expert Systems

20、: Principles and Programming, Fourth Edition21Adding FactsCLIPS 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 Edition22Displaying FactsCLIPS (facts) 事實標識符CLIPS不接受一個事實的重復輸入,視圖輸入第二個具有相同槽值事實到事實表是不會產生結果的

21、。其他不會與現(xiàn)有事實重復的事實可以很容易的增加到事實表。Expert Systems: Principles and Programming, Fourth Edition23Removing FactsJust as facts can be added, they can also be removed.Removing facts results in gaps in the fact identifier list.事實表的標識符不必連續(xù)。To remove a fact: CLIPS (retract 2) Expert Systems: Principles and Program

22、ming, Fourth Edition24Modifying FactsSlot values of deftemplate facts can be modified using the modify command:Expert Systems: Principles and Programming, Fourth Edition25Results of ModificationA new fact index is generated because when a fact is modified:The original fact is retractedThe modified f

23、act is asserted修改命令通過撤銷原事實、然后用修改過的指定槽值聲明一個新事實來完成操作,因為這一點CLIPS會為一個被修改的事實產生一個新的事實索引。The duplicate command is similar to the modify command, except it does not retract the original fact. Duplicate不撤銷原事實。Expert Systems: Principles and Programming, Fourth Edition26duplicateModify和duplicate不能用于有序事實。Expert

24、 Systems: Principles and Programming, Fourth Edition27Watch CommandThe watch command is useful for debugging purposes.If facts are “watched”, CLIPS will automatically print a message indicating an update has been made to the fact list whenever either of the following has been made:AssertionRetractio

25、nExpert Systems: Principles and Programming, Fourth Edition28事實正在被撤銷事實正在被聲明Expert Systems: Principles and Programming, Fourth Edition29Deffacts Construct自動聲明一組事實,而不用在頂層鍵入相同的聲明信息,特別適合在程序運行前已知是正確的事實(即初始知識)的情況。The deffacts construct can be used to assert a group of facts.Groups of facts representing kn

26、owledge can be defined as follows:(deffacts optional comment * )The reset command is used to assert the facts in a deffacts statement.Expert Systems: Principles and Programming, Fourth Edition30Reset命令產生的一個新事實,稱之為初始事實,一旦啟動,CLIPS自動定義以下兩個結構:(deftemplate initial-fact)(deffacts initial-fact (initial-fac

27、t)Reset命令會聲明事實(initial-fact),此初始事實的標識符總是f-0。initial-fact的作用在于啟動一個程序的執(zhí)行。Deffacts ConstructExpert Systems: Principles and Programming, Fourth Edition31The Components of a RuleTo plish work, an expert system must have rules as well as facts.專家系統(tǒng)必須定出規(guī)則與事實,事實的聲明和撤銷已經論述過,因此論述規(guī)則的工作原理。Rules can be typed int

28、o CLIPS (or loaded from a file).規(guī)則可以直接鍵入CLIPS或從編輯器創(chuàng)建的規(guī)則文件中調入。Consider the pseudocode for a possible rule:IF the emergency is a fireTHEN the response is to activate the sprinkler systemExpert Systems: Principles and Programming, Fourth Edition32Rule ComponentsFirst, we need to create the deftemplate

29、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 Edition33Rule

30、 ComponentsThe rule would be shown as follows:;Rule header(defrule fire-emergency An example rule“ ;Patterns(emergency (type fire) ;Then arrow= ;Actions(assert (response(action activate-sprinkler-system)Expert Systems: Principles and Programming, Fourth Edition34Analysis of the RuleThe header of the

31、 rule consists of three parts:Keyword defrule規(guī)則名Name of the rule fire-emergency可選的注釋字符串Optional comment string “An example rule”在規(guī)則頭之后是0個或多個條件元素 After the rule header are 1+ conditional elements pattern CEs 最簡單的條件元素是模式條件元素。Each pattern consists of 1+ constraints intended to match the fields of the d

32、eftemplate fact.每一個模式由一個或多個約束構成,其目的是匹配自定義模板事實中的字段。Expert Systems: Principles and Programming, Fourth Edition35Analysis of RuleIf all the patterns of a rule match facts, the rule is activated and put on the agenda.如果規(guī)則的所有模式與事實匹配,則規(guī)則被激活并放入議程。The agenda is a collection of activated rules.議程已被激活的規(guī)則集合。Th

33、e 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.規(guī)則的最后一部分是行為列表,當此規(guī)則觸發(fā)時這些行為就會被執(zhí)行。Expert Systems: Principles and Programming, Fourth Edition36The Agenda and ExecutionTo run the CLIPS program,

34、use the run command:CLIPS (run )- the optional argument is the maximum number of rules to be fired if omitted, rules will fire until the agenda is empty.可選參數(shù)是要被觸發(fā)的規(guī)則的最大數(shù)目。如果沒有輸入或等于-1,則規(guī)則會被觸發(fā),直到議程中無規(guī)則剩下為止。否則,觸發(fā)規(guī)定的規(guī)則個數(shù)后,規(guī)則的執(zhí)行就會停止。Expert Systems: Principles and Programming, Fourth Edition37ExecutionWhe

35、n the program runs, the rule with the highest salience on the agenda is fired.當程序運行時,議程中優(yōu)先級最高的規(guī)則會被觸發(fā)。Rules e activated whenever all the patterns of the rule are matched by facts.當規(guī)則的全部模式與事實相匹配,規(guī)則即變?yōu)榧せ顮顟B(tài)。The reset command is the key method for starting or restarting .Facts asserted by a reset satisfy

36、 the patterns of one or more rules and place activation of these rules on the agenda.Expert Systems: Principles and Programming, Fourth Edition38What is on the Agenda?To display the rules on the agenda, use the agenda command:CLIPS (agenda) Refraction is the property that rules will not fire more th

37、an once for a specific set of facts.反射特性是指對于一個特定的事實集合,規(guī)則不會觸發(fā)一次以上。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. refresh命令會使該規(guī)則再次觸發(fā),refresh命令把對于某一規(guī)則已經出發(fā)的全部激活重新放回到該議程中(條件是,觸發(fā)此激活的事實仍然還在事實表中),語法結構是(refresh )Expe

38、rt Systems: Principles and Programming, Fourth Edition39Command for Manipulating ConstructsThe list-defrules command is used to display the current list of rules maintained by CLIPS.The list-deftemplates displays the current list of deftemplates.顯示當前自定義模板列表The list-deffacts command displays the curr

39、ent list of deffacts.顯示當前自定義事實表The ppdefrule, ppdeftemplate and ppdeffacts commands display the text representations of a defrule, deftemplate, and a deffact, respectively.(漂亮打印自定義規(guī)則、自定義模板和自定義事實)Expert Systems: Principles and Programming, Fourth Edition40CommandsThe undefrule, undeftemplate, and und

40、effacts commands are used to delete a defrule, a deftemplate, and a deffact, respectively.刪除自定義規(guī)則,自定義模板和自定義事實The clear command clears the CLIPS environment and adds the initialfact-defacts to the CLIPS environment.清除CLIPS環(huán)境之后,clear命令會把initial-facts自定義事實添加到CLIPS環(huán)境。The printout command can also be use

41、d to print information.(printout *)Expert Systems: Principles and Programming, Fourth Edition41使用復合規(guī)則(defrule fire-emergency(emergency (type fire)=(printout t activate the sprinkler systemcrlf)(defrule flood-emergency(emergency (type flood)=(printout t shut down electrical equipmentcrlf)聲明(emergency

42、 (type fire)事實、然后發(fā)出run命令,會產生activate the sprinkler system輸出結果。聲明(emergency (type flood)事實、然后發(fā)出run命令,會產生shut down electrical equipment輸出結果。Expert Systems: Principles and Programming, Fourth Edition42多模式的規(guī)則規(guī)則中可以包括任意多種模式,但認識到重要一點,只有所有模式與事實匹配,該規(guī)則才被置于議程中。這種限制叫做與條件元素,因為所有規(guī)則的模式都隱含的包含在一個and條件元素中,因此,如果只有一個模式

43、得到匹配,該規(guī)則不會觸發(fā)。在一條規(guī)則的LHS得到匹配,該規(guī)則置于議程之前,所有事實必須存在。Expert Systems: Principles and Programming, Fourth Edition43Other CommandsSet-break allows execution to be halted 停止before any rule from a specified group of rules is fired.Load allows loading of rules from an external file.Save opposite of load, allows

44、saving of constructs to diskExpert Systems: Principles and Programming, Fourth Edition44Commenting and VariablesComments注釋 provide a good way to document programs to explain what constructs are doing.Variables變量 store values, syntax requires preceding with a question mark (?) 語法上是由一個問號后接一個標識字段名組成。Ex

45、pert Systems: Principles and Programming, Fourth Edition45?(clear)(deftemplate person(slot name)(slot eyes)(slot hair)(defrule find-blue-eyes(person (name ?name) (eyes blue)=(printout t ?name has blue eyes. crlf)(deffacts people(person (name Jane) (eyes blue) (hair red)(person (name Joe) (eyes green

46、) (hair brown)(person (name Jack) (eyes blue) (hair black)(person (name Jeff) (eyes green) (hair brown)(reset)(run)Expert Systems: Principles and Programming, Fourth Edition46Fact AddressesA 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 “-”.使用模式約束操作符“-”在規(guī)則的LHS將某個變量約束到匹配某個模式的事實地址上,該變量約束后,可以在事實指針處和撤銷、修改或復制命令一起使用。Expert Systems: Principles and Programming, Fourth Edition47模式約束操作符Expert Systems: Principles and Programming, Fourth Edition48Single-Field Wildcards, and Multifield

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論