1、动物识别系统实验报告动物识别系统实验报告题目:基于web的动物识别系统 院系: 信科院计算机系日期:2013年10月31日 1. 实验目的及开发工具. 1 2. 实验原理. 1 3. 数据库设计. 1 3.1 数据库设计思想 . 1 3.2 数据库系统结构 . 2 4. 系统设计. 2 4.1系统首页 . 3 4.2 动物识别 . 4 4.3 添加规则 . 6 4.4 删除规则 . 10 5. 实验心得.11 6. 实验总结.11 1. 实验目的及开发工具 编写一个基于WEB的动物识别系统,实现对动物的识别,理解产生式表达式;熟悉产生式系统的基本过程;熟悉ASP.NET与数据库结合。 本系统采
2、用的开发工具为SQL server 2005,Visual Studio 2008 ,程序设计语言为ASP.NET2.0 , C# 。 2. 实验原理 产生式系统是把一组产生式放在一起,并让它们相互配合,协同作用,一个产生式的结论可以供另一个产生式作为已知事实使用,以求得问题的解决。产生式的基本形式为P Q或者if P then Q 产生式规则分析如下: 动物识别系统的部分推理网络 3. 数据库设计 3.1 数据库设计思想 通过以上分析,我们建立了一个DBai数据库,其中存放两张表,分别是条件表Condition和规则表principle。 1 表1 :条件表Condition 项目名 数据结
3、构 说明 ConditionID Int 编号,主键 ConditionContent Varchar(20) 条件内容 表2:规则表principle 项目名 数据结构 说明 RuleID Int 编号,主键 Condition1 Int 条件1编号 Condition2 Int 条件2编号 Condition3 Int 条件3编号 Condition4 Int 条件4编号 Condition5 Int 条件5编号 Conclusion Int 结论 3.2 数据库系统结构 条件表是一个用来存放与求解问题有关的各种当前信息的数据结构。例如问题的初始状态、输入的事实、推理得到的中间结论和最终结
4、论。 规则表是一个用来存放与求解问题有关的所有规则的集合,也称为知识库。它包含了问题从初始状态转换成目标状态所需要的所有变换规则。 在推理过程中,当规则表中某条规则的前提可以和综合数据库中的已知事实相匹配时,该规则被激活。由它推出的结论将被作为新的事实放入数据库,称为后面推理的已知事实。所以数据库系统结构如下: 数据库 条件表 规则表 4. 系统设计 本系统分为三个功能模块,分别是动物识别、添加规则、删除规则。系统2 实现如下,其中distinguish.aspx实现动物识别;Addrule.aspx实现添加规则;Deleterule.aspx实现删除规则;主页实现为main.aspx和top
5、.aspx;连接数据库调用DBFunction.cs。 4.1系统首页 在浏览器上显示动物识别系统的首页如下图所示。 3 连接数据库的关键代码: public DBFunction(DBTransactionType TranType) string Constring = server=;uid=dbai;pwd=syssys;database=dbai; _con = new SqlConnection(Constring); try _con.Open(); _cmd = _con.CreateCommand(); if (TranType = DBTransactionType.Wit
6、hTran) _tran = _con.BeginTransaction(); _cmd.Transaction = _tran; catch (Exception ex) throw ex; 4.2 动物识别 单击“动物识别”按钮,页面跳转后可根据条件对动物进行判别,比如选择“有羽毛”,点击“查询”按钮,则系统显示识别的结果,如下图所示: 主要源代码: using (DBFunction DBfun = new DBFunction(DBTransactionType.NonTran) 4 /查询在这次用户选择的所有的规则能够得到的所有的规则,将这些规则的所有结论都放到congditions
7、中,作为其他的条件。 dt = DBfun.ExecforQuery(select * from principle); int l = 5; int test = 0; while (l 0) for (i = 0; i dt.Rows.Count; i+) string sql_condition_chosen = find_sql(chosen); DataTable dt_conditions = DBfun.ExecforQuery(sql_condition_chosen); bool check = true; for (int j = 1; j 6; j+) if (dt.Ro
8、wsij.ToString() = ) else if (!is_in(dt.Rowsij.ToString().Trim(), dt_conditions) check = false; break; if (check = true & rule_is_choseni = false test = 1; string to_exe; DataTable dt_temp = null; result += 从 ; to_exe = string.Format(select * from Condition a,Condition b where a.ConditionIDb.Conditio
9、nID and b.ConditionContent=0, dt.RowsiConclusion.ToString(); dt_temp = DBfun.ExecforQuery(to_exe); int number = dt_temp.Rows.Count; condition_is_chosennumber = true; rule_is_choseni = true; to_exe = string.Format(select ConditionContent from Condition where ConditionID=0 or ConditionID=1 or Conditio
10、nID=2 or ConditionID=3 or ConditionID=4, dt.RowsiCondition1.ToString(), dt.RowsiCondition2.ToString(), dt.RowsiCondition3.ToString(), 5 dt.RowsiCondition4.ToString(), dt.RowsiCondition5.ToString(); dt_temp = DBfun.ExecforQuery(to_exe); for (int n = 0; n dt_temp.Rows.Count; n+) if (n dt_temp.Rows.Cou
11、nt - 1) result += dt_temp.RowsnConditionContent.ToString() + 和; else result += dt_temp.RowsnConditionContent.ToString(); result += 可以看出,该动物; to_exe = string.Format(select ConditionContent from Condition where ConditionID=0, dt.RowsiConclusion.ToString(); dt_temp = DBfun.ExecforQuery(to_exe); result
12、+= dt_temp.Rows0ConditionContent.ToString() + nn; rule_is_choseni = true; string conclu = string.Format(select * from Condition where ConditionID0, dt.RowsiConclusion.ToString(); dt_temp = DBfun.ExecforQuery(conclu); int number_of_rule = dt_temp.Rows.Count; chosennumber_of_rule = number_of_rule; l-;
13、 if (test = 0) result += 从所选条件不能推出任意结论; this.TextBox1.Text = result; 4.3 添加规则 如果对规则库进行添加,可单击“添加规则”按钮,输入需要添加的条件和结论即可。 6 主要源代码: /添加已经在数据库中的项 for (int j = 0; j 0 & numbers = 5) string sql_insert = insert into principle(Condition1,Condition2,Condition3,Condition4,Condition5,Conclusion)values(; while (co
14、nditionposition + 1 != -1) sql_insert += conditionposition + 1.ToString() + ,; position+; for (int pos = position + 1; pos 5) this.Literal1.Text=不能选择超过5个条件; else if (numbers = 0) this.Literal1.Text = 至少要有一个前件; else this.Literal1.Text = 不能没有结论; 4.4 删除规则 单击“删除规则”按钮,可实现对规则的删除操作。 主要源代码: protected void B
15、utton1_Click(object sender, EventArgs e) int a = this.GridView1.SelectedIndex; int rule_id_to_deletel; using (DBFunction Dbfun = new DBFunction(DBTransactionType.NonTran) 10 DataTable dt = Dbfun.ExecforQuery(select * from principle); rule_id_to_deletel = Convert.ToInt32(dt.RowsaRuleID.ToString(); Dbfun.ExecForNonQuery(string.Format(delete from principle where RuleID=0, rule_id_to_deletel); Response.Redirect(Deleterule.aspx); 5. 实验心得 甘庆晴:通过本次实验,我掌握了人工智能的产生式表达式,熟悉产生式系统的基本过程。小组分工后我主