Генеалогическое древо получить члена семьи

#clips

#клипы

Вопрос:

У меня есть какое-то семейное древо, и я хочу получить дедушку с этого дерева через дефрул. Также было бы идеально, если бы мы могли найти шурин.

 (deftemplate person  (slot name)   (slot gender)   (slot father)  (slot wife))  (deffacts people  (person (name Vasya) (gender male) (wife Liza))   (person (name Liza) (gender female))   (person (name Vladimir) (gender male) (father Vasya))  (person (name Natasha) (gender female) (father Vasya))  (person (name Viktor) (gender male) (father Vasya))  (person (name Misha) (gender male) (wife Natasha))  (person (name Kostya) (gender male) (father Misha) (wife Liza))  (person (name Masha) (gender female) (father Misha)))  (deftemplate mother  (slot namel)  (slot name2))  (deftemplate brother  (slot namel)  (slot name2))  (defrule Brother  (person (name ?x) (gender male) (father ?yamp;~nil))  (person (name ?zamp;~?x) (gender male) (father ?yamp;~nil))  (not (brother (namel ?x) (name2 ?z)))  (not (brother (namel ?z) (name2 ?x)))   =gt;  (printout t ?x " brother of " ?z crlf)   (assert (brother (namel ?x) (name2 ?z))))  

Ответ №1:

Предполагая, что вы относитесь к сводным братьям как к братьям, есть две проблемы с вашим существующим правилом о братьях. Во-первых, требуется, чтобы у брата был брат мужского пола, поэтому Маша и Наташа не идентифицируются как имеющие братьев. Во-вторых, сводные братья также могут иметь общих матерей, а не только отцов.

Измените свою личную табличку, чтобы включить мать, и измените слот «жена» на «супруг».:

 (deftemplate person  (slot name)  (slot gender)  (slot mother)  (slot father)  (slot spouse))  

Создайте новую табличку для представления отношений между фактами личности:

 (deftemplate relation  (slot p1)  (slot is)  (slot p2))  

Измените свои недостатки, чтобы они отражали обновленную табличку:

 (deffacts people  (person (name Vasya) (gender male) (spouse Liza))   (person (name Liza) (gender female))   (person (name Vladimir) (gender male) (father Vasya))  (person (name Natasha) (gender female) (father Vasya))  (person (name Viktor) (gender male) (father Vasya))  (person (name Misha) (gender male) (spouse Natasha))  (person (name Kostya) (gender male) (father Misha) (spouse Liza))  (person (name Masha) (gender female) (father Misha)))  

Затем вы можете обновить правило brother:

 (defrule brother  (person (name ?name)  (mother ?mother)  (father ?father))  (or (person (name ?brotheramp;~?name)  (gender male)  (father ?fatheramp;~nil))  (person (name ?brotheramp;~?name)  (gender male)  (mother ?motheramp;~nil)))  =gt;  (assert (relation (p1 ?brother)  (is brother-of)  (p2 ?name))))  

и добавьте новые правила для дедушки и шурин:

 (defrule grandfather  (person (name ?name)  (mother ?mother)  (father ?father))  (person (name ?mother | ?father)  (father ?grandfatheramp;~nil))  =gt;  (assert (relation (p1 ?grandfather)  (is grandfather-of)  (p2 ?name))))  (defrule brother-in-law  (person (name ?name)  (spouse ?spouse))  (relation (p1 ?brother-in-law)  (is brother-of)  (p2 ?spouse))  =gt;  (assert (relation (p1 ?brother-in-law)  (is brother-in-law-of)  (p2 ?name))))  

Комментарии:

1. Спасибо, это хорошо работает. С моей стороны произошла какая-то ошибка. Это очень полезно!