// Read RelationshipGraph whose filename is passed in as a parameter. // Build a RelationshipGraph in RelationshipGraph rg private static void ReadRelationshipGraph(string filename) { rg = new RelationshipGraph(); // create a new RelationshipGraph object string name = ""; // name of person currently being read int numPeople = 0; string[] values; Console.Write("Reading file " + filename + "\n"); try { string input = System.IO.File.ReadAllText(filename); // read file input = input.Replace("\r", ";"); // get rid of nasty carriage returns input = input.Replace("\n", ";"); // get rid of nasty new lines string[] inputItems = Regex.Split(input, @";\s*"); // parse out the relationships (separated by ;) foreach (string item in inputItems) { if (item.Length > 2) // don't bother with empty relationships { values = Regex.Split(item, @"\s*:\s*"); // parse out relationship:name if (values[0] == "name") // name:[personname] indicates start of new person { name = values[1]; // remember name for future relationships rg.AddNode(name); // create the node numPeople++; } else { rg.AddEdge(name, values[1], values[0]); // add relationship (name1, name2, relationship) // handle symmetric relationships -- add the other way if (values[0] == "spouse" || values[0] == "hasFriend") { rg.AddEdge(values[1], name, values[0]); } // for parent relationships add child as well else if (values[0] == "parent") { rg.AddEdge(values[1], name, "child"); } else if (values[0] == "child") { rg.AddEdge(values[1], name, "parent"); } } } } } catch (Exception e) { Console.Write("Unable to read file {0}: {1}\n", filename, e.ToString()); } Console.WriteLine(numPeople + " people read"); }
// Read RelationshipGraph whose filename is passed in as a parameter. // Build a RelationshipGraph in RelationshipGraph rg private static void ReadRelationshipGraph(string filename) { rg = new RelationshipGraph(); // create a new RelationshipGraph object string name = ""; // name of person currently being read int numPeople = 0; // counter of how many people are read string[] values; // array of people in file Console.Write("Reading file " + filename + "\n"); // prompt user for the file try // begin processing contents of file { string input = System.IO.File.ReadAllText(filename); // read file into a variable input = input.Replace("\r", ";"); // replace carriage returns with semicolons input = input.Replace("\n", ";"); // replace new lines with semicolons string[] inputItems = Regex.Split(input, @";\s*"); // parse out the relationships with semicolons foreach (string item in inputItems) // begin processing each item in the new array { if (item.Length > 2) // skip any empty relationships { // begin processing the current item values = Regex.Split(item, @"\s*:\s*"); // parse out relationship from the name if (values[0] == "name") // check to see if the current item is a new person { // begin processing the current name name = values[1]; // remember name for future relationships rg.AddNode(name); // create the node in the relationship graph numPeople++; // increment the number of people in the graph } else { // begin processing the relationships of the current name rg.AddEdge(name, values[1], values[0]); // add the given relationship of the current name to the graph if (values[0] == "spouse" || values[0] == "friend") // check for any symmetric relationships (i.e. friends or spouse) { rg.AddEdge(values[1], name, values[0]); // handle the symmetric relationship by adding the relationship to graph } else if (values[0] == "parent") // check for any parent relationships (i.e. does name have a parent) { rg.AddEdge(values[1], name, "child"); // handle the parent relationship by adding the child relationship to the graph } else if (values[0] == "child") // check for children relationships of the current name (i.e. does name have children) { rg.AddEdge(values[1], name, "parent"); // handle the child relationship by adding a parent relationship to the graph } } // finished processing this item return to the beginning of the foreach loop } } } catch (Exception e) // if there is an error throw an exception { // begin handling the exception Console.Write("Unable to read file {0}: {1}\n", filename, e.ToString()); // write the error message to the console } // proceed if there were no exceptions while reading Console.WriteLine(numPeople + " people read"); // write the total number of people in the graph } // the main function of Program.cs has finished executing