0

我有以下问题。我有一个明确的 succesorts 列表。

(defparameter *tuples*
 '((isa budgie bird) (color budgie yellow)
   (isa tweetie budgie) (color tweetie green)
   (eats budgie seed) (has bird feathers)))

所以在这里我创建了一组规则:

; the explicit successors
(defparameter *tuples2*
 '(((isa ?b bird) => (has ?b feathers))
   ((isa ?b bird) => (has ?b brain))
   ((isa ?b budgie) => (eats ?b seed))
   ((isa ?b budgie) => (color ?b yellow))
   ((isa ?b tweetie) => (color ?b green))
   ((isa ?b tweetie) => (smart ?b small))))  
  1. 因此,如果需要 tweetie 和颜色,它应该返回绿色,

  2. 但是在 tweetie 和 eats 的情况下,应该返回种子,因为它是从 budgie 继承的

  3. 在 tweetie 和 had 的情况下,应该返回羽毛,因为 tweetie 继承自 bird。

例子

(inherit tuples 'tweetie 'heart-rate) => nil
(inherit tuples 'tweetie 'color)      => green
(inherit tuples 'tweetie 'eats)       => seeds
(inherit tuples 'tweetie 'has)        => feathers

我不知道如何检索父母的价值观。

我有一个带有 for 循环的辅助函数,它返回鸟/鹦鹉或鸣叫的值。

(defun serve-lmg (state)
  (loop for rule in *tuples*
        when (equal (first rule) state)
        collect (third rule)))

所以当我跑步时

(serve-lmg '(isa ?b bird))

我明白了

((HAS ?B FEATHERS) (HAS ?B BRAIN))

这对我来说是家庭作业,所以我不指望有人为我解决它。我只是被卡住了一段时间,我没有进展。如果您能提供一些帮助,那就太好了。干杯。

4

1 回答 1

2

好吧,这里有一些可以让你开始的东西:

(defun is-a (object kind)
  "Object is a singleton class, it is the symbol, which is
also it's own class"
  (or (eql object kind)
      (let ((super (get object :superclass)))
        (and super (is-a super kind)))))

(defun collect-requirements (kind)
  "Collect all features of this class, and all of its superclasses."
  (let ((super (get kind :superclass))
        (present (get kind :features)))
    (if super
        (append present
               (remove-if
                #'(lambda (x)
                    (some #'(lambda (y)
                              (eql (car y) (car x))) present))
                (collect-requirements super)))
        present)))

(defun could-be (object kind)
  "Try to identify an object based on the features it has,
we could know it already as a subclass of `kind', but if
it is not, then try to see if all the requirements of the
`kind' are met in this object."
  (or (is-a object kind)
      (let ((features (get object :features))
            (requirements (collect-requirements kind)))
        (every #'(lambda (x)
                   (some #'(lambda (y)
                             (eql (car y) (car x))) features))
               requirements))))

(defun is-the-same-as (object kind)
  "Very much like `could-be', except it tests for exact
correspondence."
  (or (is-a object kind)
      (let ((features (get object :features))
            (requirements (collect-requirements kind)))
        (every #'(lambda (x) (member x features :test #'equal))
               requirements))))

(defun get-feature (object feature)
  "Looks up a feature in the prototype chain and returns it
if it is there."
  (loop for (n . v) in (collect-requirements object) do
       (when (eql n feature) (return v))))

(defun parse-tuples (tuples)
  "Parses the list of tuples of the form: (feature object subject)
and infers iheritance chain and features of the objects."
  (loop for (feature object subject) in tuples do
       (import feature)
       (import object)
       (if (eql 'isa feature)
           (setf (get object :superclass) subject)
           (setf (get object :features)
                 (cons (cons feature subject)
                       (get object :features))))))

(parse-tuples
 '((isa budgie bird) 
   (color budgie yellow)
   (isa tweetie budgie)
   (color tweetie green)
   (eats budgie seed)
   (has bird feathers)))

(is-a 'budgie 'bird)
(is-a 'budgie 'crocodile)

(get-feature 'budgie 'color)
(get-feature 'tweetie 'color)

(import 'unknown-animal)
(setf (get 'unknown-animal :features)
      '((color . purple) (eats . potatoes) (has . future)))
(is-a 'unknown-animal 'bird)
(could-be 'unknown-animal 'bird)
(could-be 'unknown-animal 'budgie)
(could-be 'unknown-animal 'tweetie)

(import 'more-or-less-a-tweetie)
(setf (get 'more-or-less-a-tweetie :features)
      '((color . green) (eats . seed) (has . feathers)))

(is-the-same-as 'more-or-less-a-tweetie 'tweetie)
(is-the-same-as 'unknown-animal 'tweetie)

这描述了几种可能从基于特征和直接子类化的关系中构建的类型。它symbol-plist用作类描述的存储,它完全基于列表(根据您的要求)。

它没有做什么:当它试图理解使用is-the-same-as它的可能性时忽略了一个事实,即该特征是被继承的。也就是说,如果你给它一只新的绿色小鸟,它会尽可能地识别它tweety,但不会尽可能地识别它budgie,否则,它会使用could-be.

于 2012-12-08T15:45:52.163 回答