匹配法律移动生成器

时间:2012-12-08 10:46:36

标签: regex list lisp

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

(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和吃,应该返回种子,因为它     从budgie继承它

  3. 如果是tweetie并且因为tweetie应该返回羽毛     从鸟类继承它。

  4. 实施例

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

    我不知道如何检索父级的值。

    我有一个带有for循环的辅助函数,它返回bird / budgie或tweetie的值。

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

    这对我来说是家庭作业,所以我不希望有人为我解决。我只是被困了一段时间而且没有进展。如果你能提供一些帮助,那就太好了。欢呼声。

1 个答案:

答案 0 :(得分: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进行非常粗略的猜测