CLIPS: how to add line breaks into a sentence

600 views Asked by At

I´m using the bind function but the text to be bound is very big.

I want to split the text in more lines so when I use the print out command, it will fit in the screen properly.

Any suggestions how to do it ?

1

There are 1 answers

2
Gary Riley On

Define a 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> 

Or a message-handler

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>