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();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            QueryEngine<int, DBElement<int, string>, string> qe = new QueryEngine<int, DBElement<int, string>, string>();
            QueryEngine<string, DBElement<string, List<string>>, List<string>> qeLOS = new QueryEngine<string, DBElement<string, List<string>>, List<string>>();
            DBEngine<int, DBElement<int, string>> db = new DBEngine<int, DBElement<int, string>>();
            DBElement<int, string> elem = new DBElement<int, string>();
            DBElement<int, string> Result = new DBElement<int, string>();
            
            elem.name = "Test";
            elem.descr = "Test descr";
            elem.timeStamp = DateTime.Now;
            elem.children.AddRange(new List<int> { 1, 2, 3 });
            elem.payload = "Test Payload";
            db.insert(0, elem);

            DBElement<int, string> elem1 = new DBElement<int, string>();
            elem1.name = "Test 1";
            elem1.descr = "Test descr 1";
            elem1.timeStamp = DateTime.Now;
            elem1.children.AddRange(new List<int> { 4, 5, 6 });
            elem1.payload = "Test Payload 1";
            db.insert(1, elem1);

            DBEngine<string, DBElement<string, List<string>>> dbLOS = new DBEngine<string, DBElement<string, List<string>>>();
            DBElement<string, List<string>> elemLOS = new DBElement<string, List<string>>();
            DBElement<string, List<string>> ResultLOS = new DBElement<string, List<string>>();
            elemLOS.name = "Test";
            elemLOS.descr = "Test descr";
            elemLOS.timeStamp = DateTime.Now;
            elemLOS.children = new List<string> { "One", "Two", "Three" };
            elemLOS.payload = new List<string> { "Test Payload", "Payload" };
            dbLOS.insert("One", elemLOS);

            DBElement<string, List<string>> elemLOS1 = new DBElement<string, List<string>>();
            elemLOS1.name = "Test 2";
            elemLOS1.descr = "Test descr 2";
            elemLOS1.timeStamp = DateTime.Now;
            elemLOS1.children = new List<string> { "Four", "Five", "Six" };
            elemLOS1.payload = new List<string> { "Test Payload 2", "Payload 2" };
            dbLOS.insert("Two", elemLOS1);

            if (qe.getValue(0, db, out Result))
                WriteLine("Value of key 0:\n{0}\n  Payload:  {1}", Result.showMetaData(), Result.payload.ToString());
            else
                WriteLine("Key not found");
            WriteLine();
            if (qeLOS.getValue("One", dbLOS, out ResultLOS))
                WriteLine("Value of key:\n{0}\n  Payload: {1}", ResultLOS.showMetaData(), qeLOS.ToString(ResultLOS.payload));
            else
                WriteLine("Key not found");
            WriteLine();

            if (qe.getChildren(0, db, out children))
                WriteLine("Children of specified key: {0}", qe.ToString(children));
            else
                WriteLine("Key not found");
            if (qeLOS.getChildren("One", dbLOS, out childrenLOS))
                WriteLine("Children of specified key: {0}", qeLOS.ToString(childrenLOS));
            else
                WriteLine("Key not found");
            WriteLine();

            if (qe.searchPattern(0, db, out Keys))
                Console.WriteLine("Set of Keys: {0}", qe.ToString(Keys));
            else
                Console.WriteLine("No Keys found!");
            if (qeLOS.searchPattern("O", dbLOS, out KeysLOS))
                Console.WriteLine("Set of Keys: {0}", qeLOS.ToString(KeysLOS));
            else
                Console.WriteLine("No Keys found!");
            WriteLine();

            if (qe.searchString("descr", db, out Keys))
                Console.WriteLine("Set of Keys: {0}", qe.ToString(Keys));
            else
                Console.WriteLine("\nNo Keys found!");
            if (qeLOS.searchString("Test", dbLOS, out KeysLOS))
                Console.WriteLine("Set of Keys: {0}", qeLOS.ToString(KeysLOS));
            else
                Console.WriteLine("No Keys found!");
            WriteLine();

            if (qe.searchInterval("10/7/1999 12:00:00 AM", "", db, out Keys))
                Console.WriteLine("Set of Keys: {0}", qe.ToString(Keys));
            else
                Console.WriteLine("\nNo Keys found!");
            if (qeLOS.searchInterval("10/7/1999 12:00:00 AM", "", dbLOS, out KeysLOS))
                Console.WriteLine("Set of Keys: {0}", qeLOS.ToString(KeysLOS));
            else
                Console.WriteLine("No Keys found!");
            WriteLine();
        }