Implement Edge in Citation
This commit is contained in:
parent
5d82a2f043
commit
05b0d06aea
|
@ -1,23 +1,53 @@
|
|||
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 citedPost: Post;
|
||||
private impact: number
|
||||
private static localStore = new Map<string, Citation>;
|
||||
|
||||
constructor(source: Post, cited:Post, impact: number) {
|
||||
this.sourcePost = source;
|
||||
this.citedPost = cited;
|
||||
this.impact = impact;
|
||||
public static getAllCitations() {
|
||||
return Array.from(Citation.localStore.values());
|
||||
}
|
||||
|
||||
public getSourcePost() { return this.sourcePost; }
|
||||
public getCitedPost() { return this.citedPost; }
|
||||
public getImpact() { return this.impact; }
|
||||
public static getCitation(id: string) {
|
||||
return Citation.localStore.get(id);
|
||||
}
|
||||
|
||||
public isNeutral() { return this.impact == 0; }
|
||||
public isNegative() { return this.impact < 0; }
|
||||
public isPositive() { return this.impact > 0; }
|
||||
private _id: string;
|
||||
private _directional: boolean;
|
||||
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