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>
Related
As described by the question, I would somehow try to get an handle to a fact within a deffacts construct. The problem arises because I don't want to redefine the same thing several times in WM (since set-fact-duplication is true) and because I use a structured deftemplate in which a field is a FACT_ADDRESS.
You can't bind a fact address within a deffacts construct. What I would suggest instead is to use a symbolic link between the facts. In your case, if the name of the tourism-type, tourism-resort, and hotel facts is unique among facts of each type, you could use that slot as the symbolic link:
(deftemplate tourism-type
(slot name)
(slot score))
(deftemplate hotel
(slot name)
(slot tr)
(slot stars)
(slot price-per-night))
(deftemplate tourism-resort
(slot name)
(slot region)
(multislot type))
(deffacts the-tourism-type-list
(tourism-type (name culturale) (score 3))
(tourism-type (name enogastronomico) (score 4)))
(deffacts the-tourism-resort-list
(tourism-resort
(name Venezia)
(region Veneto)
(type culturale enogastronomico)))
(deffacts the-hotels-list
(hotel
(name hotel1)
(tr Venezia)
(stars 3)
(price-per-night 100)))
In your rules, you can then use the symbolic link to retrieve the linked fact:
(defrule food-and-wine-hotels
(hotel (name ?hotel)
(tr ?tr-name))
(tourism-resort
(name ?tr-name)
(type $? enogastronomico $?))
=>
(printout t ?hotel crlf))
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>
I am very puzzled by CLIPS. I have defined in a .clp file a deftemplate and a rule.
(deftemplate basic-ch "Basic characteristics template"
(slot ch-name
(type SYMBOL)
(default ?DERIVE)
)
(slot score
(type INTEGER)
(default 1)
(range 1 5)
)
)
(defrule make-ch
?get-ch <- (get-ch TRUE)
=>
(printout t "Enter ch name" crlf)
(bind ?name (read))
(printout t "Enter ch score" crlf)
(bind ?score (read))
(assert (basic-ch (ch-name ?name) (score ?score)))
(retract ?get-ch)
)
When i (assert (get-ch TRUE)) and (run), it prompts me for the ch name and score. However, if I enter a string for the score, the string score gets asserted by the rule! For example:
Enter ch name
hello
Enter ch score
hello
;(basic-ch (ch-name hello)(score hello)) get asserted?!
How is this possible? I have defined the score to be INTEGER and even provided the range. How can I stop this?
From Section 11, Constraint Attributes, of the Basic Programming Guide:
Two types of constraint checking are supported: static and dynamic.
When static constraint checking is enabled, constraint violations are
checked when function calls and constructs are parsed. This includes
constraint checking between patterns on the LHS of a rule when
variables are used in more than one slot. When dynamic constraint
checking is enabled, newly created data objects (such as deftemplate
facts and instances) have their slot values checked for constraint
violations. Essentially, static constraint checking occurs when a
CLIPS program is loaded and dynamic constraint checking occurs when a
CLIPS program is running. By default, static constraint checking is
enabled and dynamic constraint checking is disabled. The default
behavior can be changed by using the set-static-constraint-checking
and set-dynamic-constraint-checking functions.
If you enable dynamic constraint checking, you'll get an error when you run your program:
CLIPS> (set-dynamic-constraint-checking TRUE)
TRUE
CLIPS> (assert (get-ch TRUE))
<Fact-1>
CLIPS> (run)
Enter ch name
hello
Enter ch score
hello
[CSTRNCHK1] Slot value hello found in fact f-2
does not match the allowed types for slot score.
[PRCCODE4] Execution halted during the actions of defrule make-ch.
CLIPS>
Because it generates an error, dynamic constraint checking is useful for testing, but not for validating user input while a program is executing. If you want to validate use input, define some utility methods:
CLIPS>
(defmethod get-integer ((?query STRING))
(bind ?value FALSE)
(while (not (integerp ?value))
(printout t ?query " ")
(bind ?value (read)))
?value)
CLIPS>
(defmethod get-integer ((?query STRING) (?lower INTEGER) (?upper INTEGER))
(bind ?value FALSE)
(while (or (not (integerp ?value)) (< ?value ?lower) (> ?value ?upper))
(printout t ?query " (" ?lower " - " ?upper ") ")
(bind ?value (read)))
?value)
CLIPS> (get-integer "Pick an integer:")
Pick an integer: hello
Pick an integer: 3
3
CLIPS> (get-integer "Pick an integer" 1 5)
Pick an integer (1 - 5) -1
Pick an integer (1 - 5) hello
Pick an integer (1 - 5) 8
Pick an integer (1 - 5) 4
4
CLIPS>
I just want a function to print on item per line.
I am trying:
(deffunction myprint (?first $?rest)
(if (neq ?rest nil) then
(printout t ?first crlf)
(myprint ?rest)))
What is wrong?
Use the length function to determine if a list is empty (a return value of 0). Comparing a list to the symbol nil will always fail.
You want to print ?first even if ?rest is empty. Otherwise the last element will never be printed.
It is not necessary to use recursion.
CLIPS>
(deffunction myprint ($?rest)
(foreach ?r $?rest
(printout t ?r crlf)))
CLIPS> (myprint a b c)
a
b
c
CLIPS> (myprint (create$ a b) (create$ c d))
a
b
c
d
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))
)