Prolog初探

人工智能Prolog實(shí)驗(yàn)報(bào)告

               班級(jí):2014211309  學(xué)號(hào):2014211366  姓名:王曉宇

題目要求

用PROLOG完成以下系統(tǒng)的編寫(xiě)。給出代碼和一個(gè)運(yùn)行實(shí)例,指出某人吞入毒物后要采取什么措施。
已知下列毒物:酸(如去溴劑、碘酒)、堿(如氨水、漂白劑),以及石油產(chǎn)品(如汽油、松節(jié)油)。其他所有毒物歸為其他類型other類。中毒時(shí),應(yīng)呼叫醫(yī)師或中毒控制中心。對(duì)于酸、堿和其他類型毒物,應(yīng)該讓病人喝水或牛奶之類的液體以稀釋毒藥。對(duì)于其他類型的毒物,要嘔吐。但對(duì)于酸、堿或石油產(chǎn)品不能嘔吐。如果病人神志不清或驚厥,則不要喝水類液體,也不要嘔吐。

prolog代碼

/*main.pro*/
implement main
    open core

constants
    className = "main".
    classVersion = "".

clauses
    classInfo(className, classVersion).
    
domains
    poison_kind = acid(); alkali(); oil(); other(). %毒物分為酸,堿,石油,其他四種
    awake_kind = awake(); delirious(); convulsion(); other().%受害者分清醒,神志不清,驚厥三種
    dowhat_kind = both(); only_drink(); only_vomit(); none(); other().%應(yīng)對(duì)措施在撥打電話后,有服用類水液體并催吐,只服用類水液體,只催吐,兩者均不進(jìn)行,其他不可預(yù)計(jì)情況,五種
    
class facts
    poison : (string PString, poison_kind PKind). %毒物名稱與類型關(guān)系 
    awake : (string AString, awake_kind  AKind). %受害人狀態(tài)描述和狀態(tài)關(guān)系
    
class predicates
    solution : (string AString, string PString, string Result) multi(i,i,o).%接受毒物名稱和受害人狀態(tài)描述,得到解決措施
    dowhat : (dowhat_kind DKind, string What) procedure(i,o).%根據(jù)措施得到措施描述
    
clauses
    %毒物名稱和類型的關(guān)系
    poison("BrRemover", acid()).
    poison("iodine", acid()).
    poison("aqueous", alkali()).
    poison("whiter", alkali()).
    poison("gasoline", oil()).
    poison("turpentine", oil()).
    poison("other", other()).
    %受害者狀態(tài)描述和狀態(tài)關(guān)系
    awake("awake", awake()).
    awake("delirious", delirious()).
    awake("convulsion", convulsion()).
    %受害者清醒,并且毒物為其他類型,采取服用類水液體并催吐方式
    solution(PString, AString, Result) :- awake(AString, awake()), poison(PString, other()), dowhat(both(), Result).
    %受害者清醒,并且毒物為酸或堿,采用服用類水液體的方式
    solution(PString, Astring, Result) :- awake(AString, awake()), (poison(PString, acid()); poison(PString, alkali())), dowhat(only_drink(), Result).
    %受害者神志不清,或驚厥,或清醒并且毒物為石油,求助醫(yī)生或者中毒控制中心
    solution(PString, AString, Result) :- (awake(AString, delirious()) ; awake(AString, convulsion()) ; (awake(AString, awake()), poison(PString, oil()))), dowhat(none(), Result).
    %受害者狀態(tài)描述或毒物名稱在應(yīng)對(duì)范圍之外,求助醫(yī)生或者中毒控制中心
    solution(_, _, Result) :-  dowhat(other(), Result).
    
    %對(duì)應(yīng)上述四種solution
    dowhat(both(), "        1. Call doctor or poison control center\n        2. Get victim water/milk to dilute poison\n        3. Make victim vomit\n") :- !.
    dowhat(only_drink(), "        1. Call doctor or poison control center\n        2. Get victim water/milk to dilute poison\n        Attention: NO VOMIT\n") :- !.
    %未涉及只催吐的情況
    dowhat(only_vomit(), "        1. Call doctor or poison control center\n        2. Make victim vomit\n        Attention: NO WATER-LIKE LIQUID\n") :- !.
    dowhat(none(), "        Call doctor or poison control center\n        Attention: NO VOMIT NOR WATER-LIKE LIQUID\n") :- !.
    dowhat(other(), "        Call doctor or poison control center\n        Attention: Poison not registered or Victim's state not clear\n") :- !.
    
    run():-
        console::init(),
        stdio::write("Always remember to type \"\"\n"), %輸入string類型要同時(shí)輸入""
        stdio::write("Type the poison being mistoken (\"BrRemover\", \"iodine\",\"aqueous\", \"whiter\", \"gasoline\", \"turpentine\", \"other\") : "),
        X=stdio::read(), 
        stdio::write("Type the victim's state (awake, delirious, convulsion) : "),
        Y=stdio::read(),
        stdio::nl,
        solution(X,Y,R), stdio::write("You should:\n", R), %輸出解決措施
        stdio::write("--------------------------------------------------------------------------------"),
        stdio::nl,
        !,
        run().
    run().
