0
votes

I originally attempted using the modify function but it doesn't do anything and just prints false, I don't know what I am doing wrong.

I used

(modify ?tv (v ?x))

it didn't work. I then used

    (retract ?tv)
    (assert (v ?x))

instead, which worked. But I don't want to type that out every time I want to modify a fact, so I made a deffunction to do it for me, but

(deffunction modfact(?index ?factname ?factvalue)
    (retract ?index)
    (assert (?factname ?factvalue))
)

in this it gives a syntax error of:

[PRNTUTIL2] Syntax Error:  Check appropriate syntax for first field of a RHS pattern.

ERROR:
(deffunction MAIN::modfact
    (?index ?factname ?factvalue)
    (retract ?index)
    (assert (?factname

Which seems to me that its saying that I can't actually make this function because I can't assert a fact with the value of the variable. How can I get this to work?

1

1 Answers

0
votes

Modify only works with facts that have an associated deftemplate defined with slots:

CLIPS> 
(deftemplate task
   (slot id)
   (slot completed))
CLIPS> (watch facts)
CLIPS> (assert (task (id x) (completed no)))
==> f-1     (task (id x) (completed no))
<Fact-1>
CLIPS> 
(defrule modit
   ?f <- (task (completed ~yes))
   =>
   (modify ?f (completed yes)))
CLIPS> (run)
<== f-1     (task (id x) (completed no))
==> f-2     (task (id x) (completed yes))
CLIPS>

When using the assert command, the first field of the fact must be a symbol. If you must get around this restriction you can use the str-assert function.

CLIPS> 
(deffunction modfact (?index ?factname ?factvalue)
   (retract ?index)
   (str-assert (str-cat "(" ?factname " " ?factvalue ")")))
CLIPS> (assert (v 3))
==> f-3     (v 3)
<Fact-3>
CLIPS> (modfact 3 v 4)
<== f-3     (v 3)
==> f-4     (v 4)
<Fact-4>
CLIPS>