-
Notifications
You must be signed in to change notification settings - Fork 11
Graph Query API
This page provides the complete API reference for querying data from networkcube as well as a small tutorial on what to do with queries.
Networkcube distinguishes between
- individual objects (nodes, links, timesteps, locations, etc.. ), and
- arrays of objects, called query arrays.
Both, objects and query arrays result from queries on the dynamic network.
- Simple queries are individual function calls, e.g.
someNode.attr('label')
. - Queries can be chained as in d3 or jQuery.
This sheet explains the API for querying dynamic graphs in networkcube.
var g:networkcube.DynamicGraph = networkcube.getGraph()
.
g.nodes():NodeQuery
g.links():LinkQuery
g.times():TimeQuery
g.nodePairs():NodePairQuery
g.locations():LocationQuery
-
g.node(id:number)
returnsNode
with id id; -
g.link(id:number)
returnsLink
with id id; -
g.time(id:number)
returnsTime
with id id; -
g.location(id:number)
returnsLocation
with id id; -
g.nodePair(id:number)
returnsNodePair
with id id; -
g.selections()
:Selection[] -
future: (
g.graph({nodes:Node[], links:Link[], times:Time[]})
returns a staticWindowGraph
.)
Data Objects are all memory objects that are stored with a DynamicGraph
. e.g:
-
var n:Node = g.node(0)
; -
var l:Link = g.link(0)
; -
var p:NodePair = g.nodePair(0)
; -
var t:Time = g.time(0)
; -
var loc:Location = g.location(0)
;
DataObject
is the generic high level object for all data objets in the graph.
For var d:DataObject = g.<someTypeHere>()
, we can query:
-
d.attr(attributeName:string):any
returns the value for that attribute. Temporal attributes (e.g. neighbors of nodes, return this TimeSeries object) -
.presentIn(t1:Time, t2?:Time):boolean
returns if the data object is defined in this time. -
.isHighlighted():boolean
returns if the object is highlighted. -
.isVisible():boolean
returns if the object is visible. -
.selections():Selection[]
returns the selections this object is . -
.isSelected():boolean
returns if the object is selected. Shorthand ford.selections().length > 0
.
Queries
-
.neighbors():NodeQuery
-
.neighbors(t:Time):NodeQuery
returns neighbors for time t -
.neighbors(t1:Time, t2:Time):NodeQuery
returns neighbors between time t1 and t2 -
.outNeighbors():NodeQuery
-
.outNeighbors(t:Time):NodeQuery
-
.outNeighbors(t1:Time, t2:Time):NodeQuery
-
.inNeighbors():NodeQuery
-
.inNeighbors(t:Time):NodeQuery
-
.inNeighbors(t1:Time, t2:Time):NodeQuery
-
.links():LinkQuery
-
.links(t:Time):LinkQuery
-
.links(t1:Time, t2:Time):LinkQuery
-
.outLinks():LinkQuery
-
.outLinks(t:Time):LinkQuery
-
.outLinks(t1:Time, t2:Time):LinkQuery
-
.inLinks():LinkQuery
-
.inLinks(t:Time):LinkQuery
-
.inLinks(t1:Time, t2:Time):LinkQuery
-
.createAttribute(attributeName:string, f:Function):NodeQuery
// creates an attribute for every object in the query. The attribute value is returned by the passed function. -
.removeAttribute(attributeName:string):NodeQuery
-
.. one for every field, listed below.
The following lists the index lists in DynamicGraph
that containing only indices to other lists, but no objects.
label:string[] = [];
nodeType:string[] = [];
-
outLinks: ArrayTimeSeries<number>[] = [];
// contains link ids only, since every GRAPH has its own EDGE object instance -
inLinks: ArrayTimeSeries<number>[] = [];
// contains link ids only, since every GRAPH has its own EDGE object instance links: ArrayTimeSeries<number>[] = [];
- outNeighbors: ArrayTimeSeries[] = [];` // contains node ids only, since every GRAPH has its own NODE object instance
-
inNeighbors: ArrayTimeSeries<number>[] = [];
// contains node ids only, since every GRAPH has its own NODE object instance neighbors: ArrayTimeSeries<number>[] = [];
selections: Selection[][] = [];
locations: ScalarTimeSeries<number>[] = [];
Fields:
source:number[] = [];
target:number[] = [];
linkType:string[] = [];
directed:boolean[] = [];
-
nodePair:number[] = [];
// array of all time ids (temporally ordered) when this link is present -
presence:number[][] = [];
// array of values per time this link is present. This is a generic field // that can be used for weights, e.g. values:ScalarTimeSeries<any>[] = [];
selections: Selection[][] = [];
-
times: TimeQuery;
all times this link's weight is not undefined
source:Node;
target:Node;
links:Link[] = []
-
time:any[] = [];
// moment object -
unixTime:number[] = [];
// js date object selections: Selection[][] = [];
-
links:LinkQuery
All links that's weight is not undefined in this time
- label:string[] = [];
- longitude:number[] = [];
- latitude: number[] = [];
var s:Selection = g.getSelection(id)
;
- `s.elements():ObjectQuery
s.color():string
- s.name:string; elements:Object[]; acceptedType:string; color:string; id:number; showColor:boolean = true; filter:boolean = false; priority:number = 0;
A query is a set of data objects and provides specialized functions on those objects, described below. There are four ways you obtain queries:
- using one of the functions on the dynamic network, described above,
- call some function on an existing query,
- call some function on a data object,
- create a new query from an array of data objects: e.g.
var nodeQuery = new NodeQuery(someNodes, dynamicGraph);
The following functions are common to all queries in networkcube.
-
.elements()
returns a simple array or all elements in the query. -
.first()
returns first element -
.last()
returns first element .contains(object:any)
.add(object:any)
.addAll(addAll)
.length
-
.attr(attributeName:string)
returns different things.. -
.series(attributeName:string)
returnsTimeSeries
. -
.where(attributeName:string, any?)
returnsObjectQuery
. -
.time(modifier:string, t1:Time, t2?:Time)
returnsObjectQuery
..time('in', t1:Time)
.time('in', t1:Time, t2:Time)
.time('before', t1:Time)
.time('after', t1:Time)
.highlighted():ObjectQuery
.selected():ObjectQuery
.visible():ObjectQuery
.sort(attributeName:string)
.forEach(d:Object, i:number)
-
sources: LinkQuery
all source nodes of this set of links -
target: LinkQuery
all target nodes of this set of links
min()
max()
mean()
stdev()
- ... more stats here
Queries are run agains a DynamicNetwork and return an array of JS objects. Queries in networkcube are similar to queries in d3 and jQuery, in that they use syntax chaining.
The principles for designing this query language should be simple
and extensible
.
The examples in this section are performed on the dynamic graph g
:
var g:networkcube.DynamicGraph = networkcube.getDynamicGraph();
For the query API, see [here](Query API)
Some examples of what queries in networkcube can do: *
A detailed tutorial to queries.
A query returns one of four objects or data structures:
-
A single object. Objects in networkcube are:
Node
,Edge
,Time
,Location
,NodePair
,LinkType
, or a scalar such asString
,Number
, orBoolean
. - An array of objects. All objects have the same time.
-
A Time Series: In a time series, an index
id
refers to a time step and points to an individual element or an array. - A graph for a given time step or time interval. You can define the nodes and edge being present in this graph.
There are three types of queries in networkcube:
-
Object queries returns a set of object: etc.:
NodeQuery
,LinkQuery
,TimeQuery
- Attribute queries returning attribute values on objects
- Time queries returning a time series
- Aggregation queries returning an aggregated value on arrays or time series or numbers
- Filter queries returning only element satisfying a condition, and
- Operation queries performing an operation on a result, such as grouping or ordering by some value.
We walk through those queries individually in the following.
Return a ObjectQuery
(NodeQuery
, EdgeQuery
, TimeQuery
)
- all nodes:
g.get('nodes')
returns a list ofNodeQuery
. - all links:
g.get('links')
returns a list ofLinkQuery
. - all links:
g.get('times')
returns a list ofLinkQuery
. - all links:
g.get('locations')
returns a list ofLink
s. - node with type t:
g.get('nodes').where('type', t)
returns aNodeQuery
. - link with type t:
g.get('link').where('type', t)
returns aLinkQuery
. - node with id:
g.node(id)
short for g.get('nodes').where('id', id).get(0). returnsNode
. - time with id:
g.time(id)
returns aTime
.
Objects returned by ObjectQuery
can be stored as variables and can be the base for further queries.
var n = g.node(id);
var l = g.link(id);
var nodes = g.get('nodes');
An objects attributes can be queried by the attr
function (inspired by d3).
- a node's neighbors:
n.attr('neighbors')
--> return an arrayNodeQuery
. - a node's outgoing links:
n.attr('outLinks')
--> return an arrayLinkQuery
. - a link's
NodePair':
l.attr('nodePair')--> return a
NodePair`. - a link's
LinkType
:l.attr('linkType')
--> return aLinkType
. - all node's labels:
nodes.attr('labels')
returns aStringQuery
- all links of a set of nodes
nodes.attr('links')
returns aLinkQuery
. - all a node's locations:
n.attr('location')
returns aLocationQuery
.
.time(modifier, t1, t2?)
- links present in t:
g.get('links').time('in', t)
returnsLinkQuery
. - links present between t1 and t2:
g.get('links').time('in',t1, t2)
, returnsLinkQuery
. // - a node's neighbors in time t: n.attr('neighbors').presentIn(t) //?time('in',t1)
- a node's neighbors between time t1 and t2:
n.attr('neighbors').time('in', t1, t2)
- labels of all nodes present before t1:
g.get('nodes').time('before', t1)
. - types of all nodes present before t1:
g.get('nodes').time('before', t1).attr('type')
.
Sometimes it can be necessary to keep the result of a temporal query organized by time stamp.
Instead of querying by attr
or , query by series
. Time series return a TimeSeries
object that groups a type of query by time stamp
- a time series of node locations:
n.series('location')
returns a TimeSeries query containing a `LocationQuery' for each time step.
TODO
- order results:
n.attr('neighbors').order('label')
- group results by result attributes:
n.attr('neighbors').group('location')
- Create higher level abstractions for attribute access, e.g. n.neighbors(), l.nodePair().