void TestR7() { "Demonstrating Requirement #7".title(); Write("\n --- Test query ---"); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.timeStamp = DateTime.Now; elem1.payload = "default payload"; dbInt.insert(1, elem1); Write("\n --show orginal database--"); dbInt.showDB(); WriteLine(); Write("\n --- Query for the value of a specified key=2 ---"); QueryEngine <int, string> query = new QueryEngine <int, string>(dbInt); DBElement <int, string> elem; query.KeyQuery(2, out elem); elem.showElement(); WriteLine(); Write("\n --- Query for all children of a specified key---"); List <int> children; bool child = query.queryChildren(3, out children); if (child) { foreach (var c in children) { Write("\n child is {0}", c.ToString()); } } else { Write("\n This element doesn't contain key"); } WriteLine(); }
static void Main(string[] args) { Write("\n --- Test specific key query ---"); Write("\n ---Database contents"); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.descr = "payload desxription"; elem1.name = "element 1"; elem1.timeStamp = DateTime.Now; elem1.payload = "a payload"; WriteLine(); DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord"); elem2.descr = "star war 2"; elem2.name = "element 2"; elem2.timeStamp = new DateTime(2015, 9, 10, 12, 30, 1); elem2.payload = "The Empire strikes back!"; WriteLine(); var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot"); elem3.name = "element 3"; elem3.descr = "star war 3"; elem3.timeStamp = new DateTime(2015, 10, 2, 8, 0, 0); elem3.children = new List <int> { 1, 2, 3 }; elem3.payload = "X-Wing fighter in swamp - Oh oh!"; WriteLine(); 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); db.show <int, DBElement <int, string>, string>(); QueryEngine <int, string> QETest = new QueryEngine <int, string>(db); WriteLine(); Write("\n --- Query element which key=2"); QueryEngine <int, string> query = new QueryEngine <int, string>(db); DBElement <int, string> elem; bool valQuery = query.KeyQuery(2, out elem); if (valQuery) { Write("\n This element exist"); } Write(elem.showElement <int, string>()); WriteLine(); Write("\n --- Test queries for children of a specified key ---"); Write("\n --- Query for children of element which key=3 ---"); List <int> children; bool childQuery = query.queryChildren(3, out children); if (childQuery) { Write("\n This element has child"); } foreach (var child in children) { Write("\n Key of child: {0}", child.ToString()); } WriteLine(); Write("\n --- Test all keys that contain a specified string in their metadata section---"); Write("\n --- query for \"star war\" in metadata, return keys ---"); Func <int, bool> stringTest = QETest.defineStringQuery("star war"); List <int> keyCollection; bool stringQuery = QETest.processQuery(stringTest, out keyCollection); foreach (var k in keyCollection) { Write("\n Key: {0}", k.ToString()); } WriteLine(); WriteLine("\n --- Test query according to a specified time-date interval ---"); DateTime time1 = new DateTime(2015, 10, 1, 0, 0, 0); DateTime time2 = new DateTime(2015, 10, 2, 11, 0, 0); List <int> timeCollection; Func <int, bool> timeTest = QETest.defineTimeQuery(time1, time2); bool timeResult = QETest.processQuery(timeTest, out timeCollection); foreach (var k in timeCollection) { WriteLine("key in specific time interval: {0}", k.ToString()); } WriteLine(); WriteLine("--- When time is not provided ---"); DateTime time3 = new DateTime(); timeTest = QETest.defineTimeQuery(time1, time3); bool timeQuery2 = QETest.processQuery(timeTest, out timeCollection); foreach (var k in timeCollection) { WriteLine("key in specific time interval: {0}", k.ToString()); } WriteLine(); }