UNPKG

hravemzdy.procezor

Version:

payroll-procezor Salary, Health, Social, Taxing Properties for years 2010-2022

249 lines 10.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DependencyGraph = void 0; var ArticleDafine_1 = require("../service_types/ArticleDafine"); var ArticleTerm_1 = require("../service_types/ArticleTerm"); var tslog_1 = require("tslog"); var ArticleEdge = /** @class */ (function () { function ArticleEdge(_start, _stops) { this.start = _start; this.stops = _stops; } return ArticleEdge; }()); var QueueTerm = /** @class */ (function () { function QueueTerm(items) { this.elements = new Array(); this.elements = Array.from(items); } QueueTerm.prototype.isEmpty = function () { return this.elements.length === 0; }; QueueTerm.prototype.count = function () { return this.elements.length; }; QueueTerm.prototype.enqueue = function (item) { this.elements.push(item); }; QueueTerm.prototype.dequeue = function () { if (!this.isEmpty()) { return this.elements.shift(); } return null; }; QueueTerm.prototype.peek = function () { if (!this.isEmpty()) { return this.elements[0]; } return null; }; return QueueTerm; }()); var DependencyGraph = /** @class */ (function () { function DependencyGraph() { } DependencyGraph.articleDefsDistinctSort = function (array, termOrder) { var codeOrder = termOrder.map(function (x) { return x.code; }); var finderFun = function (defs) { return function (x) { return x.value === defs.code.value; }; }; var compareTo = function (xIndex, yIndex) { if (xIndex === -1 && yIndex === -1) { return 0; } if (xIndex === -1 && yIndex !== -1) { return -1; } if (xIndex !== -1 && yIndex === -1) { return 1; } if (xIndex > yIndex) { return 1; } if (xIndex < yIndex) { return -1; } return 0; }; var DefineCompare = function (x, y) { var xIndex = codeOrder.findIndex(finderFun(x)); var yIndex = codeOrder.findIndex(finderFun(y)); return compareTo(xIndex, yIndex); }; var distinct = new Array(); array.forEach(function (x) { var found = distinct.find(function (ax) { return ax.code.value === x.code.value; }) !== undefined; if (!found) { distinct.push(x); } }); return distinct.sort(DefineCompare); }; DependencyGraph.prototype.InitGraphModel = function (articlesModel, conceptsModel) { var vertModel = this.CreateVertModel(articlesModel); var edgeModel = this.CreateEdgeModel(articlesModel, conceptsModel); var pendModel = this.CreatePendModel(articlesModel, conceptsModel); var order = this.CreateTopoModel(vertModel, edgeModel); var paths = this.CreatePathModel(articlesModel, vertModel, pendModel, order); return [order, paths]; }; DependencyGraph.prototype.CreateVertModel = function (articlesModel) { return Array.from(Array.from(articlesModel).map(function (a) { return a.term(); }).sort(ArticleTerm_1.ArticleTerm.compareTo)); }; DependencyGraph.prototype.CreateEdgeModel = function (articlesModel, conceptsModel) { var _this = this; var reduceFunc = function (agr, x, idx, array) { return _this.MergeEdges(articlesModel, conceptsModel, agr, x); }; var init = new Set(); return Array.from(Array.from(articlesModel).reduce(reduceFunc, init)).sort(function (x, y) { if (ArticleTerm_1.ArticleTerm.compareTo(x.start, y.start) === 0) { return ArticleTerm_1.ArticleTerm.compareTo(x.stops, y.stops); } return ArticleTerm_1.ArticleTerm.compareTo(x.start, y.start); }); }; DependencyGraph.prototype.CreatePendModel = function (articlesModel, conceptsModel) { var _this = this; var reduceFunc = function (agr, x, idx, array) { return _this.MergePends(articlesModel, conceptsModel, agr, x); }; var init = new Set(); return Array.from(Array.from(articlesModel).reduce(reduceFunc, init)).sort(function (x, y) { if (ArticleTerm_1.ArticleTerm.compareTo(x.start, y.start) === 0) { return ArticleTerm_1.ArticleTerm.compareTo(x.stops, y.stops); } return ArticleTerm_1.ArticleTerm.compareTo(x.start, y.start); }); }; DependencyGraph.prototype.CreatePathModel = function (articlesModel, vertModel, edgeModel, vertOrder) { var _this = this; return new Map(Array.from(vertModel).map(function (x) { return [x, _this.MergePaths(articlesModel, edgeModel, vertOrder, x)]; })); }; DependencyGraph.prototype.CreateTopoModel = function (vertModel, edgeModel) { var log = new tslog_1.Logger({ name: "ProcezorLogger" }); var degrees = new Map(Array.from(vertModel). map(function (x) { return [x.code.value, [x.seqs.value, Array.from(edgeModel).reduce(function (agr, e) { return (e.stops.code.value === x.code.value ? agr + 1 : agr); }, 0)]]; })); var articlesOrder = new Array(); var queues = new QueueTerm(Array.from(degrees.entries()) .filter(function (x) { return (x[1][1] === 0); }) .map(function (x) { return ArticleTerm_1.ArticleTerm.get(x[0], x[1][0]); }) .sort(ArticleTerm_1.ArticleTerm.compareTo)); var index = 0; var _loop_1 = function () { index++; var article = queues.dequeue(); articlesOrder.push(article); var paths = Array.from(Array.from(edgeModel).filter(function (x) { return (x.start.code.value === article.code.value); }).map(function (x) { return x.stops; })); paths.forEach(function (p) { var v = degrees.get(p.code.value); degrees.set(p.code.value, [v[0], Math.max(0, v[1] - 1)]); if (degrees.get(p.code.value)[1] === 0) { queues.enqueue(p); } }); }; while (queues.count() !== 0) { _loop_1(); } var modelLength = Array.from(vertModel).length; if (index !== modelLength) { log.error("CreateTopoModel, build graph failed: ".concat(index, "<>").concat(modelLength)); return new Array(); } return articlesOrder; }; DependencyGraph.prototype.MergeEdges = function (articlesModel, conceptsModel, edgeModel, article) { var result = new Set(Array.from(edgeModel)); var concept = DependencyGraph.firstOrDefaultConcept(Array.from(conceptsModel), article.role); if (article !== undefined && concept !== undefined) { Array.from(article.sums).forEach(function (s) { result.add(new ArticleEdge(article.term(), DependencyGraph.GetArticleTerm(s, articlesModel))); }); Array.from(concept.path).forEach(function (p) { result.add(new ArticleEdge(DependencyGraph.GetArticleTerm(p, articlesModel), article.term())); }); } return result; }; DependencyGraph.prototype.MergePends = function (articlesModel, conceptsModel, edgeModel, article) { var result = new Set(Array.from(edgeModel)); var concept = DependencyGraph.firstOrDefaultConcept(Array.from(conceptsModel), article.role); if (article !== undefined && concept !== undefined) { Array.from(concept.path).forEach(function (p) { result.add(new ArticleEdge(DependencyGraph.GetArticleTerm(p, articlesModel), article.term())); }); } return result; }; DependencyGraph.prototype.MergePaths = function (articlesModel, edgeModel, vertOrder, article) { var _this = this; var articleInit = Array.from(edgeModel). filter(function (e) { return (e.stops.code.value === article.code.value); }). map(function (e) { return DependencyGraph.GetArticleDefs(e.start.code, articlesModel); }); var articlePath = articleInit.reduce(function (agr, x) { return _this.MergeVert(articlesModel, edgeModel, agr, x); }, articleInit); return DependencyGraph.articleDefsDistinctSort(Array.from(articlePath), Array.from(vertOrder)); }; DependencyGraph.prototype.MergeVert = function (articlesModel, edgeModel, defsModel, defs) { var _this = this; var resultInit = Array.from(edgeModel). filter(function (e) { return (e.stops.code.value === defs.code.value); }). map(function (e) { return DependencyGraph.GetArticleDefs(e.start.code, articlesModel); }); var resultList = resultInit.reduce(function (agr, item) { return _this.MergeVert(articlesModel, edgeModel, agr, item); }, resultInit); return Array.from(Array.from(defsModel).concat(Array.from(resultList))).concat(defs); }; DependencyGraph.GetArticleTerm = function (article, articlesModel) { var articleSpec = DependencyGraph.firstOrDefaultArticle(Array.from(articlesModel), article); if (articleSpec === undefined) { return ArticleTerm_1.ArticleTerm.new(); } return articleSpec.term(); }; DependencyGraph.GetArticleDefs = function (article, articlesModel) { var articleSpec = DependencyGraph.firstOrDefaultArticle(Array.from(articlesModel), article); if (articleSpec === undefined) { return ArticleDafine_1.ArticleDefine.new(); } return articleSpec.defs(); }; DependencyGraph.firstOrDefaultCode = function (array, item) { return array.find(function (x) { return (x.value === item.value); }); }; DependencyGraph.ArticleCodeDistinct = function (array) { var distinct = new Array(); array.forEach(function (x) { var found = DependencyGraph.firstOrDefaultCode(distinct, x) !== undefined; if (!found) { distinct.push(x); } }); return distinct; }; DependencyGraph.firstOrDefaultArticle = function (array, item) { return array.find(function (x) { return (x.code.value === item.value); }); }; DependencyGraph.firstOrDefaultConcept = function (array, item) { return array.find(function (x) { return (x.code.value === item.value); }); }; return DependencyGraph; }()); exports.DependencyGraph = DependencyGraph; //# sourceMappingURL=DependencyGraph.js.map