2017-08-26 6 views

Répondre

1

Définir une defFunction:

CLIPS>  
(deffunction print-to-width (?log-name ?width ?str) 
    (if (<= ?width 0) 
    then 
    (printout ?log-name ?str crlf) 
    (return)) 
    (bind ?w ?width) 
    (while (neq ?str "") 
    (bind ?pos (str-index " " ?str)) 
    (if (or (not ?pos) 
      (> ?pos (+ ?w 1))) 
     then 
     (if (and (not ?pos) (<= (str-length ?str) ?w)) 
      then 
      (printout ?log-name ?str) 
      (bind ?str "") 
      else 
      (if (!= ?w ?width) 
       then 
       (printout ?log-name crlf) 
       (bind ?w ?width) 
       else 
       (printout ?log-name (sub-string 1 ?w ?str)) 
       (bind ?str (sub-string (+ ?w 1) (str-length ?str) ?str)) 
       (if (neq ?str "") then (printout ?log-name crlf)) 
       (bind ?w ?width))) 
     else 
     (printout ?log-name (sub-string 1 ?pos ?str)) 
     (bind ?str (sub-string (+ ?pos 1) (str-length ?str) ?str)) 
     (bind ?w (- ?w ?pos))) 
    (if (eq ?str "") then (printout ?log-name crlf))) 
    (return)) 
CLIPS> (print-to-width t 0 "the quick brown fox jumped over the lazy dogs") 
the quick brown fox jumped over the lazy dogs 
CLIPS> (print-to-width t 80 "the quick brown fox jumped over the lazy dogs") 
the quick brown fox jumped over the lazy dogs 
CLIPS> (print-to-width t 40 "the quick brown fox jumped over the lazy dogs") 
the quick brown fox jumped over the lazy 
dogs 
CLIPS> (print-to-width t 20 "the quick brown fox jumped over the lazy dogs") 
the quick brown fox 
jumped over the lazy 
dogs 
CLIPS> (print-to-width t 10 "the quick brown fox jumped over the lazy dogs") 
the quick 
brown fox 
jumped 
over the 
lazy dogs 
CLIPS> 

ou d'un message de gestionnaire

CLIPS> 
(defmessage-handler STRING print-to-width (?log-name ?width) 
    (bind ?str ?self) 
    (if (<= ?width 0) 
    then 
    (printout ?log-name ?str crlf) 
    (return)) 
    (bind ?w ?width) 
    (while (neq ?str "") 
    (bind ?pos (str-index " " ?str)) 
    (if (or (not ?pos) 
      (> ?pos (+ ?w 1))) 
     then 
     (if (and (not ?pos) (<= (str-length ?str) ?w)) 
      then 
      (printout ?log-name ?str) 
      (bind ?str "") 
      else 
      (if (!= ?w ?width) 
       then 
       (printout ?log-name crlf) 
       (bind ?w ?width) 
       else 
       (printout ?log-name (sub-string 1 ?w ?str)) 
       (bind ?str (sub-string (+ ?w 1) (str-length ?str) ?str)) 
       (if (neq ?str "") then (printout ?log-name crlf)) 
       (bind ?w ?width))) 
     else 
     (printout ?log-name (sub-string 1 ?pos ?str)) 
     (bind ?str (sub-string (+ ?pos 1) (str-length ?str) ?str)) 
     (bind ?w (- ?w ?pos))) 
    (if (eq ?str "") then (printout ?log-name crlf))) 
    (return)) 
CLIPS>  
(send "the quick brown fox jumped over the lazy dogs" print-to-width t 0) 
the quick brown fox jumped over the lazy dogs 
CLIPS> (send "the quick brown fox jumped over the lazy dogs" print-to-width t 80) 
the quick brown fox jumped over the lazy dogs 
CLIPS> (send "the quick brown fox jumped over the lazy dogs" print-to-width t 40) 
the quick brown fox jumped over the lazy 
dogs 
CLIPS> (send "the quick brown fox jumped over the lazy dogs" print-to-width t 20) 
the quick brown fox 
jumped over the lazy 
dogs 
CLIPS> (send "the quick brown fox jumped over the lazy dogs" print-to-width t 10) 
the quick 
brown fox 
jumped 
over the 
lazy dogs 
CLIPS> 
+0

Im en utilisant ** ** OOP (programmation orientée objet) dans les clips. Je crois que je ne peux pas utiliser le ** deffunction ** que vous avez partagé. Une autre idée? –

+0

Utilisez un gestionnaire defmessage au lieu d'une fonction de suppression. –