swarm-support
[Top][All Lists]
Advanced

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

Re: LispArchiver, input and output


From: Benedikt Stefansson
Subject: Re: LispArchiver, input and output
Date: Thu, 25 May 2000 14:59:51 -0600

"Marcus G. Daniels" wrote:

> >>>>> "MB" == Marie-Edith Bissey <address@hidden> writes:
>
> MB> Now, I have a related question: is there any way in Swarm to save
> MB> parameter values in text format? or a printToFile method?
>
> What I do is to write a simple Scheme program, e.g. for an implementation
> like http://www.gnu.org/software/guile, that loads the .scm file and
> then prints it in some way.  You can do something similar with HDF5 and R.

It is of course always preferrable if one can stick to Swarm all the way. 
However different people have different
preferrences regarding output formats. x

One of the undervalued attributes of the Swarm libaries (the objectbase library 
to be exact) is that any subclass of
'SwarmObject' can be 'reflective', i.e. extract information on its own instance 
variables.

It is therefore remarkably easy to write a short procedure that extracts 
parameters for an object or collection of
objects and outputs them in any abitrary format.

(The rest of this message may be of little value to Java based Swarmers).

In the following example I want to dump a description of objects in XML format. 
You could as easily output only simple
text: By substituting the _fprintf_ statements with something else (also 
perhaps ignoring the class information etc.)
you can change this to accomodate any format you like. These are just two 
methods from this class, I have attached both
.h and .m files as txt documents.

<END EXERPT>

+dumpList: list key: (const char *) key toFile: (FILE *) file
{
  id index_list;
  id object;

  fprintf(file,"<dataset class='List' key='%s'>\n",key);

  index_list     = [list begin: globalZone];
  while((object  = [index_list next]))
    {
      [[self class] dumpObject: object key: "nokey" toFile: file];
    }
  [index_list drop];

  fprintf(file,"</dataset>\n");

  return self;
}

+dumpObject: object key: (const char *) key toFile: (FILE *) file
{
  id index_probes;
  id probe;
  char str[20];

  fprintf(file,"<object class='%s' key='%s'>\n",[object getTypeName],key);

  // This is flaky: Check if members are SwarmObject
  // subclasses and assume everything else is strings
  if(!([object respondsTo: M(getProbeMap)]))
    {
      fprintf(file,"\"%s\"\n",[object getC]);
    }
  else
    {
      index_probes = [[object getProbeMap] begin: globalZone];
      while((probe = [index_probes next]))
 {
   if(([probe class] == [VarProbe class]))
     {
       fprintf(file,"    <variable name='%s' type='%s'>%s</variable>\n",
        (char *)[probe getProbedVariable],
        (char *)[probe getProbedType],
        (char *)[probe probeAsString: object Buffer: str]);
     }
 }
      [index_probes drop];
    }
  fprintf(file,"</object>\n");

  return self;
}

<END EXERPT>


You call the class like this:

xmlfile     = [XMLDump openFile: outfile_xml];
[XMLDump dumpMap: somemap key: "somemap"  toFile: xmlfile];
[XMLDump dumpList: somelist  key: "somlist" toFile: xmlfile];
... etc ..
[XMLDump closeFile: xmlfile];

Example output looks like this:

<application>
<dataset class='Map' key='somemap'>
<keyvaluepair>
<key>
<object class='String' key='nokey'>"Volatility"</object>
</key>
<value>
<object class='Volatility' key='nokey'>
     <variable name='rule_type'>3</variable>
     <variable name='lag'>10</variable>
     <variable name='constant'>250</variable>
     <variable name='name'>"Volatility"</variable>
     <variable name='cutoff_low'>0.25D0</variable>
     <variable name='cutoff_high'>0.35D0</variable>
</object>
</value>
</keyvaluepair>
.... etc ...
</dataset>
<dataset class='List' key='somelist'>
<object class='Data' key='nokey'>
     <variable name='high'>29.8D0</variable>
     <variable name='opening'>29.65D0</variable>
     <variable name='low'>28.4D0</variable>
     <variable name='settlement'>1</variable>
     <variable name='value'>28.73D0</variable>
     <variable name='date'>"05-22-2000"</variable>
     <variable name='closing'>28.73D0</variable>
</object>
... etc ...
</dataset>
</application>



--
Benedikt Stefansson      | address@hidden
CASA, Inc.               | Ph : (505) 988-8807 x101
Santa Fe, NM 87501       | Fax: (505) 988-3440


// Code (C) by Benedikt Stefansson 1999
#include <objc/objc-api.h>
#include <objc/encoding.h>
#import <objectbase.h>
#import <objectbase/SwarmObject.h>

@interface XMLDump: SwarmObject

+(FILE *) openFile: (const char *) filename;
+closeFile: (FILE *) file;
+dumpObject: object key: (const char *) key toFile: (FILE *) file;
+dumpList:   list   key: (const char *) key toFile: (FILE *) file;
+dumpMap:     map   key: (const char *) key toFile: (FILE *) file;

@end
// Code (C) by Benedikt Stefansson 1999
#import "XMLDump.h"

@implementation XMLDump

+(FILE *) openFile: (const char *) filename
{
  FILE * xmlfile;

  xmlfile = fopen(filename,"w");
  fprintf(xmlfile,"<application>\n");
  return xmlfile;

}

+closeFile: (FILE *) file
{
  fprintf(file,"</application>\n");
  fclose(file);
  return self;
}

+dumpList: list key: (const char *) key toFile: (FILE *) file
{
  id index_list;
  id object;

  fprintf(file,"<dataset class='List' key='%s'>\n",key);

  index_list     = [list begin: globalZone];
  while((object  = [index_list next]))
    {
      [[self class] dumpObject: object key: "nokey" toFile: file];
    }
  [index_list drop];
  
  fprintf(file,"</dataset>\n");

  return self;
}

+dumpObject: object key: (const char *) key toFile: (FILE *) file
{
  id index_probes;
  id probe;
  char str[20];

  fprintf(file,"<object class='%s' key='%s'>\n",[object getTypeName],key);

  // This is flaky: Assume that all members are SwarmObject 
  // subclasses but catch one exception - strings
  if(!([object respondsTo: M(getProbeMap)]))
    {
      fprintf(file,"\"%s\"\n",[object getC]);
    }
  else
    {
      index_probes = [[object getProbeMap] begin: globalZone];
      while((probe = [index_probes next]))
        {
          if(([probe class] == [VarProbe class]))
            {
              fprintf(file,"    <variable name='%s' type='%s'>%s</variable>\n",
                      (char *)[probe getProbedVariable],
                      (char *)[probe getProbedType],
                      (char *)[probe probeAsString: object Buffer: str]);
            }
        }
      [index_probes drop];
    }
  fprintf(file,"</object>\n");
  
  return self;
}


+dumpMap: map key: (const char *) key toFile: (FILE *) file
{
  id index_map;
  id object;
  id objectkey;

  fprintf(file,"<dataset class='Map' key='%s'>\n",key);

  index_map     = [map begin: globalZone];
  while((object = [index_map next: &objectkey]))
    {
      fprintf(file,"<keyvaluepair>\n");
      fprintf(file,"<key>\n");
      [[self class] dumpObject: objectkey key: "nokey"   toFile: file];
      fprintf(file,"</key>\n");
      fprintf(file,"<value>\n");
      [[self class] dumpObject: object    key: "nokey" toFile: file];
      fprintf(file,"</value>\n");
      fprintf(file,"</keyvaluepair>\n");
    }
  [index_map drop];
  
  fprintf(file,"</dataset>\n");

  return self;
}

@end



reply via email to

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