//----< write details of element with enumerable Data to string >------ public static string showElement <Key, Data, T>(this DBElement <Key, Data> elem) where Data : IEnumerable <T> // constraint clause { StringBuilder accum = new StringBuilder(); accum.Append(elem.showMetaData()); if (elem.payload != null) { IEnumerable <object> d = elem.payload as IEnumerable <object>; if (d == null) { accum.Append(String.Format("\n payload: {0}", elem.payload.ToString())); } else { bool first = true; accum.Append(String.Format("\n payload:\n ")); foreach (var item in elem.payload) // won't compile without constraint clause { if (first) { accum.Append(String.Format("{0}", item)); first = false; } else { accum.Append(String.Format(", {0}", item)); } } } } return(accum.ToString()); }
//----< write metadata to string >------------------------------------- public static string showMetaData <Key, Data>(this DBElement <Key, Data> elem) { StringBuilder accum = new StringBuilder(); accum.Append(String.Format("\n name: {0}", elem.name)); accum.Append(String.Format("\n desc: {0}", elem.descr)); accum.Append(String.Format("\n time: {0}", elem.timeStamp)); if (elem.children.Count() > 0) { // convert to string format accum.Append(String.Format("\n Children: ")); bool first = true; foreach (Key key in elem.children) { if (first) { accum.Append(String.Format("{0}", key.ToString())); first = false; } else { accum.Append(String.Format(", {0}", key.ToString())); } } } return(accum.ToString()); }
public XElement persistdbelement <Key, Data>(DBElement <Key, Data> elem) { XElement element = new XElement("newelement"); XElement name = new XElement("name", elem.name); // name of elements XElement descr = new XElement("description", elem.descr); // description of elements element.Add(name); element.Add(descr); if (elem.children.Count() > 0) { XElement children = new XElement("Children"); //defines the children of key foreach (Key key in elem.children) { XElement k1 = new XElement("key", key); children.Add(k1); } element.Add(children); } XElement time = new XElement("newtime", elem.timeStamp); //provides the timestamp element.Add(time); XElement payload = new XElement("newpayload", elem.payload); element.Add(payload); return(element); }
void TestR2() { "Demonstrating Requirement #2".title(); //creating new element of type int and string DBElement <int, string> element = new DBElement <int, string>(); element.name = "first element of db"; element.descr = "int and string type"; element.timeStamp = DateTime.Now; element.children.AddRange(new List <int> { 0, 2, 4, 8 }); element.payload = "first element's payload"; element.showElement(); db.insert(1, element); db.showDB(); WriteLine(); //creating new element of type string and list of strings DBElement <string, List <string> > element2 = new DBElement <string, List <string> >(); element2.name = "second element of db"; element2.descr = "strings and strings of string types"; element2.timeStamp = DateTime.Now; element2.children.AddRange(new List <string> { "SMA", "OOD", "Project2" }); element2.payload = new List <string> { "second", "SMA", "project" }; element2.showEnumerableElement(); db2.insert("2", element2); db2.showEnumerableDB(); WriteLine(); }
{ // Main method static void Main(string[] args) { "Testing persistence Package".title('='); WriteLine(); PersistenceEngine persiseng = new PersistenceEngine(); // creating new databse and new database elements DBEngine <int, DBElement <int, string> > pdb = new DBEngine <int, DBElement <int, string> >(); DBElement <int, string> pel1 = new DBElement <int, string>("first element", "SMA"); DBElement <int, string> pel2 = new DBElement <int, string>("second element ", "OOD"); DBElement <int, string> pel3 = new DBElement <int, string>("third element", "DP"); pel1.payload = "first payload"; pel1.children.AddRange(new List <int> { 111, 112, 113 }); pel2.payload = "second payload"; pel2.children.AddRange(new List <int> { 221, 222, 223 }); pel3.payload = "third payload"; pel3.children.AddRange(new List <int> { 331, 332, 333 }); // inserting elements to database pdb.insert(1, pel1); pdb.insert(2, pel2); pdb.insert(3, pel3); pdb.showDB(); WriteLine(); persiseng.persistdb <int, DBElement <int, string>, string>(pdb); }
public void persistdb <Key, Value, Data>(DBEngine <Key, Value> db) { XDocument xml = new XDocument(); xml.Declaration = new XDeclaration("1.0", "utf-8", "yes"); XComment comment = new XComment("NoSQL Database"); //title for XML xml.Add(comment); XElement NoSQLelem = new XElement("NoSQLDB_ELEMENTS"); XElement type = new XElement("numbers", typeof(Key)); XElement payload = new XElement("name", typeof(Data)); xml.Add(NoSQLelem); NoSQLelem.Add(type); NoSQLelem.Add(payload); foreach (Key k1 in db.Keys()) { XElement tags = new XElement("open_close"); XElement key = new XElement("new_Key", k1); //tag for new keys generated tags.Add(key); Value value1; db.getValue(k1, out value1); DBElement <Key, Data> element = value1 as DBElement <Key, Data>; WriteLine(element.showElement()); XElement dbelement = persistdbelement <Key, Data>(element); tags.Add(dbelement); NoSQLelem.Add(tags); xml.Save("XML_FILE.xml"); //XML file name } }
// Main Method to test the implementation of DBElement Package static void Main(string[] args) { "Testing DBElement Package".title('='); WriteLine(); Write("\n --- Test DBElement<int,string> ---"); WriteLine(); // new elements for int and string database DBElement <int, string> elem1 = new DBElement <int, string>(); Write(elem1.showElement <int, string>()); WriteLine(); DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord"); elem2.payload = "The Empire strikes back!"; Write(elem2.showElement <int, string>()); WriteLine(); var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot"); elem3.children = new List <int> { 1, 2, 7 }; elem3.payload = "X-Wing fighter in swamp - Oh oh!"; Write(elem3.showElement <int, string>()); WriteLine(); Write("\n --- Test DBElement<string,List<string>> ---"); WriteLine(); //new elements for string and list of strings database DBElement <string, List <string> > newelem1 = new DBElement <string, List <string> >(); newelem1.name = "newelem1"; newelem1.descr = "test new type"; newelem1.payload = new List <string> { "one", "two", "three" }; Write(newelem1.showElement <string, List <string>, string>()); WriteLine(); DBElement <string, List <string> > newerelem1 = new DBElement <string, List <string> >(); newerelem1.name = "newerelem1"; newerelem1.descr = "same stuff"; newerelem1.children.Add("first_key"); newerelem1.children.Add("second_key"); newerelem1.payload = new List <string> { "alpha", "beta", "gamma" }; newerelem1.payload.AddRange(new[] { "delta", "epsilon" }); Write(newerelem1.showElement <string, List <string>, string>()); WriteLine(); Write("\n\n"); }
public List<key> querychildren<key, value, Data>(DBEngine<key, value> db, key Key) { value getqueryvalue; bool key_present = db.getValue(Key, out getqueryvalue); // check if key present DBElement<key, Data> temp = getqueryvalue as DBElement<key, Data>; // create new element to store value if (key_present) return temp.children; else Console.WriteLine("invalid key"); // if key not present error message return null; }
//----< write simple db elements out to Console >------------------ public static void show <Key, Value, Data>(this DBEngine <Key, Value> db) { foreach (Key key in db.Keys()) { Value value; db.getValue(key, out value); DBElement <Key, Data> elem = value as DBElement <Key, Data>; Write("\n\n -- key = {0} --", key); Write(elem.showElement()); } }
//----< write details of element with simple Data to string >---------- public static string showElement <Key, Data>(this DBElement <Key, Data> elem) { StringBuilder accum = new StringBuilder(); accum.Append(elem.showMetaData()); if (elem.payload != null) { accum.Append(String.Format("\n payload: {0}", elem.payload.ToString())); } return(accum.ToString()); }
// function to perform removal of relationships public static bool removeRelation <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, Key key2) { Value value; if (dbedit.getValue(key1, out value)) { DBElement <Key, Data> elem = value as DBElement <Key, Data>; if (elem.children.Contains(key2)) { elem.children.Remove(key2); } } return(true); }
// Function to perform editing of name in metedata public static bool editName <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, string new_name) { Value value; if (dbedit.getValue(key1, out value)) { DBElement <Key, Data> elem = value as DBElement <Key, Data>; elem.name = new_name; return(true); } else { return(false); } }
// function to edit instances public static bool editInstance <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, Data new_instance) { Value value; if (dbedit.getValue(key1, out value)) { DBElement <Key, Data> elem = value as DBElement <Key, Data>; elem.payload = new_instance; return(true); } else { return(false); } }
{ // Function to perform addition of relationships public static bool addRelations <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, Key key2) { Value val1, val2; bool key1_present = dbedit.getValue(key1, out val1); // check if key1 is present if (key1_present) { bool key2_present = dbedit.getValue(key2, out val2); // check if key2 is present if (key2_present) { DBElement <Key, Data> element = val1 as DBElement <Key, Data>; element.children.Add(key1); } } return(true); }
// main method to test DBExtensions package static void Main(string[] args) { "Testing DBExtensions Package".title('='); WriteLine(); // creation of new element Write("\n --- Test DBElement<int,string> ---"); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.payload = "a payload"; Write(elem1.showElement <int, string>()); // creation of new database DBEngine <int, DBElement <int, string> > dbs = new DBEngine <int, DBElement <int, string> >(); dbs.insert(1, elem1); dbs.show <int, DBElement <int, string>, string>(); WriteLine(); Write("\n --- Test DBElement<string,List<string>> ---"); // creation of new element DBElement <string, List <string> > newelem1 = new DBElement <string, List <string> >(); newelem1.name = "newelem1"; newelem1.descr = "test new type"; newelem1.children = new List <string> { "Key1", "Key2" }; newelem1.payload = new List <string> { "one", "two", "three" }; Write(newelem1.showElement <string, List <string>, string>()); DBEngine <string, DBElement <string, List <string> > > dbe = new DBEngine <string, DBElement <string, List <string> > >(); dbe.insert("key1", newelem1); dbe.show <string, DBElement <string, List <string> >, List <string>, string>(); Write("\n\n"); }
void TestR7() { //Demonstrating queries "Demonstrating Requirement #7".title(); WriteLine(); QueryEngine queryv = new QueryEngine(); DBElement <int, string> displayresc = new DBElement <int, string>(); DBElement <int, string> displayres = new DBElement <int, string>(); //demonstrating query 1 Console.WriteLine("\n \n getting value for key 1"); displayres = queryv.queryvalue <int, DBElement <int, string>, string>(db, 1); if (displayres != null) { displayres.showElement(); } DBElement <string, List <string> > valuestring = new DBElement <string, List <string> >(); Console.WriteLine("\n \n getting value for key 2"); valuestring = queryv.queryvalue <string, DBElement <string, List <string> >, string>(db2, "2"); if (valuestring != null) { valuestring.showEnumerableElement(); } // demonstrating query 2 Console.WriteLine("\n \n getting children for key 1"); List <int> tchildlist = new List <int>(); tchildlist = queryv.querychildren <int, DBElement <int, string>, string>(db, 1); if (tchildlist != null) { foreach (int i in tchildlist) { Console.WriteLine(i); } } }
void TestR3() { "Demonstrating Requirement #3".title(); //demonstrating addition and removal of key/value pairs pairs WriteLine("\n Database contents before addition"); db.showDB(); WriteLine(); WriteLine(" \n Database contents after addition-----"); DBElement <int, string> insert1 = new DBElement <int, string>(); insert1.name = "first inserted element"; insert1.descr = "int and string values database"; insert1.timeStamp = DateTime.Now; insert1.children.AddRange(new List <int> { 7, 7, 8, 3, 8, 8, 5, 5, 7 }); insert1.payload = "first inserted elements payload"; db.insert(2, insert1); DBElement <int, string> insert2 = new DBElement <int, string>(); insert2.name = "second inserted element"; insert2.descr = "int and string values"; insert2.timeStamp = DateTime.Now; insert2.children.AddRange(new List <int> { 1, 3, 5, 7 }); insert2.payload = "second inserted elements payload"; db.insert(3, insert2); db.showDB(); WriteLine(); WriteLine(" \n Database before contents are removed"); db.showDB(); WriteLine("\n \n second element to be removed"); db.remove(2); db.showDB(); WriteLine(); }
static void Main(string[] args) { "Testing DBEngine Package".title('='); WriteLine(); // New element being created Write("\n --- Test DBElement<int,string> ---"); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.payload = "a payload"; Write(elem1.showElement <int, string>()); WriteLine(); // New element being created DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord"); elem2.payload = "The Empire strikes back!"; Write(elem2.showElement <int, string>()); WriteLine(); // New element being created var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot"); elem3.children.AddRange(new List <int> { 1, 5, 23 }); elem3.payload = "X-Wing fighter in swamp - Oh oh!"; Write(elem3.showElement <int, string>()); WriteLine(); Write("\n --- Test DBEngine<int,DBElement<int,string>> ---"); int key = 0; Func <int> keyGen = () => { ++key; return(key); }; // anonymous function to generate keys DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >(); bool p1 = db.insert(keyGen(), elem1); bool p2 = db.insert(keyGen(), elem2); bool p3 = db.insert(keyGen(), elem3); if (p1 && p2 && p3) { Write("\n all inserts succeeded"); } else { Write("\n at least one insert failed"); } db.show <int, DBElement <int, string>, string>(); WriteLine(); WriteLine("\n\n removing element of key 2"); bool p4 = db.remove(2); db.show <int, DBElement <int, string>, string>(); Write("\n --- Test DBElement<string,List<string>> ---"); // New element being created of type string and list of strings DBElement <string, List <string> > newelem1 = new DBElement <string, List <string> >(); newelem1.name = "newelem1"; newelem1.descr = "test new type"; newelem1.payload = new List <string> { "one", "two", "three" }; Write(newelem1.showElement <string, List <string> >()); WriteLine(); Write("\n --- Test DBElement<string,List<string>> ---"); DBElement <string, List <string> > newerelem1 = new DBElement <string, List <string> >(); newerelem1.name = "newerelem1"; newerelem1.descr = "better formatting"; newerelem1.payload = new List <string> { "alpha", "beta", "gamma" }; newerelem1.payload.Add("delta"); newerelem1.payload.Add("epsilon"); Write(newerelem1.showElement <string, List <string>, string>()); WriteLine(); //new elements being created DBElement <string, List <string> > newerelem2 = new DBElement <string, List <string> >(); newerelem2.name = "newerelem2"; newerelem2.descr = "better formatting"; newerelem1.children.AddRange(new[] { "first", "second" }); newerelem2.payload = new List <string> { "a", "b", "c" }; newerelem2.payload.Add("d"); newerelem2.payload.Add("e"); Write(newerelem2.showElement <string, List <string>, string>()); WriteLine(); Write("\n --- Test DBEngine<string,DBElement<string,List<string>>> ---"); int seed = 0; string skey = seed.ToString(); Func <string> skeyGen = () => { ++seed; skey = "string" + seed.ToString(); skey = skey.GetHashCode().ToString(); return(skey); }; // create a new database of string and list of string type DBEngine <string, DBElement <string, List <string> > > newdb = new DBEngine <string, DBElement <string, List <string> > >(); newdb.insert(skeyGen(), newerelem1); newdb.insert(skeyGen(), newerelem2); newdb.show <string, DBElement <string, List <string> >, List <string>, string>(); WriteLine(); // removing element WriteLine("removing element "); newdb.remove("newerelem2"); newdb.show <string, DBElement <string, List <string> >, List <string>, string>(); // testing editing of metadata text "testing edits".title(); db.show <int, DBElement <int, string>, string>(); DBElement <int, string> editElement = new DBElement <int, string>(); db.getValue(1, out editElement); editElement.showElement <int, string>(); editElement.name = "editedName"; editElement.descr = "editedDescription"; db.show <int, DBElement <int, string>, string>(); Write("\n\n"); }
//Main method public static void Main() { "Testing QueryEngine Package".title('='); Console.WriteLine(); QueryEngine queryv = new QueryEngine(); // new database created for different types DBEngine<int, DBElement<int, string>> newdbq = new DBEngine<int, DBElement<int, string>>(); DBEngine<string, DBElement<string, List<string>>> newdbqs = new DBEngine<string, DBElement<string, List<string>>>(); //new elements created for int ,string database DBElement<int, string> newqelem1 = new DBElement<int, string>(); DBElement<int, string> newqelem2 = new DBElement<int, string>(); DBElement<int, string> newqelem3 = new DBElement<int, string>(); newqelem1.name = "query elemnt 1"; newqelem1.descr = "testing qurey get value for 1"; newqelem1.payload = "new query element payload 1"; newqelem1.children.AddRange(new List<int> { 1, 22, 333 }); newqelem2.name = "query elemnt 2"; newqelem2.descr = "testing query get value for 2"; newqelem2.payload = "new query element payload 2"; newqelem2.children.AddRange(new List<int> { 010, 020, 030 }); newqelem3.name = "query elemnt 3"; newqelem3.descr = "testing quey get value for 3"; newqelem3.payload = "new query element payload 3"; newqelem3.children.AddRange(new List<int> { 777, 222, 333 }); newdbq.insert(1, newqelem1); newdbq.insert(2, newqelem2); newdbq.insert(3, newqelem3); newdbq.showDB(); // testing query 1 DBElement<int, string> displayres = new DBElement<int, string>(); Console.WriteLine("\n \n getting value for key 2"); displayres = queryv.queryvalue<int, DBElement<int, string>, string>(newdbq, 2); if (displayres != null) displayres.showElement(); //testing query 2 Console.WriteLine("\n \n getting children for key 3"); List<int> childlist = new List<int>(); childlist = queryv.querychildren<int, DBElement<int, string>, string>(newdbq, 3); if (childlist != null) { foreach (int i in childlist) Console.WriteLine(i); } //new elements created for string,list of string database DBElement<string, List<string>> newelem1q = new DBElement<string, List<string>>(" element 1", "1 is first"); DBElement<string, List<string>> newelem2q = new DBElement<string, List<string>>(" element 2", "2 is second"); DBElement<string, List<string>> newelem3q = new DBElement<string, List<string>>(" element 3", " 3 is third"); newelem1q.children.AddRange(new List<string> { " one", "one", "one" }); newelem1q.payload = new List<string> { "h", "e", "l", "l", "o" }; newelem2q.children.AddRange(new List<string> { " two", "two", "two" }); newelem2q.payload = new List<string> { "h", "d", "w", "f", "b" }; newelem3q.children.AddRange(new List<string> { " three", "three", "three" }); newelem3q.payload = new List<string> { "a", "e", "i", "o", "u" }; newdbqs.insert("one", newelem1q); newdbqs.insert("two", newelem2q); newdbqs.insert("three", newelem3q); newdbqs.showEnumerableDB(); // testing query one DBElement<string, List<string>> valuestring = new DBElement<string, List<string>>(); Console.WriteLine("\n \n getting value for key 2"); valuestring = queryv.queryvalue<string, DBElement<string, List<string>>, string>(newdbqs, "two"); if (valuestring != null) valuestring.showEnumerableElement(); //testing query two DBElement<string, List<string>> valuestrings = new DBElement<string, List<string>>(); Console.WriteLine("\n \n getting children for key five"); List<string> childlists = new List<string>(); childlists = queryv.querychildren<string, DBElement<string, List<string>>, string>(newdbqs, "five"); if (childlists != null) { foreach (string cstring in childlists) Console.WriteLine(cstring); } }
static void Main(string[] args) { "Testing ItemEditor Package".title('='); WriteLine(); Write("\n --- Test DBElement<int,string> ---"); // new database created to test the functionaity DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >(); // new elements are created and inserted to the database DBElement <int, string> elem1 = new DBElement <int, string>("first element", "first element description"); DBElement <int, string> elem2 = new DBElement <int, string>("second element", "second element description"); DBElement <int, string> elem3 = new DBElement <int, string>("third element", "third element description"); elem1.payload = "Payload of 1"; elem1.children.AddRange(new List <int> { 1, 2, 3 }); elem2.payload = "Payload of 2"; elem2.children.AddRange(new List <int> { 11, 22, 33 }); elem3.payload = "Payload of 3"; elem3.children.AddRange(new List <int> { 111, 222, 333 }); // inserting elements to database db.insert(1, elem1); db.insert(2, elem2); db.insert(3, elem3); db.showDB(); // testing addition of relationships to elements Write("\n\n Testing of addition of new relationships"); bool addr1 = db.addRelations <int, DBElement <int, string>, string>(1, 2); bool addr2 = db.addRelations <int, DBElement <int, string>, string>(2, 3); bool addr3 = db.addRelations <int, DBElement <int, string>, string>(3, 111); db.showDB(); // testing removing relationships to elements Write("\n\n Testing of removal of relations"); bool remr1 = db.removeRelation <int, DBElement <int, string>, string>(2, 11); bool remr2 = db.removeRelation <int, DBElement <int, string>, string>(1, 3); db.showDB(); if (remr1 && remr2) { Write("\n \n Removal succeded"); } // Testing editing of text data Write("\n\n Testing of editing of text data"); bool ed_name1 = db.editName <int, DBElement <int, string>, string>(1, "renaming of element 1"); bool ed_descr = db.editDescr <int, DBElement <int, string>, string>(1, "editing description of element 1"); bool ed_inst = db.editInstance <int, DBElement <int, string>, string>(1, "new instance for element 1"); db.showDB(); Write("\n\n --- Test DBElement<string,List<string>> ---"); // creating elements to new database of type string,List of strings DBElement <string, List <string> > new_elem1 = new DBElement <string, List <string> >("new element 1", "Description of 1"); DBElement <string, List <string> > new_elem2 = new DBElement <string, List <string> >("new element 2", "Description of 2"); DBElement <string, List <string> > new_elem3 = new DBElement <string, List <string> >("new element 3", "Description of 3"); //creating new database DBEngine <string, DBElement <string, List <string> > > new_db = new DBEngine <string, DBElement <string, List <string> > >(); new_elem1.payload = new List <string> { "First data in payload ", "Second data in payload", "Third data in payload" }; new_elem1.children.AddRange(new List <string> { "one", "two", "three" }); new_elem2.payload = new List <string> { "DP", "SMA", "OOD" }; new_elem2.children.AddRange(new List <string> { "four", "five", "six" }); new_elem3.payload = new List <string> { "CE", "CS", "EE" }; new_elem3.children.AddRange(new List <string> { "seven", "eight", "nine" }); // inserting elements to database new_db.insert("One", new_elem1); new_db.insert("Two", new_elem2); new_db.insert("Three", new_elem3); new_db.showEnumerableDB(); // testing addition of relationships to elements Write("\n\n testing addition of relationship "); bool add1 = new_db.addRelations <string, DBElement <string, List <string> >, List <string> >("One", "two"); bool add2 = new_db.addRelations <string, DBElement <string, List <string> >, List <string> >("Two", "three"); bool add3 = new_db.addRelations <string, DBElement <string, List <string> >, List <string> >("Three", "one"); new_db.showEnumerableDB(); if (add1 && add2 && add3) { Write("\n \n Adding relationship successful."); } // testing removing relationships to elements Write("\n \n Now going to test removing of relationships in DB-element: Element-One"); bool rem1 = new_db.removeRelation <string, DBElement <string, List <string> >, List <string> >("One", "Nine"); bool rem2 = new_db.removeRelation <string, DBElement <string, List <string> >, List <string> >("One", "two"); new_db.showEnumerableDB(); if (rem1 && rem2) { Write("\n \n Deleting of relationships succeeded "); } // Testing editing of text data Write("\n \n Now going to test edition of name, description and replacing instance of payload with new instance in Element-One."); new_db.editName <string, DBElement <string, List <string> >, List <string> >("One", "Edited name for Element-One"); new_db.editDescr <string, DBElement <string, List <string> >, List <string> >("One", "New description for Element-One"); new_db.editInstance <string, DBElement <string, List <string> >, List <string> >("One", new List <string> { "New payload - String One", "New payload - String Two", "New payload - String Three" }); new_db.showEnumerableDB(); WriteLine(); }
static void Main(string[] args) { "Testing DBEngine Package".title('=');; WriteLine(); "Test db of scalar elements".title(); WriteLine(); //creation of new elements DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.payload = "a payload"; DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord"); elem2.payload = "The Empire strikes back!"; var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot"); elem3.payload = "X-Wing fighter in swamp - Oh oh!"; if (verbose) { Write("\n --- Test DBElement<int,string> ---"); WriteLine(); elem1.showElement(); WriteLine(); elem2.showElement(); WriteLine(); elem3.showElement(); WriteLine(); } Write("\n --- Test DBEngine<int,DBElement<int,string>> ---"); WriteLine(); int key = 0; Func <int> keyGen = () => { ++key; return(key); }; // function to generate keys // new database created DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >(); bool p1 = db.insert(keyGen(), elem1); bool p2 = db.insert(keyGen(), elem2); bool p3 = db.insert(keyGen(), elem3); if (p1 && p2 && p3) { Write("\n all inserts succeeded"); } else { Write("\n at least one insert failed"); } db.showDB(); WriteLine(); "Test db of enumerable elements".title(); WriteLine(); //new element created DBElement <string, List <string> > newelem1 = new DBElement <string, List <string> >(); newelem1.name = "newelem1"; newelem1.descr = "test new type"; newelem1.payload = new List <string> { "one", "two", "three" }; //new element created DBElement <string, List <string> > newerelem1 = new DBElement <string, List <string> >(); newerelem1.name = "newerelem1"; newerelem1.descr = "better formatting"; newerelem1.payload = new List <string> { "alpha", "beta", "gamma" }; newerelem1.payload.Add("delta"); newerelem1.payload.Add("epsilon"); //new element created DBElement <string, List <string> > newerelem2 = new DBElement <string, List <string> >(); newerelem2.name = "newerelem2"; newerelem2.descr = "better formatting"; newerelem2.children.AddRange(new List <string> { "first", "second" }); newerelem2.payload = new List <string> { "a", "b", "c" }; newerelem2.payload.Add("d"); newerelem2.payload.Add("e"); if (verbose) { Write("\n --- Test DBElement<string,List<string>> ---"); WriteLine(); newelem1.showEnumerableElement(); WriteLine(); newerelem1.showEnumerableElement(); WriteLine(); newerelem2.showEnumerableElement(); WriteLine(); } Write("\n --- Test DBEngine<string,DBElement<string,List<string>>> ---"); int seed = 0; string skey = seed.ToString(); Func <string> skeyGen = () => { ++seed; skey = "string" + seed.ToString(); skey = skey.GetHashCode().ToString(); return(skey); }; DBEngine <string, DBElement <string, List <string> > > newdb = new DBEngine <string, DBElement <string, List <string> > >(); newdb.insert(skeyGen(), newelem1); newdb.insert(skeyGen(), newerelem1); newdb.insert(skeyGen(), newerelem2); newdb.showEnumerableDB(); Write("\n\n"); }
//calling function to show elements of type strings and list of strings public static void showEnumerableElement(this DBElement <string, List <string> > enumElement) { Console.Write(enumElement.showElement <string, List <string>, string>()); }
{ // calling function to show elements of type int and strings public static void showElement(this DBElement <int, string> element) { Console.Write(element.showElement <int, string>()); }
static void Main(string[] args) { "Testing Payload Wrapper".title('='); WriteLine(); ListOfStrings los = new ListOfStrings(); los.theWrappedData.AddRange(new List <string> { "one", "two", "three" }); "showing List of strings:".title(); foreach (string item in los.theWrappedData) { Write("\n {0}", item); } WriteLine(); "Output from PayloadWrapper.ToString()".title(); Write("\n{0}", los.ToString()); //WriteLine(); "output from PayloadWrapper.ToXml()".title(); Write("\n{0}", los.ToXml()); WriteLine(); "output from elem.showElement<string, ListOfStrings>()".title(); DBElement <string, ListOfStrings> elem = new DBElement <string, ListOfStrings>(); elem.name = "test element"; elem.descr = "created to test PayloadWrapper"; elem.payload = new ListOfStrings(); elem.payload.theWrappedData = new List <string> { "alpha", "beta", "gamma", "delta", "epsilon" }; Write(elem.showElement <string, ListOfStrings>()); // The sequence of statements below should be turned into an // extension method: static public void clone(this List<string>) "test cloning elem".title('='); WriteLine(); DBElement <string, ListOfStrings> cloned = new DBElement <string, ListOfStrings>(); cloned.name = String.Copy(elem.name); cloned.descr = String.Copy(elem.descr); cloned.timeStamp = DateTime.Parse(elem.timeStamp.ToString()); cloned.children = new List <string>(); cloned.children.AddRange(elem.children); cloned.payload = elem.payload.clone() as ListOfStrings; "output from elem.showElement<string, ListOfStrings>()".title(); Write(elem.showElement <string, ListOfStrings>()); "output from cloned.showElement<string, ListOfStrings>()".title(); Write(cloned.showElement <string, ListOfStrings>()); //"Hashcodes are not reliable object identifiers".title(); //Write(string.Format("\n hashcode of elem = {0}", elem.GetHashCode())); //Write(string.Format("\n hashcode of elem.name = {0}", elem.name.GetHashCode())); //Write(string.Format("\n hashcode of elem.descr = {0}", elem.descr.GetHashCode())); //Write(string.Format("\n hashcode of elem.payload = {0}", elem.payload.GetHashCode())); //WriteLine(); //Write(string.Format("\n hashcode of cloned = {0}", cloned.GetHashCode())); //Write(string.Format("\n hashcode of cloned.name = {0}", cloned.name.GetHashCode())); //Write(string.Format("\n hashcode of cloned.descr = {0}", cloned.descr.GetHashCode())); //Write(string.Format("\n hashcode of cloned.payload = {0}", cloned.payload.GetHashCode())); //WriteLine(); "testing for equality immediately after cloning".title(); showEquality(cloned.name, elem.name, "cloned.name vs. elem.name"); showEquality(cloned.descr, elem.descr, "cloned.descr vs. elem.descr"); showDateTimeEquality(cloned.timeStamp, elem.timeStamp, "cloned.timeStamp vs. elem.timeStamp"); showListEquality(cloned.children, elem.children, "cloned.children vs. elem.children"); showListEquality <string>(cloned.payload.theWrappedData, elem.payload.theWrappedData, "cloned.payload vs. elem.payload"); WriteLine(); "modifying clone's properties".title(); WriteLine(); cloned.name = cloned.name + " modified"; cloned.descr = cloned.descr + " modified"; cloned.timeStamp = DateTime.Today; cloned.children.AddRange(new List <string> { "key11", "key13" }); cloned.payload.theWrappedData.AddRange(new List <string> { "zeta", "eta" }); "output from elem.showElement<string, ListOfStrings>()".title(); Write(elem.showElement <string, ListOfStrings>()); "output from cloned.showElement<string, ListOfStrings>()".title(); Write(cloned.showElement <string, ListOfStrings>()); "Testing equality after modifying clone's values".title(); showEquality(cloned.name, elem.name, "cloned.name vs. elem.name"); showEquality(cloned.descr, elem.descr, "cloned.descr vs. elem.descr"); showDateTimeEquality(cloned.timeStamp, elem.timeStamp, "cloned.timeStamp vs. elem.timeStamp"); showListEquality(cloned.children, elem.children, "cloned.children vs. elem.children"); showListEquality <string>(cloned.payload.theWrappedData, elem.payload.theWrappedData, "cloned.payload vs. elem.payload"); Write("\n\n"); }