Project has been moved to GitHub as Arborium

Examples

Creating trees

Creating single node (or tree root):

AElement node = new AElement();


Creating single tree:

AElement tree = new AElement();
AElement child1 = new AElement();
AElement child2 = new AElement();
AElement child3 = new AElement();

tree += child1;
child1 += child2;
tree.Add(child3);

child1.Remove(child2);
tree -= child1;


Adding childs with the constructor:

AElement tree = new AElement(null, null, 
    new AElement(),
    new AElement(),
    new AElement()
);


Adding nodes with LINQ:

AElement tree = new AElement();

tree.Add(
    new byte[] {5, 9, 3, 6, 7}.Select(n => new AElement(AData.FromByte(n)))
);

Adding data and/or metadata

Adding data and/or metadata to a node:

AElement tree = new AElement();
AElement decimalGuidNode = new AElement();

decimalGuidNode.Data = AData.FromGuid(Guid.NewGuid());
decimalGuidNode.MetaData = AData.FromDecimal(1.978M);

tree.Add(decimalGuidNode);


Add data/metadata through properties:

AElement tree = new AElement();

AElement decimalGuidNode = new AElement()
{
    Data = AData.FromGuid(Guid.NewGuid()),
    MetaData = AData.FromDecimal(1.978M)
};

tree.Add(decimalGuidNode);


Adding data and/or metadata to a node with the constructor:

AElement child1 = new AElement(AData.FromGuid(testGuid));
AElement child2 = new AElement(null, AData.FromDecimal(1.978M));
AElement child3 = new AElement(new MemoryStream(new byte[] { 3 }), AData.FromDecimal(1.978M));

AElement tree = new AElement();

tree += child1;
tree.Add(child2, child3);

Accessing data and/or metadata

Accessing the data or metadata of a node:

bool boolValue = node.Data.AsBool();
DateTime dateTimeValue = node.MetaData.AsDateTime();

Addressing child nodes

Accessing children (IEnumerable):

foreach (AElement child in node.Children)
{
    child.Data = AData.FromVarInt(z++);
}

AElement child = (from child in node.Children where child.MetaData.AsInt() > 100 select child).FirstOrDefault();


Accessing the data of child node #2 (addressing child nodes with the indexer):

long longValue = tree[2].Data.AsLong();
long varIntValue = tree[2].Data.AsVarInt();


Accessing the metadata of child node #2 of child node #5:

string stringValue = tree[5][2].MetaData.AsUtf16String();

Save and load the tree

Save tree into a file or stream:

tree.Save(@"c:\windows\temp\file1.arbo");
tree.Save(someNetworkStream);


Load tree from a file or stream:

AElement tree = AElement.Load(@"c:\windows\temp\file1.arbo");
AElement treeFromMemoryStream = AElement.Load(someMemoryStream);

Conversion to string

Creating string from a node (i.e. to display it's metadata and data):

AElement node = tree[1][3][7];
string nodeString = node.ToString();


Creating string from a tree (i.e. for debug purposes):

string treeString = tree.TreeToString();

Stream compatibility

More Arborium tree can be saved into the same stream/file:

using (MemoryStream stream = new MemoryStream())
{
    AElement treeA = new AElement() { Data = AData.FromAsciiString("A") };

    treeA.Add(
        new AElement { Data = AData.FromAsciiString("A1") },
        new AElement { Data = AData.FromAsciiString("A2") }
    );

    AElement treeB = new AElement() { Data = AData.FromAsciiString("B") };

    treeB.Add(
        new AElement { Data = AData.FromAsciiString("B1") },
        new AElement { Data = AData.FromAsciiString("B2") }
    );

    treeA.Save(stream);
    treeB.Save(stream);

    stream.Position = 0;

    AElement tree2A = AElement.Load(stream);
    AElement tree2B = AElement.Load(stream);

    string sTreeA = treeA.TreeToString();
    string sTreeB = treeB.TreeToString(); 

    string sTree2A = tree2A.TreeToString();
    string sTree2B = tree2B.TreeToString(); 

    Assert.AreEqual(sTreeA, sTree2A);
    Assert.AreEqual(sTreeB, sTree2B);
}

Events

Adding save (saving/saved) and load (loading/loaded) events:

AElement tree = new AElement(AData.FromUtf8String("ROOT"), null, 
    new AElement(AData.FromUtf8String("NODE1"), null, null, null),
    new AElement(AData.FromUtf8String("NODE2"), null, null),
    new AElement(AData.FromUtf8String("NODE3"), null)
);

int savingEventFired = 0;
int savedEventFired = 0;

int loadingEventFired = 0;
int loadedEventFired = 0;

tree.OnDataSaving += (s, a) =>
{
    savingEventFired++;
};

tree.OnDataSaved += (s, a) =>
{
    savedEventFired++;
};

tree.Save(@"c:\windows\temp\ut10.arbo");

List<string> loadingNodes = new List<string>();
List<string> loadedNodes = new List<string>();

AElement.OnDataLoading += (s, a) =>
{
    loadingNodes.Add(s.Data.AsUtf8String());
};

AElement.OnDataLoaded += (s, a) =>
{
    loadedNodes.Add(s.Data.AsUtf8String());
};

AElement tree2 = AElement.Load(@"c:\windows\temp\ut10.arbo");

F# example

open Arborium

let longValue = 100L

let tree = AElement()
tree.Add(AElement(AData.FromDateTime(System.DateTime.Now), null))
tree.MetaData <- AData.FromUtf8String("Hello Arborium Demo")
tree.[0].MetaData <- AData.FromLong(longValue)

let base64EncodedTree = tree.ToBase64String()
let longValueBack = tree.[0].MetaData.AsLong()

tree.Save("c:\\windows\\temp\\FromFSharp.arbo")

Last edited Apr 16, 2016 at 6:23 PM by fuszenecker, version 23

Comments

No comments yet.