public IEnumerable <IConceptInfo> FindByType(Type conceptType, bool includeDerivations)
 {
     if (includeDerivations)
     {
         return(_conceptsByType
                .Where(conceptsGroup => conceptType.IsAssignableFrom(conceptsGroup.Key))
                .SelectMany(conceptsGroup => conceptsGroup.Value));
     }
     else
     {
         List <IConceptInfo> result = null;
         if (_conceptsByType.TryGetValue(conceptType, out result))
         {
             return(result);
         }
         return(new List <IConceptInfo>());
     }
 }
예제 #2
0
        private static string ProcessCommands(MultiDictionary<string, MultiDictionary<string, string>> phoneBook)
        {
            StreamReader commandsReader = new StreamReader("commands.txt");

            StringBuilder result = new StringBuilder();

            using (commandsReader)
            {
                string command = commandsReader.ReadLine();

                while (command != null)
                {
                    int indexOfOpenBracket = command.IndexOf('(');
                    string withoutCommandName = command.Substring(indexOfOpenBracket + 1).TrimEnd(')');

                    var splitted = withoutCommandName.Split(',');
                    string nameFromCommand = splitted[0].Trim();

                    // if the command is search by name only
                    if (splitted.Length == 1)
                    {
                        // get all elements which have got the same name as the searched name from command
                        var resultOnlyByName = phoneBook.Where(p => p.Key == nameFromCommand);

                        foreach (var nameEntries in resultOnlyByName)
                        {
                            foreach (var townPhoneCollection in nameEntries.Value)
                            {
                                foreach (var phoneCollection in townPhoneCollection)
                                {
                                    foreach (var phoneNumber in phoneCollection.Value)
                                    {
                                        string addingLineToResult = string.Format("{0} from {1} -> {2}", nameEntries.Key, phoneCollection.Key, phoneNumber);
                                        result.AppendLine(addingLineToResult);
                                    }
                                }
                            }
                        }

                        // added to divide different commands results
                        result.AppendLine("------------------------------------------------");
                    }
                    else if (splitted.Length == 2)
                    {
                        // if the command is search by name and location
                        string town = splitted[1].Trim();

                        // get all elements which have got the same name as the searched name from command
                        var currentResultByName = phoneBook.Where(p => p.Key == nameFromCommand);

                        foreach (var nameEntries in currentResultByName)
                        {
                            foreach (var townPhoneCollection in nameEntries.Value)
                            {
                                // then get all elements which have got the same town as the searched town from command
                                var resultAndByTown = townPhoneCollection.Where(t => t.Key == town);

                                foreach (var phoneCollection in resultAndByTown)
                                {
                                    foreach (var phoneNumber in phoneCollection.Value)
                                    {
                                        string addingLineToResult = string.Format("{0} from {1} -> {2}", nameEntries.Key, phoneCollection.Key, phoneNumber);
                                        result.AppendLine(addingLineToResult);
                                    }
                                }
                            }
                        }

                        // added to divide different commands results
                        result.AppendLine("------------------------------------------------");
                    }

                    command = commandsReader.ReadLine();
                }
            }

            return result.ToString();
        }
