Is there anyway to make a rule fire earlier given a specific slot value that a fact may have? I was wondering if there is anything like dynamic salience but for slot values Thank you
Is there anyway to make a rule fire earlier given a specific slot value that a fact may have? I was wondering if there is anything like dynamic salience but for slot values Thank you
It's likely there's a more elegant solution to whatever problem you're solving than changing the salience of a rule based on a slot value, but since you can use a global variable for the salience value of a rule, you can dynamically change the salience for a rule by assigning the global a value pulled from a fact slot:
CLIPS (6.31 6/12/19)
CLIPS> (set-salience-evaluation every-cycle)
when-defined
CLIPS>
(defglobal ?*r-1* = 0
?*r-2* = 0)
CLIPS>
(deftemplate rule-priority
(slot rule)
(slot salience))
CLIPS>
(deffacts start
(rule-priority (rule r-1) (salience -10))
(rule-priority (rule r-2) (salience -10))
(prime))
CLIPS>
(defrule assign
(declare (salience 10000))
(rule-priority (rule ?r) (salience ?s))
=>
(eval (str-cat "(bind ?*" ?r "* " ?s ")")))
CLIPS>
(defrule start
?rp <- (rule-priority (rule r-2) (salience ~10))
=>
(modify ?rp (salience 10)))
CLIPS>
(defrule r-1
(declare (salience ?*r-1*))
(prime)
=>)
CLIPS>
(defrule r-2
(declare (salience ?*r-2*))
(prime)
=>)
CLIPS> (reset)
CLIPS> (agenda)
10000 assign: f-2
10000 assign: f-1
0 r-1: f-3
0 r-2: f-3
0 start: f-2
For a total of 5 activations.
CLIPS> (run 2)
CLIPS> (agenda)
0 start: f-2
-10 r-1: f-3
-10 r-2: f-3
For a total of 3 activations.
CLIPS> (run 1)
CLIPS> (agenda)
10000 assign: f-4
-10 r-1: f-3
-10 r-2: f-3
For a total of 3 activations.
CLIPS> (run 1)
CLIPS> (agenda)
10 r-2: f-3
-10 r-1: f-3
For a total of 2 activations.
CLIPS>
You can also dynamically assign the salience of a rule using a deffuction and retrieve the slot value using the fact query functions:
CLIPS> (clear)
CLIPS> (set-salience-evaluation every-cycle)
every-cycle
CLIPS>
(deftemplate rule-priority
(slot rule)
(slot salience))
CLIPS>
(deffunction get-salience (?rule)
(do-for-fact ((?rp rule-priority))
(eq ?rp:rule ?rule)
(return ?rp:salience))
(return 0))
CLIPS>
(deffacts priorities
(rule-priority (rule r-1) (salience -10))
(rule-priority (rule r-2) (salience -10))
(prime))
CLIPS>
(defrule start
?rp <- (rule-priority (rule r-2) (salience ~10))
=>
(modify ?rp (salience 10)))
CLIPS>
(defrule r-1
(declare (salience (get-salience r-1)))
(prime)
=>)
CLIPS>
(defrule r-2
(declare (salience (get-salience r-2)))
(prime)
=>)
CLIPS> (reset)
CLIPS> (agenda)
0 start: f-2
-10 r-1: f-3
-10 r-2: f-3
For a total of 3 activations.
CLIPS> (run 1)
CLIPS> (agenda)
10 r-2: f-3
-10 r-1: f-3
For a total of 2 activations.
CLIPS>