private static void PrintGraph(BaseGraph graph) { using (TextWriter writer = File.CreateText("output.txt")) { writer.Write(graph); } }
private Book(string name, BaseGraph baseGraph) { Name = name; this.CreateDate = DateTime.Now; this.m_Paragraphs = new Dictionary<int, Paragraph>(); this.m_Edges = new Dictionary<int, List<Edge>>(); foreach (var vertex in baseGraph.Vertices) { var paragraph = new Paragraph(vertex); if (baseGraph.Descriptions.ContainsKey(vertex)) { paragraph.Description = baseGraph.Descriptions[vertex]; } m_Paragraphs.Add(vertex, paragraph); m_Edges.Add(vertex, new List<Edge>()); } foreach (var simpleEdge in baseGraph.Edges) { var edge = new Edge(GetParagraph(simpleEdge.From), GetParagraph(simpleEdge.To), true); m_Edges[simpleEdge.From].Add(edge); } AvailableItems = new List<ItemType>(); }
public void Update(BaseGraph baseGraph) { this.LastUpdateDate = DateTime.Now; foreach (var vertex in baseGraph.Vertices) { Paragraph paragraph; if (m_Paragraphs.ContainsKey(vertex)) { paragraph = m_Paragraphs[vertex]; } else { paragraph = new Paragraph(vertex); m_Paragraphs.Add(vertex, paragraph); m_Edges.Add(vertex, new List<Edge>()); } if (baseGraph.Descriptions.ContainsKey(vertex)) { paragraph.Description = baseGraph.Descriptions[vertex]; } } foreach (var simpleEdge in baseGraph.Edges) { var edges = this.GetEdges(simpleEdge.From, simpleEdge.To); // TODO: Check condition when to add/not add new edge if (!edges.Any(e => e.IsDefault)) { var edge = new Edge(m_Paragraphs[simpleEdge.From], m_Paragraphs[simpleEdge.To], true); m_Edges[simpleEdge.From].Add(edge); } } }
public override BaseGraph CreateGraphFromText(string text) { var result = new BaseGraph(); string[] split = text.ToLower().Split(); int currentParagraph = 0; int state = 0; // 0 -- base state, nothing found int currentDescriptionPosition = 0; int start; string description; foreach (string part in split) { string str = this.RemoveSpecialCharacters(part); if (str == "иди") { state = 1; // 1 -- "иди" was found continue; } if (state == 1 && str == "на") { state = 2; // 2 -- "на" was found continue; } int num; if (int.TryParse(str, out num)) { if (state == 2) { if (num != currentParagraph) { result.AddEdge(currentParagraph, num); } state = 0; continue; } if (num == currentParagraph + 1) { if (currentParagraph > 0) { var regex = new Regex(string.Format(@"(?<P>\b{0}\b.*?)\b(?<Next>{1}\b)", currentParagraph, currentParagraph + 1), RegexOptions.Singleline); var match = regex.Match(text, currentDescriptionPosition); description = match.Groups["P"].Value; result.Descriptions.Add(currentParagraph, description); currentDescriptionPosition = match.Groups["Next"].Index; } currentParagraph++; state = 0; continue; } } state = 0; } start = text.IndexOf(currentParagraph.ToString(), currentDescriptionPosition); description = text.Substring(start); result.Descriptions.Add(currentParagraph, description); return result; }