Prolog初探

人工智能Prolog实验报告

               班级:2014211309  学号:2014211366  姓名:王晓宇

题目要求

用PROLOG完成以下系统的编写。給出代码和一个运行实例,指出某人吞入毒物后要采取什么措施。
已知下列毒物:酸(如去溴剂、碘酒)、碱(如氨水、漂白剂),以及石油产品(如汽油、松节油)。其他所有毒物归为其他类型other类。中毒时,应呼叫医师或中毒控制中心。对于酸、碱和其他类型毒物,应该让病人喝水或牛奶之类的液体以稀释毒药。对于其他类型的毒物,要呕吐。但对于酸、碱或石油产品不能呕吐。如果病人神志不清或惊厥,则不要喝水类液体,也不要呕吐。

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().%应对措施在拨打电话后,有服用类水液体并催吐,只服用类水液体,只催吐,两者均不进行,其他不可预计情况,五种
    
class facts
    poison : (string PString, poison_kind PKind). %毒物名称与类型关系 
    awake : (string AString, awake_kind  AKind). %受害人状态描述和状态关系
    
class predicates
    solution : (string AString, string PString, string Result) multi(i,i,o).%接受毒物名称和受害人状态描述,得到解决措施
    dowhat : (dowhat_kind DKind, string What) procedure(i,o).%根据措施得到措施描述
    
clauses
    %毒物名称和类型的关系
    poison("BrRemover", acid()).
    poison("iodine", acid()).
    poison("aqueous", alkali()).
    poison("whiter", alkali()).
    poison("gasoline", oil()).
    poison("turpentine", oil()).
    poison("other", other()).
    %受害者状态描述和状态关系
    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).
    %受害者神志不清,或惊厥,或清醒并且毒物为石油,求助医生或者中毒控制中心
    solution(PString, AString, Result) :- (awake(AString, delirious()) ; awake(AString, convulsion()) ; (awake(AString, awake()), poison(PString, oil()))), dowhat(none(), Result).
    %受害者状态描述或毒物名称在应对范围之外,求助医生或者中毒控制中心
    solution(_, _, Result) :-  dowhat(other(), Result).
    
    %对应上述四种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类型要同时输入""
        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).

运行结果

误服酸性毒物,清醒状态

Microsoft Windows [版本 6.3.9600]
(c) 2013 Microsoft Corporation。保留所有权利。

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。保留所有权利。

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
--------------------------------------------------------------------------------

误服不在上述中的毒物,或状态不在清醒,神志不清,惊厥中

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
--------------------------------------------------------------------------------

你可能感兴趣的:(Prolog初探)