Skip to content
Snippets Groups Projects
graph.ts 2.82 KiB
Newer Older
  • Learn to ignore specific revisions
  • interface Link {
        source: string;
        target: string;
        type?: string;
    }
    
    
    export interface LinkData {
        source: NodeData;
        target: NodeData;
        type?: string;
    }
    
    export interface NodeData {
        id: string;
        name: string;
        description?: string;
    
        icon?: string;
        banner?: string;
    
        type?: string;
        video?: string;
    
        references?: string[];
    
        neighbors: NodeData[];
        links: LinkData[];
    }
    
    export interface Coordinate {
        x: number;
        y: number;
        z: number;
    }
    
    
    export default class Graph {
    
        public nodes: NodeData[];
        public links: LinkData[];
        private idToNode: Map<string, NodeData>;
    
    
        constructor(nodes: NodeData[], links: Link[]) {
    
            this.nodes = nodes;
            this.idToNode = new Map<string, NodeData>();
            nodes.forEach((node) => {
                this.idToNode.set(node.id, node);
            });
    
    
            this.links = links.map((link) => {
                return {
                    source: this.idToNode.get(link.source),
                    target: this.idToNode.get(link.target),
                    type: link.type,
                };
            });
    
            this.resetNodeData();
    
            this.updateNodeData();
        }
    
        resetNodeData() {
            for (const node of this.nodes) {
                node.neighbors = [];
                node.links = [];
            }
        }
    
        /**
         * Updates the graph data structure to contain additional values.
         * Creates a 'neighbors' and 'links' array for each node object.
         */
        updateNodeData() {
            this.resetNodeData();
    
            this.links.forEach((link) => {
                const a = link.source;
                const b = link.target;
                a.neighbors.push(b);
                b.neighbors.push(a);
                a.links.push(link);
                b.links.push(link);
            });
        }
    
        node(id: string): NodeData {
            return this.idToNode.get(id);
        }
    
        /**
         * Returns an array containing the different edge types of the graph.
         * @returns {*[]}
         */
        getLinkClasses(): string[] {
            const linkClasses: string[] = [];
    
            this.links.forEach((link) => linkClasses.push(link.type));
            return [...new Set(linkClasses)].map((c) => String(c));
        }
    
        getNodeClasses(): string[] {
            const nodeClasses: string[] = [];
            this.nodes.forEach((node) => nodeClasses.push(node.type));
            return [...new Set(nodeClasses)].map((c) => String(c));
        }
    
        view(
            nodeTypes: Map<string, boolean>,
            linkTypes: Map<string, boolean>
        ): Graph {
    
            const links = this.links.filter((l) => linkTypes.get(l.type));
    
    
            return new Graph(
                this.nodes.filter((l) => nodeTypes.get(l.type)),
    
                links.map((link) => {
                    return {
                        source: link.source.id,
                        target: link.target.id,
                        type: link.type,
                    };
                })