Saving and loading objects that reference of objects?

Discussion of Common Lisp
Post Reply
Cass
Posts: 6
Joined: Thu Oct 24, 2013 8:14 am

Saving and loading objects that reference of objects?

Post by Cass » Mon Aug 24, 2015 3:36 pm

Hi there again,

I was wondering whether there was a way to save instances of objects that include instances of other objects.

For instance, I might have a family object that contains a slot for children and have multiple instances of person in the children slot, who might themselves have children and so on... Or I might have a bunch of information that I want to store in years, so that I can ask for the information concerning Scotland in 2005 by going something like: (population (get-me 'scotland (countries 2005)))

Stuff like that seems like it might be useful to be able to do. Though, I'm not sure whether it's typical to do that or whether people try to cram all the information into one master object so they only have to write one method to read the thing.

I'd know how to do it if it was just recurring down and across a list, but I don't know how to generate a representation of the object version of the same structure which can be saved out and loaded easily, (nor how to make the whole lot output to the screen in a sensible manner. Which, while it doesn't seem like it should be strictly necessary, seems likely to be entailed.)

At the moment I'm writing output methods for each class and trying to use those as needed, but it's a mess. Is there an easier way?

Thanks for reading :)

David Mullen
Posts: 78
Joined: Mon Dec 01, 2014 12:29 pm
Contact:

Re: Saving and loading objects that reference of objects?

Post by David Mullen » Tue Aug 25, 2015 3:57 pm

There isn't any way that's typical. Various approaches have been taken in real-world systems. An initial consideration is whether or not you need to deal with multiple references to the same object. If the objects are restricted to hierarchical relationships or can be seen as a hierarchy then that makes your life easier insofar as representing the whole structure in a data file. Otherwise, you need to keep track of objects by assigning IDs so that you can reconstruct an equivalent pointer structure in memory.

Having dealt with that, there are a few approaches to data file formats. Firstly, if you're using a textual format then you can have methods describing each object in terms of more primitive structures, such as lists—the loader can dispatch on the first element of each list in order to make different kinds of CLOS objects. Alternatively, you can set up reader macros that build each object upon being dispatched by the Lisp reader. Common Lisp has a built-in reader macro—#S—for objects defined with DEFSTRUCT, but you'll have to do your own thing for CLOS objects. There's a whole raft of printer control variables, such as *PRINT-READABLY*, that can be taken as advice to PRINT-OBJECT methods to produce the right output for each situation.

The other way is to go for some kind of binary representation. Common Lisp itself has the concept of a compiled file—which is likely binary, but not portable—and provides a hook, MAKE-LOAD-FORM, into that subsystem, complete with a specification of equivalence between a loaded object and a previously saved object. Rolling your own binary file format gives you more control—that's what a lot of persistent object systems have done. I've written one such system—it's CCL-specific and doesn't use the MOP, so that's one way it differs from Rucksack and the like. Either way, these systems are a little more complex than just printing forms to a stream.

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Saving and loading objects that reference of objects?

Post by edgar-rft » Tue Aug 25, 2015 9:54 pm

The standard way to save inter-dependency and values of all currently defined Lisp objects is dumping a Lisp image. The Lisp image can be loaded at any time later and still will contain all Lisp objects with their inter-dependency and values from the time when the image was dumped.

Unfortunately Lisp images are not portable across Common Lisp implementations, and how to dump an image is implementation dependent and not defined in the ANSI standard, but it is explained in the documentation that comes with the Common Lisp implementation.

Dumping and loading Lisp images might not be the best approach for user-world applications, but it is definitely the easiest way to save and load Lisp data.

- edgar

Post Reply