Edi Edi - 5 months ago 8
Java Question

Remove temporary facts in Jess

I am trying to display all instance slot values and in the end remove it after all its bindings are processed through the following rule:

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


But, the rule outputs only one pollution source, because the
unmake-instance
removes the instance and stops other bindings to be displayed.

EDIT:
Motivated by @laune comment I tried the following approach using
foreach
loop, but yet again not output is received:

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

Edi Edi
Answer

Thank Allah and then @laune for opening my mind to solve this issue. The main problem came from slot-get which returns a list not a string. The solution rule follows:

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