Question

Using Jess as the rule engine, we can assert a fact that some witness has seen a person, at some place, and with time associated:

(deffacts witnesses
    (witness Batman Gotham 18)
    (witness Hulk NYC 19)
    (witness Batman Gotham 2)
    (witness Superman Chicago 22)
    (witness Batman Gotham 10)
)

With a rule, I want to know if several witnesses have seen the same person at the same place, not considering time.

In Jess documentation we got this example for counting employees making salraries of 100K and more:

(defrule count-highly-paid-employees
    ?c <- (accumulate (bind ?count 0)                        ;; initializer
    (bind ?count (+ ?count 1))                    ;; action
    ?count                                        ;; result
    (employee (salary ?s&:(> ?s 100000)))) ;; CE
    =>
    (printout t ?c " employees make more than $100000/year." crlf))

So I based my code on previous example:

(defrule count-witnesses
    (is-lost ?plost)
    (witness ?pseen ?place ?time)
    ?c <- (accumulate (bind ?count 0)
    (bind ?count (+ ?count 1))
    ?count
    (test ())  ; conditional element of accumulate
    (test (= ?plost ?pseen))
    (test (>= ?count 3))
    =>
    (assert (place-seen ?place))
)

With the '(deffacts)' instruction provided above and the rule, the engine should assert the fact

(place-seen Gotham)

because we have Batman seen three times in Gotham.

I have no idea how to use the conditional element (CE) part of 'accumulate'. Can I use a 'test' to retain facts with same person and place?

Any idea how to achieve this?

Thank you!


Note : synthax of 'accumulate' is

(accumulate <initializer> <action> <result> <conditional element>)
Was it helpful?

Solution

I'm going to leave out the stuff about ?plost, as you don't explain what that is, exactly; you can add it back in yourself if you need to.

A basic rule that (almost) does what you want is as follows. The CE part you weren't getting is just a pattern that we want to accumulate over; here, it matches facts with the same person witnessed at the same place as the fact matched by the first person:

(defrule count-witnesses
  ;; Given that some person ?person was seen in place ?place
  (witness ?person ?place ?)  
  ;; Count all the sightings of that person in that place  
  ?c <- (accumulate (bind ?count 0)
                    (bind ?count (+ ?count 1))
                    ?count
                    (witness ?person ?place ?))
  ;; Don't fire unless the count is at least 3
  (test (>= ?c 3))
  =>
  (assert (place-seen ?person ?place))
)

Now, the only problem with this rule is that it'll fire three times for your deffacts, once for every one of the Batman/Gotham facts. We can stop this by changing the first pattern to match only the earliest sighting of a person at a given place:

(defrule count-witnesses
  ;; Given that some person ?person was seen in place ?place, and there is no other 
  ;; sighting of the same person at the same place at an earlier time
  (witness ?person ?place ?t1)    
  (not (witness ?person ?place ?t2&:(< ?t2 ?t1)))
  ;; Count all the sightings of that person in that place  
  ?c <- (accumulate (bind ?count 0)
                    (bind ?count (+ ?count 1))
                    ?count
                    (witness ?person ?place ?))
  ;; Don't fire unless the count is at least 3
  (test (>= ?c 3))
  =>
  (assert (place-seen ?person ?place))
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top