删除 Jess 中的临时事实

Remove temporary facts in Jess

我正在尝试显示所有实例槽值,并在通过以下规则处理所有绑定后最终将其删除:

(defrule findSourcesOfPollution
    (object 
        (is-a http..#PollutionSources)              
        (OBJECT ?sitepoll)          
        (http..#potentialPollutant          
        $? ?b&:(eq (instance-name ?b)(instance-name http..#BiochemicalOxygenDemand)) $?) 
        (..#pollutionSourceName ?psName) 
        (http..#pollutionType ?psType) 
    )     
    (object 
        (is-a http..#MeasurementSite) 
        (OBJECT ?loc)
        (http..#hasSourcesOfPollution $? ?sitepoll $?)
    )
    (object 
            (is-a http..#tmpModerateMeasurement) 
            (OBJECT ?mob)
            (http..#observationResultLocation ?loc)
    )
=> 
    (printout t "pollution sources: " (instance-name ?sitepoll) " (Type: " ?psType ")" crlf)
    (unmake-instance ?mob)
)

但是,规则只输出一个污染源,因为unmake-instance删除了实例并停止显示其他绑定。

编辑: 受@laune 评论的启发,我使用 foreach 循环尝试了以下方法,但仍然没有收到输出:

(defrule findSourcesOfPollution 
    (object 
        (is-a http..#MeasurementSite) 
        (OBJECT ?loc)
        (http..#hasSourcesOfPollution $?sitepoll)
    )
    (object 
            (is-a http..#tmpModerateBODMeasurement) 
            (OBJECT ?mob)
            (http..#observationResultLocation ?loc)
    )
    (bind ?poll nil)
=> 
    (foreach ?poll ?sitepoll 
        (slot-get ?poll http..#potentialPollutant)
        (if(eq ?poll http..#BiochemicalOxygenDemand) then 
            (printout t "Pollution source: " (instance-name ?poll) " (Type: " ?loc ")" crlf)
        )
    )
)

将站点列表绑定到列表变量

(object 
    (is-a http..#MeasurementSite) 
    (OBJECT ?loc)
    (http..#hasSourcesOfPollution $?sitepoll )
) 

并在右侧迭代列表:

(foreach ?site ?sitepoll (printout ...))

感谢真主,然后感谢@laune 打开我的思路来解决这个问题。主要问题来自 slot-get,其中 returns 是列表而不是字符串。求解规则如下:

(defrule findBOD5SourcesOfPollution 
    (object 
        (is-a http..#MeasurementSite) 
        (OBJECT ?loc)
        (http..#hasSourcesOfPollution $?sitepoll)
    )
    (object 
            (is-a http..#tmpModerateBODMeasurement) 
            (OBJECT ?mob)
            (http..#observationResultLocation ?loc)
    )
=> 
    (foreach ?poll ?sitepoll 
        (foreach ?pollLsItem (slot-get ?poll http..#potentialPollutant)
            (if(eq (instance-name ?pollLsItem) http..#BiochemicalOxygenDemand) then 
                (printout t "BOD5 pollution source: " (instance-name ?poll) " (Type: " (instance-name ?loc) ")" crlf)
            )
                )
    )
    (unmake-instance ?mob)
)