@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
92 lines (91 loc) • 4.37 kB
JavaScript
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 { REOPTIMIZE, ItemAlreadyAssigned, JobNotFound, InvalidParameter } from "../../models";
import { JobStrategyFactory } from "./strategies";
import { RouteResultEditorBase } from "./route-result-editor-base";
/**
* Editor for managing jobs in a route planner result
*/
export class RouteResultJobEditor extends RouteResultEditorBase {
assignJobs(agentIndex, jobIndexes, options = {}) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
this.validateAgent(agentIndex);
this.validateJobs(jobIndexes, agentIndex);
const strategy = JobStrategyFactory.createAssignStrategy((_a = options.strategy) !== null && _a !== void 0 ? _a : REOPTIMIZE);
const result = yield strategy.execute(this, agentIndex, jobIndexes, options);
this.updateIssues();
return result;
});
}
removeJobs(jobIndexes, options = {}) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
this.validateJobs(jobIndexes);
const strategy = JobStrategyFactory.createRemoveStrategy((_a = options.strategy) !== null && _a !== void 0 ? _a : REOPTIMIZE);
const result = yield strategy.execute(this, jobIndexes, options);
this.updateIssues();
return result;
});
}
addNewJobs(agentIndex, jobs, options = {}) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const jobsRaw = jobs.map(job => job.getRaw());
this.validateAgent(agentIndex);
this.ensureNewItemsValid(jobsRaw, "jobs");
this.validateNewJobsHaveLocations(jobsRaw);
const newJobIndexes = this.appendJobsToInput(jobsRaw);
const strategy = JobStrategyFactory.createAssignStrategy((_a = options.strategy) !== null && _a !== void 0 ? _a : REOPTIMIZE);
const result = yield strategy.execute(this, agentIndex, newJobIndexes, options);
this.updateIssues();
return result;
});
}
validateJobs(jobIndexes, agentIndex) {
this.ensureItemsProvided(jobIndexes, "jobs");
this.ensureItemsUnique(jobIndexes, "jobs");
for (const jobIndex of jobIndexes) {
this.validateJobAssignment(jobIndex, agentIndex);
}
}
validateJobAssignment(jobIndex, agentIndex) {
const jobAgentIndex = this.getAgentIndexForJob(jobIndex);
if (jobAgentIndex != undefined) {
this.validateJobExists(jobIndex);
}
if (agentIndex !== undefined && jobAgentIndex === agentIndex) {
throw new ItemAlreadyAssigned(`Job with index ${jobIndex} already assigned to agent with index ${agentIndex}`, 'job', jobIndex, agentIndex);
}
}
validateJobExists(jobIndex) {
const jobFound = this.rawData.properties.params.jobs[jobIndex];
if (!jobFound) {
throw new JobNotFound(`Job with index ${jobIndex} not found`, jobIndex);
}
}
appendJobsToInput(jobsRaw) {
const params = this.rawData.properties.params;
if (!params.jobs) {
params.jobs = [];
}
const startIndex = params.jobs.length;
params.jobs.push(...jobsRaw);
return jobsRaw.map((_, i) => startIndex + i);
}
validateNewJobsHaveLocations(jobsRaw) {
for (let i = 0; i < jobsRaw.length; i++) {
const job = jobsRaw[i];
if (job.location === undefined && job.location_index === undefined) {
throw new InvalidParameter(`New job at position ${i} must have either location or location_index`, "jobs");
}
}
}
}