CLIPS Programming Language.Error removing clips.help me - expert-system

What error problem?
ERROR-1: [ARGACCES5]function >= expected argument #2 to be of type integer or float.
ERROR-2: [CSTRCPSR1] expected the begining of a contrust.
How to fix this error?

For the first error, you're passing a non-numeric argument as the second argument into the >= function. When an error occurs within a construct, CLIPS will show you what's been parsed to help you locate the problem:
CLIPS>
(defrule example
(test (>= 3 a))
=>)
[ARGACCES5] Function >= expected argument #2 to be of type integer or float
ERROR:
(defrule MAIN::example
(test (>= 3 a)
CLIPS>
Typically the second error occurs when you have an extra or missing parenthesis when loading constructs. For example, if you create a file named example.clp with the following contents:
(defrule example-1 =>)
(defrule example-2 =>)
) ; Extra parenthesis
(defrule example-3 =>)
(defrule example-4 =>)
You'll get this error when you try to load it:
CLIPS> (watch compilations)
CLIPS> (load "example.clp")
Defining defrule: example-1 +j+j
Defining defrule: example-2 +j
[CSTRCPSR1] Expected the beginning of a construct.
Defining defrule: example-3 +j
Defining defrule: example-4 +j
FALSE
CLIPS>
If you're watching compilations, you can get a better idea of where the error is occurring. In this case it's between the rules example-2 and example-3.

Related

Atomic Operations for Multithreading in SBCL

I'm getting an error when loading a function that contains an atomic operation. As a simple example, the file test.lisp contains:
(defparameter *count* 0)
(defun test ()
(sb-ext:atomic-incf *count*))
which generates the following error:
* (load "d:\\test.lisp")
; file: d:/test.lisp
; in: DEFUN TEST
; (ATOMIC-INCF *COUNT*)
;
; caught ERROR:
; during macroexpansion of (ATOMIC-INCF *COUNT*). Use *BREAK-ON-SIGNALS* to
; intercept.
;
; Invalid first argument to ATOMIC-INCF: *COUNT*
;
; compilation unit finished
; caught 1 ERROR condition
T
*
Why is *count* invalid?
From the docstring:
PLACE must access one of the following:
- a DEFSTRUCT slot with declared type (UNSIGNED-BYTE 64)
or AREF of a (SIMPLE-ARRAY (UNSIGNED-BYTE 64) (*))
The type SB-EXT:WORD can be used for these purposes.
- CAR or CDR (respectively FIRST or REST) of a CONS.
- a variable defined using DEFGLOBAL with a proclaimed type of FIXNUM.
Macroexpansion is performed on PLACE before expanding ATOMIC-INCF.
I suspect these are in place to avoid runtime checks when doing compare-and-swap.
To answer a question from above about how to use atomic-incf with a proclaimed global variable as a fixnum, this is what worked for me:
(declaim (fixnum **var**))
(sb-ext:defglobal **var** 0)
(sb-ext:atomic-incf **var**)
(defparameter *count* (list 0))
(defun test ()
(sb-ext:atomic-incf (car *count*)))
(test)

Using an integer slot in Protege in a Jess rule

I have written the following Jess rule to use it in a Protege ontology.
(mapclass Cliente)
(defrule perfil-familia-numerosa
?cliente <- (object (is-a Cliente)
(nombre ?name)
(discapacidad? ?discapacity)
(distrito_deseado ?desired_district)
(n_miembros_familia ?n)
(presupuesto_maximo ?max)
(presupuesto_minimo ?min))
(test (> n 4))
=>
(assert (perfil-cliente ?name soltero)))
When I try entering it in the Jess tab, I get a type error Jess reported an error in routine > [...] java.lang.Integer cannot be cast to java.lang.String.
However, the slot in question is an Integer, so it is not clear to me why Jess it's treating it as a String. Any help?
The problem is here:
(test (> n 4))
A reference to a bound variable retains the '?', so you have to write
(test (> ?n 4))
However, it might be better to add this constraint to (n_miembros_familia ?n).
(n_miembros_familia ?n&:(> ?n 4))

CLIPS weird behaviour

I don't understand why this binding expression is being interpreted fine on dialog but not inside a defrule:
CLIPS> (bind ?test (nth$ 1 (create$ 1)))
1
New window:
(defrule testrule
(bind ?test2 (nth$ 1 (create$ 1)))
=>
(assert (nothing here)))
Output after the untitled window's "Load Buffer":
CLIPS> Loading Selection...
Defining defrule: testrule
[PRNTUTIL2] Syntax Error: Check appropriate syntax for defrule.
ERROR:
(defrule MAIN::testrule
(bind ?test2 (
CLIPS>
How can it even fail being the same on both cases?
I have tested this problem several times, so to clarify it isn't problem from rest of defrule syntax, which you can verify by looking the defrule parsing cuts on the binding line.
Thanks.
It's a problem with your defrule syntax. You use the test conditional element to evaluate an expression in the conditions of a rule. The syntax you've used indicates that you're attempting to match a fact with the relation name bind. The analog to what you're doing at the command line is to execute the function call to bind from the actions of the rule:
CLIPS> (bind ?test (nth$ 1 (create$ 1)))
1
CLIPS>
(defrule testrule
=>
(bind ?test (nth$ 1 (create$ 1)))
(printout t ?test crlf))
CLIPS> (run)
1
CLIPS>
Because parentheses are extensively used as delimiters in CLIPS, there are many cases where context determines the meaning of a piece of code. For example, here's a call to the printout command from the command prompt:
CLIPS> (printout t Hello crlf)
Hello
CLIPS>
Here's a similar call from the actions of a rule:
CLIPS>
(defrule hello
=>
(printout t Hello crlf))
CLIPS> (run)
Hello
CLIPS>
Moving the printout code to the conditions of the rule changes the meaning of the code from a function call to a pattern intended to match a fact:
CLIPS>
(defrule hello
(printout t Hello crlf)
=>)
CLIPS> (agenda)
CLIPS> (facts)
f-0 (initial-fact)
For a total of 1 fact.
CLIPS> (assert (printout t Hello crlf))
<Fact-1>
CLIPS> (agenda)
0 hello: f-1
For a total of 1 activation.
CLIPS> (facts)
f-0 (initial-fact)
f-1 (printout t Hello crlf)
For a total of 2 facts.
CLIPS>
The test conditional element can be used in the conditions of a rule to indicate that the enclosed code is a function call and not a pattern matching a fact:
CLIPS>
(defrule hello (test (printout t Hello crlf)) =>)
Hello
CLIPS>
Some rule-based languages allow you to bind a variable to a derived value in the conditions of a rule, however, CLIPS does not support this, so can't get around this limitation by placing the bind functional call within a test conditional element:
CLIPS> (defrule hello (test (bind ?x 1)) => (printout t ?x crlf))
[PRCCODE3] Undefined variable x referenced in RHS of defrule.
ERROR:
(defrule MAIN::hello
(test (bind ?x 1))
=>
(printout t ?x crlf))
CLIPS>

JessTab: Finding the youngest person in family ontology

I am using the [family ontology][1] to test Jess rules. Everything works fine unless manipulating data with Jess built-in functions e.g. min and max. I have designed the following rule:
(defrule print_people_min_age
(object (https://wiki.csc.calpoly.edu/OntologyTutorial/family_example.owl#age ?a))
=>
(printout t "Min age " (min ?a) crlf))
The rule compiles well, but I am not getting the desired output. It outputs me ages of each person in the ontology. I tried to put the min function in the LHS but it results in error.
[1]: Family Ontology https://wiki.csc.calpoly.edu/OntologyTutorial/attachment/wiki/AddingRuleWithJessTab/family_example_for_rules.owl
Functions (min <numeric-expresion>+) and (max <numeric-expresion>+) are meant to be applied to a number of arguments - you are calling it with just one argument. The rule fires once for each object, and the minimum of that single age is - that age.
This rule illustrates how to find a minimum:
(defrule print_people_min_age
(object (https://wiki.csc.calpoly.edu/OntologyTutorial/family_example.owl#age ?a1))
(not (object (https://wiki.csc.calpoly.edu/OntologyTutorial/family_example.owl#age ?a2&:(< ?a2 ?a1))))
=>
(printout t "Min age " ?a1 crlf))

Struct layout: can't make a constructor to set the field I need

The code below "compiles", but doesn't function properly:
(defstruct (image-info
(:conc-name img-)
(:constructor %make-img-info (&key file))
(:print-function print-img-info))
(file nil :type string)
(gd-image nil :type (or cl-gd::image null))
(size '(0 . 0) :type cons)
(position '(0 . 0) :type cons))
(defun print-img-info (info stream level)
(declare (ignore level))
(let ((size (img-size info))
(pos (img-position info)))
(format stream "~s[width: ~d, height: ~d, x: ~d, y: ~d]"
(img-file info)
(car size) (cdr size) (car pos) (cdr pos))))
(defun make-img-info (path)
(let ((image (cl-gd:create-image-from-file path))
(info (%make-img-info :file path))) ; <--- problem here
(setf (img-gd-image info) image
(img-size info)
(cons (cl-gd:image-width image)
(cl-gd:image-height image))) info))
SBCL infers correctly the type of the argument to %make-img-info, as can be seen here:
(describe '%make-img-info)
SPRITESHEET::%MAKE-IMG-INFO
[symbol]
%MAKE-IMG-INFO names a compiled function:
Lambda-list: (&KEY (FILE NIL))
Declared type: (FUNCTION (&KEY (:FILE STRING))
(VALUES IMAGE-INFO &OPTIONAL))
But when I try to compile the make-img-info, I get this:
note: deleting unreachable code
warning:
Derived type of PATH is
(VALUES CL-GD::IMAGE &OPTIONAL),
conflicting with its asserted type
STRING.
I'm passing the correct argument (a string) to this function, but it still fails to call it because it "believes" that it has to be cl-gd:image. I suspect that the problem is that the layout is somehow alphabetical, and gd-image comes up before file in the list... but how do I then address this? I don't really want to rename the field?
Now I believe this was some sort of a glitch related to SLIME and SBCL not cooperating very well when compiling structs. I cannot consistently reproduce this behaviour, but it happens now and then with other structs too so that some times I need to kill SLIME and SWANK, restart SBCL and recompile because recompiling only the related parts of the struct will not work.
I'm not deleting the question because if anyone will come across similar behaviour, maybe it will help to restart the Lisp, so this experience can be useful.

Resources