2016-04-17 13 views
0

私はCLIPSを初めて使用しており、その言語を理解するのが難しいです。私はかなり混乱のタイトルを知っているので、私はできる限り明確で正確なものにしようとします。CLIPS:出現数に基づいて最も一般的な事実を見つける

私は学生の記入項目で表される事実の基盤を持っており、参加した学生の中で最も一般的な都市(最も頻繁に出現する都市)を知りたいと思います。

これは、事実の私のベースである:

(deftemplate student 
      (slot name) 
      (slot city) 
      (slot age) 
      (multislot subjects) 
      (multislot grades) 
) 

(deffacts students 
    (student (name John) (city Florida) (age 20) (subjects RRE SSE DDE SRE) (grades D B F F) 
    (student (name Jesse) (city Miami) (age 21) (subjects RRE SSE DDE SRE) (grades C C D D) 
    (student (name Sasha) (city Florida) (age 20) (subjects RRE SSE DDE SRE) (grades B A B B) 
    (student (name Mark) (city Florida) (age 20) (subjects RRE SSE DDE SRE) (grades C C F F) 
) 

私は数日間の答えを探してきたが、クリップは非常に一般的なプログラミング言語ではないので、私はこれに関するご質問や答えを見つけることができません問題と私が見つけるドキュメントはしばしば曖昧です。

私は何か答えをいただきありがとうございます。乾杯!

答えて

0

一部の言語では、パターンに一致するすべてのファクトを収集できる集計演算子が用意されています。この機能により、この種の問題を簡単に解決できます。 CLIPSはこの機能をサポートしていないため、代替アプローチを使用する必要があります。ここに2つあります:

CLIPS> 
(deftemplate student 
      (slot name) 
      (slot city) 
      (slot age) 
      (multislot subjects) 
      (multislot grades)) 
CLIPS> 
(deffacts students 
    (student (name John) (city Florida) (age 20) (subjects RRE SSE DDE SRE) (grades D B F F)) 
    (student (name Jesse) (city Miami) (age 21) (subjects RRE SSE DDE SRE) (grades C C D D)) 
    (student (name Sasha) (city Florida) (age 20) (subjects RRE SSE DDE SRE) (grades B A B B)) 
    (student (name Mark) (city Florida) (age 20) (subjects RRE SSE DDE SRE) (grades C C F F))) 
CLIPS> 
(deffunction find_cities_with_most_students_1() 
    (bind ?cities (create$)) 
    (bind ?counted_cities (create$)) 
    (bind ?largest_count 0) 
    (do-for-all-facts ((?s student)) TRUE 
     (if (not (member$ ?s:city ?counted_cities)) 
     then 
     (bind ?counted_cities (create$ ?s:city ?counted_cities)) 
     (bind ?count (length$ (find-all-facts ((?s2 student)) (eq ?s2:city ?s:city)))) 
     (if (= ?count ?largest_count) 
      then 
      (bind ?cities (create$ ?s:city ?cities)) 
      else 
      (if (> ?count ?largest_count) 
       then 
       (bind ?largest_count ?count) 
       (bind ?cities (create$ ?s:city))))))  
    (return ?cities)) 
CLIPS> 
(defrule largest_count_1 
    (declare (salience -10)) 
    => 
    (bind ?cities (find_cities_with_most_students_1)) 
    (foreach ?c ?cities 
     (printout t "1 " ?c " has the most students" crlf))) 
CLIPS>  
(deftemplate city_count 
    (slot city) 
    (multislot facts)) 
CLIPS> 
(defrule count_cities_2 
    (student (city ?city)) 
    (not (city_count (city ?city))) 
    => 
    (assert (city_count (city ?city) 
         (facts (find-all-facts ((?s student)) 
               (eq ?s:city ?city)))))) 
CLIPS> 
(defrule largest_count_2 
    (declare (salience -10)) 
    (city_count (city ?city) (facts $?facts)) 
    (not (city_count (city ~?city) (facts $?facts2&:(> (length$ ?facts2) (length$ ?facts))))) 
    => 
    (printout t "2 " ?city " has the most students" crlf)) 
CLIPS> (watch rules) 
CLIPS> (watch facts) 
CLIPS> (reset) 
<== f-0  (initial-fact) 
==> f-0  (initial-fact) 
==> f-1  (student (name John) (city Florida) (age 20) (subjects RRE SSE DDE SRE) (grades D B F F)) 
==> f-2  (student (name Jesse) (city Miami) (age 21) (subjects RRE SSE DDE SRE) (grades C C D D)) 
==> f-3  (student (name Sasha) (city Florida) (age 20) (subjects RRE SSE DDE SRE) (grades B A B B)) 
==> f-4  (student (name Mark) (city Florida) (age 20) (subjects RRE SSE DDE SRE) (grades C C F F)) 
CLIPS> (run) 
FIRE 1 count_cities_2: f-4,* 
==> f-5  (city_count (city Florida) (facts <Fact-1> <Fact-3> <Fact-4>)) 
FIRE 2 count_cities_2: f-2,* 
==> f-6  (city_count (city Miami) (facts <Fact-2>)) 
FIRE 3 largest_count_2: f-5,* 
2 Florida has the most students 
FIRE 4 largest_count_1: * 
1 Florida has the most students 
CLIPS> (unwatch all) 
CLIPS> (reset) 
CLIPS> (run) 
2 Florida has the most students 
1 Florida has the most students 
CLIPS> 
+0

ありがとうございます!あなたの答えはとても役に立ちました! –

関連する問題