Persistency.borg

Persistency.borg


{
object(x,y)::
    {
    setxy(X,Y):: {x:=X; y:=Y};
    print():: display(x+y);
    clone()
    };


container()::
    {
    free:1;
    elements[10]:void;
    add(o)::
        {
        elements[free]:=o;
        free:=free+1
        };
    print()::
        for(i:1,i<=size(elements),i:=i+1,
            {
            o:elements[i];
            display(text(i)+": ");
            if (is_void(o),
                display(""+eoln),
                {o.print();
                display(eoln)})
            });
    clone()
    };


c:container();

saveandload()::
    {
    ` first we create the container and add some objects
    c.add(object(1,100));
    c.add(object(2,200));
    c.add(object(3,300));
    c.add(object(4,400));
    c.add(object(5,500));
    c.add(object(6,600));
    c.add(object(7,700));
    c.print();
    ` now we save everything, but we unlink all dictionaries
    ` at the agent level.
    saveexp(c,agentself,"test.exp");
    ` load everything and relink at agentself again
    d:loadexp(agentself,"test.exp");
    display("c~d = ",text(c~d),eoln);
    d.print();
    ` one of the nice features of this is that we can sandbox
    ` the loaded expression this way. For example. We redefine
    ` the display operator and we link the loaded expression to
    ` this redefined environment.
    olddisplay:display;
    display(txt):: olddisplay("--",txt);
    d:=loadexp(clone(),"test.exp");
    d.print()
    };

display("saveandload()")
}