UNPKG

@geoapify/route-planner-sdk

Version:

TypeScript SDK for the Geoapify Route Planner API. Supports route optimization, delivery planning, and timeline visualization in browser and Node.js

65 lines (64 loc) 2.98 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { AgentPlanRecalculator, WaypointBuilder } from "../preserve-order"; import { RouteViolationValidator } from "../../validations"; /** * Strategy that removes jobs while preserving the order of remaining jobs. * Also removes waypoints and rebuilds legs. */ export class JobRemovePreserveOrderStrategy { execute(context, jobIndexes, options) { return __awaiter(this, void 0, void 0, function* () { const impactedAgentIndexes = new Set(); for (const jobIndex of jobIndexes) { const agentIndex = this.removeJobFromResult(context, jobIndex); if (agentIndex !== -1) { impactedAgentIndexes.add(agentIndex); } } for (const agentIndex of impactedAgentIndexes) { yield AgentPlanRecalculator.recalculate(context, agentIndex); RouteViolationValidator.validate(context, agentIndex); } return true; }); } removeJobFromResult(context, jobIndex) { const agentIndex = context.getAgentIndexForJob(jobIndex); if (agentIndex === undefined) { return -1; } const feature = context.getAgentFeature(agentIndex); const waypoints = feature.properties.waypoints; const legs = feature.properties.legs || []; const legDataMap = WaypointBuilder.buildLegDataMap(waypoints, legs); WaypointBuilder.removeJobsFromWaypoints(waypoints, jobIndex); const cleanupResult = WaypointBuilder.removeEmptyWaypoints(waypoints, legDataMap); feature.properties.waypoints = cleanupResult.waypoints; if (cleanupResult.legs) { feature.properties.legs = cleanupResult.legs; } this.addToUnassignedJobs(context, jobIndex); return agentIndex; } addToUnassignedJobs(context, jobIndex) { const rawData = context.getRawData(); if (!rawData.properties.issues) { rawData.properties.issues = {}; } const issues = rawData.properties.issues; if (!issues.unassigned_jobs) { issues.unassigned_jobs = []; } if (!issues.unassigned_jobs.includes(jobIndex)) { issues.unassigned_jobs.push(jobIndex); } } }