大三知识工程chapter_第1页
大三知识工程chapter_第2页
大三知识工程chapter_第3页
大三知识工程chapter_第4页
大三知识工程chapter_第5页
已阅读5页,还剩46页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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它支持基于规则的、面向对象的和面向过程的编程Syntactically, CLIPS resembles:EclipseCLIPS/R2JESS在语法上,CLIPS与Eclipse、CLIPS/R2、JESS语言规则极为相似。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 内容规定的一类值要替换掉 is to be made.* following a description means that the description can be replaced by zero or more occurrences of the specified value.表示语句可以用规定的值替换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.该描述所规定的一个或多个值应该被用来代替这个语句描述+ 等价于 *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 数字字段The floats and integers make up the numeric fields simply numbers.Integers have only a sign and digits 数字.Floats have a decimal 小数 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、由用户自定义函数返回的外部数据结构的地址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.事实被创建之前,必须告知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、。其他不会与现有事实重复的事实可以很容易的增加到事实表。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.事实表的标识符不必连续。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的作用在于启动一个程序的执行。Deffacts ConstructExpert Systems: Principles and Programming, Fourth Edition31The Components of a RuleTo plish work, an expert system must have rules as well as facts.专家系统必须定出规则与事实,事实的声明和撤销已经论述过,因此论述规则的工作原理。Rules can be typed int

28、o CLIPS (or loaded from a file).规则可以直接键入CLIPS或从编辑器创建的规则文件中调入。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规则名Name of the rule fire-emergency可选的注释字符串Optional comment string “An example rule”在规则头之后是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.如果规则的所有模式与事实匹配,则规则被激活并放入议程。The agenda is a collection of activated rules.议程已被激活的规则集合。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.规则的最后一部分是行为列表,当此规则触发时这些行为就会被执行。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.可选参数是要被触发的规则的最大数目。如果没有输入或等于-1,则规则会被触发,直到议程中无规则剩下为止。否则,触发规定的规则个数后,规则的执行就会停止。Expert Systems: Principles and Programming, Fourth Edition37ExecutionWhe

35、n the program runs, the rule with the highest salience on the agenda is fired.当程序运行时,议程中优先级最高的规则会被触发。Rules e 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

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.反射特性是指对于一个特定的事实集合,规则不会触发一次以上。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命令会使该规则再次触发,refresh命令把对于某一规则已经出发的全部激活重新放回到该议程中(条件是,触发此激活的事实仍然还在事实表中),语法结构是(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.(漂亮打印自定义规则、自定义模板和自定义事实)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.删除自定义规则,自定义模板和自定义事实The clear command clears the CLIPS environment and adds the initialfact-defacts to the CLIPS environment.清除CLIPS环境之后,clear命令会把initial-facts自定义事实添加到CLIPS环境。The printout command can also be use

41、d to print information.(printout *)Expert Systems: Principles and Programming, Fourth Edition41使用复合规则(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)事实、然后发出run命令,会产生activate the sprinkler system输出结果。声明(emergency (type flood)事实、然后发出run命令,会产生shut down electrical equipment输出结果。Expert Systems: Principles and Programming, Fourth Edition42多模式的规则规则中可以包括任意多种模式,但认识到重要一点,只有所有模式与事实匹配,该规则才被置于议程中。这种限制叫做与条件元素,因为所有规则的模式都隐含的包含在一个and条件元素中,因此,如果只有一个模式

43、得到匹配,该规则不会触发。在一条规则的LHS得到匹配,该规则置于议程之前,所有事实必须存在。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 “-”.使用模式约束操作符“-”在规则的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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论