簡體   English   中英

避免模​​式匹配(錯誤),直到正確設置插槽為止

[英]Avoid pattern matching (errors) until a slot is set correctly

規則R_blup的LHS包含

(test (>= ?s2 2))

也就是說,它檢查?s2是否大於或等於?s2對應於名為s2的實例插槽。

不幸的是,我得到了錯誤

Function >= expected argument #1 to be of type integer or float

問題是我的代碼在設置參數#1之前,即在將s2設置為整數或浮點值之前執行了(test ...應該在python調用中將s2設置為整數。由另一個規則R_blah觸發。

該錯誤在屬於另一個規則R_xyz的另一個python調用的中間觸發。 此python調用通過clips_instance.Slots["slot_name"] = some_value修改實例。

通常如何處理? 我看到了我不太喜歡的三種解決方案:

  1. 設置s2的默認(整數)值。
  2. 修改(test ...以首先檢查nil
  3. 添加另一個檢查/規則以等待s2不再nil

是否有可能嘗試/排除/傳遞錯誤?

使用函數object-pattern-match-delay延遲模式匹配,以針對一系列更改創建原子操作:

CLIPS> (defclass POINT (is-a USER) (slot x) (slot y))
CLIPS> 
(defrule check 
   (object (is-a POINT) (x ?s2))
   (test (>= ?s2 2))
   =>)
CLIPS> (make-instance [p1] of POINT)
[ARGACCES5] Function >= expected argument #1 to be of type integer or float

[DRIVE1] This error occurred in the join network
   Problem resides in associated join
      Of pattern #1 in rule check

[p1]
CLIPS> (agenda)
CLIPS> 
(object-pattern-match-delay
   (make-instance [p2] of POINT)
   (make-instance [p3] of POINT)
   (send [p2] put-x 3)
   (send [p3] put-x 0))
0
CLIPS> (agenda)
0      check: [p2]
For a total of 1 activation.
CLIPS> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM