Skip to main content

Overview

The AlgorithmService 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

import { AlgorithmService } from 'src/app/_services/AlgorithmService/algorithm.service';

@Component({
  selector: 'app-algorithm-sort',
  templateUrl: './algorithm-sort.component.html',
  standalone: false
})
export class AlgorithmSortComponent {
  constructor(public algorithmService: AlgorithmService) {}
}
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:
HttpClient
HttpClient
required
Angular’s HTTP client for making API requests
ConfigService
ConfigService
required
Configuration service that provides base URLs for different backend APIs

Common methods

_Algorithm_GetAppVersion

Retrieves the application version from the C++ backend API.
_Algorithm_GetAppVersion(): Observable<string>
Returns: An Observable<string> containing the application version. Usage example:
this.algorithmService._Algorithm_GetAppVersion()
  .subscribe({
    next: (version) => console.log('App version:', version),
    error: (err) => console.error('Error fetching version:', err)
  });

_Algorithm_GetCPPSTDVersion

Retrieves the C++ standard version used by the backend.
_Algorithm_GetCPPSTDVersion(): Observable<string>
Returns: An 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.
getRandomVertex(vertexSize: Number, sourcePoint: Number): Observable<string>
vertexSize
Number
required
The number of vertices in the graph
sourcePoint
Number
required
The starting point for the algorithm
Returns: An Observable<string> containing the graph data.

getRandomVertexCpp

Generates a random vertex graph using the C++ backend implementation.
getRandomVertexCpp(vertexSize: Number, sourcePoint: Number): Observable<string>
Parameters: Same as getRandomVertex

getRandomVertexSpringBoot

Generates a random vertex graph using the Spring Boot (Java) backend implementation.
getRandomVertexSpringBoot(vertexSize: Number, sourcePoint: Number): Observable<string>
Parameters: Same as getRandomVertex

Sorting algorithms

SortBenchMark_getNewSort_C_Sharp

Generates a new unsorted list for sorting demonstrations using the C# backend.
SortBenchMark_getNewSort_C_Sharp(): Observable<string>
Returns: An Observable<string> containing the unsorted list. Usage example:
public GetNewSort(): void {
  this.algorithmService.SortBenchMark_getNewSort_C_Sharp()
    .subscribe({
      next: (sortInfo: string) => {
        console.info('New unsorted list:', sortInfo);
        this.mensajes.nativeElement.innerHTML = sortInfo;
      },
      error: (err) => {
        console.error('Error getting new sort:', err.message);
      }
    });
}

SortBenchMark_getSort_C_Sharp

Executes a sorting algorithm on a given list using the C# backend.
SortBenchMark_getSort_C_Sharp(p_sortAlgoritm: number, p_unsortedList: string): Observable<string>
p_sortAlgoritm
number
required
The sorting algorithm identifier (1 = Bubble Sort, 2 = Quick Sort, etc.)
p_unsortedList
string
required
The unsorted list as a delimited string
Returns: An Observable<string> containing the sorting steps and final sorted list. Real-world usage:
public GetSort() {
  let p_sortAlgorith: number = this.SortAlgorithmList.nativeElement.value;
  let p_unsortedList: string = this.mensajes.nativeElement.innerHTML;
  
  let GetSortInfo: Observable<string>;
  
  // Select backend based on language preference
  switch(this._progLangId) {
    case 1: // C#
      GetSortInfo = this.algorithmService.SortBenchMark_getSort_C_Sharp(
        p_sortAlgorith, 
        p_unsortedList
      );
      break;
    case 2: // C++
      GetSortInfo = this.algorithmService.getSort_CPP(
        p_sortAlgorith, 
        p_unsortedList
      );
      break;
  }
  
  GetSortInfo.subscribe({
    next: (data: string) => {
      this.stringMatrix = data.split("■");
      this.DrawStepMain();
    },
    error: (err: Error) => {
      console.error('Error during sort:', err.message);
      this.status_message.set("An error occurred");
    }
  });
}

getSort_CPP

Executes a sorting algorithm using the C++ backend implementation.
getSort_CPP(p_sortAlgoritm: number, p_unsortedList: string): Observable<string>
Parameters: Same as SortBenchMark_getSort_C_Sharp

Regular expression methods

_GetXmlData

Retrieves XML data containing regex patterns from the backend.
_GetXmlData(): Observable<string>
Returns: An Observable<string> containing XML data with regex patterns.

_SetXmlDataToCache

Caches XML regex data on the backend server.
_SetXmlDataToCache(_prefix: string | undefined): void
_prefix
string | undefined
required
The base URL prefix for the API endpoint
Usage example:
private setupCache(): void {
  const baseUrl = this._configService.getConfigValue('baseUrlNetCore');
  
  if (baseUrl) {
    this.algorithmService._SetXmlDataToCache(baseUrl);
  }
}

_RegExEval

Evaluates a regular expression against input text using the C# backend.
_RegExEval(tagSearchIndex: number, textSearchValue: string): Observable<string>
tagSearchIndex
number
required
The index of the regex pattern to use
textSearchValue
string
required
The text to evaluate against the regex pattern
Returns: An Observable<string> containing the regex evaluation result.

_RegExEval_CPP

Evaluates a regular expression using the C++ backend implementation.
_RegExEval_CPP(tagSearchIndex: number, textSearchValue: string): Observable<string>
Parameters: Same as _RegExEval

Inheritance

The AlgorithmService extends BaseService, which provides common HTTP options:
Used for text responses with Accept: application/text header.
public HTTPOptions_Text = {
  headers: new HttpHeaders({
    'Accept':'application/text'
  }),
  'responseType': 'text' as 'json'
};

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.

Source location

~/workspace/source/src/app/_services/AlgorithmService/algorithm.service.ts

Build docs developers (and LLMs) love