Is there a way to delay execution in CLIPS - clips

I'm writing a clips code that goes through for loop , and it print facts .
I wanna know if there is a way to delay execution for 10 seconds , after printing the first fact , then another 10 second at the second iteration ?
so is there a delay function like java?

There's nothing built-in that allows you to sleep the CLIPS process for a fix amount of time, but you can write a function which loops for a specified amount of time before returning:
CLIPS>
(deffunction pause (?delay)
(bind ?start (time))
(while (< (time) (+ ?start ?delay)) do))
CLIPS> (pause 5)
FALSE
CLIPS>

Related

CLIPS check the boundaries of a (read) value

I'm in the process of designing an expert system from a decision tree and one of the tests is to check the wildlife score. The user is asked to enter the wildlife score and 3 possible outcomes are decided.
A score of at least 20 rejects the proposal and ends the program.
A score of more than 10 but less than 20 moves on to test 4 but asserts the outcome will be second-best.
A score of no more than 10 simply moves to test 4.
The read line from the previous test:
(defrule wildlife-score(or(energy-level 2)(energy-level 3))
=> (printout t "What is the wildlife impact score?" crlf)
(assert(wildlife-impact(read))))
The following is where I am having trouble in comparing the read value to the outcome values. Any help would be appreciated.
(defrule reject-wildlife
(wildlife-impact ? (> ?wildlife-impact 20))
=> (assert(reject))
(printout t "Reject - completely unsuitable due to wildlife impact." crlf))
The correct syntax for your comparison is (wildlife-impact ?varname&:(> ?varname 20)).
(defrule reject-wildlife
(wildlife-impact ?score&:(> ?score 20))
=>
(assert (reject))
(printout t "Reject - completely unsuitable due to wildlife impact." crlf))

clips: modifying assert's variable in antecedent

I've been trying to modify one variable from one assert in the antecedent, but I haven't been able to do it.
In the consequent I can modify variables, with bind, assignments, etc. but I'd like to do the following:
(defrule test
?h<-(Currentposition ?x ?y)
(not (Explored (+ ?x 1) ?y))
=> (whatever)
So, the problem is that I have a character moving through a map, and I want to explore unknown cells, so I want to go to them, and in order to do so, I mark them as Explored.
In the example, I want to check if the south cell has been explored, how can I do it? I add 1 to x because I move 1 row below, x-> rows, y-> columns
Thanks
Use =, the return value constraint:
(defrule test
?h <- (Currentposition ?x ?y)
(not =(Explored (+ ?x 1) ?y))
=>)

Having trouble with a function in Scheme

so i am trying to understand this piece of code, and after staring at it for far too long i decided to ask here if anyone could help me understand how and why it works
(define knock-knock
(letrec ([dig (lambda (i)
(cons (* i (list-ref knock-knock (- i 1)))
(dig (+ i 1))))])
(cons 1 (dig 1))))
the function is then called by name with the value:
(list-ref knock-knock 5)
So my main problem is that i can not see where the letrec would end. the other thing is that i am not given a list, so what is the 4th element in the list that i am supposed to reference in line 3?
First, a note: this is not normal Scheme, as it requires lazy evaluation.
In lazy evaluation, values are only computed when they are needed. So, for defining knock-knock, we can just do
(cons 1 <thunk: (dig 1)>)
i.e., we generate a pair, but we don't need the second element, so we defer its evaluation until later.
When we actually want to evaluate the second element, we will already have knock-knock defined, so we can reference it.
The next element is computed by taking the previous (i-1-st) element, and multiplies it by i. So this will generate the series {n!}: 1,1,2,6,24,...
A straightforward translation of this code to the (normally lazy) Haskell language goes like this:
knock :: [Int]
knock = 1 : dig 1
where dig i = (i * knock !! (i-1)) : dig (i+1)

What are test cases in scheme and how do I use them?

I'm a complete beginner at scheme, and want to know what test cases are, or even do.
For example, if I wanted to write a test case for the negative root quadratic function I already coded and tested, how would I do it?
(define (quadnegative a b c)
(* (/ (+ (sqrt (-(square b) (* 4 a c))) b) 2 a) -1))
;Value: quadnegative
(quadnegative 1 3 -4)
;Value: -4
Thank you in advance.
Start by taking a look at the documentation of your interpreter for the specific details, for instance in Racket here is the testing framework available out of the box.
In essence, a test case will compare the actual value of an expression with the expected value - if they match, then the test succeeds. Here's a basic example of how this works in Racket (assuming that you've selected an appropriate language such as "Beginning Student"):
(define (add-one x)
(+ 2 x)) ; an error!
(check-expect (* 21 2) 42) ; test will succeed
(check-expect (add-one 1) 2) ; test will fail
The above will produce an output like this:
Ran 2 tests.
1 of the 2 tests failed.
No signature violations.
Check failures:
Actual value 3 differs from 2, the expected value.
at line 5, column 0
For your tests, try to imagine test values of interest. Write some tests for inputs that return real values:
(check-expect (quadnegative 1 3 -4) -4)
And then, test for inputs that return imaginary values ... and so on. Try to be thorough in your tests, covering as many cases as possible, specially the unusual or "weird" cases that might result in "strange" output values.

(Random) in Common Lisp Not So Random?

Okay, final question and I'll have finished my number guessing game in Common Lisp! :D Whenever the game starts (or a new game begins after the first game), the following function is called.
;;; Play the game
(defun play ()
;; If it's their first time playing this session,
;; make sure to greet the user.
(unless (> *number-of-guesses* 0)
(welcome-user))
;; Reset their remaining guesses
(setq *number-of-guesses* 0)
;; Set the target value
(setq *target*
;; Random can return float values,
;; so we must round the result to get
;; an integer value.
(round
;; Add one to the result, because
;; (random 100) yields a number between
;; 0 and 99, whereas we want a number
;; from 1 to 100 inclusive.
(+ (random 100) 1)))
(if (eql (prompt-for-guess) t)
(play)
(quit)))
So supposedly, each time the player starts a game, *target* should be set to a new random integer between 1-100. However, each time, *target* defaults to 82. How do I make (random) act... randomly?
You need to seed the random state at the start of the program.
(setf *random-state* (make-random-state t))
;; # this initializes the global random state by
;; "some means" (e.g. current time.)
I think that if you define a function with a random number in it, it is not called when you call the function, actually, it will be determined when you load in the file and when it runs this definition it is fixed to that value. Then you are calling the function each time, the number will always be the same. When I passed in a variable to the function with a random each time it was called, then it was random each time. At least, that what I experienced in my program
In Gimp Scheme (Lisp derivative) I found that I needed to use the following:
(set! *seed* (car (gettimeofday)))
This is the Random Number Seed From Clock (seed is global)
You may also need:
(random-next)
This generates the next Random Number Using Seed
The non-truly random method is to set the seed:
(srand 12345)
; used in cases where the result is to be repeatable - useful in graphic design contexts.

Resources