public void Test1() { var graph = new Graph(); var a = new StringVertex("A"); var b = new StringVertex("B"); var c = new StringVertex("C"); var d = new StringVertex("D"); graph.AddVertex(a); graph.AddVertex(b); graph.AddVertex(c); graph.AddVertex(d); graph.AddEdge(new Edge(a, b)); //graph.AddEdge(new Edge(a, c)); graph.AddEdge(new Edge(a, d)); graph.AddEdge(new Edge(b, c)); graph.AddEdge(new Edge(b, d)); graph.AddEdge(new Edge(c, d)); Console.WriteLine(GraphPrinter.ToDot(graph)); var cycles = graph.GetCycles(true); Assert.IsTrue(cycles.Count == 2); Assert.IsTrue(cycles[0].EdgeCount == 3); Assert.IsTrue(cycles[1].EdgeCount == 3); List <List <Edge> > overlap = cycles[0].Overlap(cycles[1]); Assert.IsTrue(overlap.Count == 1); Assert.IsTrue(overlap[0].Count == 1); }
public void Print2() { var graph = new Graph(); var v1 = new StringVertex("blue"); graph.AddVertex(v1); var v2 = new StringVertex("green"); graph.AddVertex(v2); var v3 = new StringVertex("yellow"); graph.AddVertex(v3); var v4 = new StringVertex("yellow"); graph.AddVertex(v4); var v5 = new StringVertex("blue"); graph.AddVertex(v5); var v6 = new StringVertex("green"); graph.AddVertex(v6); graph.AddEdge(new StringEdge(v1, v4, "green")); graph.AddEdge(new StringEdge(v2, v5, "yellow")); graph.AddEdge(new StringEdge(v3, v6, "blue")); graph.AddEdge(new StringEdge(v4, v5, "green")); graph.AddEdge(new StringEdge(v5, v6, "yellow")); graph.AddEdge(new StringEdge(v6, v4, "blue")); string graphString = GraphPrinter.ToDot(graph, true, true); File.WriteAllText("print2.gv", graphString); }
public void Test_NormalGraph() { var printer = new GraphPrinter(); var graph1 = new UndirectedGraph(); var vertexA = new Vertex("A"); var vertexB = new Vertex("B"); var vertexC = new Vertex("C"); var vertexD = new Vertex("D"); var vertexE = new Vertex("E"); graph1.AddEdge(new UndirectedEdge(vertexA, vertexB)); graph1.AddEdge(new UndirectedEdge(vertexA, vertexC)); graph1.AddEdge(new UndirectedEdge(vertexB, vertexC)); graph1.AddEdge(new UndirectedEdge(vertexB, vertexD)); graph1.AddEdge(new UndirectedEdge(vertexC, vertexE)); graph1.AddEdge(new UndirectedEdge(vertexD, vertexE)); graph1.AddVertex(vertexA); graph1.AddVertex(vertexB); graph1.AddVertex(vertexC); graph1.AddVertex(vertexD); graph1.AddVertex(vertexE); Assert.AreEqual("({A; B; C; D; E}, {(A, B); (A, C); (B, C); (B, D); (C, E); (D, E)})", printer.GraphToString(graph1), "Ошибка: граф №1, GraphToString"); Assert.AreEqual("{(A, B); (A, C); (B, C); (B, D); (C, E); (D, E)}", printer.EdgesToString(graph1), "Ошибка: граф №1, EdgesToString"); Assert.AreEqual("{A; B; C; D; E}", printer.VerticesToString(graph1), "Ошибка: граф №1, VerticesToString"); }
public void Test_EmptyGraph() { var printer = new GraphPrinter(); var graph3 = new UndirectedGraph(); Assert.AreEqual("({\x00D8}, {\x00D8})", printer.GraphToString(graph3), "Ошибка: граф №3, GraphToString"); Assert.AreEqual("{\x00D8}", printer.EdgesToString(graph3), "Ошибка: граф №3, EdgesToString"); Assert.AreEqual("{\x00D8}", printer.VerticesToString(graph3), "Ошибка: граф №3, VerticesToString"); }
protected override void OnKeyDown(KeyRoutedEventArgs e) { base.OnKeyDown(e); if (e.Handled) { return; } switch (e.Key) { case VirtualKey.Control: _isCtrlPressed = true; break; case VirtualKey.P: if (_isCtrlPressed) { e.Handled = true; var action = GraphPrinter.PrintAsync(Graph); } break; case VirtualKey.N: if (_isCtrlPressed) { e.Handled = true; NewFunction(); } break; case VirtualKey.F11: { ApplicationView view = ApplicationView.GetForCurrentView(); if (view.IsFullScreenMode) { view.ExitFullScreenMode(); e.Handled = true; } else { e.Handled = view.TryEnterFullScreenMode(); } } break; #if PERF_TEST case VirtualKey.F5: PerfTest(); break; #endif } }
public void Print1() { var graph = new Graph(); var v1 = new IntVertex(1); graph.AddVertex(v1); var v2 = new IntVertex(2); graph.AddVertex(v2); var v3 = new IntVertex(3); graph.AddVertex(v3); var v4 = new IntVertex(2); graph.AddVertex(v4); var v5 = new IntVertex(3); graph.AddVertex(v5); var v6 = new IntVertex(2); graph.AddVertex(v6); var e1 = new Edge(v1, v2); graph.AddEdge(e1); var e2 = new Edge(v2, v3); graph.AddEdge(e2); var e3 = new Edge(v1, v4); graph.AddEdge(e3); var e4 = new Edge(v2, v5); graph.AddEdge(e4); var e5 = new Edge(v3, v6); graph.AddEdge(e5); var e6 = new Edge(v4, v5); graph.AddEdge(e6); var e7 = new Edge(v5, v6); graph.AddEdge(e7); string graphString = GraphPrinter.ToDot(graph); File.WriteAllText("print1.gv", graphString); }
public void Test_GraphWithoutEdges() { var printer = new GraphPrinter(); var graph2 = new UndirectedGraph(); var vertex1 = new Vertex("1"); var vertex2 = new Vertex("2"); var vertex3 = new Vertex("3"); var vertex4 = new Vertex("4"); var vertex5 = new Vertex("5"); graph2.AddVertex(vertex1); graph2.AddVertex(vertex2); graph2.AddVertex(vertex3); graph2.AddVertex(vertex4); graph2.AddVertex(vertex5); Assert.AreEqual("({1; 2; 3; 4; 5}, {\x00D8})", printer.GraphToString(graph2), "Ошибка: граф №2, GraphToString"); Assert.AreEqual("{\x00D8}", printer.EdgesToString(graph2), "Ошибка: граф №2, EdgesToString"); Assert.AreEqual("{1; 2; 3; 4; 5}", printer.VerticesToString(graph2), "Ошибка: граф №1, VerticesToString"); }
public void Print(string fileName) { GraphPrinter graphPrinter = new GraphPrinter(fileName); graphPrinter.Begin(); if (!Root.IsFinalNode()) { graphPrinter.PrintNode(Root); } else { graphPrinter.PrintNode(Root, Color.BLACK, Color.BLACK, Style.DASHED); } graphPrinter.PrintNodeLabel(Root); PrintRecursively(null, Root, Color.BLUE, graphPrinter); graphPrinter.End(); }
public void Test() { Dungeon = new Graph(); int victoryPathLength = 10; REssential[] victoryPath = new REssential[victoryPathLength]; for (int i = 0; i < victoryPathLength; i++) { victoryPath[i] = new REssential(); Dungeon.AddVertex(victoryPath[i]); if (i > 0) { Dungeon.AddEdge(new Edge(victoryPath[i], victoryPath[i - 1])); } } for (int i = 0; i < 2; i++) { var builder = new ReplacementRuleBuilder(); builder.MappedVertex <REssential>("loop") .MappedVertexWithEdge <REssential, Edge>() .MappedVertexWithEdge <REssential, Edge>() .MappedVertexWithEdge <REssential, Edge>() .ReplacementVertexWithEdge <RBasic, Edge>() .ReplacementEdge <Edge>() .MoveToTag("loop"); ReplacementRule addShortcut = builder.GetResult(); Assert.IsTrue(addShortcut.Pattern.Vertices.Count == 4); Assert.IsTrue(addShortcut.Mapping.Count == 4); Assert.IsTrue(addShortcut.Replacement.Vertices.Count == 5); bool success = Dungeon.Replace(addShortcut, randomMatch: true); Assert.IsTrue(success); } var bossTreasure = new ReplacementRule(); var anchorP = new RBasic(); bossTreasure.Pattern.AddVertex(anchorP); var anchorR = new RBasic(); var boss = new RBoss(); var treasure = new RTreasure(); bossTreasure.Replacement.AddVertex(anchorR); bossTreasure.Replacement.AddVertex(boss); bossTreasure.Replacement.AddVertex(treasure); bossTreasure.Replacement.AddEdge(new Edge(anchorR, boss)); bossTreasure.Replacement.AddEdge(new Edge(boss, treasure)); bossTreasure.Mapping[anchorP] = anchorR; Assert.IsTrue(bossTreasure.Pattern.Vertices.Count == 1); Assert.IsTrue(bossTreasure.Mapping.Count == 1); Assert.IsTrue(bossTreasure.Replacement.Vertices.Count == 3); bool success2 = Dungeon.Replace(bossTreasure, randomMatch: true); Assert.IsTrue(success2); File.WriteAllText("dungeon.gv", GraphPrinter.ToDot(Dungeon)); }
public void Test1() { // 1 // / \ // 3 -- 2 Graph graph = new Graph(); var v1 = new Vertex(); var v2 = new Vertex(); var v3 = new Vertex(); graph.AddVertex(v1); graph.AddVertex(v2); graph.AddVertex(v3); graph.AddEdge(new Edge(v1, v2)); graph.AddEdge(new Edge(v2, v3)); graph.AddEdge(new Edge(v3, v1)); string initialGraphString = GraphPrinter.ToDot(graph); // 1 -- 2 Graph pattern = new Graph(); var vp1 = new Vertex(); var vp2 = new Vertex(); pattern.AddVertex(vp1); pattern.AddVertex(vp2); pattern.AddEdge(new Edge(vp1, vp2)); // 1 -- 2 -- 3 Graph replacement = new Graph(); var vr1 = new Vertex(); var vr2 = new Vertex(); var vr3 = new Vertex(); replacement.AddVertex(vr1); replacement.AddVertex(vr2); replacement.AddVertex(vr3); replacement.AddEdge(new Edge(vr1, vr2)); replacement.AddEdge(new Edge(vr2, vr3)); var mapping = new Dictionary <Vertex, Vertex> { { vp1, vr1 }, { vp2, vr3 } }; graph.Replace(pattern, replacement, mapping, true); Assert.IsTrue(graph.Vertices.Count == 4); Assert.IsTrue(graph.Vertices.TrueForAll(v => v.Edges.Count == 2)); Assert.IsTrue(graph.Vertices.Contains(vr1)); Assert.IsTrue(graph.Vertices.Contains(vr2)); Assert.IsTrue(graph.Vertices.Contains(vr3)); string afterReplaceString = GraphPrinter.ToDot(graph); File.WriteAllText("before.gv", initialGraphString); File.WriteAllText("after.gv", afterReplaceString); }
private async void PrintButton_ClickAsync(object sender, RoutedEventArgs e) => await GraphPrinter.PrintAsync(Graph).ConfigureAwait(false);
public void Start() { if (seed == 0) { seed = (int)System.DateTime.Now.Ticks; } Console.WriteLine("Seed: " + seed); rnd = new Random(seed); var builder = new ReplacementRuleBuilder(); builder.MappedVertex <StartingRoom>("start") .ReplacementVertexWithEdge <BasicRoom, Edge>().ReplacementVertexWithEdge <BasicRoom, Edge>().MoveToTag("start") .ReplacementVertexWithEdge <BasicRoom, Edge>().ReplacementVertexWithEdge <BasicRoom, Edge>().MoveToTag("start") .ReplacementVertexWithEdge <BasicRoom, Edge>().ReplacementVertexWithEdge <BasicRoom, Edge>(); var initialRule = builder.GetResult(); var dungeon = new Graph(); dungeon.AddVertex(new StartingRoom()); dungeon.Replace(initialRule, true); for (int i = 0; i < 15; i++) { builder.Reset() .MappedVertex <BasicRoom>("a") .PatternVertexWithEdge <BasicRoom, Edge>("b") .MoveToTag("a").ReplacementVertexWithEdge <Junction, Edge>("j") .ReplacementVertexWithEdge <BasicRoom, Edge>().MoveToTag("j") .ReplacementVertexWithEdge <BasicRoom, Edge>().MapToTag("b"); var addJunction = builder.GetResult(); builder.Reset() .MappedVertex <BasicRoom>("a") .PatternVertexWithEdge <BasicRoom, Edge>("b").MoveToTag("a") .ReplacementVertexWithEdge <BasicRoom, Edge>() .ReplacementVertexWithEdge <BasicRoom, Edge>().MapToTag("b"); var stretch = builder.GetResult(); builder.Reset() .MappedVertex <BasicRoom>("a") .PatternVertexWithEdge <Junction, Edge>("j") .PatternVertexWithEdge <BasicRoom, Edge>("b").MoveToTag("j") .PatternVertexWithEdge <BasicRoom, Edge>("c").MoveToTag("a") .ReplacementVertexWithEdge <BasicRoom, Edge>().MapToTag("b") .ReplacementVertexWithEdge <BasicRoom, Edge>().MapToTag("c") .ReplacementEdge <Edge>().MoveToTag("a"); var transformJunction = builder.GetResult(); builder.Reset() .MappedVertex <BasicRoom>("a") .MappedVertexWithEdge <BasicRoom, Edge>() .MappedVertexWithEdge <BasicRoom, Edge>() .MappedVertexWithEdge <BasicRoom, Edge>() .ReplacementEdge <Edge>().MoveToTag("a"); var createLoop = builder.GetResult(); builder.Reset() .MappedVertex <BasicRoom>() .ReplacementVertexWithEdge <BasicRoom, Edge>(); var addRoom = builder.GetResult(); var rules = new Tuple <ReplacementRule, int>[] { Tuple.Create(addJunction, 3), Tuple.Create(stretch, 2), Tuple.Create(createLoop, 2), Tuple.Create(transformJunction, 1) }; int acc = 0; int[] absoluteDistribution = rules.Select(t => acc += t.Item2).ToArray(); int endurance = 10; int ruleIndex; bool ruleSuccess; do { if (endurance-- == 0) { dungeon.Replace(addRoom, true); break; } int r = rnd.Next(acc); for (ruleIndex = 0; ruleIndex < rules.Length; ruleIndex++) { if (r < absoluteDistribution[ruleIndex]) { break; } } ruleSuccess = dungeon.Replace(rules[ruleIndex].Item1, true); } while (!ruleSuccess); } File.WriteAllText("advancedDungeon.gv", GraphPrinter.ToDot(dungeon)); }
private void PrintRecursively(HybridTrieNode previousNode, HybridTrieNode nextNode, Color color, GraphPrinter graphPrinter) { if (nextNode != null) { if (previousNode != null) { if (color.Equals(Color.BLUE)) { graphPrinter.PrintEdge(previousNode, nextNode, Color.BLUE); } else if (color.Equals(Color.RED)) { graphPrinter.PrintEdge(previousNode, nextNode, Color.RED); } else if (color.Equals(Color.GREEN)) { graphPrinter.PrintEdge(previousNode, nextNode, Color.GREEN); } if (nextNode.IsFinalNode()) { if (color.Equals(Color.BLUE)) { graphPrinter.PrintNode(nextNode, Color.BLUE, Color.BLUE, Style.DASHED); } else if (color.Equals(Color.RED)) { graphPrinter.PrintNode(nextNode, Color.RED, Color.RED, Style.DASHED); } else if (color.Equals(Color.GREEN)) { graphPrinter.PrintNode(nextNode, Color.GREEN, Color.GREEN, Style.DASHED); } } graphPrinter.PrintNodeLabel(nextNode); } PrintRecursively(nextNode, nextNode.LeftChild, Color.BLUE, graphPrinter); PrintRecursively(nextNode, nextNode.MiddleChild, Color.RED, graphPrinter); PrintRecursively(nextNode, nextNode.RightChild, Color.GREEN, graphPrinter); } }
private void InitToolBarCommands() { #region Первый этап #region Добавление рёбер var phase1AddEdgeCommand = new ToolBarToggleCommand( () => { IsMouseVerticesMovingEnabled = false; IsEgesAddingEnabled = true; _state = State.EdgesAdding; }, () => { IsMouseVerticesMovingEnabled = true; IsEgesAddingEnabled = false; _state = State.Nothing; }, () => _state == State.Nothing, () => true ) { Image = new BitmapImage(GetImageUri("Arrow.png")), Description = Strings.Strings_RU.buttonEdges }; #endregion #region Завершить этап var allSubgraphs = new ObservableCollection <IGraph>(); var phase1Command = new ToolBarInstantCommand( () => { var solve = true; var gp = new GraphPrinter(); UserActionsManager.RegisterInfo(Strings.Strings_RU.stage1Check + gp.GraphToString(CurrentGraph)); CurrentGraph.Vertices.ForEach(v1 => CurrentGraph.Vertices.ForEach(v2 => { solve = solve && (v1.Equals(v2) || (CurrentGraph[v1, v2] == null ^ GivenGraph[ GivenGraph.Vertices.Single(v1.Equals), GivenGraph.Vertices.Single(v2.Equals)] == null )); } )); if (solve) { UserActionsManager.RegisterInfo(Strings.Strings_RU.stage1Done); GivenGraph = CurrentGraph; CurrentGraph = new UndirectedGraph(); Phase1ToolBarVisibility = Visibility.Collapsed; Phase2ToolBarVisibility = Visibility.Visible; L1 = Strings.Strings_RU.subgraph; FindAllSubgraphs(GivenGraph, 0, GivenGraph.Vertices.ToList(), allSubgraphs); new SimpleDialog("Справка", Strings.Strings_RU.stage2Help).Show(); } else { UserActionsManager.RegisterMistake(Strings.Strings_RU.stage1Mistake1, 10); } }, () => _state == State.Nothing ) { Image = new BitmapImage(GetImageUri("Check.png")), Description = Strings.Strings_RU.stage1DoneButtonDisc }; #endregion #region Справка var phase1HelpCommand = new ToolBarInstantCommand( () => { new SimpleDialog("Справка", Strings.Strings_RU.stage1Help).Show(); }, () => _state == State.Nothing ) { Description = Strings.Strings_RU.buttonHelp, Image = new BitmapImage(GetImageUri("Info.png")) }; #endregion #region Молния var thunderbolt1 = new ToolBarInstantCommand( () => { UserActionsManager.RegisterInfo(Strings.Strings_RU.stage1ThunderCall); CurrentGraph.Vertices.ForEach(v1 => CurrentGraph.Vertices.ForEach(v2 => { if (!v1.Equals(v2) && CurrentGraph[v1, v2] == null && GivenGraph[ GivenGraph.Vertices.Single(v1.Equals), GivenGraph.Vertices.Single(v2.Equals)] == null) { CurrentGraph.AddEdge(new UndirectedEdge((Vertex)v1, (Vertex)v2)); } if (!v1.Equals(v2) && CurrentGraph[v1, v2] != null && GivenGraph[ GivenGraph.Vertices.Single(v1.Equals), GivenGraph.Vertices.Single(v2.Equals)] != null) { CurrentGraph.RemoveEdge(CurrentGraph[v1, v2]); } } )); }, () => _state == State.Nothing ) { Description = "Молния", Image = new BitmapImage(GetImageUri("thunder.png")) }; #endregion #endregion #region Второй этап #region Добавление вершин var vertexDialogCommand = new ToolBarInstantCommand( () => { var dialog = new VertexDialog((UndirectedGraph)CurrentGraph, GivenGraph.Vertices); dialog.Show(); dialog.Closed += (sender, args) => { var buf = CurrentGraph; CurrentGraph = null; CurrentGraph = buf; }; }, () => _state == State.Nothing) { Image = new BitmapImage(GetImageUri("Vertexes.png")), Description = Strings.Strings_RU.buttonVertexDialog }; #endregion #region Добавление рёбер var phase2AddEdgeCommand = new ToolBarToggleCommand( () => { IsMouseVerticesMovingEnabled = false; IsEgesAddingEnabled = true; _state = State.EdgesAdding; }, () => { IsMouseVerticesMovingEnabled = true; IsEgesAddingEnabled = false; _state = State.Nothing; }, () => _state == State.Nothing, () => true ) { Image = new BitmapImage(GetImageUri("Arrow.png")), Description = Strings.Strings_RU.buttonEdges }; #endregion #region Добавление подграфов var subgraphCommand = new ToolBarInstantCommand( () => { var subgraph = true; var unique = Unique((UndirectedGraph)CurrentGraph, GraphLib.Lib); var gp = new GraphPrinter(); UserActionsManager.RegisterInfo(Strings.Strings_RU.stage2Check + gp.GraphToString(CurrentGraph)); CurrentGraph.Vertices.ForEach(v1 => { CurrentGraph.Vertices.ForEach(v2 => { subgraph &= v1.Equals(v2) || (CurrentGraph[v1, v2] == null ^ GivenGraph[ GivenGraph.Vertices.Single(v1.Equals), GivenGraph.Vertices.Single(v2.Equals)] != null); } ); } ); if (CurrentGraph.VerticesCount == 0) { return; } if (!subgraph) { UserActionsManager.RegisterMistake(Strings.Strings_RU.stage2Mistake1, 10); return; } if (!unique) { UserActionsManager.RegisterMistake(Strings.Strings_RU.stage2Mistake2, 10); return; } var newGraph = UndirectedGraph.CreateEmpty(CurrentGraph.VerticesCount); for (var i = 0; i < CurrentGraph.VerticesCount; i++) { for (var j = i + 1; j < CurrentGraph.VerticesCount; j++) { if (CurrentGraph[CurrentGraph.Vertices[i], CurrentGraph.Vertices[j]] != null) { newGraph.AddEdge(new UndirectedEdge(newGraph.Vertices[i], newGraph.Vertices[j])); } } } UserActionsManager.RegisterInfo(Strings.Strings_RU.stage2Subgraph); GraphLib.Lib.Add(newGraph); }, () => _state == State.Nothing ) { Image = new BitmapImage(GetImageUri("Collection.png")), Description = Strings.Strings_RU.buttonCheckSubgraph }; #endregion #region Справка var phase2HelpCommand = new ToolBarInstantCommand( () => { new SimpleDialog("Справка", Strings.Strings_RU.stage2Help).Show(); }, () => _state == State.Nothing ) { Description = Strings.Strings_RU.buttonHelp, Image = new BitmapImage(GetImageUri("Info.png")) }; #endregion #region Молния var thunderbolt2 = new ToolBarInstantCommand( () => { UserActionsManager.RegisterInfo(Strings.Strings_RU.stage2ThunderCall); allSubgraphs.ForEach(s => { if (Unique(s, GraphLib.Lib)) { GraphLib.Lib.Add(s); } }); var g = UndirectedGraph.CreateEmpty(GivenGraph.VerticesCount); for (var i = 0; i < g.VerticesCount; i++) { for (var j = i + 1; j < g.VerticesCount; j++) { if (GivenGraph[GivenGraph.Vertices[i], GivenGraph.Vertices[j]] != null) { g.AddEdge(new UndirectedEdge(g.Vertices[i], g.Vertices[j])); } } } GraphLib.Lib.Add(g); }, () => _state == State.Nothing ) { Description = "Молния", Image = new BitmapImage(GetImageUri("thunder.png")) }; #endregion #region Завершить этап var phase2Command = new ToolBarInstantCommand( () => { if (GraphLib.Lib.Count > allSubgraphs.Count) { UserActionsManager.RegisterInfo(Strings.Strings_RU.stage2Done); UserActionsManager.ReportThatTaskFinished(); } else { UserActionsManager.RegisterMistake(Strings.Strings_RU.stage2Mistake3, 10); } }, () => _state == State.Nothing ) { Image = new BitmapImage(GetImageUri("Check.png")), Description = Strings.Strings_RU.stage2DoneButtonDisc }; #endregion #endregion Phase1ToolBarCommands = new ObservableCollection <ToolBarCommandBase> { phase1AddEdgeCommand, phase1Command, phase1HelpCommand #if DEBUG , thunderbolt1 #endif }; Phase2ToolBarCommands = new ObservableCollection <ToolBarCommandBase> { vertexDialogCommand, phase2AddEdgeCommand, subgraphCommand, phase2Command, phase2HelpCommand #if DEBUG , thunderbolt2 #endif }; }