@geoapify/route-planner-sdk
Version:
A TypeScript SDK for the Geoapify Route Planner API that simplifies route optimization requests, and helps visualize and edit resulting routes.
162 lines (161 loc) • 6.93 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 { RouteResultEditorBase } from "./route-result-editor-base";
export class RouteResultJobEditor extends RouteResultEditorBase {
assignJobs(agentId, jobIds) {
return __awaiter(this, void 0, void 0, function* () {
this.validateAgent(agentId);
this.validateJobs(jobIds, agentId);
for (const jobId of jobIds) {
yield this.assignJob(jobId, agentId);
}
return true;
});
}
removeJobs(jobIds) {
return __awaiter(this, void 0, void 0, function* () {
this.validateJobs(jobIds);
for (const jobId of jobIds) {
yield this.removeJob(jobId);
}
return true;
});
}
addNewJobs(agentId, jobs) {
return __awaiter(this, void 0, void 0, function* () {
let jobsRaw = jobs.map(job => job.getRaw());
this.validateAgent(agentId);
this.validateNewJobs(jobsRaw);
yield this.addNewJobsToAgent(agentId, jobsRaw);
return true;
});
}
assignJob(jobId, agentId) {
return __awaiter(this, void 0, void 0, function* () {
let jobInfo = this.result.getJobInfo(jobId);
let newAgentSolution = this.result.getAgentSolution(agentId);
if (newAgentSolution && jobInfo) {
yield this.addJobToExistingAgent(agentId, jobId);
yield this.removeJobFromExistingAgent(jobInfo);
}
if (newAgentSolution && !jobInfo) {
yield this.addJobToExistingAgent(agentId, jobId);
}
if (!newAgentSolution && jobInfo) {
yield this.addJobToNonExistingAgent(agentId, jobId);
yield this.removeJobFromExistingAgent(jobInfo);
}
if (!newAgentSolution && !jobInfo) {
yield this.addJobToNonExistingAgent(agentId, jobId);
}
});
}
removeJob(jobId) {
return __awaiter(this, void 0, void 0, function* () {
let jobInfo = this.result.getJobInfo(jobId);
if (jobInfo) {
yield this.removeJobFromExistingAgent(jobInfo);
}
else {
let jobInitialIndex = this.getInitialJobIndex(jobId);
this.result.getRaw().unassignedJobs =
this.result.getRaw().unassignedJobs.filter((jobIndex) => jobIndex !== jobInitialIndex);
}
});
}
addNewJobsToAgent(agentId, jobs) {
return __awaiter(this, void 0, void 0, function* () {
let existingAgentSolution = this.result.getAgentSolution(agentId);
this.result.getRaw().inputData.jobs.push(...jobs);
let newAgentInput = this.addJobsToAgent(agentId, jobs.map((job) => job.id), existingAgentSolution);
let optimizedRouterPlan = yield this.optimizeRoute(newAgentInput);
this.updateAgent(optimizedRouterPlan);
});
}
addJobToNonExistingAgent(agentId, jobId) {
return __awaiter(this, void 0, void 0, function* () {
let newAgentInput = this.addJobsToAgent(agentId, [jobId]);
let optimizedRouterPlan = yield this.optimizeRoute(newAgentInput);
this.updateAgent(optimizedRouterPlan);
});
}
addJobToExistingAgent(agentId, jobId) {
return __awaiter(this, void 0, void 0, function* () {
let existingAgentSolution = this.result.getAgentSolution(agentId);
let newAgentInput = this.addJobsToAgent(agentId, [jobId], existingAgentSolution);
let optimizedRouterPlan = yield this.optimizeRoute(newAgentInput);
this.updateAgent(optimizedRouterPlan);
});
}
removeJobFromExistingAgent(jobInfo) {
return __awaiter(this, void 0, void 0, function* () {
let existingAgentSolution = jobInfo.getAgent();
let newAgentInput = this.removeJobFromAgent(existingAgentSolution, jobInfo.getAction().getJobId());
let optimizedRouterPlan = yield this.optimizeRoute(newAgentInput);
this.updateAgent(optimizedRouterPlan);
});
}
addJobsToAgent(agentId, jobIds, existingAgent) {
let optimizedAgentInput = this.generateOptimizeAgentInput(agentId, existingAgent);
jobIds.forEach(jobId => {
optimizedAgentInput.agentJobIds.add(jobId);
});
return optimizedAgentInput;
}
removeJobFromAgent(existingAgent, jobId) {
let optimizedAgentInput = this.generateOptimizeAgentInput(existingAgent.getAgentId(), existingAgent);
optimizedAgentInput.agentJobIds.delete(jobId);
return optimizedAgentInput;
}
validateJobs(jobIds, agentId) {
if (jobIds.length == 0) {
throw new Error("No jobs provided");
}
if (!this.checkIfArrayIsUnique(jobIds)) {
throw new Error("Jobs are not unique");
}
jobIds.forEach((jobId) => {
let jobInfo = this.result.getJobInfo(jobId);
if (jobInfo == undefined) {
this.validateJobExists(jobId);
}
if (agentId) {
if ((jobInfo === null || jobInfo === void 0 ? void 0 : jobInfo.getAgentId()) == agentId) {
throw new Error(`Job with id ${jobId} already assigned to agent ${agentId}`);
}
}
});
}
validateJobExists(jobId) {
let jobIndex = this.getInitialJobIndex(jobId);
if (jobIndex == -1) {
throw new Error(`Job with id ${jobId} not found`);
}
else {
let isUnassignedJob = this.result.getUnassignedJobs().includes(jobIndex);
if (!isUnassignedJob) {
throw new Error(`Job with id ${jobId} not found`);
}
}
}
validateNewJobs(jobs) {
if (jobs.length == 0) {
throw new Error("No jobs provided");
}
if (!this.checkIfArrayIsUnique(jobs)) {
throw new Error("Jobs are not unique");
}
jobs.forEach((job) => {
if (job.id == undefined) {
throw new Error("Job id is undefined");
}
});
}
}