如何将事实配对以创建新事实?

时间:2019-06-14 10:45:36

标签: xcode instruments clips

我是CLIPS的新手,正在尝试使用它在Xcode中创建自定义工具,以对枚举中的状态更改进行建模。

我想最后得出一系列事实,这些事实代表枚举处于给定状态下的开始和结束时间。

每次我的代码中发生状态更改时,我都会发出事件路标,可以将其简化为:

(deftemplate signpost
    (slot time (type INTEGER))
    (slot state (type STRING))
)

我正在尝试将它们配对以生成状态间隔事实,如下所示:

(deftemplate state-interval
    (slot start (type INTEGER))
    (slot duration (type INTEGER))
    (slot state (type STRING))
)

给出输入数据:

(signpost (time 0) (state "one"))
(signpost (time 2) (state "two"))
(signpost (time 5) (state "three"))
(signpost (time 10) (state "four"))

我希望看到以下输出:

(state-interval (start 0) (duration 2) (state "one"))
(state-interval (start 2) (duration 3) (state "two"))
(state-interval (start 5) (duration 5) (state "three"))

到目前为止,我最接近的尝试是:

(deftemplate signpost
    (slot time (type INTEGER))
    (slot state (type STRING))
)

(deftemplate state-update
    (slot time (type INTEGER))
    (slot state (type STRING))
)

(deftemplate state-interval
    (slot start (type INTEGER))
    (slot duration (type INTEGER))
    (slot state (type STRING))
)

(defrule update-state
    ?signpost <- (signpost (time ?time) (state ?state))
    (not (state-update (time ?) (state ?)))
    =>
    (retract ?signpost)
    (assert (state-update (time ?time) (state ?state)))
)

(defrule pair-state
    ?signpost <- (signpost (time ?time) (state ?state))
    ?update <- (state-update (time ?time1) (state ?state1))
    =>
    (modify ?update (time ?time) (state ?state))
    (retract ?signpost)
    (bind ?duration (- ?time1 ?time))
    (assert (state-interval (start ?time1) (duration ?duration) (state ?state1)))
)

(deffacts sample-data
    (signpost (time 0) (state "one"))
    (signpost (time 2) (state "two"))
    (signpost (time 5) (state "three"))
    (signpost (time 10) (state "four"))
)

我得到以下输出:

FIRE    1 update-state: f-4,*
<== f-4     (signpost (time 10) (state "four"))
==> f-5     (state-update (time 10) (state "four"))
==> Activation 0      pair-state: f-3,f-5
==> Activation 0      pair-state: f-2,f-5
==> Activation 0      pair-state: f-1,f-5
<== Activation 0      update-state: f-3,*
<== Activation 0      update-state: f-2,*
<== Activation 0      update-state: f-1,*
FIRE    2 pair-state: f-1,f-5
<== f-5     (state-update (time 10) (state "four"))
<== Activation 0      pair-state: f-2,f-5
<== Activation 0      pair-state: f-3,f-5
==> Activation 0      update-state: f-1,*
==> Activation 0      update-state: f-2,*
==> Activation 0      update-state: f-3,*
==> f-6     (state-update (time 0) (state "one"))
==> Activation 0      pair-state: f-3,f-6
==> Activation 0      pair-state: f-2,f-6
==> Activation 0      pair-state: f-1,f-6
<== Activation 0      update-state: f-3,*
<== Activation 0      update-state: f-2,*
<== Activation 0      update-state: f-1,*
<== f-1     (signpost (time 0) (state "one"))
<== Activation 0      pair-state: f-1,f-6
==> f-7     (state-interval (start 10) (duration 10) (state "four"))
FIRE    3 pair-state: f-2,f-6
<== f-6     (state-update (time 0) (state "one"))
<== Activation 0      pair-state: f-3,f-6
==> Activation 0      update-state: f-2,*
==> Activation 0      update-state: f-3,*
==> f-8     (state-update (time 2) (state "two"))
==> Activation 0      pair-state: f-3,f-8
==> Activation 0      pair-state: f-2,f-8
<== Activation 0      update-state: f-3,*
<== Activation 0      update-state: f-2,*
<== f-2     (signpost (time 2) (state "two"))
<== Activation 0      pair-state: f-2,f-8
==> f-9     (state-interval (start 0) (duration -2) (state "one"))
FIRE    4 pair-state: f-3,f-8
<== f-8     (state-update (time 2) (state "two"))
==> Activation 0      update-state: f-3,*
==> f-10    (state-update (time 5) (state "three"))
==> Activation 0      pair-state: f-3,f-10
<== Activation 0      update-state: f-3,*
<== f-3     (signpost (time 5) (state "three"))
<== Activation 0      pair-state: f-3,f-10
==> f-11    (state-interval (start 2) (duration -3) (state "two"))
<== Focus MAIN
4 rules fired        Run time is 0.00531400000909343 seconds.
752.728640036717 rules per second.
5 mean number of facts (5 maximum).
1 mean number of instances (1 maximum).
2 mean number of activations (4 maximum).

