专家系统作业报告
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
华南农业大学信息学院
动物识别专家系统
一、实验内容
按下列规则,写出一个分类专家系统:
(1)有毛的动物是哺乳类;
(2)有奶的动物是哺乳类;
(3)有羽毛的动物是鸟类;
(4)若动物会飞且会生蛋,则它是鸟类;
(5)吃肉的哺乳类是肉食动物;
(6)犬牙利爪,眼睛向前的是肉食动物;
(7)反刍食物的哺乳类是有蹄类;
(8)有蹄的哺乳类是有蹄类;
(9)黄褐色有暗斑点的肉食类是金钱豹;
(10)黄褐色有黑条纹的肉食类是老虎;
(11)长腿长脖子有黄褐色暗斑的有蹄类是长颈鹿;
(12)有黑白条纹的有蹄类是斑马;
(13)不会飞长腿长脖的鸟是鸵鸟;
(14)不会飞善游泳黑白色的鸟是企鹅;
(15)善飞的鸟是信天翁。
二、实验目的
1.加深对产生式表示法的认识;
2.熟悉数据库的制作与使用。
3. 专家系统的学习与应用
三、实验原理
四、设计过程
1.环境与配置
windows系统,Delphi7.0(可视化程序设计)
2结果如下
3.主要源代码实现过程
//unit2.dfm
unit Unit1;
interface
uses
Windows, Messages, SysUtils, V ariants, Classes, Graphics, Controls, Forms, Dialogs, DB, DBTables, Grids, DBGrids, DBCtrls, StdCtrls, ExtCtrls,
CheckLst;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
Query1: TQuery;
DataSource2: TDataSource;
Table1: TTable;
GroupBox1: TGroupBox;
Panel2: TPanel;
Panel1: TPanel;
Panel3: TPanel;
Splitter1: TSplitter;
Splitter2: TSplitter;
ListBox1: TListBox;
ListBox2: TListBox;
Button1: TButton;
Button3: TButton;
Button5: TButton;
Label1: TLabel;
Label2: TLabel;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
var sql,strWhere:String;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
sql:='SELECT 结论FROM 推导';
with table1 do
begin
first;
while not eof do
begin
ListBox1.Items.Add(fieldbyname('条件').asString);
next;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
i:=0;
while i<Listbox1.Items.Count do
if ListBox1.Selected[i] then
begin
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Delete(i);
end
Else
i:=i+1;
end;
procedure TForm1.Button3Click(Sender: TObject);
var i:integer;
begin
i:=0;
while i<Listbox2.Items.Count do
if ListBox2.Selected[i] then
ListBox2.Items.Delete(i)
Else
i:=i+1;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ListBox2.Items.Clear;
end;
procedure TForm1.Button5Click(Sender: TObject);
var i,tag:integer;
sqlTest,sWtest,sub:String;
begin
i:=0;
strWhere:='';
while i<Listbox2.Items.Count do
begin
if strWhere='' then
begin
sWtest:='WHERE 条件like '+'''%'+ListBox2.Items[i]+'%'' ';
sWtest:=sWtest+'and '+'条件not like ''%不'+ListBox2.Items[i]+'%'' ';
sqlTest:=sql+sWtest;
tag:=0;
if Query1.Active then query1.Close;
query1.SQL.Clear;
query1.SQL.Add(sqlTest);
query1.Active:=true;
query1.First;
while not query1.Eof do
begin
if(Pos('类',query1.fieldbyname('结论').asString)<>0)then
begin
if strWhere='' then strWhere:='WHERE 条件like '+'''%'+query1.fieldbyname('结论').asString+'%'' '
else
strWhere:=strWhere+'or 条件like '+'''%'+query1.fieldbyname('结论').asString+'%'' ';
tag:=1;
end;
query1.Next;
end;
if tag=0 then
begin
strWhere:='WHERE 条件like '+'''%'+ListBox2.Items[i]+'%'' ';
strWhere:=strWhere+'and '+'条件not like ''%不'+ListBox2.Items[i]+'%'' ';
end;
end
else
begin
sWtest:='WHERE 条件like '+'''%'+ListBox2.Items[i]+'%'' ';
sWtest:=sWtest+'and '+'条件not like ''%不'+ListBox2.Items[i]+'%'' ';
sqlTest:=sql+sWtest;
if Query1.Active then query1.Close;
query1.SQL.Clear;
query1.SQL.Add(sqlTest);
query1.Active:=true;
query1.First;
tag:=0;
while not query1.Eof do
begin
if(Pos('类',query1.fieldbyname('结论').asString)<>0)then
begin
sub:=query1.fieldbyname('结论').asString;
if(Pos(sub,strWhere)<>0)then
begin
tag:=1;
break;
end
else
begin
strWhere:=strWhere+'and 条件like '+'''%'+query1.fieldbyname('结论').asString+'%'' ';
tag:=1;
end;
end;
query1.Next;
end;
if tag=1 then begin i:=i+1;continue; end;
sWtest:=strWhere+'and 条件like '+'''%'+ListBox2.Items[i]+'%'' ';
sWtest:=sWtest+'and '+'条件not like ''%不'+ListBox2.Items[i]+'%'' ';
sqlTest:=sql+sWtest;
if Query1.Active then query1.Close;
query1.SQL.Clear;
query1.SQL.Add(sqlTest);
query1.Active:=true;
query1.First;
tag:=0;
while not query1.Eof do
begin
if(Pos('类',query1.fieldbyname('结论').asString)<>0)then
begin
strWhere:=strWhere+'and 条件like '+'''%'+query1.fieldbyname('结论').asString+'%'' ';
tag:=1;
end;
query1.Next;
end;
if tag=0 then
begin
strWhere:=strWhere+'and 条件like '+'''%'+ListBox2.Items[i]+'%'' ';
strWhere:=strWhere+'and '+'条件not like ''%不'+ListBox2.Items[i]+'%'' ';
end;
end;
i:=i+1;
end;
sql:=sql+strWhere;
if Query1.Active then query1.Close;
query1.SQL.Clear;
query1.SQL.Add(sql);
query1.Active:=true;
if query1.RecordCount=1 then
begin
form2:=Tform2.create(nil);
form2.Left:=form1.Left+30;
form2.Top:=form1.Top+20;
sub:=query1.fieldbyname('结论').asString;
tag:=0;
if sub='金钱豹' then tag:=1
else if sub='老虎' then tag:=2
else if sub='长颈鹿' then tag:=3
else if sub='斑马' then tag:=4
else if sub='鸵鸟' then tag:=5
else if sub='企鹅' then tag:=6
else if sub='信天翁' then tag:=7;
case tag of
1:begin
form2.Image1.Picture.LoadFromFile('图片/金钱豹.jpg');
bel2.Caption:='金钱豹';
end;
2:begin
form2.Image1.Picture.LoadFromFile('图片/老虎.jpg');
bel2.Caption:='老虎';
end;
3:begin
form2.Image1.Picture.LoadFromFile('图片/长颈鹿.jpg');
bel2.Caption:='长颈鹿';
end;
4:begin
form2.Image1.Picture.LoadFromFile('图片/斑马.jpg');
bel2.Caption:='斑马';
end;
5:begin
form2.Image1.Picture.LoadFromFile('图片/鸵鸟.jpg');
bel2.Caption:='鸵鸟';
end;
6:begin
form2.Image1.Picture.LoadFromFile('图片/企鹅.jpg');
bel2.Caption:='企鹅';
end;
7:begin
form2.Image1.Picture.LoadFromFile('图片/信天翁.jpg');
bel2.Caption:='信天翁';
end;
else
begin
form2.Image1.V isible:=false;
form2.DBGrid1.Visible:=true;
end;
end;
form1.Enabled:=false;
end
else
if query1.RecordCount>1 then
ShowMessage('条件不足,请输入充足的条件')
else
showMessage('条件相悖,不存在这样的动物');
sql:='SELECT 结论FROM 推导'; ListBox1.Clear;
ListBox2.Clear;
with table1 do
begin
first;
while not eof do
begin
ListBox1.Items.Add(fieldbyname('条件').asString);
next;
end;
end;
end;
end.
//unit2.dfm
unit Unit2;
interface
uses
Windows, Messages, SysUtils, V ariants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, Grids, DBGrids, jpeg;
type
TForm2 = class(TForm)
Label2: TLabel;
Image1: TImage;
DBGrid1: TDBGrid;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses Unit1;
{$R *.dfm}
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
form1.Enabled:=true; form2.Release; end;
end.
五、实验总结
通过本次实验对人工智能有了新的高度的认识,尤其是在实现专家系统中,对数据库的操作有了新的认识。