LISP File I / O - Extracting and Converting Information

I have a file (furniture.lisp) that looks basically like this (with lots of entries):

(addgv :furniture 'stove
  (make-instance 'stove
    :pose (tf:make-pose-stamped
            "map"                         ; frame-id
            0.0
            (tf:make-3d-vector -3.1 -0.9 0)     ; translation/origin
            (tf:euler->quaternion :az 0))))

(addgv :furniture 'drawers-cupboard
  (make-instance 'cupboard
    :pose (tf:make-pose-stamped
            "map"
            0.0
            (tf:make-3d-vector -3.1 0.1 0)
            (tf:euler->quaternion :az 0))))

Now I would like to have a function (get-locations "furniture.lisp" "locations.txt")that extracts the coordinates of objects in a 3d vector and writes its output to a file:

(location stove -3.1 -0.9 9)
(location drawers-cupboard -3.1 0.1 0)
...

I started by writing an expression that reads in a file (so far without parameterization) in turn:

(ql:quickload "split-sequence")

(with-open-file (stream "furniture.lisp")
(do ((line (read-line stream nil)
           (read-line stream nil)))
    ((null line))
    (princ (split-sequence::split-sequence #\Space line)) ; Just for demonstration
 ))

But I realized that I have no chance / idea to "connect" the name of the object (for example, a stove) and its coordinates. I will need the second character after "(addgv" for the name and the variable "word distance" for coordinates. Therefore, I tried to read the file into one large list:

(defun make-list-from-text (fn)
  (with-open-file (stream fn)
    (loop for line = (read-line stream nil nil)
          while line
          collect
               (split-sequence::split-sequence #\Space line))))

( , , , "" ). . , , - .

EDIT:

Svante ! , (, :export :make-3d-vector). , :key #'car , "" , (, (make-instance ...)) (, addgv). :

(defun find-helper (list-or-symbol)
    (if (listp list-or-symbol)
       (car list-or-symbol)
        list-or-symbol))

#'car #'find-helper.

+4
3

tf , read , . - (untested):

(eval-when (:compile-toplevel :load-toplevel :execute)
  (unless (find-package #:tf)
    (defpackage #:tf)))

(defun extract-location-file ()
  (let ((*read-eval* nil))
    (with-open-file (in "furniture.lisp")
      (with-open-file (out "locations.txt"
                           :direction :output
                           :if-exists :supersede
                           :if-does-not-exist :create)
        (loop :for form := (read in nil)
              :while form
              :do (print (extract-location form) out)
                  (terpri)))))

(defun extract-location (form)
  `(location ,(third form)
             ,@(rest (find 'tf::make-3d-vector
                           (find 'tf::make-pose-stamped
                                 (find 'make-instance
                                       form
                                       :key #'car)
                                 :key #'car)
                           :key #'car))))

*read-eval* nil.

+1

:

  • (. )
  • (cl-ppcre:regex-replace-all "tf::?" content ""), .. tf, , .
  • '()
  • read
  • , .
0

, :

  • .
  • , .

, LispWorks - ( ):

CL-USER 60 > (defun test ()
               (handler-bind ((conditions:package-not-found-reader
                               (lambda (c)
                                 (continue c)))
                              (conditions:simple-reader-error
                               (lambda (c)
                                 (continue c))))
                     (read-from-string "'(foo27:bar19 bar18:foo44)")))
TEST

CL-USER 61 > (test)
(QUOTE (FOO27::BAR19 BAR18::FOO44))

, , . , ...

0

Source: https://habr.com/ru/post/1525735/


All Articles