Menu

Can a rule act on instance deletion?

Help
2019-04-06
2019-04-08
  • Serge Lozovsky

    Serge Lozovsky - 2019-04-06

    Hi,

    a rule can easily act on instance creation. Can a rule act on deletion of each instance of particular class? (there is an option to use delete handlers, but is it possible to do with rules).

    Here is my test:

    (watch instances)
    (watch slots)
    (watch activations)
    
    (defclass A (is-a USER)
     (slot foo)
     (slot bar))
    
    (defrule match-A-s
     (not (object (is-a A) (foo ?f)))
    =>
      (printout t "=== Removed!" crlf ))
    
    (make-instance a of A (foo 200))
    (make-instance a1 of A (foo 200))
    
    (run)
    
    (unmake-instance [a])
    (unmake-instance [a1])
    
    (run)
    
    (exit)
    

    Run results:

    $ ./clips 
             CLIPS (6.31 4/10/18)
    CLIPS> (batch test2.clp)
    TRUE
    CLIPS> (watch instances)
    CLIPS> (watch slots)
    CLIPS> (watch activations)
    CLIPS> 
    (defclass A (is-a USER)
     (slot foo)
     (slot bar))
    CLIPS> 
    (defrule match-A-s
     (not (object (is-a A) (foo ?f)))
    =>
      (printout t "=== Removed!" crlf ))
    ==> Activation 0      match-A-s: *
    CLIPS> 
    (make-instance a of A (foo 200))
    ==> instance [a] of A
    ::= local slot foo in instance a <- 200
    ::= local slot bar in instance a <- nil
    <== Activation 0      match-A-s: *
    [a]
    CLIPS> (make-instance a1 of A (foo 200))
    ==> instance [a1] of A
    ::= local slot foo in instance a1 <- 200
    ::= local slot bar in instance a1 <- nil
    [a1]
    CLIPS> 
    (run)
    CLIPS> 
    (unmake-instance [a])
    <== instance [a] of A
    TRUE
    CLIPS> (unmake-instance [a1])
    <== instance [a1] of A
    ==> Activation 0      match-A-s: *
    TRUE
    CLIPS> 
    (run)
    === Removed!
    CLIPS> 
    (exit)
    

    "not" acts on deletion of the last instance only (as expected) and instance name/slot values can't be used in RHS.

    There can be work arounds as marking instances for deletion and make rules to act on this mark. Is there more straightforward way?

     
  • Gary Riley

    Gary Riley - 2019-04-08

    There's not a good way to do it other than marking the instance or generating an instance from a delete handler that the rules can match on.

         CLIPS (6.31 4/1/19)
    CLIPS> 
    (defclass EULOGY
    (is-a USER)
    (slot iname)
    (slot class))
    CLIPS> 
    (defclass TRACK
    (is-a USER))
    CLIPS> 
    (defmessage-handler TRACK delete before ()
    (make-instance of EULOGY
      (iname (instance-name ?self))
      (class (class ?self))))
    CLIPS> (defclass A (is-a TRACK))
    CLIPS> 
    (defrule detect-delete
    ?o <- (object (is-a EULOGY) (iname ?n))
    =>
    (unmake-instance ?o)
    (printout t "Deleted " ?n crlf))
    CLIPS> (make-instance a1 of A)
    [a1]
    CLIPS> (make-instance a2 of A)
    [a2]
    CLIPS> (watch instances)
    CLIPS> (watch activations)
    CLIPS> (unmake-instance [a1])
    ==> instance [gen1] of EULOGY
    ==> Activation 0      detect-delete: [gen1]
    <== instance [a1] of A
    TRUE
    CLIPS> (unmake-instance [a2])
    ==> instance [gen2] of EULOGY
    ==> Activation 0      detect-delete: [gen2]
    <== instance [a2] of A
    TRUE
    CLIPS> (run)
    <== instance [gen2] of EULOGY
    Deleted [a2]
    <== instance [gen1] of EULOGY
    Deleted [a1]
    CLIPS>
    
     
  • Serge Lozovsky

    Serge Lozovsky - 2019-04-08

    Thanks.

     

Log in to post a comment.

MongoDB Logo MongoDB