Ramachandran
Ramachandran

Reputation: 1

variable bound check in a clips rule RHS

checkIntfIntVlanMemberConfigRule = """
    (defrule checkSubIntfIntVlanMemberConfigRule
    (checkIntf (intf ?intf) )
    (SwitchIntfConfig (intf ?intf) (switchportMode "routed") (nativeVlan          ?intVlan))
    (or (not (VlanStatus (vlan ?intVlan) (intf ?intf)) )
     ?f <- (VlanStatus (vlan ?intVlan) (intf ?intf)) )
    =>
    (if (isbound ?f) then (printout t "PASS: vlanStatus exists for " ?intf " " ?intVlan crlf) (return 0) )
    (printout t "vlanStatus does not exist for " ?intf " " ?intVlan crlf)
    )"""

In the above clips rule, what is the equivalent clips buildin function for (isbound ?f) ? In general is there any buildin function to check in RHS if a variable was bound in LHS?

Upvotes: 0

Views: 296

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

There is no functionality for determining if a variable has been bound. The or conditional element is implemented by creating rules for each conditional element contained with the or, so your existing rule is converted to the following:

 (defrule checkSubIntfIntVlanMemberConfigRule-1
    (checkIntf (intf ?intf) )
    (SwitchIntfConfig (intf ?intf) (switchportMode "routed") (nativeVlan ?intVlan))
    (not (VlanStatus (vlan ?intVlan) (intf ?intf)) 
    =>
    (if (isbound ?f) then (printout t "PASS: vlanStatus exists for " ?intf " " ?intVlan crlf) (return 0) )
    (printout t "vlanStatus does not exist for " ?intf " " ?intVlan crlf)
    )

 (defrule checkSubIntfIntVlanMemberConfigRule-2
    (checkIntf (intf ?intf) )
    (SwitchIntfConfig (intf ?intf) (switchportMode "routed") (nativeVlan ?intVlan))
    ?f <- (VlanStatus (vlan ?intVlan) (intf ?intf)) 
    =>
    (if (isbound ?f) then (printout t "PASS: vlanStatus exists for " ?intf " " ?intVlan crlf) (return 0) )
    (printout t "vlanStatus does not exist for " ?intf " " ?intVlan crlf)
    )

You need to implement this as two separate rules so the RHS for each can be different:

(defrule checkSubIntfIntVlanMemberConfigRule-1
    (checkIntf (intf ?intf) )
    (SwitchIntfConfig (intf ?intf) (switchportMode "routed") (nativeVlan ?intVlan))
    (not (VlanStatus (vlan ?intVlan) (intf ?intf))) 
    =>
    (printout t "vlanStatus does not exist for " ?intf " " ?intVlan crlf)
    )

 (defrule checkSubIntfIntVlanMemberConfigRule-2
    (checkIntf (intf ?intf) )
    (SwitchIntfConfig (intf ?intf) (switchportMode "routed") (nativeVlan ?intVlan))
    ?f <- (VlanStatus (vlan ?intVlan) (intf ?intf)) 
    =>
    (printout t "PASS: vlanStatus exists for " ?intf " " ?intVlan crlf)
    )

Alternately you can test for the existence of the fact from the RHS of the rule using the fact query functions:

  (defrule checkSubIntfIntVlanMemberConfigRule
    (checkIntf (intf ?intf) )
    (SwitchIntfConfig (intf ?intf) (switchportMode "routed") (nativeVlan ?intVlan))
    (VlanStatus (vlan ?intVlan) (intf ?intf)) 
    =>
    (if (any-factp ((?f VlanStatus)) (and (eq ?f:vlan ?intVlan) (eq ?f:intf ?intf)))
      then
      (printout t "PASS: vlanStatus exists for " ?intf " " ?intVlan crlf)
      else 
     (printout t "vlanStatus does not exist for " ?intf " " ?intVlan crlf)))

Upvotes: 0

Related Questions