Class BarnesHutLayout
- All Implemented Interfaces:
AttributeSink,ElementSink,Pipe,Sink,Source,Layout,org.miv.pherd.ParticleBoxListener
public abstract class BarnesHutLayout extends SourceBase implements Layout, org.miv.pherd.ParticleBoxListener
This base class creates the space decomposition method and manages the main
loop of the simulation. The simulation is made of NodeParticle and
EdgeSpring elements that are created and linked for you in response
to graph events received via the Sink interface. However you have to
provide an implementation of the abstract NodeParticle class (by
overriding the abstract method newNodeParticle(String)).
As almost all the repulsion/attraction forces computation is done in the
NodeParticle class, this is the most important one.
This algorithm can be configured using several attributes put on the graph :
- layout.force : a floating point number (default 0.5f), that allows to define the importance of movement of each node at each computation step. The larger the value the quicker nodes move to their position of lowest energy. However too high values can generate non stable layouts and oscillations.
- layout.quality : an integer between 0 and 4. With value 0 the layout is faster but it also can be farther from equilibrium. With value 4 the algorithm tries to be as close as possible from equilibrium (the n-tree and Barnes-Hut algorithms are disabled), but the computation can take a lot of time (the algorithm becomes O(n^2)). TODO change this into layout.barneshut or something similar.
- layout.weight : The force of repulsion of a node. The larger the value, the more the node repulses its neighbors.
- layout.weight : the multiplier for the desired edge length. By default the algorithm tries to make each edge of length one. This is the position of lowest energy for a spring. This coefficient allows to modify this target spring length. Value larger than one will make the edge longer. Values between 0 and 1 will make the edge smaller.
- layout.stabilization-limit : the stabilization of a layout is a number between 0 and 1. 1 means fully stable, but this value is rare. Therefore one can consider the layout stable at a lower value. The default is 0.9. You can fix it with this attribute.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.graphstream.stream.SourceBase
SourceBase.ElementType -
Constructor Summary
Constructors Constructor Description BarnesHutLayout()New 2D Barnes-Hut simulation.BarnesHutLayout(boolean is3D)New Barnes-Hut simulation.BarnesHutLayout(boolean is3D, Random randomNumberGenerator)New Barnes-Hut simulation. -
Method Summary
Modifier and Type Method Description voidclear()Clears the whole nodes and edges structuresvoidcompute()Method to call repeatedly to compute the layout.voidedgeAdded(String graphId, long time, String edgeId, String fromNodeId, String toNodeId, boolean directed)An edge was inserted in graph.voidedgeAttributeAdded(String graphId, long time, String edgeId, String attribute, Object value)A edge attribute was added.voidedgeAttributeChanged(String graphId, long time, String edgeId, String attribute, Object oldValue, Object newValue)A edge attribute was changed.voidedgeAttributeRemoved(String graphId, long time, String edgeId, String attribute)A edge attribute was removed.voidedgeRemoved(String graphId, long time, String edgeId)An edge of graph was removed.The nodes the edge connects may already have been removed from the graph.voidfreezeNode(String id, boolean on)Freeze or un-freeze a node.doublegetBarnesHutTheta()The Barnes-Hut theta value used to know if we use a pole or not.Point3getCenterPoint()EnergiesgetEnergies()doublegetForce()The current layout force.doublegetGravityFactor()A gravity factor that attracts all nodes to the center of the layout to avoid flying components.Point3getHiPoint()Largest point in space of the layout bounding box.longgetLastStepTime()Time in nanoseconds used by the last call to step().abstract StringgetLayoutAlgorithmName()Name of the layout algorithm.Point3getLowPoint()Smallest point in space of the layout bounding box.intgetNodeMovedCount()How many nodes moved during the last step?.doublegetQuality()The current layout algorithm quality.RandomgetRandom()org.miv.pherd.ParticleBoxgetSpatialIndex()The spatial index as a n-tree.doublegetStabilization()Estimate of how close to stabilization the layout algorithm is.doublegetStabilizationLimit()Above which value a correct stabilization is achieved?intgetSteps()Number of calls made to step() so far.doublegetViewZone()voidgraphAttributeAdded(String graphId, long time, String attribute, Object value)A graph attribute was added.voidgraphAttributeChanged(String graphId, long time, String attribute, Object oldValue, Object newValue)A graph attribute was changed.voidgraphAttributeRemoved(String graphId, long time, String attribute)A graph attribute was removed.voidgraphCleared(String graphId, long time)The whole graph was cleared.booleanis3D()voidmoveNode(String id, double x, double y, double z)Move a node by force to a new location.abstract NodeParticlenewNodeParticle(String id)Factory method to create node particles.voidnodeAdded(String graphId, long time, String nodeId)A node was inserted in the given graph.voidnodeAttributeAdded(String graphId, long time, String nodeId, String attribute, Object value)A node attribute was added.voidnodeAttributeChanged(String graphId, long time, String nodeId, String attribute, Object oldValue, Object newValue)A node attribute was changed.voidnodeAttributeRemoved(String graphId, long time, String nodeId, String attribute)A node attribute was removed.voidnodeRemoved(String graphId, long time, String nodeId)A node was removed from the graph.voidparticleAdded(Object id, double x, double y, double z)voidparticleAdded(Object id, double x, double y, double z, Object mark)voidparticleAttributeChanged(Object id, String attribute, Object newValue, boolean removed)voidparticleMarked(Object id, Object mark)voidparticleMoved(Object id, double x, double y, double z)voidparticleRemoved(Object id)doublerandomXInsideBounds()doublerandomYInsideBounds()doublerandomZInsideBounds()voidsetBarnesHutTheta(double theta)Change the barnes-hut theta parameter allowing to know if we use a pole or not.voidsetForce(double value)The general "speed" of the algorithm.voidsetGravityFactor(double value)Set the gravity factor that attracts all nodes to the center of the layout to avoid flying components.voidsetQuality(double qualityLevel)Set the overall quality level, a number between 0 and 1 with 1 the highest quality available, but often with a slower computation.voidsetSendNodeInfos(boolean on)If true, node informations messages are sent for every node.voidsetStabilizationLimit(double value)Change the stabilization limit for this layout algorithm.voidshake()Add a random vector whose length is 10% of the size of the graph to all node positions.voidstepBegins(String graphId, long time, double step)Since dynamic graphs are based on discrete event modifications, the notion of step is defined to simulate elapsed time between events.voidstepFinished(int time)Methods inherited from class org.graphstream.stream.SourceBase
addAttributeSink, addElementSink, addSink, attributeSinks, clearAttributeSinks, clearElementSinks, clearSinks, elementSinks, removeAttributeSink, removeElementSink, removeSink, sendAttributeChangedEvent, sendAttributeChangedEvent, sendEdgeAdded, sendEdgeAdded, sendEdgeAttributeAdded, sendEdgeAttributeAdded, sendEdgeAttributeChanged, sendEdgeAttributeChanged, sendEdgeAttributeRemoved, sendEdgeAttributeRemoved, sendEdgeRemoved, sendEdgeRemoved, sendGraphAttributeAdded, sendGraphAttributeAdded, sendGraphAttributeChanged, sendGraphAttributeChanged, sendGraphAttributeRemoved, sendGraphAttributeRemoved, sendGraphCleared, sendGraphCleared, sendNodeAdded, sendNodeAdded, sendNodeAttributeAdded, sendNodeAttributeAdded, sendNodeAttributeChanged, sendNodeAttributeChanged, sendNodeAttributeRemoved, sendNodeAttributeRemoved, sendNodeRemoved, sendNodeRemoved, sendStepBegins, sendStepBeginsMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.graphstream.stream.Source
addAttributeSink, addElementSink, addSink, clearAttributeSinks, clearElementSinks, clearSinks, removeAttributeSink, removeElementSink, removeSink
-
Constructor Details
-
BarnesHutLayout
public BarnesHutLayout()New 2D Barnes-Hut simulation. -
BarnesHutLayout
public BarnesHutLayout(boolean is3D)New Barnes-Hut simulation.- Parameters:
is3D- If true the simulation dimensions count is 3 else 2.
-
BarnesHutLayout
New Barnes-Hut simulation.- Parameters:
is3D- If true the simulation dimensions count is 3 else 2.randomNumberGenerator- The random number generator to use.
-
-
Method Details
-
getLowPoint
Description copied from interface:LayoutSmallest point in space of the layout bounding box.- Specified by:
getLowPointin interfaceLayout
-
getHiPoint
Description copied from interface:LayoutLargest point in space of the layout bounding box.- Specified by:
getHiPointin interfaceLayout
-
randomXInsideBounds
public double randomXInsideBounds() -
randomYInsideBounds
public double randomYInsideBounds() -
randomZInsideBounds
public double randomZInsideBounds() -
getCenterPoint
-
getGravityFactor
public double getGravityFactor()A gravity factor that attracts all nodes to the center of the layout to avoid flying components. If set to zero, the gravity computation is disabled.- Returns:
- The gravity factor, usually between 0 and 1.
-
setGravityFactor
public void setGravityFactor(double value)Set the gravity factor that attracts all nodes to the center of the layout to avoid flying components. If set to zero, the gravity computation is disabled.- Parameters:
value- The new gravity factor, usually between 0 and 1.
-
getSpatialIndex
public org.miv.pherd.ParticleBox getSpatialIndex()The spatial index as a n-tree.- Returns:
- The n-tree.
-
getLastStepTime
public long getLastStepTime()Description copied from interface:LayoutTime in nanoseconds used by the last call to step().- Specified by:
getLastStepTimein interfaceLayout
-
getLayoutAlgorithmName
Description copied from interface:LayoutName of the layout algorithm.- Specified by:
getLayoutAlgorithmNamein interfaceLayout
-
getNodeMovedCount
public int getNodeMovedCount()Description copied from interface:LayoutHow many nodes moved during the last step?. When this method returns zero, the layout stabilized.- Specified by:
getNodeMovedCountin interfaceLayout
-
getStabilization
public double getStabilization()Description copied from interface:LayoutEstimate of how close to stabilization the layout algorithm is.- Specified by:
getStabilizationin interfaceLayout- Returns:
- a value between 0 and 1. 1 means fully stabilized.
-
getStabilizationLimit
public double getStabilizationLimit()Description copied from interface:LayoutAbove which value a correct stabilization is achieved?- Specified by:
getStabilizationLimitin interfaceLayout- Returns:
- The stabilization limit.
-
getSteps
public int getSteps()Description copied from interface:LayoutNumber of calls made to step() so far. -
getQuality
public double getQuality()Description copied from interface:LayoutThe current layout algorithm quality. A number between 0 and 1 with 1 the highest (but probably slowest) quality.- Specified by:
getQualityin interfaceLayout- Returns:
- A number between 0 and 1.
-
is3D
public boolean is3D() -
getForce
public double getForce()Description copied from interface:LayoutThe current layout force. -
getRandom
-
getEnergies
-
getBarnesHutTheta
public double getBarnesHutTheta()The Barnes-Hut theta value used to know if we use a pole or not.- Returns:
- The theta value (between 0 and 1).
-
getViewZone
public double getViewZone() -
setSendNodeInfos
public void setSendNodeInfos(boolean on)Description copied from interface:LayoutIf true, node informations messages are sent for every node. This is mainly for debugging and slows down the process a lot. The contents of the node information is specific to the algorithm, and sent via a specific "layout.info" attribute.- Specified by:
setSendNodeInfosin interfaceLayout- Parameters:
on- If true, send node informations to a "layout.info" attribute.
-
setBarnesHutTheta
public void setBarnesHutTheta(double theta)Change the barnes-hut theta parameter allowing to know if we use a pole or not.- Parameters:
theta- The new value for theta (between 0 and 1).
-
setForce
public void setForce(double value)Description copied from interface:LayoutThe general "speed" of the algorithm. For some algorithm this will have no effect. For most "dynamic" algorithms, this change the way iterations toward stabilization are done. -
setStabilizationLimit
public void setStabilizationLimit(double value)Description copied from interface:LayoutChange the stabilization limit for this layout algorithm.The stabilization is a number between 0 and 1 that indicates how close to stabilization (no nodes need to move) the layout is. The value 1 means the layout is fully stabilized. Naturally this is often only an indication only, for some algorithms, it is difficult to determine if the layout is correct or acceptable enough. You can get the actual stabilization limit using
Layout.getStabilizationLimit(). You can get the actual stabilization usingLayout.getStabilization().Be careful, most layout classes do not use the stabilization limit, this number is mostly used the process that control the layout, like the
LayoutRunnerfor example. The stabilization limit is only an indication with a default set for each layout algorithm. However this default can be changed using this method, or by storing on the graph an attribute "layout.stabilization-limit" (or "layout.stabilisation-limit").The convention is that the value 0 means that the process controlling the layout will not stop the layout (will therefore not consider the stabilization limit). In other words the layout will compute endlessly.
- Specified by:
setStabilizationLimitin interfaceLayout- Parameters:
value- The new stabilization limit, 0 means no need to stabilize. Else a value larger than zero or equal to 1 is accepted.
-
setQuality
public void setQuality(double qualityLevel)Description copied from interface:LayoutSet the overall quality level, a number between 0 and 1 with 1 the highest quality available, but often with a slower computation.- Specified by:
setQualityin interfaceLayout- Parameters:
qualityLevel- The quality level, a number between 0 and 1.
-
clear
public void clear()Description copied from interface:LayoutClears the whole nodes and edges structures -
compute
public void compute()Description copied from interface:LayoutMethod to call repeatedly to compute the layout.This method implements the layout algorithm proper. It must be called in a loop, until the layout stabilizes. You can know if the layout is stable by using the
Layout.getNodeMovedCount()method that returns the number of node that have moved during the last call to step().The listener is called by this method, therefore each call to step() will also trigger layout events, allowing to reproduce the layout process graphically for example. You can insert the listener only when the layout stabilized, and then call step() anew if you do not want to observe the layout process.
-
shake
public void shake()Description copied from interface:LayoutAdd a random vector whose length is 10% of the size of the graph to all node positions. -
moveNode
Description copied from interface:LayoutMove a node by force to a new location. It is preferable to first freeze the node before moving it by force, and then un-freeze it. -
freezeNode
Description copied from interface:LayoutFreeze or un-freeze a node. The freezed node position will not be changed by the algorithm until un-freezed.- Specified by:
freezeNodein interfaceLayout- Parameters:
id- The node identifier.on- If true the node is frozen.
-
particleAdded
-
particleAdded
- Specified by:
particleAddedin interfaceorg.miv.pherd.ParticleBoxListener
-
particleMarked
-
particleMoved
- Specified by:
particleMovedin interfaceorg.miv.pherd.ParticleBoxListener
-
particleRemoved
- Specified by:
particleRemovedin interfaceorg.miv.pherd.ParticleBoxListener
-
stepFinished
public void stepFinished(int time)- Specified by:
stepFinishedin interfaceorg.miv.pherd.ParticleBoxListener
-
particleAttributeChanged
public void particleAttributeChanged(Object id, String attribute, Object newValue, boolean removed)- Specified by:
particleAttributeChangedin interfaceorg.miv.pherd.ParticleBoxListener
-
edgeAdded
public void edgeAdded(String graphId, long time, String edgeId, String fromNodeId, String toNodeId, boolean directed)Description copied from interface:ElementSinkAn edge was inserted in graph.- Specified by:
edgeAddedin interfaceElementSink- Parameters:
graphId- Identifier of the graph where the edge was added.edgeId- Identifier of the added edge.fromNodeId- Identifier of the first node of the edge.toNodeId- Identifier of the second node of the edge.directed- If true, the edge is directed.
-
nodeAdded
Description copied from interface:ElementSinkA node was inserted in the given graph.- Specified by:
nodeAddedin interfaceElementSink- Parameters:
graphId- Identifier of the graph where the node was added.nodeId- Identifier of the added node.
-
edgeRemoved
Description copied from interface:ElementSinkAn edge of graph was removed.The nodes the edge connects may already have been removed from the graph.- Specified by:
edgeRemovedin interfaceElementSink- Parameters:
graphId- The graph where the edge will be removed.edgeId- The edge that will be removed.
-
nodeRemoved
Description copied from interface:ElementSinkA node was removed from the graph.- Specified by:
nodeRemovedin interfaceElementSink- Parameters:
graphId- Identifier of the graph where the node will be removed.nodeId- Identifier of the removed node.
-
graphCleared
Description copied from interface:ElementSinkThe whole graph was cleared. All the nodes, edges and attributes of the graph are removed.- Specified by:
graphClearedin interfaceElementSink- Parameters:
graphId- The graph cleared.
-
stepBegins
Description copied from interface:ElementSinkSince dynamic graphs are based on discrete event modifications, the notion of step is defined to simulate elapsed time between events. So a step is a event that occurs in the graph, it does not modify it but it gives a kind of timestamp that allow the tracking of the progress of the graph over the time.
This kind of event is useful for dynamic algorithms that listen to the dynamic graph and need to measure the time in the graph's evolution.
- Specified by:
stepBeginsin interfaceElementSink- Parameters:
graphId- Identifier of the graph where the step starts.time- A numerical value that may give a timestamp to track the evolution of the graph over the time.
-
graphAttributeAdded
Description copied from interface:AttributeSinkA graph attribute was added.- Specified by:
graphAttributeAddedin interfaceAttributeSink- Parameters:
graphId- Identifier of the graph where the attribute changed.attribute- The attribute name.value- The attribute new value.
-
graphAttributeChanged
public void graphAttributeChanged(String graphId, long time, String attribute, Object oldValue, Object newValue)Description copied from interface:AttributeSinkA graph attribute was changed.- Specified by:
graphAttributeChangedin interfaceAttributeSink- Parameters:
graphId- Identifier of the graph where the attribute changed.attribute- The attribute name.oldValue- The attribute old value.newValue- The attribute new value.
-
graphAttributeRemoved
Description copied from interface:AttributeSinkA graph attribute was removed.- Specified by:
graphAttributeRemovedin interfaceAttributeSink- Parameters:
graphId- Identifier of the graph where the attribute was removed.attribute- The removed attribute name.
-
nodeAttributeAdded
public void nodeAttributeAdded(String graphId, long time, String nodeId, String attribute, Object value)Description copied from interface:AttributeSinkA node attribute was added.- Specified by:
nodeAttributeAddedin interfaceAttributeSink- Parameters:
graphId- Identifier of the graph where the change occurred.nodeId- Identifier of the node whose attribute changed.attribute- The attribute name.value- The attribute new value.
-
nodeAttributeChanged
public void nodeAttributeChanged(String graphId, long time, String nodeId, String attribute, Object oldValue, Object newValue)Description copied from interface:AttributeSinkA node attribute was changed.- Specified by:
nodeAttributeChangedin interfaceAttributeSink- Parameters:
graphId- Identifier of the graph where the change occurred.nodeId- Identifier of the node whose attribute changed.attribute- The attribute name.oldValue- The attribute old value.newValue- The attribute new value.
-
nodeAttributeRemoved
Description copied from interface:AttributeSinkA node attribute was removed.- Specified by:
nodeAttributeRemovedin interfaceAttributeSink- Parameters:
graphId- Identifier of the graph where the attribute was removed.nodeId- Identifier of the node whose attribute was removed.attribute- The removed attribute name.
-
edgeAttributeAdded
public void edgeAttributeAdded(String graphId, long time, String edgeId, String attribute, Object value)Description copied from interface:AttributeSinkA edge attribute was added.- Specified by:
edgeAttributeAddedin interfaceAttributeSink- Parameters:
graphId- Identifier of the graph where the change occurred.edgeId- Identifier of the edge whose attribute changed.attribute- The attribute name.value- The attribute new value.
-
edgeAttributeChanged
public void edgeAttributeChanged(String graphId, long time, String edgeId, String attribute, Object oldValue, Object newValue)Description copied from interface:AttributeSinkA edge attribute was changed.- Specified by:
edgeAttributeChangedin interfaceAttributeSink- Parameters:
graphId- Identifier of the graph where the change occurred.edgeId- Identifier of the edge whose attribute changed.attribute- The attribute name.oldValue- The attribute old value.newValue- The attribute new value.
-
edgeAttributeRemoved
Description copied from interface:AttributeSinkA edge attribute was removed.- Specified by:
edgeAttributeRemovedin interfaceAttributeSink- Parameters:
graphId- Identifier of the graph where the attribute was removed.edgeId- Identifier of the edge whose attribute was removed.attribute- The removed attribute name.
-
newNodeParticle
Factory method to create node particles.- Parameters:
id- The identifier of the new node/particle.- Returns:
- The new node/particle.
-