From c7051b3106c35d181d8f3f590ab0cf8491e83ef8 Mon Sep 17 00:00:00 2001 From: Neloreck Date: Thu, 2 Nov 2023 01:49:46 +0200 Subject: [PATCH] Remove not needed util. Action methods chaining. --- src/AbstractAction.test.ts | 20 ++++-------- src/AbstractAction.ts | 20 +++++++----- src/planner/AbstractPlanner.ts | 15 +++------ src/utils/graph/DirectedGraph.test.ts | 9 ++++++ src/utils/graph/DirectedGraph.ts | 12 +++++++ src/utils/graph/DirectedWeightedGraph.ts | 2 +- src/utils/planner.ts | 41 ++++-------------------- 7 files changed, 52 insertions(+), 67 deletions(-) diff --git a/src/AbstractAction.test.ts b/src/AbstractAction.test.ts index 2a787b3..f8809ef 100644 --- a/src/AbstractAction.test.ts +++ b/src/AbstractAction.test.ts @@ -65,13 +65,11 @@ describe("AbstractAction class", () => { expect(action.getPreconditions()).toEqual([]); - action.addPrecondition(first); - action.addPrecondition(second); + action.addPrecondition(first).addPrecondition(second); expect(action.getPreconditions()).toEqual([first, second]); - action.addPrecondition(first); - action.addPrecondition(second); + action.addPrecondition(first).addPrecondition(second); expect(action.getPreconditions()).toEqual([first, second]); }); @@ -84,8 +82,7 @@ describe("AbstractAction class", () => { expect(action.getPreconditions()).toEqual([]); - action.addPrecondition(first); - action.addPrecondition(second); + action.addPrecondition(first).addPrecondition(second); expect(action.getPreconditions()).toEqual([first, second]); @@ -114,13 +111,11 @@ describe("AbstractAction class", () => { expect(action.getEffects()).toEqual([]); - action.addEffect(first); - action.addEffect(second); + action.addEffect(first).addEffect(second); expect(action.getEffects()).toEqual([first, second]); - action.addEffect(first); - action.addEffect(second); + action.addEffect(first).addEffect(second); expect(action.getEffects()).toEqual([first, second]); }); @@ -133,8 +128,7 @@ describe("AbstractAction class", () => { expect(action.getEffects()).toEqual([]); - action.addEffect(first); - action.addEffect(second); + action.addEffect(first).addEffect(second); expect(action.getEffects()).toEqual([first, second]); @@ -142,7 +136,7 @@ describe("AbstractAction class", () => { expect(action.getEffects()).toEqual([first]); - action.removeEffect(second.id); + action.removeEffect(second.id).removeEffect(second.id); expect(action.getEffects()).toEqual([first]); diff --git a/src/AbstractAction.ts b/src/AbstractAction.ts index 0df809d..6f4438d 100644 --- a/src/AbstractAction.ts +++ b/src/AbstractAction.ts @@ -28,10 +28,12 @@ export abstract class AbstractAction { /** * @param property - new precondition property to add */ - public addPrecondition(property: Property): void { + public addPrecondition(property: Property): typeof this { if (this.preconditions.findIndex((it) => it.id === property.id) === -1) { this.preconditions.push(property); } + + return this; } /** @@ -40,16 +42,16 @@ export abstract class AbstractAction { * @param id - the id of precondition which is going to be removed * @returns if the precondition was removed */ - public removePrecondition(id: PropertyId): boolean { + public removePrecondition(id: PropertyId): typeof this { for (let it = 0; it < this.preconditions.length; it++) { if (this.preconditions[it].id === id) { this.preconditions.splice(it, 1); - return true; + return this; } } - return false; + return this; } /** @@ -62,26 +64,28 @@ export abstract class AbstractAction { /** * @param property - world precondition to remove from the action */ - public addEffect(property: Property): void { + public addEffect(property: Property): typeof this { if (this.effects.findIndex((it) => it.id === property.id) === -1) { this.effects.push(property); } + + return this; } /** * @param id - the id of effect which is going to be removed * @returns if the effect was removed */ - public removeEffect(id: PropertyId): boolean { + public removeEffect(id: PropertyId): typeof this { for (let it = 0; it < this.effects.length; it++) { if (this.effects[it].id === id) { this.effects.splice(it, 1); - return true; + return this; } } - return false; + return this; } /** * Checks if the current action of the action queue is finished. Gets called until it returns true. diff --git a/src/planner/AbstractPlanner.ts b/src/planner/AbstractPlanner.ts index a0ac123..32a1ac7 100644 --- a/src/planner/AbstractPlanner.ts +++ b/src/planner/AbstractPlanner.ts @@ -5,12 +5,7 @@ import { Optional, Queue } from "@/types"; import { IUnit } from "@/unit/IUnit"; import { IWeightedGraph, WeightedEdge, WeightedPath } from "@/utils/graph"; import { createWeightedPath } from "@/utils/path"; -import { - addEgdeWithWeigth, - addNodeToGraphPath, - areAllPreconditionsMet, - extractActionsFromGraphPath, -} from "@/utils/planner"; +import { addNodeToGraphPath, areAllPreconditionsMet, extractActionsFromGraphPath } from "@/utils/planner"; /** * Class for generating a queue of goap actions. @@ -194,7 +189,7 @@ export abstract class AbstractPlanner { (graphNode.preconditions.length === 0 || areAllPreconditionsMet(graphNode.preconditions, this.startNode.effects)) ) { - addEgdeWithWeigth(graph, this.startNode, graphNode, new WeightedEdge(), 0); + graph.addEdge(this.startNode, graphNode, new WeightedEdge(0)); if (!nodesToWorkOn.includes(graphNode)) { nodesToWorkOn.push(graphNode); @@ -251,12 +246,10 @@ export abstract class AbstractPlanner { if (areAllPreconditionsMet(otherNodeInGraph.preconditions, node.getEffectState(pathToListNode))) { connected = true; - addEgdeWithWeigth( - graph, + graph.addEdge( node, otherNodeInGraph, - new WeightedEdge(), - (node.action as AbstractAction).generateCost(this.unit) + new WeightedEdge((node.action as AbstractAction).generateCost(this.unit)) ); otherNodeInGraph.addGraphPath( diff --git a/src/utils/graph/DirectedGraph.test.ts b/src/utils/graph/DirectedGraph.test.ts index ea28ea0..958c21e 100644 --- a/src/utils/graph/DirectedGraph.test.ts +++ b/src/utils/graph/DirectedGraph.test.ts @@ -88,4 +88,13 @@ describe("DirectedGraph class", () => { expect([...graph.getEdges().values()]).toEqual([]); expect([...graph.getVertices().values()]).toEqual([0, 1, 2]); }); + + it("should correctly add many vertices", () => { + const graph: DirectedGraph = new DirectedGraph(); + + graph.addVertex(1).addVertex(2).addVertices([3, 4, 5]); + + expect(graph.getVertices().size).toBe(5); + expect([...graph.getVertices().values()]).toEqual([1, 2, 3, 4, 5]); + }); }); diff --git a/src/utils/graph/DirectedGraph.ts b/src/utils/graph/DirectedGraph.ts index 115e41d..50bab9c 100644 --- a/src/utils/graph/DirectedGraph.ts +++ b/src/utils/graph/DirectedGraph.ts @@ -27,6 +27,18 @@ export class DirectedGraph implements IGraph< return this; } + /** + * @param vertices - new vertices list to add in graph + * @returns this + */ + public addVertices(vertices: Array): typeof this { + for (const vertex of vertices) { + this.content.set(vertex, new Map()); + } + + return this; + } + /** * @param firstVertex - vertex to set connection from * @param secondVertex - vertex to set connection to diff --git a/src/utils/graph/DirectedWeightedGraph.ts b/src/utils/graph/DirectedWeightedGraph.ts index c7237ca..c8ec489 100644 --- a/src/utils/graph/DirectedWeightedGraph.ts +++ b/src/utils/graph/DirectedWeightedGraph.ts @@ -5,7 +5,7 @@ import { WeightedEdge } from "@/utils/graph/WeightedEdge"; /** * A DirectedWeightedGraph using WeightedEdges as edges. */ -export class DirectedWeightedGraph +export class DirectedWeightedGraph extends DirectedGraph implements IWeightedGraph { diff --git a/src/utils/planner.ts b/src/utils/planner.ts index eed7454..b3b476e 100644 --- a/src/utils/planner.ts +++ b/src/utils/planner.ts @@ -1,8 +1,8 @@ import { AbstractAction } from "@/AbstractAction"; import { GraphNode } from "@/planner/GraphNode"; import { Property } from "@/Property"; -import { Optional, Queue } from "@/types"; -import { IWeightedGraph, WeightedEdge, WeightedPath } from "@/utils/graph"; +import { Optional } from "@/types"; +import { IWeightedGraph, Path, WeightedEdge, WeightedPath } from "@/utils/graph"; import { createWeightedPath } from "@/utils/path"; /** @@ -14,19 +14,19 @@ import { createWeightedPath } from "@/utils/path"; * @returns a queue in which all actions from the given path are listed */ export function extractActionsFromGraphPath( - path: WeightedPath, + path: Path, start: GraphNode, end: GraphNode -): Queue { - const actionQueue: Queue = []; +): Array { + const queue: Array = []; for (const node of path.getVertexList()) { if (node !== start && node !== end) { - actionQueue.push(node.action as AbstractAction); + queue.push(node.action as AbstractAction); } } - return actionQueue; + return queue; } /** @@ -52,33 +52,6 @@ export function addNodeToGraphPath( return createWeightedPath(graph, baseGraphPath.getStartVertex(), nodeToAdd, vertices, edges); } -/** - * Convenience function for adding a weighted edge to an existing Graph. - * - * @param graph - the Graph the edge is added to - * @param firstVertex - start vertex - * @param secondVertex - target vertex - * @param edge - edge reference - * @param weight - the weight of the edge being created - * @return if the creation of the edge was successful or not - */ -export function addEgdeWithWeigth( - graph: IWeightedGraph, - firstVertex: V, - secondVertex: V, - edge: E, - weight: number -): boolean { - try { - graph.addEdge(firstVertex, secondVertex, edge); - graph.setEdgeWeight(graph.getEdge(firstVertex, secondVertex) as WeightedEdge, weight); - - return true; - } catch (error) { - return false; - } -} - /** * Function for testing if all preconditions in a given set are also in another set (effects) with the same values. *