UnitGraph is a simple Graph traversal library for io.js. It is intended for quick, synchronous in-memory traversals including route tracing and finding closest nodes to a target.
UnitGraph is available via npm. Simply run npm install ug-ts
and then:
import ug from 'ug-ts'
To use the package. :)
import ug from 'ug-ts'
let graph = new ug.Graph();
graph.createNode('person', {name: 'Rachael'});
graph.createNode('person', {name: 'Stephanie'});
graph.createNode('person', {name: 'Michael'});
graph.createNode('person', {name: 'Donovan'});
graph.nodes('person').query().filter({name__ilike: 'ae'}).units();
// [ Node (person {name: Rachael}), Node (person {name: Michael}) ]
import ug from 'ug-ts'
let graph = new ug.Graph();
let civilian = graph.createNode('person', {name: 'Clark Kent'});
let superman = graph.createNode('superhero', {name: 'Superman'});
graph.createEdge('wears_glasses').link(superman, civilian);
graph.trace(
graph.nodes('person').query().filter({name: 'Clark Kent'}).first(),
graph.nodes('superhero').query().filter({name: 'Superman'}).first()
);
// Path: Node (person {name: "Clark Kent"}) << Edge (wears_glasses {}) << Node (superhero {name: 'Superman'})
import ug from 'ug-ts'
let graph = new ug.Graph();
let classification = graph.createNode('classification', {name: 'Sharing Economy'});
let corps = {
uber: graph.createNode('corporation', {name: 'Uber'}),
storefront: graph.createNode('corporation', {name: 'Storefront'}),
airbnb: graph.createNode('corporation', {name: 'AirBnB'})
};
let industries = {
vc: graph.createNode('industry', {name: 'Venture Capital'}),
hospitality: graph.createNode('industry', {name: 'Hospitality'}),
taxi: graph.createNode('industry', {name: 'Taxi'})
};
graph.createEdge('business_model').link(corps.uber, classification);
graph.createEdge('business_model').link(corps.airbnb, classification);
graph.createEdge('business_model').link(corps.storefront, classification);
graph.createEdge('emotion', {type: 'happy'}).link(industries.vc, classification);
graph.createEdge('emotion', {type: 'sad'}).link(industries.hospitality, classification);
graph.createEdge('emotion', {type: 'sad'}).link(industries.taxi, classification);
graph.closest(
graph.nodes('classification').query().first(), // grab Sharing Economy node
{
compare: function(node) {
// forget industries and uber!
return node.entity !== 'industry' && node.get('name') !== 'Uber';
},
direction: -1 // only track nodes that feed in to this one
}
);
// returns two paths, one from Sharing Economy << (business_model) << AirBnB
// and Sharing Economy << business_model << Storefront,
// ordered by their distance
javascript
Graph()
Constructor. Part of the ug
namespace. Use with new
keyword, i.e.
import { Graph } from 'ug-ts'
let graph = new Graph();
unit( [Number] uniqid )
returns [Unit] ([Node] or [Edge])
Grabs a unit (node or edge) by their unique id (automatically assigned by their parent Graph object).
nodeCount()
returns [Number]
Returns the total number of nodes that belong to the graph.
edgeCount()
returns [Number]
Returns the total number of edges that belong to the graph.
createNode( [String] entity, [Object] properties )
returns [Node]
Creates a node belonging to the parent graph, with entity type entity
and
calls Unit#load
to attach properties
to the node.
Automatically creates a NodeCollection
of type entity
belonging to the
parent graph if one does not yet exist.
createEdge( [String] entity, [Object] properties )
returns [Edge]
Creates an edge belonging to the parent graph, with entity type entity
and
calls Unit#load
to attach properties
to the edge.
Automatically creates an EdgeCollection
of type entity
belonging to the
parent graph if one does not yet exist.
nodes( [String] entity )
returns [NodeCollection]
Returns the parent graph's NodeCollection
object of the specified entity
.
Invoking this method will create a NodeCollection
if one does not yet exist.
edges( [String] entity )
returns [EdgeCollection]
Returns the parent graph's EdgeCollection
object of the specified entity
.
Invoking this method will create a EdgeCollection
if one does not yet exist.
trace( [Node] fromNode, [Node] toNode, [Number] direction )
returns [Path]
Finds the shortest distance Path
from fromNode
to toNode
. If there are
multiple paths of the same distance, it will return the first one it finds.
direction
can be -1
(incoming nodes only), 0
(doesn't matter) or 1
(outgoing nodes only).
You should not depend on this method to always return the same Path
.
For finding all paths of a specific distance, use Graph#closest
.
closest( [Node] node, [Object] options )
returns [Array] of [Path]
Finds all closest nodes to node
and returns their Path
s in an array, ordered
by total distance. Nodes are filtered based on the parameters passed in
options
.
These include:
options.compare
: A function containing a comparison constraint for the node.
Should return true
for an inclusion of the target node, and false
to
ignore it.
Example:
let options = {
compare: function(node) {
return node.entity === 'person';
}
}
This will make sure only nodes with the entity 'person'
are included in your
results.
options.count
: A number indicating the amount of results to return. 0 will
return all results.
options.direction
: Which direction can we traverse the graph in?
Can be -1
(incoming nodes only), 0
(doesn't matter) or 1
(outgoing nodes only).
options.minDepth
: The minimum distance from our target at which to start
counting nodes in our result set.
options.maxDepth
: The maximum distance from our target at which we can finish
counting nodes in our result set.
toJSON()
returns [String]
Creates a JSON string representation of our graph using the toJSON
of graph
consituents.
fromJSON( [String] json )
returns [self: Graph]
Synchronously prepares a graph from a json
string representation.
save( [String] filename, [Function] callback )
returns [self: Graph]
Save the current graph to a file, asynchronously. Specify full path in filename
.
callback
is of the form function(err) {}
.
load( [String] filename, [Function] callback )
returns [self: Graph]
Load the current graph to a file, asynchronously, from filename
.
callback
is of the form function(err) {}
.
Unit()
Inaccessible constructor. Base prototype for Node
and Edge
.
load( [Object] properties )
returns [self: Unit]
Load all properties for the Unit
from properties
. Creates a shallow copy
of the object provided.
set( [String] property, [Any] value )
returns [Any]
Set a specific property of the Unit
. Returns the set property value.
unset( [String] property )
returns [Boolean]
Unsets property
of the Unit
. Returns true
on success, false
on failure.
has( [String] property )
returns [Boolean]
Returns true
if Unit
has property property
, otherwise returns false
.
get( [String] property )
returns [Any]
Returns the associated property value of Unit
.
toString()
returns [String]
Returns a string representation of the Unit
.
valueOf()
returns [String]
See: Unit#toString
.
Node()
extends [Unit]
Inaccessible constructor. Inherits from Unit
.
Use Graph#createNode
to invoke this constructor.
unlink()
returns true
De-references all connected edges from itself, and itself from all connected edges.
Edge()
extends [Unit]
Inaccessible constructor. Inherits from Unit
.
Use Graph#createEdge
to invoke this constructor.
link( [Node] fromNode, [Node] toNode, [Boolean] duplex )
returns [self: Edge]
Links two nodes directionally (fromNode
to toNode
) or bi-directionally
if duplex
is set to true
.
unlink()
returns true
De-references both connected nodes from itself, and itself from both connected nodes.
setDistance( [Number] distance )
returns [self: Edge]
Sets the distance (length) of the edge.
setWeight( [Number] weight )
returns [self: Edge]
Sets the distance (length) of the edge to 1 / weight.
oppositeNode( [Node] node )
returns [Node]
Returns the node opposite to the one provided (if provided node is connected to
the edge). Otherwise returns undefined
.
Collection()
Inaccessible constructor. Base prototype for NodeCollection
and EdgeCollection
.
name()
returns [String]
Returns the entity name of the collection.
indices()
returns [Array] of [String]
Provides an array of all indexed fields in the collection
createIndex( [String] field )
returns [self: Collection]
Adds field
as an index on the collection. Useful for Collection#find
and
Collection#destroy
.
createIndex( [Array] fieldList )
returns [self: Collection]
Adds each fieldList
entry as an index on the collection.
Useful for Collection#find
and Collection#destroy
.
find( [String or Number] id )
find( [String] index, [String or Number] id )
returns [Unit] ([Node or Edge])
Returns the Unit
(node or edge) associated with the supplied index
and id
.
If no index
is provided, it will use the first index added to the Collection.
destroy( [String or Number] id )
destroy( [String] index, [String or Number] id )
returns [Unit] ([Node or Edge])
Removes the Unit
(node or edge) associated with the supplied index
and id
from the collection and returns it.
If no index
is provided, it will use the first index added to the Collection.
query()
returns [Query]
Creates a new Query
object with all units in the collection.
NodeCollection()
extends [Collection]
Inaccessible constructor. Inherits from Collection
.
Use Graph#nodes(entity)
to invoke this constructor automatically.
EdgeCollection()
extends [Collection]
Inaccessible constructor. Inherits from Collection
.
Use Graph#edges(entity)
to invoke this constructor automatically.
Query()
extends [Collection]
Inaccessible constructor. Inherits from Collection
.
Use Collection#query
to instantiate this object.
filter( [Array] filtersObjects )
filter( [Object] filters_1, ..., [Object] filters_n )
returns [Query]
Returns a Query
object containing a subset of units
that has been filtered
based on supplied filtersObjects
. Can be passed in as an array or separate
arguments.
See DataCollection.js examples for a better idea of how these filters work. Note: the implementations are not completely identical.
Supported filters for UnitGraph's Query
object are currently:
is
not
gt
lt
gte
lte
ilike
like
in
not_in
exclude( [Array] filtersObjects )
exclude( [Object] filters_1, ..., [Object] filters_n )
returns [Query]
Returns the complementary set of units when compared to Query#filter
.
(Excludes instead of includes filter values).
first()
returns [Unit] ([Node or Edge])
Returns the first unit in the query set.
last()
returns [Unit] ([Node or Edge])
Returns the last unit in the query set.
units()
returns [Array] of [Unit] ([Node or Edge])
Returns all units in the query set.
Path()
Inaccessible constructor. Returned from Graph#trace
and Graph#closest
.
start()
returns [Node]
Returns the first node in the path.
end()
returns [Node]
Returns the last node in the path.
length()
returns [Number]
Returns an integer indicating the number of edges in the path.
distance()
returns [Number]
Returns a number indicating the total distance of the path.
prettify()
returns [String]
Provides a human-readable string representation of the path.
toString()
returns [String]
Alias for Path#prettify
.
This project is maintained by myself, Khalil Stemmler, @stemmlerjs.
UnitGraphTypescript is MIT licenced, so have fun with it!
Thank you to the original author, Keith Horwood, @keithwhor for putting together the original version of this useful library.