next up previous

12.9.5 Asserting a String

The assert-string function is similar to assert in that it will add a fact to the factlist. However, assert-string takes a single string representing a fact (expressed in either ordered or deftemplate format ) and asserts it. Only one fact may be asserted with each assert-string statement.

Syntax

(assert-string <string-expression>)

If an identical copy of the fact already exists in the factlist, the fact will not be added (however, this behavior can be changed, see sections 13.4.4 and 13.4.5). Fields within the fact may contain a string by escaping the quote character with a backslash. Note that this function takes a string and turns it into fields. If the fields within that string are going to contain special characters (such as a backslash), they need to be escaped twice (because you are literally embedding a string within a string and the backslash mechanism ends up being applied twice). Global variables and expressions can be contained within the string. The value returned by this function is the factaddress of the newly created fact. If the assertion of the newly created fact causes an error, or if an identical copy of the newly created fact already exists in the factlist, then the symbol FALSE is returned.

Example

CLIPS> (clear)
CLIPS> (deftemplate foo (slot x) (slot y))
CLIPS> (assert-string "(status valve open)")
CLIPS> (assert-string "(light \"red\")")
CLIPS> (assert-string "(a\\b \"c\\\\d\")")
CLIPS> (assert-string "(foo (x 3))")
CLIPS> (assert-string "(foo (y 7))")
CLIPS> (facts)
f-0     (status valve open)
f-1     (light "red")
f-2     (a\b "c\d")
f-3     (foo (x 3) (y nil))
f-4     (foo (x nil) (y 7))
For a total of 5 facts.
CLIPS>


next up previous