package flare.analytics.graph {
import flare.animate.Transitioner;
import flare.util.Property;
import flare.vis.data.Data;
import flare.vis.data.EdgeSprite;
import flare.vis.data.NodeSprite;
import flare.vis.operator.Operator;
/**
* Calculates the link distance from a source node based on a breadth-first
* traversal. Link distance is calculated as the smallest number of edges
* connecting a node to a source node. Any edge weights are ignored, to
* include weighted edges in the calculation, use the
* ShortestPaths
operator instead.
*
*
true
indicates that the edge
* participates in a shortest path, false
indicates that
* the edge does not lie along a shortest path. The default value is
* "props.onpath". */
public function get onpathField():String { return _p.name; }
public function set onpathField(f:String):void { _p = Property.$(f); }
/** The link type to consider when calculating link distance. Should
* be one of NodeSprite.GRAPH_LINKS
,
* NodeSprite.IN_LINKS
, or
* NodeSprite.OUT_LINKS
. */
public function get links():int { return _links; }
public function set links(linkType:int):void {
if (linkType == NodeSprite.GRAPH_LINKS ||
linkType == NodeSprite.IN_LINKS ||
linkType == NodeSprite.OUT_LINKS)
{
_links = linkType;
} else {
throw new Error("Unsupported link type: "+linkType);
}
}
// --------------------------------------------------------------------
/**
* Creates a new LinkDistance operator.
* @param sources an Array specifying the roots of the breadth-first
* search. The elements of the array should either be
* NodeSprite
instances or integer indices into the node
* array.
*/
public function LinkDistance(sources:Array=null)
{
this.sources = sources;
}
/** @inheritDoc */
public override function operate(t:Transitioner=null):void
{
calculate(visualization.data, sources);
}
/**
* Calculates link distances from a set of source nodes for for each
* node in the graph. Each node in the graph will be annotated with
* its link distance from the nearest source node.
* @param data the graph to calculate distances for
* @param sources one or more source nodes from which to measure the
* distance. This input can either be a single node or an array of
* nodes. Nodes can be indicated as either a NodeSprite
* instance or an integer index into the data.nodes
* property.
*/
public function calculate(data:Data, sources:*):void
{
var i:int, n:NodeSprite;
data.edges.setProperty(_e.name, false);
data.nodes.visit(function(n:NodeSprite):void {
_d.setValue(n, Number.POSITIVE_INFINITY);
_p.setValue(n, null);
});
// initialize queue
var roots:Array = sources is Array ? sources as Array : [sources];
var queue:Array = [];
for (i=0; i