删除Jess

时间:2016-05-24 20:54:55

标签: java protege 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)
        )
    )
)

2 个答案:

答案 0 :(得分:0)

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

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

并在右侧迭代列表:

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

答案 1 :(得分:-1)

感谢Allah,然后@laune开始思考解决这个问题。主要问题来自slot-get,它返回的列表不是字符串。解决方案规则如下:

(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)
)