ConceptPortal-public/rsconcept/frontend/src/models/OssLoader.ts

97 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-06-04 23:00:22 +03:00
/**
* Module: OSS data loading and processing.
*/
2024-07-21 15:19:57 +03:00
import { Graph } from './Graph';
import { ILibraryItem, LibraryItemID } from './library';
2024-07-26 00:34:08 +03:00
import {
IOperation,
IOperationSchema,
IOperationSchemaData,
IOperationSchemaStats,
OperationID,
OperationType
} from './oss';
2024-06-04 23:00:22 +03:00
/**
* Loads data into an {@link IOperationSchema} based on {@link IOperationSchemaData}.
*
*/
export class OssLoader {
2024-07-21 15:19:57 +03:00
private oss: IOperationSchemaData;
private graph: Graph = new Graph();
2024-08-06 14:39:00 +03:00
private operationByID = new Map<OperationID, IOperation>();
private schemaIDs: LibraryItemID[] = [];
private items: ILibraryItem[];
2024-06-04 23:00:22 +03:00
constructor(input: IOperationSchemaData, items: ILibraryItem[]) {
2024-07-21 15:19:57 +03:00
this.oss = input;
this.items = items;
2024-06-04 23:00:22 +03:00
}
produceOSS(): IOperationSchema {
2024-07-21 15:19:57 +03:00
const result = this.oss as IOperationSchema;
this.prepareLookups();
this.createGraph();
2024-07-26 00:34:08 +03:00
this.extractSchemas();
this.inferOperationAttributes();
2024-07-21 15:19:57 +03:00
result.operationByID = this.operationByID;
result.graph = this.graph;
result.schemas = this.schemaIDs;
2024-07-26 00:34:08 +03:00
result.stats = this.calculateStats();
2024-06-04 23:00:22 +03:00
return result;
}
2024-07-21 15:19:57 +03:00
private prepareLookups() {
this.oss.items.forEach(operation => {
this.operationByID.set(operation.id, operation as IOperation);
2024-07-21 15:19:57 +03:00
this.graph.addNode(operation.id);
});
}
private createGraph() {
this.oss.arguments.forEach(argument => this.graph.addEdge(argument.argument, argument.operation));
}
2024-07-26 00:34:08 +03:00
private extractSchemas() {
this.schemaIDs = this.oss.items.map(operation => operation.result).filter(item => item !== null);
2024-07-26 00:34:08 +03:00
}
private inferOperationAttributes() {
this.graph.topologicalOrder().forEach(operationID => {
const operation = this.operationByID.get(operationID)!;
const schema = this.items.find(item => item.id === operation.result);
2024-08-17 12:17:13 +03:00
operation.is_consolidation = this.inferConsolidation(operationID);
operation.is_owned = !schema || (schema.owner === this.oss.owner && schema.location === this.oss.location);
operation.substitutions = this.oss.substitutions.filter(item => item.operation === operationID);
operation.arguments = this.oss.arguments
.filter(item => item.operation === operationID)
.map(item => item.argument);
});
}
2024-08-17 12:17:13 +03:00
private inferConsolidation(operationID: OperationID): boolean {
const inputs = this.graph.expandInputs([operationID]);
if (inputs.length === 0) {
return false;
}
const ancestors = [...inputs];
inputs.forEach(input => {
ancestors.push(...this.graph.expandAllInputs([input]));
});
const unique = new Set(ancestors);
return unique.size < ancestors.length;
}
2024-07-26 00:34:08 +03:00
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.schemaIDs.length
2024-07-26 00:34:08 +03:00
};
}
2024-06-04 23:00:22 +03:00
}