Skip to content

Commit

Permalink
feat/ Lane route
Browse files Browse the repository at this point in the history
  • Loading branch information
brunolnetto committed Jan 25, 2022
1 parent bc438b1 commit 967ae12
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 23 deletions.
5 changes: 4 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { createRequire } from 'module';
import fs from 'fs';

import {
startAndFinishNodes,
nodeToLane,
parseBlueprintToGraph,
fromStartToFinishAcyclicPaths,
fromStartToFinishCombsAcyclicPaths,
} from './utils/workflow/parsers';
} from './utils/workflow/parsers.js';

const require = createRequire(import.meta.url);
const app = express();
Expand Down Expand Up @@ -48,6 +50,7 @@ app.get('/', (req, res) => {
}

res.send(descriptions);

} else {
const fname = bps_root + blueprint_fname;
const blueprint_i = require(fname);
Expand Down
100 changes: 78 additions & 22 deletions utils/workflow/parsers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Iter from 'es-iter';
import Graph from '../../data-structures/graph/Graph';
import GraphVertex from '../../data-structures/graph/GraphVertex';
import GraphEdge from '../../data-structures/graph/GraphEdge';
import { getUniques, getAllIndexes } from '../arrays/arrays';

import Graph from '../../data-structures/graph/Graph.js';
import GraphVertex from '../../data-structures/graph/GraphVertex.js';
import GraphEdge from '../../data-structures/graph/GraphEdge.js';
import { getUniques, getAllIndexes } from '../arrays/arrays.js';

export const parseBlueprintToGraph = (blueprint) => {
const { nodes } = blueprint.blueprint_spec;
Expand Down Expand Up @@ -46,8 +47,62 @@ export const parseBlueprintToGraph = (blueprint) => {
return graph;
};

export const startAndFinishNodes = (blueprint) => {
const { nodes } = blueprint.blueprint_spec;
const startNodes = [];
const finishNodes = [];

for (const node of nodes) {
if (node.type.toLowerCase() === 'start') {
startNodes.push(node.id);
}

if (node.type.toLowerCase() === 'finish') {
finishNodes.push(node.id);
}
}

return {'start_nodes': [...startNodes], 'finish_nodes': [...finishNodes]}
}

export const nodeToLane = (blueprint) => {
const { nodes } = blueprint.blueprint_spec;
const node_to_lane = {};

for (const node of nodes) {
node_to_lane[node.id] = node.lane_id
}

return node_to_lane
}

export const nodeRouteToLaneRoute = (node_route,
indexes_to_vertices,
node_id_to_lane) => {
let lane_route = [];

for(let vertex_j of node_route){
let lane_vertex_j = node_id_to_lane[indexes_to_vertices[vertex_j]];

if(lane_route.length==0){
lane_route.push(lane_vertex_j);

} else {
if(lane_route[lane_route.length-1] !== lane_vertex_j) {
lane_route.push(lane_vertex_j);
} else {
continue;
}
}
}

return lane_route;
}

export const fromStartToFinishAcyclicPaths = (blueprint, start_key, finish_key) => {
const bp_graph = parseBlueprintToGraph(blueprint);
let indexes_to_vertices = bp_graph.getIndicesToVertices();
let node_id_to_lane = nodeToLane(blueprint);

const looseNodes = bp_graph.looseNodes();
const orphanNodes = bp_graph.orphanNodes();
Expand All @@ -69,34 +124,35 @@ export const fromStartToFinishAcyclicPaths = (blueprint, start_key, finish_key)
}

const routes = bp_graph.acyclicPaths(start_key, finish_key);
const route_describe = { length: routes.length, routes };
const route_describe = { 'length': routes.length,
'routes': []};
let lane_route_i = []

for(const i of Iter.range(routes.length)) {
lane_route_i = nodeRouteToLaneRoute(routes[i], indexes_to_vertices, node_id_to_lane);

route_describe.routes.push(
{
'nodes_path': routes[i],
'lanes_path': lane_route_i,
}
);
}

return route_describe;
};

export const fromStartToFinishCombsAcyclicPaths = (blueprint) => {
const { nodes } = blueprint.blueprint_spec;
const finishNodes = [];
const startNodes = [];

for (const node of nodes) {
if (node.type.toLowerCase() === 'start') {
startNodes.push(node.id);
}

if (node.type.toLowerCase() === 'finish') {
finishNodes.push(node.id);
}
}
const sf_nodes = startAndFinishNodes(blueprint);

const acyclic_paths = {};
let startNode;
let finishNode;

for (const i of Iter.range(startNodes.length)) {
startNode = startNode[i];
for (const j of Iter.range(finishNodes.length)) {
finishNode = finishNodes[j];
for (const i of Iter.range(sf_nodes.start_nodes.length)) {
startNode = sf_nodes.start_nodes[i];
for (const j of Iter.range(sf_nodes.finish_nodes.length)) {
finishNode = sf_nodes.finish_nodes[j];

const label = `${startNode}_${finishNode}`;
acyclic_paths[label] = fromStartToFinishAcyclicPaths(blueprint, startNode, finishNode);
Expand Down

0 comments on commit 967ae12

Please sign in to comment.