JESS function similar to findall in Prolog? - prolog

I am wondering if it is possible to write a jess function or rules to behave findall in Prolog.
For instance, we have a KB like
(person (name jason) (school nyu))
(person (name john) (school nyu))
(person (name liz) (school cmu))
...
and the input is (school nyu), we want to collect all the names of person
like [jason,john]
Any comments would be appreciated!

You can write a rule like this using the accumulate CE, or you can write it as a function using a defquery. Neither approach is using a rule engine the way it's really intended to be used, though; best to write a rule which matches one such entity and acts on it, and then let the rule fire once for each match.

Related

Clips: Restriction in the variable of a vector to search

This is the Clips code that i am trying to arrange. I have some vector in de BH and I want to search among them, those which ?P is common between them. Moreover in the second vector to search the restriction is that ?E can only be that types. Please help me.
(defrule padre
(es-padre ?P ?H)
(?E & :(tigre | leopardo | jirafa | cebra | avestruz | pinguino | albatros) ?P)
=>
(assert (?E ?H))
)
Is there any solution implementing a Switch case, or the unique solution is making more rules?
You have multiple syntax errors, so it's hard to tell what you're trying to do. To start, the first field of a pattern or fact you're asserting can't be a variable. If you include examples in your question (such as the facts you're asserting and what should happen when your rule executes) it will make it easier to answer your question.

Get rule body in CLIPS

I am new to CLIPS development and I need to retrieve a rule body and store it in string in order to parse it. I tried to redirect the defrule stream but without success.
Is there any way to do it like that or does it exist a special command that I would have forgotten.
(defrule one
(fact a)
=>
(assert (fact b)))
(bind ?str (ppdefrule one))
one rule displaying but ?str is empty
Thank you for your time and consideration.
There's not a clean way to do this out of the box, but there's a C API for retrieving the text, so you could extend CLIPS with a user-defined function to allow you to do this. The alternative would be to use dribble-on/dribble-off to capture the output in a file, but this would also display the output to the screen each time you retrieved the rule text.

Forward Chaining using variables in rules in CLIPS

So, I'm trying to make a "golf club recommendation system" for a 18 hole course. Now, after having defined the basic templates for the golfcourse, golf club and the golf player, I'm stuck due to the large search space this particular problem presents. So currently I have:
(defrule teeoff
?g <- (golfer (position "tee"))
=>
(retract ?g)
(assert (golfer (position "fairway") (Current_club "driver") (Yardage 650))
After this, the ball is on fairway and can have a combination of factors say, it can be on sand, it can be on rough or it could be on a normal green. My question is instead of making a rule for every possibility can I have one or two rules like:
(defrule makemove
?m <- (golfer (position ?x))
?go <- (golfcourse (obstacles ?$y)
=>
(assert (golfer (Current_club ?c)))
If not, then what alternatives do I have?
I suggest to you to design an object or attribute to manage the ground_material (sand, green, ...) and include it inside the rule.
You can write rule for each ground or use IF-THEN condition inside a single rule.
Hope this helps you.
bye
Nic

CLIPS: Retrieving a fact and getting a pointer to it

I am working with CLIPS embedded in a C program, so I have to do everything by C function calls and can't use the CLIPS command line.
Let's say I have asserted a few facts like this:
AssertString("(pizza 1)");
AssertString("(cauliflower 7)");
AssertString("(cheesecake 0)");
Obviously I do not get (let alone retain) any pointers to my facts at this point. When I want to retract a fact later by using Retract(factPtr), I obviously need the pointer to the fact I want to retract. So, after the lines above, how would I find the fact (cauliflower 7) again and get a pointer to it?
Do I have to get the entire fact list by GetFactList([...]), loop through it and compare strings? If so, how would I do that in the multifield DATA_OBJECT this function returns? Or is there a better way?
I would be grateful for any ideas or even code examples.
You can use the fact query functions to query the fact-list and perform actions. You can invoke this through the EvalFunction:
DATA_OBJECT result;
Eval("(do-for-all-facts ((?f pizza)) (eq ?f:implied (create$ 1)) (retract ?f))",&result);
Eval("(do-for-all-facts ((?f cauliflower)) TRUE (retract ?f))",&result);
In the first call, only the pizza fact with the value 1 is retracted. In the second call, all cauliflower facts are retracted.

Scheme let syntax error

(define (rec base height)
(let ((product (* base height))(half 0.5))
(let ((sum (* product half)))
(display "Area is")
(display sum))))
let: expected only one expression after the name-defining sequence, but found one extra part in: (display sum)
I am having the error as above, I don't know which part goes wrong
In full Scheme, this is allowed. However, you are probably using one of the teaching variants of Scheme (such as Intermediate Student or Advanced Student) that Racket provides, which disallows functions with more than one expression.
I'd say you can work around it by using begin, but Intermediate Student doesn't provide begin either (Advanced Student appears to, which helps). If you're using Intermediate Student, I guess you're just not meant to use multiple expressions, and that's that. :-)
The beginning and intermediate student languages really aren't a good fit for programs that use I/O. If your teacher is assigning problems that require you to use one of these languages and also require you to print something out, that would be a somewhat inappropriate assignment.
For the record, I certainly make mistakes like this myself....

Resources