对于这个Prolog脚本有点困惑..
/*框架表示 */ frame(name(bird), isa(animal), hasproperty([fly, feathers, sing])). frame(name(canary),isa(bird), hasproperty([yellow, nervous, easily_frightened])). frame(name(tweety), isa(canary), hasproperty([baby, my_pet])). frame(name(barn_owl), isa(bird), hasproperty([nocturnal,large_eyes])). frame(name(barny), isa(barn_owl), hasproperty([sick,forward_facing])). /* 继承 - 使用递归*/ inherit(Concept, Prop):- frame(name(Concept), _, hasproperty(Prop)). inherit(Concept, Prop):- frame(name(Concept), isa(Parent), _), write(Parent), nl, frame(name(Parent), _, hasproperty(PP)), write(PP), nl, inherit(Parent, NewProp).
我理解第一个规则,它检查概念是否具有某个属性,但是我对第二个规则不太理解。我知道它应该检查继承的框架是否具有某个属性,但我不知道它是如何检查的,尤其是当属性名称从PP变为NewProp时。另外,当有两个同名的规则时,Prolog如何知道执行哪个规则?感谢任何帮助!
回答:
我想在inherit
的第二个子句中,你想要做的是找到概念的父级,然后立即调用inherit。现在,你只完成了其中的一半工作。
inherit(Concept, Prop):- frame(name(Concept), _, hasproperty(Prop)).inherit(Concept, Prop):- frame(name(Concept), isa(Parent), _), write(Parent), nl, inherit(Parent, Prop).