swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: InFile


From: Benedikt Stefánsson
Subject: RE: InFile
Date: Tue, 14 Sep 1999 01:20:08 +0100

Thanks Marcus for clarifying some of the Archiver issues. However there are a 
couple of questions:

The code example you showed for LispArchiver assumes that the user knows the 
key value to pass to the archiver to retrieve the MyObject instance. What if 
you want to deserialize a collection, is it possible to put in the Scheme list 
a series of key,value pairs and use a while() loop to iterate through the list, 
i.e.. have a file like this:

(list
 (cons 'myObject
       (make-instance 'MyObject #:date "1/1/99" #:value 1.0)
  )
 (cons 'myObject
       (make-instance 'MyObject #:date "1/2/99" #:value 2.0)
 )
...
 (cons 'myObject
       (make-instance 'MyObject #:date "12/31/99" #:value 365.0)
 )
)

The reason I am asking is that I have tried literally dozens of permutations of 
trying to read these kinds of expressions with lispIn:, lispInCreate: in defobj 
(with the aid of the InStream class) or getObject: in Archiver but I can´t get 
this to work in Swarm 1.4.1. To create an instance of defobj the lispIn method 
works well and one can use the InSream to iterate through a series of 
expressions like this:

(#:date "1/1/99" #:value 1.0)
(#:date "1/2/99" #:value 2.0)

(#:date "12/31/99" #:value 365.0)

What bugs me is that I need to store information relating to which class to 
create in meta data (one trick is to use an abstract class and use a macro to 
figure out the name of the subclass to be instantiated from a string ivar in 
the abstract class). 

It seems that this is what the getObject: or lispInCreate: methods were made 
for, but I can´t figure out if either works in Swarm 1.4.1 or are just teasers, 
they are not used anywhere in the libraries or demoapps as far as my grep tells 
me...And I have not been able to use the archiver at all for this purpose.

I´m stalling in installing 2.0 as you may note...

Regards,
Benedikt

-----Original Message-----
From:   Marcus G. Daniels [SMTP:address@hidden
Sent:   11. september 1999 23:01
To:     address@hidden
Subject:        Re: InFile

>>>>> "PT" == Pietro Terna <address@hidden> writes:

PT>     Reading the documentation I see that InFile and OutFile
PT> protocol are now deprecated.

PT>     What is the easy alternative? My need is to read and write the
PT> contents of some arrays of float.

I suggest you use either the Lisp or HDF5 archivers.  (FYI, InFile and OutFile
won't go away any time soon for Objective C users.)

Suppose you have a floating point matrix in a file like below that you
want to load into a Swarm object.

$ cat ary.txt
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0

The first thing to do it to convert this data into a format Swarm can load. 
Swarm currently knows about two formats: a Scheme-like format and HDF5.

To create HDF5 for Swarm, the procedure to load this file involves running 
a script in R like this:

$ cat saveary.R
v <- scan("ary.txt") # load vector of data
m <- matrix(v, nrow=2,byrow=TRUE) # convert vector to array
myObject <- list(ary=m) # wrap array in an object
attr(myObject,"type") <- "MyObject" # identify as a MyObject class (for Swarm)
hdf5save("ary.hdf",myObject) # save it to HDF5

Which you can run like this:

$ R BATCH saveary.R

The result is the file ary.hdf (not itself human-readable).  Note that
HDF5 is a standalone library that has a well-defined API; it's not
necessary to use R if you have another tool or application that makes
calls to the HDF5 library.

Here is what the text file above would look like converted to Scheme:¹

$ cat ary.scm
(list
 (cons 'myObject
       (make-instance 'MyObject 
                      #:ary
                      #2((1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0) 
                         (10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0))
                      )))

This format is list of pairs where the first member of the pair
(a.k.a. a "cons") is a key the the second is a value.  The key,
"myObject" is the name you give to the object (an instance of MyObject
that contains an array).

Once you've created either ary.hdf or ary.scm, then you can use either
the HDF5Archiver or LispArchiver to load it.  The crucial thing to
look at in the program below are the "#ifdef USE_HDF5" alternatives and
the call to getObject: that follows.

#import <simtools.h>
#import <defobj/Create.h>
#import <defobj.h>

#define ROWS 2
#define COLUMNS 10

@interface MyObject: CreateDrop
{
  double ary[ROWS][COLUMNS];
}
@end

@implementation MyObject
- (void)describe: outputCharStream
{
  unsigned i;

  for (i = 0; i < ROWS; i++)
    {
      unsigned j;

      for (j = 0; j < COLUMNS; j++)
        printf (" %f", ary[i][j]);
      putchar ('\n');
    }
}
@end

@interface Controller: CreateDrop
{
  id ary;
}
+ createBegin: aZone;
@end

@implementation Controller

+ createBegin: aZone
{
  Controller *obj = [super createBegin: aZone];

  obj->ary = [MyObject create: globalZone];
  return obj;
}

@end

int
main (int argc, const char **argv) 
{

  initSwarmBatch (argc, argv);

  {
    id archiver;
    id controller;

#ifdef USE_HDF5
    archiver = [[[HDF5Archiver createBegin: globalZone]
                  setPath: "ary.hdf"]
                 createEnd];
#else
    archiver = [[[LispArchiver createBegin: globalZone]
                  setPath: "ary.scm"]
                 createEnd];
#endif
    
    xprint ([archiver getObject: "myObject"]);
    controller = [Controller create: globalZone];
  }
  return 0;
}

/*
Local Variables:
compile-command: "$SWARMHOME/bin/libtool-swarm --mode=link gcc -DUSE_HDF5 -o 
ary -g -Wno-import -I$SWARMHOME/include -L$SWARMHOME/lib ary.m -lswarm -lobjc"
End:
*/



¹ Instead reformatting by hand, you can also use program to do it for you.
Here's a Scheme program that converts a text file to the right Scheme format.

$ cat convert-ary.sc
(define (load-text text-file)
    (with-input-from-file text-file
      (lambda ()
        (let join-strings ((expr ""))
             (let ((line (read-line)))
               (if (eof-object? line)
                   (call-with-input-string (string-append "#2(" expr ")")
                                           read)
                   (join-strings (string-append expr "(" line ")"))))))))

(define (wrap filename ary)
    (with-output-to-file filename
      (lambda ()
        (write `(list
                 (cons 'myObject
                  (make-instance 'MyObject #:ary ,ary)))))))

$ guile -l convert-ary.scm -c '(wrap "ary.scm" (load-text "ary.txt"))'


                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.

<<application/ms-tnef>>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]