Portal/rsconcept/frontend/src/models/OssLoader.ts

68 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
/**
* Module: OSS data loading and processing.
*/
2024-07-20 18:26:32 +03:00
import { Graph } from './Graph';
2024-07-26 00:33:22 +03:00
import { LibraryItemID } from './library';
import {
IOperation,
IOperationSchema,
IOperationSchemaData,
IOperationSchemaStats,
OperationID,
OperationType
} from './oss';
2024-06-07 20:17:03 +03:00
/**
* Loads data into an {@link IOperationSchema} based on {@link IOperationSchemaData}.
*
*/
export class OssLoader {
2024-07-20 18:26:32 +03:00
private oss: IOperationSchemaData;
private graph: Graph = new Graph();
private operationByID: Map<OperationID, IOperation> = new Map();
2024-07-26 00:33:22 +03:00
private schemas: LibraryItemID[] = [];
2024-06-07 20:17:03 +03:00
constructor(input: IOperationSchemaData) {
2024-07-20 18:26:32 +03:00
this.oss = input;
2024-06-07 20:17:03 +03:00
}
produceOSS(): IOperationSchema {
2024-07-20 18:26:32 +03:00
const result = this.oss as IOperationSchema;
this.prepareLookups();
this.createGraph();
2024-07-26 00:33:22 +03:00
this.extractSchemas();
2024-07-20 18:26:32 +03:00
result.operationByID = this.operationByID;
result.graph = this.graph;
2024-07-26 00:33:22 +03:00
result.schemas = this.schemas;
result.stats = this.calculateStats();
2024-06-07 20:17:03 +03:00
return result;
}
2024-07-20 18:26:32 +03:00
private prepareLookups() {
this.oss.items.forEach(operation => {
this.operationByID.set(operation.id, operation);
this.graph.addNode(operation.id);
});
}
private createGraph() {
this.oss.arguments.forEach(argument => this.graph.addEdge(argument.argument, argument.operation));
}
2024-07-26 00:33:22 +03:00
private extractSchemas() {
this.schemas = this.oss.items.map(operation => operation.result as LibraryItemID).filter(item => item !== null);
}
private calculateStats(): IOperationSchemaStats {
const items = this.oss.items;
return {
count_operations: items.length,
count_inputs: items.filter(item => item.operation_type === OperationType.INPUT).length,
count_synthesis: items.filter(item => item.operation_type === OperationType.SYNTHESIS).length,
count_schemas: this.schemas.length
};
}
2024-06-07 20:17:03 +03:00
}