首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >剪辑:多插槽内的插槽

剪辑:多插槽内的插槽
EN

Stack Overflow用户
提问于 2014-09-16 04:00:29
回答 1查看 3.5K关注 0票数 0

我目前正在剪辑和我是新的工作。我正在尝试将以下信息复制到CLIPS缺陷模板中:

代码语言:javascript
复制
[Person, [Class,Class],[[M 9,11],[F,9,11]]]

它有一个人,多个课程,他们可以参加的时间,他们可以上这门课。我试图在下面的deftemplate中复制这些信息:

代码语言:javascript
复制
(deftemplate person 
   (slot Name)
   (multislot Class)
   (multislot Available))

我的问题是,我不明白我应该在可用的多时隙中做什么,因为它有三个输入。有什么方法可以在多个插槽中设置插槽吗?我已经在网上寻找了这样做的方法,但没有能够正确地解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-17 03:44:48

以下是四种不同的方法。我建议采取类似于方法3或4的做法,因为这些做法涉及事实/实例之间的简单联系。

代码语言:javascript
复制
CLIPS> (clear) ; Approach 1
CLIPS> 
(deftemplate person 
   (slot Name)
   (multislot Class)
   (multislot Available))
CLIPS>    
(deffacts people
   (person (Name Frank)
           (Class Biology Calculus)
           (Available M 9 11 F 9 11)))
CLIPS> 
(deffunction #-of-triplets (?mf)
   (div (length$ ?mf) 3))
CLIPS>    
(deffunction nth-triplet (?mf ?n)
   (subseq$ ?mf (+ 1 (* (- ?n 1) 3))(* ?n 3)))
CLIPS>    
(defrule print-availability
    (person (Name ?name)
            (Available $?a))
    =>
    (loop-for-count (?i (#-of-triplets ?a))
       (bind ?triplet (nth-triplet ?a ?i))
       (bind ?d (nth$ 1 ?triplet))
       (bind ?b (nth$ 2 ?triplet))
       (bind ?e (nth$ 3 ?triplet))
       (printout t ?name " " ?d " " ?b " " ?e crlf)))
CLIPS> (reset)
CLIPS> (run)
Frank M 9 11
Frank F 9 11
CLIPS> (clear) ; Approach 2
CLIPS> 
(deftemplate person 
   (slot Name)
   (multislot Class)
   (multislot Available-Weekday)
   (multislot Available-Begin)
   (multislot Available-End))
CLIPS>    
(deffacts people
   (person (Name Frank)
           (Class Biology Calculus)
           (Available-Weekday M F)
           (Available-Begin 9 9)
           (Available-End 11 11)))
CLIPS> 
(defrule print-availability
    (person (Name ?name)
            (Available-Weekday $?f1 ?d $?)
            (Available-Begin $?f2 ?b $?)
            (Available-End $?f3 ?e $?))
    (test (= (length$ ?f1)
             (length$ ?f2)
             (length$ ?f3)))
    =>
    (printout t ?name " " ?d " " ?b " " ?e crlf))
CLIPS> (reset)
CLIPS> (run)
Frank M 9 11
Frank F 9 11
CLIPS> (clear) ; Approach 3
CLIPS> 
(deftemplate person 
   (slot Name)
   (slot ID)
   (multislot Class))
CLIPS>    
(deftemplate availability
   (slot owner-ID)
   (slot Weekday)
   (slot Begin)
   (slot End))
CLIPS>    
(deffacts information
   (person (Name Frank) (ID 1)
           (Class Biology Calculus))
   (availability (owner-ID 1) (Weekday M) (Begin 9) (End 11))
   (availability (owner-ID 1) (Weekday F) (Begin 9) (End 11)))
CLIPS>  
(defrule print-availability
    (person (Name ?name) (ID ?id))
    (availability (owner-ID ?id) (Weekday ?d) (Begin ?b) (End ?e))
    =>
    (printout t ?name " " ?d " " ?b " " ?e crlf))
CLIPS> (reset)
CLIPS> (run)
Frank F 9 11
Frank M 9 11
CLIPS> (clear) ; Approach 4
CLIPS> 
(defclass PERSON
   (is-a USER)
   (slot Name)
   (multislot Class)
   (multislot Available))
CLIPS>    
(defclass AVAILABILITY
   (is-a USER)
   (slot Weekday)
   (slot Begin)
   (slot End))
CLIPS>    
(definstances information
   (of PERSON (Name Frank)
              (Class Biology Calculus)
              (Available (make-instance of AVAILABILITY (Weekday M) (Begin 9) (End 11))
                         (make-instance of AVAILABILITY (Weekday F) (Begin 9) (End 11)))))
CLIPS>                          
(defrule print-availability
    (object (is-a PERSON) (Name ?name) (Available $? ?a $?))
    (object (is-a AVAILABILITY) (name ?a))
    =>
    (printout t ?name " " (send ?a get-Weekday) 
                      " " (send ?a get-Begin) 
                      " " (send ?a get-End) crlf)))
CLIPS> (reset)
CLIPS> (run)
Frank F 9 11
Frank M 9 11
CLIPS> 
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25860432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档