Skip to content

Commit

Permalink
Remove not needed util. Action methods chaining.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neloreck committed Nov 1, 2023
1 parent fcede3c commit c7051b3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 67 deletions.
20 changes: 7 additions & 13 deletions src/AbstractAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
Expand All @@ -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]);

Expand Down Expand Up @@ -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]);
});
Expand All @@ -133,16 +128,15 @@ 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.removeEffect(second.id);

expect(action.getEffects()).toEqual([first]);

action.removeEffect(second.id);
action.removeEffect(second.id).removeEffect(second.id);

expect(action.getEffects()).toEqual([first]);

Expand Down
20 changes: 12 additions & 8 deletions src/AbstractAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ export abstract class AbstractAction<T = any> {
/**
* @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;
}

/**
Expand All @@ -40,16 +42,16 @@ export abstract class AbstractAction<T = any> {
* @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;
}

/**
Expand All @@ -62,26 +64,28 @@ export abstract class AbstractAction<T = any> {
/**
* @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.
Expand Down
15 changes: 4 additions & 11 deletions src/planner/AbstractPlanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions src/utils/graph/DirectedGraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, Edge> = 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]);
});
});
12 changes: 12 additions & 0 deletions src/utils/graph/DirectedGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ export class DirectedGraph<VertexType, EdgeType extends Edge> implements IGraph<
return this;
}

/**
* @param vertices - new vertices list to add in graph
* @returns this
*/
public addVertices(vertices: Array<VertexType>): 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
Expand Down
2 changes: 1 addition & 1 deletion src/utils/graph/DirectedWeightedGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WeightedEdge } from "@/utils/graph/WeightedEdge";
/**
* A DirectedWeightedGraph using WeightedEdges as edges.
*/
export class DirectedWeightedGraph<VertexType, EdgeType extends WeightedEdge>
export class DirectedWeightedGraph<VertexType, EdgeType extends WeightedEdge = WeightedEdge>
extends DirectedGraph<VertexType, EdgeType>
implements IWeightedGraph<VertexType, EdgeType>
{
Expand Down
41 changes: 7 additions & 34 deletions src/utils/planner.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand All @@ -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<GraphNode, WeightedEdge>,
path: Path<GraphNode, WeightedEdge>,
start: GraphNode,
end: GraphNode
): Queue<AbstractAction> {
const actionQueue: Queue<AbstractAction> = [];
): Array<AbstractAction> {
const queue: Array<AbstractAction> = [];

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;
}

/**
Expand All @@ -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<V, E extends WeightedEdge>(
graph: IWeightedGraph<V, E>,
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.
*
Expand Down

0 comments on commit c7051b3

Please sign in to comment.