Implement Edge in Citation
This commit is contained in:
parent
5d82a2f043
commit
05b0d06aea
|
@ -1,23 +1,53 @@
|
||||||
import { Post } from "./post";
|
import { Post } from "./post";
|
||||||
|
import { Edge, Vertex } from "../graph.interface";
|
||||||
|
import { randomUUID } from "crypto";
|
||||||
|
|
||||||
export class Citation {
|
export class Citation implements Edge {
|
||||||
|
|
||||||
private sourcePost: Post;
|
private static localStore = new Map<string, Citation>;
|
||||||
private citedPost: Post;
|
|
||||||
private impact: number
|
|
||||||
|
|
||||||
constructor(source: Post, cited:Post, impact: number) {
|
public static getAllCitations() {
|
||||||
this.sourcePost = source;
|
return Array.from(Citation.localStore.values());
|
||||||
this.citedPost = cited;
|
|
||||||
this.impact = impact;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getSourcePost() { return this.sourcePost; }
|
public static getCitation(id: string) {
|
||||||
public getCitedPost() { return this.citedPost; }
|
return Citation.localStore.get(id);
|
||||||
public getImpact() { return this.impact; }
|
}
|
||||||
|
|
||||||
public isNeutral() { return this.impact == 0; }
|
private _id: string;
|
||||||
public isNegative() { return this.impact < 0; }
|
private _directional: boolean;
|
||||||
public isPositive() { return this.impact > 0; }
|
private _sourcePost: Post;
|
||||||
|
private _citedPost: Post;
|
||||||
|
private _impact: number
|
||||||
|
|
||||||
|
constructor(id: string | null, source: Post, cited: Post, impact: number) {
|
||||||
|
this._id = id ? id : randomUUID();
|
||||||
|
this._sourcePost = source;
|
||||||
|
this._citedPost = cited;
|
||||||
|
this._directional = true;
|
||||||
|
this._impact = impact;
|
||||||
|
Citation.localStore.set(this._id, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public get id() { return this._id; }
|
||||||
|
public get directional() { return this._directional; }
|
||||||
|
public get parentVertex() { return this._citedPost as Vertex; }
|
||||||
|
public get childVertex() { return this._sourcePost as Vertex; }
|
||||||
|
public get sourcePost() { return this._sourcePost; }
|
||||||
|
public get citedPost() { return this._citedPost; }
|
||||||
|
public get impact() { return this._impact; }
|
||||||
|
|
||||||
|
|
||||||
|
public isNeutral() { return this._impact == 0; }
|
||||||
|
public isNegative() { return this._impact < 0; }
|
||||||
|
public isPositive() { return this._impact > 0; }
|
||||||
|
|
||||||
|
public getAdjacent(vertex: Vertex): Vertex {
|
||||||
|
const parentVertex = this._citedPost as Vertex;
|
||||||
|
const childVertex = this._sourcePost as Vertex;
|
||||||
|
if (vertex == childVertex) return parentVertex;
|
||||||
|
else if (vertex == parentVertex) return childVertex;
|
||||||
|
else throw new Error(`Edge ${this._id} is not connected to vertex ${vertex.id}`);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue