Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1260847
  • 博文数量: 135
  • 博客积分: 10588
  • 博客等级: 上将
  • 技术积分: 1325
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-18 11:12
文章分类

全部博文(135)

文章存档

2013年(6)

2012年(3)

2011年(11)

2010年(7)

2009年(14)

2008年(6)

2007年(42)

2006年(46)

分类:

2006-09-18 10:29:28

三.Jess语言基础介绍jess语言体系里面几个重要的概念

2.1 原子,变量和函数

       一个jess程序由原子(或符号,其实是我们平时所说的常量,想想为什么叫原子呢?),数字,字符串组成。专用原子有nilTRUEFALSE。一个基本的句子单元称为listJess中所有形式都是通过list实现的函数调用的形式。例如:(colour orange) (+ 4 5 6)

    也可以用变量来替换原子。

(在命令行下输入:java jess.Main 可以进入jess〉这个界面)

Jess> (bind ?name “Terry”)

“Terry”

Jess> ?name

“Terry”

自定义函数可以用deffunction这一关键字

2.2 知识库

    有两类事实,一类叫ordered facts例如:(shopping-list eggs milk bread)

很清楚易懂把。最前面那个叫事实名或者种类。还有一类是unordered facts : 由称为slot的数据字段组成。例如 (automobile (make Ford) (model Explorer) (year 1999))

Unordered facts需要用 deftemplate 来创建。

Jess> (deftemplate automobile

"A specific car."

(slot make)

(slot model)

(slot year (type INTEGER)))

我们可以用assert函数将事实加入到知识库中,然后用facts命令显示知识库的内容。例如:Jess> (assert (automobile (make Chrysler) (model LeBaron) (year 1997)))

Jess> (facts)

f-0 (automobile (make Chrysler)(model LeBaron)(year 1997)(color white))

For a total of 1 facts.

2.3 规则

规则类似于if then 语句,他也是用defrule来构建的。例:

Jess> (defrule switch-on-kettle

"If kettle is not on, switch it on.如果罐子没打开,则打开它"

(kettle-is-off)

=>

(switch-on-kettle))

这里(kettle-is-off) 称为模式 pattern,它由事实(facts)组成。,(=>) 后是动作action (switch-on-kettle),是一个函数调用。

我们可以使用逻辑运算符AND OR来构建更复杂的规则。

2.4 例子:根据输入的轮子的个数,及是否有引擎等信息推导出是汽车还是自行车的例子,是不是很傻?程序节选如下:

(deffacts initial-data

(assert (printout t "Enter number of wheels: "))

(number-of-wheels (read))

(assert (printout t "Does it have an engine [yes/no]: "))

(has-engine (read))

(assert (printout t "Enter number of doors: "))

(number-of-doors (read))

(assert (printout t "Enter size of vehicle [large/medium/small]: "))

(vehicle-size (read))

)

(defrule bicycle

(vehicleType cycle)

(number-of-wheels 2)

(has-engine no)

=>

(assert (vehicle bicycle))

(printout t "Vehicle is a bicycle"))

...

(watch all)

(reset)

(run)

 

阅读(2895) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~