end implement main

goal
    mainExe::run(main::run).

運(yùn)行結(jié)果

誤服酸性毒物,清醒狀態(tài)

Microsoft Windows [版本 6.3.9600]
(c) 2013 Microsoft Corporation。保留所有權(quán)利。

C:\Users\wxiaoyu>"C:\Users\wxiaoyu\Documents\Visual Prolog Projects\AI2017\Exe\A
I2017.exe"
Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "BrRemover"
Type the victim's state (awake, delirious, convulsion) : "awake"

You should:
        1. Call doctor or poison control center
        2. Get victim water/milk to dilute poison
        Attention: NO VOMIT
--------------------------------------------------------------------------------

誤服酸性毒物,神志不清

Microsoft Windows [版本 6.3.9600]
(c) 2013 Microsoft Corporation。保留所有權(quán)利。

C:\Users\wxiaoyu>"C:\Users\wxiaoyu\Documents\Visual Prolog Projects\AI2017\Exe\A
I2017.exe"
Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "iodine"
Type the victim's state (awake, delirious, convulsion) : "delirious"

You should:
        Call doctor or poison control center
        Attention: NO VOMIT NOR WATER-LIKE LIQUID
--------------------------------------------------------------------------------

誤服酸性毒物,驚厥

Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "iodine"
Type the victim's state (awake, delirious, convulsion) : "convulsion"

You should:
        Call doctor or poison control center
        Attention: NO VOMIT NOR WATER-LIKE LIQUID
--------------------------------------------------------------------------------

誤服堿性毒物,清醒

Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "aquenous"
Type the victim's state (awake, delirious, convulsion) : "awake"

You should:
        Call doctor or poison control center
        Attention: Poison not registered or Victim's state not clear
--------------------------------------------------------------------------------

誤服堿性毒物,神志不清

Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "whiter"
Type the victim's state (awake, delirious, convulsion) : "delirious"

You should:
        Call doctor or poison control center
        Attention: NO VOMIT NOR WATER-LIKE LIQUID
--------------------------------------------------------------------------------

誤服堿性毒物,驚厥

Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "whiter"
Type the victim's state (awake, delirious, convulsion) : "convulsion"

You should:
        Call doctor or poison control center
        Attention: NO VOMIT NOR WATER-LIKE LIQUID
--------------------------------------------------------------------------------

誤服石油毒物,清醒

Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "gasoline"
Type the victim's state (awake, delirious, convulsion) : "awake"

You should:
        Call doctor or poison control center
        Attention: NO VOMIT NOR WATER-LIKE LIQUID
--------------------------------------------------------------------------------

誤服石油毒物,神志不清

Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "gasoline"
Type the victim's state (awake, delirious, convulsion) : "delirious"

You should:
        Call doctor or poison control center
        Attention: NO VOMIT NOR WATER-LIKE LIQUID
--------------------------------------------------------------------------------

誤服石油毒物,驚厥

Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "turpentine"
Type the victim's state (awake, delirious, convulsion) : "convulsion"

You should:
        Call doctor or poison control center
        Attention: NO VOMIT NOR WATER-LIKE LIQUID
--------------------------------------------------------------------------------

誤服其他毒物,清醒

Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "other"
Type the victim's state (awake, delirious, convulsion) : "awake"

You should:
        1. Call doctor or poison control center
        2. Get victim water/milk to dilute poison
        3. Make victim vomit
--------------------------------------------------------------------------------

誤服其他毒物,神志不清

Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "other"
Type the victim's state (awake, delirious, convulsion) : "delirious"

You should:
        Call doctor or poison control center
        Attention: NO VOMIT NOR WATER-LIKE LIQUID
--------------------------------------------------------------------------------

誤服其他毒物,驚厥

Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "other"
Type the victim's state (awake, delirious, convulsion) : "convulsion"

You should:
        Call doctor or poison control center
        Attention: NO VOMIT NOR WATER-LIKE LIQUID
--------------------------------------------------------------------------------

誤服不在上述中的毒物,或狀態(tài)不在清醒,神志不清,驚厥中

Always remember to type ""
Type the poison being mistoken ("BrRemover", "iodine","aqueous", "whiter", "gaso
line", "turpentine", "other") : "NotRegisteredPoison"
Type the victim's state (awake, delirious, convulsion) : "NotClearState"

You should:
        Call doctor or poison control center
        Attention: Poison not registered or Victim's state not clear
--------------------------------------------------------------------------------
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容