Type parameters
-
T
The type of any user data associated with each vertex in the graph.
Hierarchy
- IDirectedGraph
Index
Properties
Readonly edgeCount
The number of edges in the graph.
console.log(`the graph has ${graph.edgeCount} edges`);
Readonly edges
An iterable of all edges in the graph.
for (const edge of graph.edges) {
console.log(`edge: ${edge.src.id} -> ${edge.dst.id}`);
}
Readonly vertexCount
The number of vertices in the graph.
console.log(`the graph has ${graph.vertexCount} vertices`);
Readonly vertices
An iterable of all vertices in the graph.
for (const vertex of graph.vertices) {
console.log(`vertex: ${vertex.id}`);
}
Methods
addVertex
-
Add a vertex or set of vertices to the graph.
// for vertices with undefined "data" (no data associated with each vertex) const undefGraph = mpSdk.Graph.createDirectedGraph<undefined>(); graph.addVertex( { id: 'a' }, { id: 'b' }, ); // for vertices with any other data const graph = mpSdk.Graph.createDirectedGraph<number>(); graph.addVertex( { id: 'a', data: 1 }, { id: 'b', data: 2 }, );
Parameters
-
Rest ...vertexData: T extends undefined | void ? VertexIdDescriptor[] : Array<VertexDescriptor<T>>
A variable number of VertexDescriptors to use to create nodes in the graph.
Returns void
-
commit
-
Trigger any attached observers if there were changes to this graph. If there are no changes to the graph, this is a no-op and no callbacks will be triggered.
graph.commit();
Returns void
dispose
-
Remove all vertices and edges from the graph. Also call an optional
onDispose
provided at construction.graph.dispose(); // graph.vertexCount === 0 // graph.edgeCount === 0
Returns void
edge
-
Get the edge that connects one vertex to another.
const edge = graph.edge(a, b); if (edge) { console.log(`Found an edge from "${edge.src.id}" to "${edge.dst.id}" with weight ${edge.weight}`); }
Parameters
-
src: Graph.Vertex<T>
The source vertex.
-
dst: Graph.Vertex<T>
The destination vertex.
Returns Graph.Edge<T> | undefined
If an edge from
src
todst
exists, returns theEdge
. Otherwise,undefined
. -
hasEdge
-
Test if an edge from one vertex to another is present in the graph.
if (graph.hasEdge(a, b)) { console.log('an edge from "a" to "b" was found'); }
Parameters
-
src: Graph.Vertex<T>
The source vertex.
-
dst: Graph.Vertex<T>
The destination vertex.
Returns boolean
If an edge from
src
todst
exists, returnstrue
. Otherwise,false
. -
hasVertex
-
Test if a vertex associated with
id
is present in the graph.if (graph.hasVertex('a')) { console.log('a vertex with id "a" was found'); }
Parameters
-
id: string
The vertex's id.
Returns boolean
If a vertex with
id
exists, returnstrue
. Otherwise,false
. -
onEdgesChanged
-
Subscribe to edge changes.
After this graph's edges have been updated using setEdge or removeEdge, the
observer
attached will have itsonChanged
function called when commit is called.graph.onEdgesChanged({ onChanged() { console.log(`this graph's edges have changed`); } });
Parameters
-
observer: IObserver<void>
Returns ISubscription
-
onVerticesChanged
-
Subscribe to vertex changes.
After this graph's vertices have been updated using addVertex or removeVertex, the
observer
attached will have itsonChanged
function called when commit is called.graph.onVerticesChanged({ onChanged() { console.log(`this graph's vertices have changed`); } });
Parameters
-
observer: IObserver<void>
Returns ISubscription
-
removeEdge
-
Remove an edge or a set of edges from the graph.
graph.removeEdge(e1, e2);
Parameters
-
Rest ...edges: Array<Graph.Edge<T>>
A variable number of edges to remove.
Returns void
-
removeVertex
-
Remove a vertex or a set of vertices from the graph.
graph.removeVertex(a, b);
Parameters
-
Rest ...vertices: Array<Graph.Vertex<T>>
A variable number of vertices to remove.
Returns void
-
setEdge
-
Set the weight of an edge or set of edges between pairs of vertices representing the connections from
src
todst
.If an error is thrown, the graph is left untouched, no edges are added.
graph.setEdge( { src: a, dst: b, weight: 5 }, { src: b, dst: c, weight: 10 }, ); // update edge ab graph.setEdge( { src: a, dst: b, weight: 10 }, );
Parameters
-
Rest ...edgeDescs: Array<Graph.EdgeDescriptor<T>>
Returns void
-
vertex
-
Get the vertex associated with
id
.const a = graph.vertex('a'); if (a) { console.log(`Found a vertex with id "${a.id}"`); }
Parameters
-
id: string
The vertex's id.
Returns Graph.Vertex<T> | undefined
If a vertex with
id
exists, returns theVertex
. Otherwise,undefined
. -
watch
-
Watch a collection and update the graph as the collection changes.
Note: If you need a graph of enabled sweeps, use Sweep.createGraph instead of the code snippet below
// create a graph of enabled sweeps const graph = mpSdk.createGraph(); const sub = graph.watch({ collection: mpSdk.Sweep.collection, isNeighborOf(sweepSrc, sweepDst) { return sweepSrc.data.neighbors.includes(sweepDst.id); }, neighborsOf(sweepVertex) { return sweepVertex.data.neighbors.values(); }, weightBetween(sweepSrc, sweepDst) { const dx = sweepDst.data.position.x - sweepSrc.data.position.x; const dy = sweepDst.data.position.y - sweepSrc.data.position.y; const dz = sweepDst.data.position.z - sweepSrc.data.position.z; return Math.sqrt(dx ** 2 + dy ** 2 + dz ** 2); }, shouldAdd(sweep) { return sweep.enabled; }, }); // some time later when the graph no longer needs updating sub.cancel();
Parameters
-
collectionAdaptor: Graph.ICollectionAdaptor<T>
Returns ISubscription
ISubscription
-
A directed, weighted graph data structure.