예제 #1
0
        public LogicManager(MainWindow _window)
        {
            graphToVisualize = null;

            citiesLocations = null;
            citiesConnections = null;

            defaultCitiesLocations = new CitiesLocations();
            defaultCitiesConnections = new CitiesConnections();

            algCitiesLocations = new CitiesLocations();

            startCity = null;
            finalCity = null;

            algSpeed = Alg_Speed.Fast;
            algHeuristic = Heuristic.Distance;

            resources = _window.Resources;
            window = _window;

            graphManager = new GraphManager(window);

            algorithm = null;
            algThread = null;
            IsRunningAlg = false;
        }
        /// <summary>
        /// Updates the vertex internal information.
        /// Maintains consistency between the vertex's position on the
        /// screen and ins internal information.
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="graphLayout"></param>
        public void UpdateVertexInfo(ref VertexCity vertex, ref GraphLayoutCity graphLayout)
        {
            if (vertex != null)
            {
                // Get the vertex control
                VertexControl vertexControl = graphLayout.GetVertexControl(vertex);

                // If we could find the verex control
                if (vertexControl != null)
                {
                    double tempXCoord, tempYCoord;

                    // Get the vertex screen coordinates
                    tempXCoord = GraphCanvas.GetX(vertexControl);
                    tempYCoord = GraphCanvas.GetY(vertexControl);

                    // Update the internal information if the coordinates
                    // are in range from 0 to 800
                    if ((tempXCoord > 0) && (tempXCoord < 800) &&
                        (tempYCoord > 0) && (tempYCoord < 800))
                    {
                        vertex.CityCoordinates.setX((int)tempXCoord);
                        vertex.CityCoordinates.setY((int)tempYCoord);
                    }

                    // Set the new screen coordinates
                    GraphCanvas.SetX(vertexControl, vertex.CityCoordinates.getX());
                    GraphCanvas.SetY(vertexControl, vertex.CityCoordinates.getY());
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Start A-star algorithm in a separate thread
        /// </summary>
        /// <param name="graphLayout"></param>
        /// <param name="richTextBoxLog"></param>
        /// <param name="window"></param>
        public void StartAlgorithm(ref GraphLayoutCity graphLayout, ref TextBox richTextBoxLog,
                                   MainWindow window)
        {
            // If already running
            if (IsRunningAlg)
            {
                algorithm.WaitMutex.WaitOne();
                algorithm.CanContinue = true;
                algorithm.WaitMutex.ReleaseMutex();
            }
            // If not, then start a new thread
            else
            {
                algorithm = new Algorithm(citiesLocations, citiesConnections,
                                          startCity.City, finalCity.City, algSpeed, algHeuristic,
                                          ref graphLayout, ref richTextBoxLog, resources, graphManager);
                // Set window and create a mutex
                algorithm.Window    = window;
                algorithm.WaitMutex = new Mutex();

                // Null the start and end cities
                startCity = null;
                finalCity = null;

                // Create and start the thread
                algThread = new Thread(new ThreadStart(algorithm.RunAlgThread));
                algThread.SetApartmentState(ApartmentState.STA);
                algThread.Start();

                // Allow the algorithm to continue
                algorithm.WaitMutex.WaitOne();
                algorithm.CanContinue = true;
                algorithm.WaitMutex.ReleaseMutex();
            }
        }
예제 #4
0
        public LogicManager(MainWindow _window)
        {
            graphToVisualize = null;

            citiesLocations   = null;
            citiesConnections = null;

            defaultCitiesLocations   = new CitiesLocations();
            defaultCitiesConnections = new CitiesConnections();

            algCitiesLocations = new CitiesLocations();

            startCity = null;
            finalCity = null;

            algSpeed     = Alg_Speed.Fast;
            algHeuristic = Heuristic.Distance;

            resources = _window.Resources;
            window    = _window;

            graphManager = new GraphManager(window);

            algorithm    = null;
            algThread    = null;
            IsRunningAlg = false;
        }
        private void FinalCityItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem   menuItem = (MenuItem)sender;
            VertexCity vertex   = (VertexCity)menuItem.DataContext;

            logicManager.MarkFinalCity(vertex, ref graphLayout);
        }
        /// <summary>
        /// Apply the style to the vertex. Must be called from the UI
        /// thread because it does not use a deispatcher.
        /// </summary>
        /// <param name="vertex">
        /// Tartget vertex for the style
        /// </param>
        /// <param name="graphLayout">
        /// Graph layout
        /// </param>
        /// <param name="style">
        /// The style to apply
        /// </param>
        public void SetStyle(VertexCity vertex, GraphLayoutCity graphLayout,
                             Style style)
        {
            // Get vertex control and update style if possible
            VertexControl vertexControl = graphLayout.GetVertexControl(vertex);

            if (vertexControl != null)
            {
                vertexControl.Style = style;
            }
        }
예제 #7
0
        /// <summary>
        /// Update the vertex information in the locations file and also update the
        /// vertex internal information
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="graphLayout"></param>
        public void UpdateVertexInfo(VertexCity vertex, ref GraphLayoutCity graphLayout)
        {
            graphManager.UpdateVertexInfo(ref vertex, ref graphLayout);

            if (vertex != null)
            {
                Coordinates tempCityCoordinates;
                if (citiesLocations.locations.TryGetValue(vertex.City, out tempCityCoordinates))
                {
                    tempCityCoordinates.setX(vertex.CityCoordinates.getX());
                    tempCityCoordinates.setY(vertex.CityCoordinates.getY());
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Mark the city as Final City on the screen
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="graphLayout"></param>
        public void MarkFinalCity(VertexCity vertex, ref GraphLayoutCity graphLayout)
        {
            if (finalCity != null)
            {
                Style oldStyle = (Style)resources["DefaultCityStyle"];
                graphManager.SetStyle(finalCity, graphLayout, oldStyle);
            }

            if ((startCity != null) && (startCity.Equals(vertex)))
            {
                startCity = null;
            }

            finalCity = vertex;

            Style newStyle = (Style)resources["FinalCityStyle"];

            graphManager.SetStyle(vertex, graphLayout, newStyle);
        }
 /// <summary>
 /// Apply the style to the vertex. Must be called from the UI 
 /// thread because it does not use a deispatcher.
 /// </summary>
 /// <param name="vertex">
 /// Tartget vertex for the style
 /// </param>
 /// <param name="graphLayout">
 /// Graph layout
 /// </param>
 /// <param name="style">
 /// The style to apply
 /// </param>
 public void SetStyle(VertexCity vertex, GraphLayoutCity graphLayout,
     Style style)
 {
     // Get vertex control and update style if possible
     VertexControl vertexControl = graphLayout.GetVertexControl(vertex);
     if (vertexControl != null)
     {
         vertexControl.Style = style;
     }
 }
 /// <summary>
 /// Delete the city and all connected edges
 /// </summary>
 /// <param name="cityToDelete">
 /// City we want to delete
 /// </param>
 /// <param name="graphLayout">
 /// Graph layout
 /// </param>
 public void DeleteCity(VertexCity cityToDelete, ref GraphLayoutCity graphLayout)
 {
     graphLayout.Graph.RemoveVertex(cityToDelete);
 }
예제 #11
0
        /// <summary>
        /// Update the vertex information in the locations file and also update the 
        /// vertex internal information
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="graphLayout"></param>
        public void UpdateVertexInfo(VertexCity vertex, ref GraphLayoutCity graphLayout)
        {
            graphManager.UpdateVertexInfo(ref vertex, ref graphLayout);

            if (vertex != null)
            {
                Coordinates tempCityCoordinates;
                if (citiesLocations.locations.TryGetValue(vertex.City, out tempCityCoordinates))
                {
                    tempCityCoordinates.setX(vertex.CityCoordinates.getX());
                    tempCityCoordinates.setY(vertex.CityCoordinates.getY());
                }
            }
        }
예제 #12
0
        /// <summary>
        /// Mark start city on the screen
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="graphLayout"></param>
        public void MarkStartCity(VertexCity vertex, ref GraphLayoutCity graphLayout)
        {
            if (startCity != null)
            {
                Style oldStyle = (Style)resources["DefaultCityStyle"];
                graphManager.SetStyle(startCity, graphLayout, oldStyle);
            }

            if ((finalCity != null) && (finalCity.Equals(vertex)))
            {
                finalCity = null;
            }

            startCity = vertex;

            Style newStyle = (Style)resources["StartCityStyle"];
            graphManager.SetStyle(vertex, graphLayout, newStyle);
        }
예제 #13
0
 /// <summary>
 /// Delete city from the locations and the connections files. Also
 /// delete it from the screen.
 /// </summary>
 /// <param name="cityToDelete"></param>
 /// <param name="graphLayout"></param>
 public void DeleteCity(VertexCity cityToDelete, ref GraphLayoutCity graphLayout)
 {
     graphManager.DeleteCity(cityToDelete, ref graphLayout);
     DeleteCityFromData(cityToDelete.City);
 }
예제 #14
0
 public bool Equals(VertexCity toCompare)
 {
     return((toCompare != null) &&
            (City.Equals(toCompare.City)));
 }
예제 #15
0
 /// <summary>
 /// Delete city from the locations and the connections files. Also
 /// delete it from the screen.
 /// </summary>
 /// <param name="cityToDelete"></param>
 /// <param name="graphLayout"></param>
 public void DeleteCity(VertexCity cityToDelete, ref GraphLayoutCity graphLayout)
 {
     graphManager.DeleteCity(cityToDelete, ref graphLayout);
     DeleteCityFromData(cityToDelete.City);
 }
        /// <summary>
        /// Updates the vertex internal information.
        /// Maintains consistency between the vertex's position on the
        /// screen and ins internal information.
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="graphLayout"></param>
        public void UpdateVertexInfo(ref VertexCity vertex, ref GraphLayoutCity graphLayout)
        {
            if (vertex != null)
            {
                // Get the vertex control
                VertexControl vertexControl = graphLayout.GetVertexControl(vertex);

                // If we could find the verex control
                if (vertexControl != null)
                {
                    double tempXCoord, tempYCoord;

                    // Get the vertex screen coordinates
                    tempXCoord = GraphCanvas.GetX(vertexControl);
                    tempYCoord = GraphCanvas.GetY(vertexControl);

                    // Update the internal information if the coordinates
                    // are in range from 0 to 800
                    if ((tempXCoord > 0) && (tempXCoord < 800) &&
                        (tempYCoord > 0) && (tempYCoord < 800))
                    {
                        vertex.CityCoordinates.setX((int)tempXCoord);
                        vertex.CityCoordinates.setY((int)tempYCoord);
                    }

                    // Set the new screen coordinates
                    GraphCanvas.SetX(vertexControl, vertex.CityCoordinates.getX());
                    GraphCanvas.SetY(vertexControl, vertex.CityCoordinates.getY());
                }
            }
        }
 /// <summary>
 /// Delete the city and all connected edges
 /// </summary>
 /// <param name="cityToDelete">
 /// City we want to delete
 /// </param>
 /// <param name="graphLayout">
 /// Graph layout
 /// </param>
 public void DeleteCity(VertexCity cityToDelete, ref GraphLayoutCity graphLayout)
 {
     graphLayout.Graph.RemoveVertex(cityToDelete);
 }
예제 #18
0
        /// <summary>
        /// Start A-star algorithm in a separate thread
        /// </summary>
        /// <param name="graphLayout"></param>
        /// <param name="richTextBoxLog"></param>
        /// <param name="window"></param>
        public void StartAlgorithm(ref GraphLayoutCity graphLayout, ref TextBox richTextBoxLog,
            MainWindow window)
        {
            // If already running
            if (IsRunningAlg)
            {
                algorithm.WaitMutex.WaitOne();
                algorithm.CanContinue = true;
                algorithm.WaitMutex.ReleaseMutex();
            }
            // If not, then start a new thread
            else
            {

                algorithm = new Algorithm(citiesLocations, citiesConnections,
                    startCity.City, finalCity.City, algSpeed, algHeuristic,
                    ref graphLayout, ref richTextBoxLog, resources, graphManager);
                // Set window and create a mutex
                algorithm.Window = window;
                algorithm.WaitMutex = new Mutex();

                // Null the start and end cities
                startCity = null;
                finalCity = null;

                // Create and start the thread
                algThread = new Thread(new ThreadStart(algorithm.RunAlgThread));
                algThread.SetApartmentState(ApartmentState.STA);
                algThread.Start();

                // Allow the algorithm to continue
                algorithm.WaitMutex.WaitOne();
                algorithm.CanContinue = true;
                algorithm.WaitMutex.ReleaseMutex();
            }
        }