如何将事实配对以创建新事实?
Posted
技术标签:
【中文标题】如何将事实配对以创建新事实?【英文标题】:How can I pair up facts to create new facts? 【发布时间】:2019-06-14 10:45:36 【问题描述】:我是 CLIPS 的新手,我正在尝试使用它在 Xcode 中创建自定义 Instrument 来模拟枚举中的状态变化。
我想以一系列事实结束,这些事实表示枚举的开始和结束时间处于给定状态。
每当我的代码中发生状态更改时,我都会发出一个事件路标,这可以简化为:
(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】: 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>
【讨论】:
啊,条件语法正是我所缺少的。我猜每个 LHS 比赛都是轮流进行的,所以 CLIP 不会循环遍历所有可能的组合。这很有意义 - 谢谢。以上是关于如何将事实配对以创建新事实?的主要内容,如果未能解决你的问题,请参考以下文章