Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
HexMerlin committed Dec 18, 2024
1 parent bce07b0 commit 19b2add
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 27 deletions.
7 changes: 6 additions & 1 deletion Automata.App/Automata.App.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0-windows10.0.26100.0</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<PackageReadmeFile></PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Automata.Core\Automata.Core.csproj" />
<ProjectReference Include="..\Automata.Visualization\Automata.Visualization.csproj" />
Expand Down
51 changes: 45 additions & 6 deletions Automata.App/Program.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@

using Automata.Core;
using Automata.Visualization;
using Microsoft.Msagl.Drawing;
using Spectre.Console;

namespace Automata.App;

/// <summary>
/// A sample program that demonstrates how to create a graph from a collection of sequences and display it in a separate window.
/// </summary>
public static class Program
{
public static void Main()
{

Console.WriteLine("Creating graph..."); // Write some text output to the console window

var sequences = Enumerable.Range(0, 10).Select(_ => Enumerable.Range(0, 8).Select(_ => Random.Shared.Next(4).ToString())); //Create some random sequences


IFsa fsa = new NFA(sequences).ToDFA().Minimized();
Graph graph = GraphFactory.CreateGraph(fsa); // Create a graph object to display using the sequences

//Alternatively: you can replace the two lines above, with the line below which creates a minimized graph directly from a collection of sequences
//Graph graph = GraphFactory.CreateGraph(sequences, minimize: true); // Create a displayable graph object directly using the sequences

ConsoleWindow consoleWindow = ConsoleWindow.Create(); // Creates the main console window.
GraphView graphView = GraphView.Create(); //Create a non-modal window for displaying graphs

consoleWindow.WriteLine("Creating graph...", System.Drawing.Color.Blue); // Write some colored text output to the console window
// graphView.ShowGraph(graph); //Display the graph in the graph view

var sequences = Enumerable.Range(0, 10).Select(_ => Enumerable.Range(0, 8).Select(_ => Random.Shared.Next(4).ToString())); //Create some random sequences
AnsiConsole.MarkupLine("Graph is displayed in a separate window");

var graph = GraphFactory.CreateGraph(sequences, minimize: true); // Create a graph object to display using the sequences
}

consoleWindow.ShowGraph(graph); // Open a new non-modal window that displays the graph in it.

consoleWindow.WriteLine("Graph created.", System.Drawing.Color.Green); // Write some more colored text output to the console window

/// <summary>
/// Displays the specified graph in the graph view.
/// </summary>
/// <param name="graph">The graph to display.</param>
//public static void ShowGraph(Graph graph)
//{
// if (!IsAlive) return;
// // Check if we're on the UI thread
// if (InvokeRequired)
// {
// // If not on UI thread, re-invoke this method on the UI thread
// Invoke(new Action(() => ShowGraph(graph)));
// return;
// }
// GraphView graphView = new GraphView();
// // Safe to update the GViewer directly here
// graphView.GViewer.Graph = graph;
// graphView.Show();
// graphView.Activate();
//}
}
32 changes: 16 additions & 16 deletions Automata.Visualization/ConsoleWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,22 @@ private ConsoleWindow()
/// Displays the specified graph in the graph view.
/// </summary>
/// <param name="graph">The graph to display.</param>
public void ShowGraph(Graph graph)
{
if (!IsAlive) return;
// Check if we're on the UI thread
if (InvokeRequired)
{
// If not on UI thread, re-invoke this method on the UI thread
Invoke(new Action(() => ShowGraph(graph)));
return;
}
GraphView graphView = new GraphView();
// Safe to update the GViewer directly here
graphView.GViewer.Graph = graph;
graphView.Show();
graphView.Activate();
}
//public void ShowGraph(Graph graph)
//{
// if (!IsAlive) return;
// // Check if we're on the UI thread
// if (InvokeRequired)
// {
// // If not on UI thread, re-invoke this method on the UI thread
// Invoke(new Action(() => ShowGraph(graph)));
// return;
// }
// GraphView graphView = new GraphView();
// // Safe to update the GViewer directly here
// graphView.GViewer.Graph = graph;
// graphView.Show();
// graphView.Activate();
//}

/// <summary>
/// Writes a line of text to the console window.
Expand Down
66 changes: 62 additions & 4 deletions Automata.Visualization/GraphView.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,84 @@
using Microsoft.Msagl.Drawing;
using Microsoft.Msagl.GraphViewerGdi;
using Color = System.Drawing.Color;

namespace Automata.Visualization;

///<summary>
/// A form that displays a graph using the MSAGL library.
/// A class for displaying finite-state automata as graphs in a separate window.
///</summary>
internal partial class GraphView : Form
///<remarks>
///This class uses the MSAGL library for rendering and displaying graphs.
///</remarks>
public partial class GraphView : Form
{
///<summary>
/// Gets the GViewer control used to display the graph.
///</summary>
internal GViewer GViewer => gViewer;

public bool IsAlive => !Disposing && !IsDisposed;

///<summary>
/// Initializes a new instance of the <see cref="GraphView"/> class.
///</summary>
public GraphView()
private GraphView()
{
InitializeComponent();
GViewer.PanButtonPressed = true;

}

public static GraphView Create()
{
using ManualResetEvent guiInitialized = new ManualResetEvent(false);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
GraphView? graphView = null;
Thread guiThread = new Thread(() =>
{
graphView = new GraphView();

EventHandler handler = null!;
handler = (sender, args) =>
{
guiInitialized.Set();
graphView.Shown -= handler;
};
graphView.Shown += handler;

graphView.Visible = false;
Application.Run(new ApplicationContext(graphView));


});
guiThread.SetApartmentState(ApartmentState.STA);
guiThread.Start();
guiInitialized.WaitOne();

return graphView!;
}


/// <summary>
/// Displays the specified graph in the graph view.
/// </summary>
/// <param name="graph">The graph to display.</param>
public void ShowGraph(Graph graph)
{
if (!IsAlive) return;
// Check if we're on the UI thread
if (InvokeRequired)
{
// If not on UI thread, re-invoke this method on the UI thread
Invoke(new Action(() => ShowGraph(graph)));
return;
}

// Safe to update the GViewer directly here
this.GViewer.Graph = graph;
this.Show();
this.Activate();
}

///<inheritdoc/>
Expand Down

0 comments on commit 19b2add

Please sign in to comment.