布林邏輯的推論法 -- 程式實作

程式作品

C 語言

Java

C#

JavaScript

常用函數

文字處理

遊戲程式

衛星定位

系統程式

資料結構

網路程式

自然語言

人工智慧

機率統計

資訊安全

等待完成

訊息

相關網站

參考文獻

最新修改

簡體版

English

[フレーム]

根據 Horn 的前向推論法與 Robinson 的 Refutation 方法,我們用 Java 寫了一個推理引擎,其程式碼位於 BooleanLogic.java 中,該程式中的 unitResolution() 函數是採用 Robinson 的推論法則,而 hornReasoning() 函數則採用 Horn 的推論法則,兩者的程式碼分別如下。

檔案:BooleanLogic.java

程式內容

importjava.util.*;
 
classBooleanLogic{Randomrandom = newRandom();
 
 publicstaticvoidmain(String[]args){Stringrules = "a<-b,c;b<-d,e;c<-d,f;d;e;f";
 hornReasoning(rules.split(";"));
 unitResolution(rules.split(";"));
 }publicstaticStringneg(StringpTerm){if(pTerm.startsWith("-"))returnpTerm.substring(1);
 elsereturn"-"+pTerm;
 }publicstaticStringrewrite(Stringrule){StringBufferrzRule = newStringBuffer();
 Stringhead = head(rule, "<-");
 Stringbody = tail(rule, "<-");
 rzRule.append(head);
 if(body.length() > 0){String[]conds = body.split(",");
 for(inti=0; i<conds.length; i++)rzRule.append(","+neg(conds[i]));
 }returnrzRule.toString();
 }publicstaticvoidunitResolution(String[]pRules){Vectorrules = newVector();
 for(intri=0; ri<pRules.length; ri++)rules.add(rewrite(pRules[ri]));
 for(intri=0; ri<rules.size(); ri++){Stringrule1 = (String)rules.get(ri);
 if(rule1.indexOf(",")>0)continue; // it's not unit rule.Stringterm = rule1;
 StringnegTerm = neg(term);
 for(intrj=0; rj<rules.size(); rj++){Stringrule2 = (String)rules.get(rj);
 Stringexp = ","+rule2+",";
 if(exp.indexOf(","+negTerm+",")>0){StringunifyExp = replace(exp, ","+negTerm+",", ",");
 StringnewRule = unifyExp.substring(1, unifyExp.length()-1);
 rules.add(newRule);
 System.out.println("unfiy("+rule1+"|"+rule2+")="+newRule);
 }}}}publicstaticvoidhornReasoning(String[]rules){TreeMaptrueMap = newTreeMap();
 intsatisfyRuleCount;
 do{satisfyRuleCount = 0;
 for(intri=0; ri<rules.length; ri++){if(rules[ri].length() == 0)continue;
 Stringhead = head(rules[ri], "<-");
 Stringbody = tail(rules[ri], "<-");
 String[]conds = body.split(",");
 booleanisSatisfy = true;
 for(intci=0; ci<conds.length; ci++){if(conds[ci].length() == 0)continue;
 if(trueMap.get(conds[ci]) == null)isSatisfy = false;
 }if(isSatisfy){System.out.println(rules[ri]);
 trueMap.put(head, "1");
 rules[ri] = "";
 satisfyRuleCount ++;
 }}}while(satisfyRuleCount > 0);
 }publicstaticStringhead(StringpStr, StringpSpliter){intspliterPos = pStr.indexOf(pSpliter);
 if(spliterPos < 0)returnpStr;
 returnpStr.substring(0,spliterPos);
 }publicstaticStringtail(StringpStr, StringpSpliter){intspliterPos = pStr.indexOf(pSpliter);
 if(spliterPos < 0)return"";
 returnpStr.substring(spliterPos+pSpliter.length());
 }publicstaticStringreplace(StringpStr, StringfromPat, StringtoPat){if(fromPat.length()==0)returnpStr;
 if(pStr.indexOf(fromPat)<0)returnpStr;
 StringBufferrzStr = newStringBuffer();
 intstrIdx = 0, nextIdx;
 while((nextIdx = pStr.indexOf(fromPat, strIdx))>=0){rzStr.append(pStr.substring(strIdx, nextIdx));
 rzStr.append(toPat);
 strIdx = nextIdx + fromPat.length();
 }rzStr.append(pStr.substring(strIdx));
 returnrzStr.toString();
 }}

執行結果

D:\wikidot\LR>javac BooleanLogic.java
Note: BooleanLogic.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
D:\wikidot\LR>java BooleanLogic
d
e
f
b<-d,e
c<-d,f
a<-b,c
unfiy(d|b,-d,-e)=b,-e
unfiy(d|c,-d,-f)=c,-f
unfiy(e|b,-d,-e)=b,-d
unfiy(e|b,-e)=b
unfiy(f|c,-d,-f)=c,-d
unfiy(f|c,-f)=c
unfiy(b|a,-b,-c)=a,-c
unfiy(c|a,-b,-c)=a,-b
unfiy(c|a,-c)=a

結語

布林邏輯雖然是一種簡單的邏輯系統,但其數學基礎非常紮實,是西洋文化的精隨之所在。如果我們能欣賞這種邏輯系統的美麗之處,那麼將能理解西方數學的重要性,進而將其應用到電腦領域,發展出更好的邏輯程式。

Facebook

[フレーム]

Facebook

[フレーム]

Wikidot

Post preview:

(will not be published)


本網頁的作者、授權與引用方式

作者
陳鍾誠,於金門大學資訊工程系,電子郵件:wt.ude.uqn|ccc#wt.ude.uqn|ccc,網站:http://ccckmit.wikidot.com
授權
本文採用創作共用 (Creative Common) 3.0 版的 姓名標示─非商業性─相同方式分享 授權條款,歡迎轉載或修改使用,但若做為商業使用時必須取得授權,引用本文時請參考下列格式。
中文版 (APA格式)
陳鍾誠 (07 Sep 2010 02:56),(網頁標題) 布林邏輯的推論法 — 程式實作,(網站標題) 陳鍾誠的網站,取自 http://ccckmit.wikidot.com/code:boolean ,網頁修改第 6 版。
英文版 (APA格式)
Chung-Chen Chen (07 Sep 2010 02:56), Retrieved from http://ccckmit.wikidot.com/code:boolean , Page Revision 6.
page revision: 6, last edited: 03 Nov 2010 02:20
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License
Click here to edit contents of this page.
Click here to toggle editing of individual sections of the page (if possible). Watch headings for an "edit" link when available.
Append content without editing the whole page source.
Check out how this page has evolved in the past.
If you want to discuss contents of this page - this is the easiest way to do it.
View and manage file attachments for this page.
A few useful tools to manage this Site.
Change the name (also URL address, possibly the category) of the page.
View wiki source for this page without editing.
View/set parent page (used for creating breadcrumbs and structured layout).
Notify administrators if there is objectionable content in this page.
Something does not work as expected? Find out what you can do.
General Wikidot.com documentation and help section.
Wikidot.com Terms of Service - what you can, what you should not etc.
Wikidot.com Privacy Policy.

AltStyle によって変換されたページ (->オリジナル) /