clips: avoiding connective constraints - clips

Let's suppose we have the following CLIPS template and rule:
;; The queue sequence starts at 0.
;; -1 is a placeholder value to identify a newly inserted element.
;; The idea is to put a newly inserted element at the end of the queue.
(deftemplate queue-element
(slot order (type INTEGER) (default -1))
(deftemplate put-at-the-end
?e1 <- (queue-element (order -1))
?e2 <- (queue-element (order ?o1))
(not (queue-element (order ?o2&:(> ?o2 ?o1))))
=>
(modify ?e1 (order (+ ?o1 + 1))))
Is there a way to move the "connective constraint" (> ?o2 ?o1) out of the pattern and move it to something similar to a (test (> ?o2 ?o1)) construct instead?
The idea is to avoid these in-line conditions enterily.

You can use the and conditional element to place several conditional elements within a not conditional element.
CLIPS>
(deftemplate queue-element
(slot order (type INTEGER) (default -1)))
CLIPS>
(defrule put-at-the-end
?e1 <- (queue-element (order -1))
?e2 <- (queue-element (order ?o1))
(not (and (queue-element (order ?o2))
(test (> ?o2 ?o1))))
=>
(modify ?e1 (order (+ ?o1 1))))
CLIPS>

Related

How to stop rule repeating activation on facts that have already been modified?

I have a rule I have made in CLIPS that deletes two values from a multislot field. Even though it does this, the rule repeats itself on the same facts that have now deleted the two values and this goes on infinitely.
Below are my fact templates and the rule
(deftemplate ar-node
(slot group
(type SYMBOL)
(allowed-symbols grp1 grp2 grp3 grp4) )
(slot name
(type SYMBOL)
(allowed-symbols oc nps ef sef yn))
(slot direction
(type SYMBOL)
(allowed-symbols + -))
(slot element
(type INTEGER)
(range 1 3))
(slot trip
(type INTEGER)
(range 1 4))
(multislot allowed-values
(type SYMBOL)
(allowed-symbols D R L A C S nil)
(default D))
(slot value
(type SYMBOL)
(allowed-symbols D R L A C S nil)
(default D)))
(defrule 22_061
(ar-node (group ?group)
(name ?name)
(direction ?direction)
(element ?element)
(trip ?trip)
(value ?value&R))
=>
(do-for-all-facts ((?fact ar-node)) (and (eq ?fact:group ?group)
(eq ?fact:name ?name)
(eq ?fact:direction ?direction)
(eq ?fact:element 1)
(> ?fact:trip 1))
(modify ?fact (allowed-values (delete-member$ ?fact:allowed-values C S))))
)
Here is also some example facts that will cause the rule to execute (the rule would only delete the C and S from the second fact here)
(ar-node (group grp1) (name cool) (direction +) (element 1) (trip 1) (allowed-values L D R A C S) (value R))
(ar-node (group grp1) (name cool) (direction +) (element 1) (trip 2) (allowed-values L D R A C S) (value L))
I have tried using more parameters to only delete values if the values are present such as (eq $member ?fact:allowed-values C S) in the RHS (and) statement or even to the LHS of the rule. However these either doesn't work or the rule won't execute at all.
I think the solution will be some way to check that the fact has C or S in the multifield on the LHS but I don't know how I could search all the facts before hand like I do on the RHS. Also I would prefer not to have to edit the fact template too though if its necessary to store something that.
Any advice or suggestions are welcome and I am new to CLIPS so sorry if this may be trivial but I'm super stumped even after using a bunch of functions from the documentation.
You can modify the query in the RHS to check for the presence of C or S:
(defrule 22_061
(ar-node (group ?group)
(name ?name)
(direction ?direction)
(value R))
=>
(do-for-all-facts ((?fact ar-node)) (and (eq ?fact:group ?group)
(eq ?fact:name ?name)
(eq ?fact:direction ?direction)
(eq ?fact:element 1)
(> ?fact:trip 1)
(or (member$ C ?fact:allowed-values)
(member$ S ?fact:allowed-values)))
(modify ?fact (allowed-values (delete-member$ ?fact:allowed-values C S))))
)
Or you can use pattern matching in the LHS and allow the rule to fire multiple times to modify all the facts:
(defrule 22_061
(ar-node (group ?group)
(name ?name)
(direction ?direction)
(value R))
?fact <- (ar-node (group ?group)
(name ?name)
(direction ?direction)
(element 1)
(trip ?trip&:(> ?trip 1))
(allowed-values $?b C | S $?e))
=>
(modify ?fact (allowed-values (delete-member$ (create$ ?b ?e) C S))))

I need help comparing numbers in defrule with multislot objects in CLIPS

I have the following code
(deftemplate choice-evaluation
(slot x_front)
(slot y_left)
(deftemplate es_result
(slot classp)
(slot classr)
(slot nr) )
(defrule MAIN::one
(x_front ?val)
(bind ?temp 1)
(choice-evaluation(y_left -1) (test (<= ?val ?temp ))(class ?x))
?c<-(choice-evaluation(y_left -1) (test(<= ?val ?temp ))(class ?x))
=>
(assert (es_result (classp 0) (classr ?x) (nr 1)))
(retract ?c))
and I get the error : ERROR:
(defrule MAIN::one
(x_front ?val)
(choice-evaluation (y_left -1) (test
I have tried a lot of things but I will always either get the error above or syntax error for defrules . I do not understand how I can have a comparison in defrules with multislot objects.
I tried creating a rule like the examples here :https://www.csie.ntu.edu.tw/~sylee/courses/clips/advpattern.htm and they do work but it doesnt work for my particular case.
There are several errors in your code which are easier to see if you ident the code:
(defrule MAIN::one
(x_front ?val)
(bind ?temp 1)
(choice-evaluation (y_left -1)
(test (<= ?val ?temp))
(class ?x))
?c <- (choice-evaluation (y_left -1)
(test (<= ?val ?temp))
(class ?x))
=>
(assert (es_result (classp 0) (classr ?x) (nr 1)))
(retract ?c))
You've placed test conditional elements inside choice-evaluation conditional elements so the parser treats the test symbols as slot names and since the choice-evaluation deftemplate has no slot named test you get an error. You'll also get an error for using class as a slot name since this slot is also not defined in the choice-evaluation deftemplate.
Also, (bind ?temp 1) will be treated as a fact pattern, not a function call. If you want to retrieve a value in the conditions of a rule, assert a fact containing that value and assign it to a variable using a pattern like the x_front pattern in your rule. Duplication of the choice-evaluation pattern serves no purpose--Just keep the 2nd one since it binds ?c to the fact-address matching the pattern which is need by the retract command in the actions of the rule.
#Gary Riley thanks for your input it helped a lot!
(choice-evaluation (x_front ?val) (y_left -1) (class ?x))
(test (<= ?val 1 ))
?c <- (choice-evaluation (x_front ?val) (y_left -1) (class ?x))
=>
(assert (es_result (classp 0) (classr ?x) (nr 1)))
(retract ?c))
So turns out this is how it is supposed to be used. Leaving it here in case someone else in the future has a similar problem
credits : I. Xatziligeroudis

Avoid pattern matching (errors) until a slot is set correctly

The LHS of a rule R_blup contains
(test (>= ?s2 2))
that is, it checks if ?s2 is greater or equal to 2. ?s2 corresponds to an instance slot named s2.
Unfortunately, I get the error
Function >= expected argument #1 to be of type integer or float
The problem is that my code executes the (test ... before I can set argument #1, i.e. before I can set s2 to an integer or float value. s2 is supposed to be set to an integer inside a python-call that is triggered by another rule R_blah.
The error is triggered in the middle of another python-call belonging to another rule R_xyz. This python-call modifies an instance via clips_instance.Slots["slot_name"] = some_value.
How is this normally handled? I see three solutions I don't like too much:
Setting a default (integer) value for s2.
Modifying the (test ... to check against nil first.
Adding another check/rule to wait until s2 is not nil any more
Is it maybe possible to try/except/pass the error?
Use the function object-pattern-match-delay to delay pattern matching to create an atomic operation for a series of changes:
CLIPS> (defclass POINT (is-a USER) (slot x) (slot y))
CLIPS>
(defrule check
(object (is-a POINT) (x ?s2))
(test (>= ?s2 2))
=>)
CLIPS> (make-instance [p1] of POINT)
[ARGACCES5] Function >= expected argument #1 to be of type integer or float
[DRIVE1] This error occurred in the join network
Problem resides in associated join
Of pattern #1 in rule check
[p1]
CLIPS> (agenda)
CLIPS>
(object-pattern-match-delay
(make-instance [p2] of POINT)
(make-instance [p3] of POINT)
(send [p2] put-x 3)
(send [p3] put-x 0))
0
CLIPS> (agenda)
0 check: [p2]
For a total of 1 activation.
CLIPS>

return two values in clips function

I am writing a project in clips where I have some coordinated entities (squares of a board game). I define their templates like this:
(deftemplate square
(slot x (type INTEGER))
(slot y (type INTEGER))
)
So I want a function that can get a direction argument like right, left, up, down and the ?x, ?y coords and return the coords of the square lying in that direction (bordering the current one).
The problem is that functions can return a single value while I need both x, y.
I have tried
(return ?x ?y)
and
(return (?x ?y))
but they both give syntax errors.
is there a way to achieve that or I need to workaround it?
Thank you for your time.
Use create$ to place multiple values within a multifield value. You can then use nth$ to retrieve individual values:
CLIPS>
(deffunction direction ()
(return (create$ 1 -1)))
CLIPS> (direction)
(1 -1)
CLIPS> (nth$ 1 (direction))
1
CLIPS> (nth$ 2 (direction))
-1
CLIPS>

How can I run the clips with out reset the fact when using CLIPS

Here is my situation:
I want to run the CLIPS periodically and the system can record how many times it runs.
for example: I type in the terminal "run" many times to call the system periodically. then the system can record how many the system runs and show it on the screen. Here is my .clp file
(defglobal ?*lock* = 0)
(deftemplate counter
(slot number))
(deffacts initial_data
(counter (number 0))
)
(defrule set_counter
?f<-(counter (number ?x))
(test (= ?*lock* 0))
=>
(bind ?*lock* 1)
(printout t "plus 1" crlf)
(modify ?f (number (+ ?x 1)))
)
(defrule show_result
?f<-(counter (number ?x))
(test (= ?*lock* 1))
=>
(printout t "the counter value has been changed:" crlf)
(ppfact (fact-index ?f) t)
(bind ?*lock* 0)
)
I use a global value as a lock to control the rules and store the running times in the fact named counter. Now is my problem: Once the system finishes running for the first time. There are no rules in the agenda any more. I want the system can run again with out resetting the facts and the system can process the facts saved form the first running process. How can I refresh the agenda or rematch the rule without reset the facts?
I have find some commands like (refresh rule-name) and (refresh-agenda) but they can not solve my problem. I just type in "(refresh set_counter)" and "(refresh-agenda)" after "(run)". However, no rules are added into agenda.
I don't know if there are solution to my problem or clips can not work like this?
Another question is I try (refresh rule-name) with
(defglobal ?*lock* = 0)
(deftemplate counter
(slot number))
(deftemplate set
(slot number))
(deffacts initial_data
(counter (number 0))
)
(defrule set_counter
(initial-fact)
=>
(bind ?*lock* (+ ?*lock* 1))
(printout t ?*lock* crlf)
)
It works fine. I don't know why it doesn't work in the first example?
really thanks for any advice.
Global variables don't trigger pattern matching so you shouldn't use them in the condition of a rule unless the value is never changed. If you use a fact to represent the lock, the rules will execute for as many cycles as you specify:
(deftemplate lock
(slot value))
(deftemplate counter
(slot number))
(deffacts initial_data
(counter (number 0))
(lock (value 0))
)
(defrule set_counter
?f <- (counter (number ?x))
?l <- (lock (value 0))
=>
(modify ?l (value 1))
(printout t "plus 1" crlf)
(modify ?f (number (+ ?x 1)))
)
(defrule show_result
?f <- (counter (number ?x))
?l <- (lock (value 1))
=>
(printout t "the counter value has been changed:" crlf)
(ppfact (fact-index ?f) t)
(modify ?l (value 0))
)

Resources