2012-12-08 46 views
0

我有以下問題。我有一個明確的成功清單。匹配合法移動生成器

(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. 所以需要特威特和顏色的情況下,它應該返回綠色,

  2. 但在特威特的情況下,和吃應該返回種子,因爲它 繼承它從budgie

  3. 在tweetie的情況下,應該返回羽毛,因爲tweetie 繼承它從鳥。

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

我不知道該如何檢索父的價值觀。

我有一個輔助函數,用for循環返回鳥/ 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)) 

這對我來說功課,所以我不希望有人來解決這個問題對我來說。我只是被卡住了一段時間,我沒有進步。如果你能提供一些幫助,那會很好。乾杯。

+0

是的好像我沒有解釋好,我會修改它,我們需要使用列表來完成這個任務。 –

+0

那麼,它應該使用單一的繼承,沒有限制後裔的數量,基於你所說的「鴨子打字」,並由(對象;這種類型或值的特徵)定義 –

+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進行非常原始的猜測。

+0

嗯,我只能感激你的時間和知識,乾杯隊友! –