Overview
TheAlgorithmService is a foundational service in the Angular PWA Demo that provides access to various algorithm implementations. It extends BaseService and communicates with multiple backend APIs (.NET Core, C++, Spring Boot) to execute algorithms for sorting, Dijkstra’s shortest path, and regular expression evaluation.
Service configuration
The
AlgorithmService is registered with providedIn: 'root', making it a singleton service available throughout the application without needing to add it to any module’s providers array.Dependencies
The service requires the following dependencies:Angular’s HTTP client for making API requests
Configuration service that provides base URLs for different backend APIs
Common methods
_Algorithm_GetAppVersion
Retrieves the application version from the C++ backend API.Observable<string> containing the application version.
Usage example:
_Algorithm_GetCPPSTDVersion
Retrieves the C++ standard version used by the backend.Observable<string> containing the C++ standard version.
Shortest path algorithms
getRandomVertex
Generates a random vertex graph for Dijkstra’s algorithm using the .NET Core backend.The number of vertices in the graph
The starting point for the algorithm
Observable<string> containing the graph data.
getRandomVertexCpp
Generates a random vertex graph using the C++ backend implementation.getRandomVertex
getRandomVertexSpringBoot
Generates a random vertex graph using the Spring Boot (Java) backend implementation.getRandomVertex
Sorting algorithms
SortBenchMark_getNewSort_C_Sharp
Generates a new unsorted list for sorting demonstrations using the C# backend.Observable<string> containing the unsorted list.
Usage example:
SortBenchMark_getSort_C_Sharp
Executes a sorting algorithm on a given list using the C# backend.The sorting algorithm identifier (1 = Bubble Sort, 2 = Quick Sort, etc.)
The unsorted list as a delimited string
Observable<string> containing the sorting steps and final sorted list.
Real-world usage:
getSort_CPP
Executes a sorting algorithm using the C++ backend implementation.SortBenchMark_getSort_C_Sharp
Regular expression methods
_GetXmlData
Retrieves XML data containing regex patterns from the backend.Observable<string> containing XML data with regex patterns.
_SetXmlDataToCache
Caches XML regex data on the backend server.The base URL prefix for the API endpoint
_RegExEval
Evaluates a regular expression against input text using the C# backend.The index of the regex pattern to use
The text to evaluate against the regex pattern
Observable<string> containing the regex evaluation result.
_RegExEval_CPP
Evaluates a regular expression using the C++ backend implementation._RegExEval
Inheritance
TheAlgorithmService extends BaseService, which provides common HTTP options:
- HTTPOptions_Text
- HTTPOptions_Text_Plain
- HTTPOptions_JSON
Used for text responses with
Accept: application/text header.Best practices
Error handling
Always implement error handlers in your subscriptions to gracefully handle API failures.
Unsubscribe
Use operators like
takeUntilDestroyed() to automatically unsubscribe when components are destroyed.Backend selection
Allow users to choose between different backend implementations (C#, C++, Java) for performance comparisons.
Status updates
Update UI status messages during long-running algorithm operations.
Related services
- ConfigService - Provides configuration values and base URLs
- BackendService - Handles general backend operations and logging