结果:

CLIPS> (facts)
f-0     (initial-fact)
f-7     (state-interval (start 10) (duration 10) (state "four"))
f-9     (state-interval (start 0) (duration -2) (state "one"))
f-10    (state-update (time 5) (state "three"))
f-11    (state-interval (start 2) (duration -3) (state "two"))
For a total of 5 facts.

我觉得这里缺少基本的东西。在我的脑海中,我假设Xcode将随着时间的流逝按顺序喂给我这些路标事实。

1 个答案:

答案 0 :(得分:1)

         CLIPS (6.31 4/1/19)
CLIPS> 
(deftemplate signpost
    (slot time (type INTEGER))
    (slot state (type STRING)))
CLIPS> 
(deftemplate state-interval
    (slot start (type INTEGER))
    (slot duration (type INTEGER))
    (slot state (type STRING)))
CLIPS> 
(defrule find-interval

   ;; Find the first signpost

   (signpost (time ?time1) (state ?state))

   ;; And a second signpost that comes later

   (signpost (time ?time2&:(> ?time2 ?time1)))

   ;; And there's no other signpost between the two

   (not (signpost (time ?time3&:(> ?time3 ?time1)
                              &:(< ?time3 ?time2))))
   =>

   (assert (state-interval (start ?time1)
                           (duration (- ?time2 ?time1))
                           (state ?state))))
CLIPS>    
(deffacts sample-data
    (signpost (time 0) (state "one"))
    (signpost (time 2) (state "two"))
    (signpost (time 5) (state "three"))
    (signpost (time 10) (state "four")))
CLIPS> (watch rules)
CLIPS> (watch facts)
CLIPS> (watch activations)
CLIPS> (reset)
<== f-0     (initial-fact)
==> f-0     (initial-fact)
==> f-1     (signpost (time 0) (state "one"))
==> f-2     (signpost (time 2) (state "two"))
==> Activation 0      find-interval: f-1,f-2,*
==> f-3     (signpost (time 5) (state "three"))
==> Activation 0      find-interval: f-2,f-3,*
==> f-4     (signpost (time 10) (state "four"))
==> Activation 0      find-interval: f-3,f-4,*
CLIPS> (run)
FIRE    1 find-interval: f-3,f-4,*
==> f-5     (state-interval (start 5) (duration 5) (state "three"))
FIRE    2 find-interval: f-2,f-3,*
==> f-6     (state-interval (start 2) (duration 3) (state "two"))
FIRE    3 find-interval: f-1,f-2,*
==> f-7     (state-interval (start 0) (duration 2) (state "one"))
CLIPS> (facts)
f-0     (initial-fact)
f-1     (signpost (time 0) (state "one"))
f-2     (signpost (time 2) (state "two"))
f-3     (signpost (time 5) (state "three"))
f-4     (signpost (time 10) (state "four"))
f-5     (state-interval (start 5) (duration 5) (state "three"))
f-6     (state-interval (start 2) (duration 3) (state "two"))
f-7     (state-interval (start 0) (duration 2) (state "one"))
For a total of 8 facts.
CLIPS>