예제 #3
0
        public static void Main(string[] args)
        {
            StreamReader phones = new StreamReader("..\\..\\phones.txt");

            var phonebook = new MultiDictionary<string, MultiDictionary<string, string>>(false);
            using (phones)
            {
                while (!phones.EndOfStream)
                {
                    string entry = phones.ReadLine();
                    string[] entryInfo = entry.Split('|');
                    string name = entryInfo[0].Trim();
                    string location = entryInfo[1].Trim();
                    string phoneNumber = entryInfo[2].Trim();

                    if (phonebook.ContainsKey(name))
                    {
                        var nameInPhoneBook = phonebook[name].First();
                        if (nameInPhoneBook.ContainsKey(location))
                        {
                            nameInPhoneBook[location].Add(phoneNumber);
                        }
                        else
                        {
                            nameInPhoneBook.Add(location, phoneNumber);
                        }
                    }
                    else
                    {
                        phonebook.Add(name, new MultiDictionary<string, string>(true));
                        var nameInPhoneBook = phonebook[name].First();
                        nameInPhoneBook.Add(location, phoneNumber);
                    }
                }
            }

            var commands = new StreamReader("..\\..\\commands.txt");
            using (commands)
            {
                while (!commands.EndOfStream)
                {
                    string commandString = commands.ReadLine();
                    string[] parameters = commandString
                        .Substring(commandString.IndexOf('(') + 1, commandString.IndexOf(')') - commandString.IndexOf('(') - 1)
                        .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    string name = parameters[0].Trim();
                    string location = string.Empty;

                    if (parameters.Length == 2)
                    {
                        location = parameters[1].Trim();
                    }

                    Console.WriteLine(commandString);

                    if (location == string.Empty)
                    {
                        var resultsByName = phonebook.Where(x => x.Key == name);
                        foreach (var nameEntries in resultsByName)
                        {
                            foreach (var locationPhoneCollection in nameEntries.Value)
                            {
                                foreach (var phonesCollection in locationPhoneCollection)
                                {
                                    foreach (var phoneNumber in phonesCollection.Value)
                                    {
                                        Console.WriteLine("{0} -> {1} -> {2}",
                                            nameEntries.Key, phonesCollection.Key, phoneNumber);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        var resultsByName = phonebook.Where(x => x.Key == name);

                        foreach (var nameEntries in resultsByName)
                        {
                            foreach (var locationPhoneCollection in nameEntries.Value)
                            {
                                var nameAndLocation = locationPhoneCollection.Where(x => x.Key == location);

                                foreach (var phonesCollection in nameAndLocation)
                                {
                                    foreach (var phoneNumber in phonesCollection.Value)
                                    {
                                        Console.WriteLine("{0} -> {1} -> {2}", nameEntries.Key, phonesCollection.Key, phoneNumber);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #4
0
        private static string ProcessCommands(MultiDictionary <string, MultiDictionary <string, string> > phoneBook)
        {
            StreamReader commandsReader = new StreamReader("commands.txt");

            StringBuilder result = new StringBuilder();

            using (commandsReader)
            {
                string command = commandsReader.ReadLine();

                while (command != null)
                {
                    int    indexOfOpenBracket = command.IndexOf('(');
                    string withoutCommandName = command.Substring(indexOfOpenBracket + 1).TrimEnd(')');

                    var    splitted        = withoutCommandName.Split(',');
                    string nameFromCommand = splitted[0].Trim();

                    // if the command is search by name only
                    if (splitted.Length == 1)
                    {
                        // get all elements which have got the same name as the searched name from command
                        var resultOnlyByName = phoneBook.Where(p => p.Key == nameFromCommand);

                        foreach (var nameEntries in resultOnlyByName)
                        {
                            foreach (var townPhoneCollection in nameEntries.Value)
                            {
                                foreach (var phoneCollection in townPhoneCollection)
                                {
                                    foreach (var phoneNumber in phoneCollection.Value)
                                    {
                                        string addingLineToResult = string.Format("{0} from {1} -> {2}", nameEntries.Key, phoneCollection.Key, phoneNumber);
                                        result.AppendLine(addingLineToResult);
                                    }
                                }
                            }
                        }

                        // added to divide different commands results
                        result.AppendLine("------------------------------------------------");
                    }
                    else if (splitted.Length == 2)
                    {
                        // if the command is search by name and location
                        string town = splitted[1].Trim();

                        // get all elements which have got the same name as the searched name from command
                        var currentResultByName = phoneBook.Where(p => p.Key == nameFromCommand);

                        foreach (var nameEntries in currentResultByName)
                        {
                            foreach (var townPhoneCollection in nameEntries.Value)
                            {
                                // then get all elements which have got the same town as the searched town from command
                                var resultAndByTown = townPhoneCollection.Where(t => t.Key == town);

                                foreach (var phoneCollection in resultAndByTown)
                                {
                                    foreach (var phoneNumber in phoneCollection.Value)
                                    {
                                        string addingLineToResult = string.Format("{0} from {1} -> {2}", nameEntries.Key, phoneCollection.Key, phoneNumber);
                                        result.AppendLine(addingLineToResult);
                                    }
                                }
                            }
                        }

                        // added to divide different commands results
                        result.AppendLine("------------------------------------------------");
                    }

                    command = commandsReader.ReadLine();
                }
            }

            return(result.ToString());
        }
예제 #5
0
        public static void Main(string[] args)
        {
            StreamReader phones = new StreamReader("..\\..\\phones.txt");

            var phonebook = new MultiDictionary <string, MultiDictionary <string, string> >(false);

            using (phones)
            {
                while (!phones.EndOfStream)
                {
                    string   entry       = phones.ReadLine();
                    string[] entryInfo   = entry.Split('|');
                    string   name        = entryInfo[0].Trim();
                    string   location    = entryInfo[1].Trim();
                    string   phoneNumber = entryInfo[2].Trim();

                    if (phonebook.ContainsKey(name))
                    {
                        var nameInPhoneBook = phonebook[name].First();
                        if (nameInPhoneBook.ContainsKey(location))
                        {
                            nameInPhoneBook[location].Add(phoneNumber);
                        }
                        else
                        {
                            nameInPhoneBook.Add(location, phoneNumber);
                        }
                    }
                    else
                    {
                        phonebook.Add(name, new MultiDictionary <string, string>(true));
                        var nameInPhoneBook = phonebook[name].First();
                        nameInPhoneBook.Add(location, phoneNumber);
                    }
                }
            }

            var commands = new StreamReader("..\\..\\commands.txt");

            using (commands)
            {
                while (!commands.EndOfStream)
                {
                    string   commandString = commands.ReadLine();
                    string[] parameters    = commandString
                                             .Substring(commandString.IndexOf('(') + 1, commandString.IndexOf(')') - commandString.IndexOf('(') - 1)
                                             .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    string name     = parameters[0].Trim();
                    string location = string.Empty;

                    if (parameters.Length == 2)
                    {
                        location = parameters[1].Trim();
                    }

                    Console.WriteLine(commandString);

                    if (location == string.Empty)
                    {
                        var resultsByName = phonebook.Where(x => x.Key == name);
                        foreach (var nameEntries in resultsByName)
                        {
                            foreach (var locationPhoneCollection in nameEntries.Value)
                            {
                                foreach (var phonesCollection in locationPhoneCollection)
                                {
                                    foreach (var phoneNumber in phonesCollection.Value)
                                    {
                                        Console.WriteLine("{0} -> {1} -> {2}",
                                                          nameEntries.Key, phonesCollection.Key, phoneNumber);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        var resultsByName = phonebook.Where(x => x.Key == name);

                        foreach (var nameEntries in resultsByName)
                        {
                            foreach (var locationPhoneCollection in nameEntries.Value)
                            {
                                var nameAndLocation = locationPhoneCollection.Where(x => x.Key == location);

                                foreach (var phonesCollection in nameAndLocation)
                                {
                                    foreach (var phoneNumber in phonesCollection.Value)
                                    {
                                        Console.WriteLine("{0} -> {1} -> {2}", nameEntries.Key, phonesCollection.Key, phoneNumber);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }