netkit.graph
Class Node

java.lang.Object
  extended by netkit.graph.Node
All Implemented Interfaces:
java.lang.Comparable<Node>

public final class Node
extends java.lang.Object
implements java.lang.Comparable<Node>

This class represents a node in the Graph object. It holds it's identifier name and the Attributes container for all its fields. Nodes keep track of their Edge's and Node neighbors and all the values for its Attributes.

Author:
Kaveh R. Ghazi
See Also:
Graph, Attributes, Edge, EdgeType

Constructor Summary
Node(java.lang.String name, Attributes attributes, int index)
          The constructor must be provided with a name, an Attributes container and an index.
 
Method Summary
 void addEdge(Edge newEdge)
          Adds the supplied edge to this node; the Edge must not already exist.
 void addValues()
          If the Attributes container (AKA nodeType) has been increased in size by adding one or more Attribute fields to it, this method will resize the internal values array to match.
 int compareTo(Node n)
          Specifies a natural ordering for Nodes; compare node types first, then node names.
 Node copy(int index)
          The constructor must be provided with a name, an Attributes container and an index.
 boolean equals(java.lang.Object o)
          Indicates whether some other object is "equal to" this one; Nodes are equal if they share the same name and type.
 int getAttributeIndex(java.lang.String attribute)
          Get the integer index of a single value Attribute within this container.
 Attributes getAttributes()
          Get the Attributes container detailing the attributes contained within this node.
 Edge getEdge(java.lang.String edgeTypeName, Node neighbor)
          Gets the edge connecting this node to a neighbor node.
 Edge[] getEdges()
          Get all the edges of this Node irrespective of EdgeType; the order is unspecified.
 Edge[] getEdgesByType(java.lang.String edgeTypeName)
          Get the Edges of this Node whose EdgeType name is the supplied parameter; the order is unspecified.
 Edge[] getEdgesByType(java.lang.String edgeTypeName, NodeFilter nf)
          Get the Edges of this Node whose EdgeType name is the supplied parameter and whose destination Nodes match the supplied NodeFilter; the order is unspecified.
 Edge[] getEdgesToNeighbor(Node destinationNode)
          Get all the edges of this Node whose destination Node is the supplied node; the order is unspecified.
 Edge[] getEdgesToNeighbor(java.lang.String destinationNodeType)
          Get all the edges of this Node whose destination Node is of the supplied type; the order is unspecified.
 Edge[] getEdgesToNeighbor(java.lang.String destinationNodeType, NodeFilter nf)
          Get all the edges of this Node whose destination Node is of the supplied type and which matches the supplied NodeFilter; the order is unspecified.
 int getIndex()
          Get the index of this node.
 java.lang.String getName()
          Get the name of this node.
 Node[] getNeighbors()
          Gets the adjacent nodes connected to this node irrespective of the EdgeType; the order is unspecified.
 java.util.List<Edge> getNeighbors(java.util.List<EdgeType> edgeTypePath)
          Same as getNeighbors(List,NodeFilter) except that the NodeFilter always accepts Nodes, nothing is filtered out.
 java.util.List<Edge> getNeighbors(java.util.List<EdgeType> edgeTypePath, NodeFilter nf)
          Gets a List of Edges to neighboring Nodes based on the supplied EdgeType path.
 Node[] getNeighbors(java.lang.String edgeTypeName)
          Gets the adjacent nodes connected to this node through an Edge with the supplied EdgeType name; the order is unspecified.
 java.lang.String getType()
          Get the type name of this node.
 int getUnweightedDegree()
          Get the number of outgoing Edges.
 int getUnweightedDegree(EdgeType edgeType)
          Get the number of outbound Edges of the supplied EdgeType.
 int getUnweightedDegree(java.lang.String nodeType)
          Get the number of Edges which connect to a destination Node of the supplied node type.
 double getValue(int index)
          Get the value associated with the attribute field at the supplied index.
 double getValue(java.lang.String fieldName)
          Get the value associated with the named attribute field.
 double[] getValues()
          Gets all of the values associated with this node.
 double getWeightedDegree()
          Get the sum of all weights of outgoing Edges.
 double getWeightedDegree(EdgeType edgeType)
          Get the sum of all weights of outbound Edges of the supplied EdgeType.
 double getWeightedDegree(java.lang.String nodeType)
          Get the sum of all weights of Edges which connect to a destination Node of the supplied node type.
 int hashCode()
          Returns a hash code value for this object.
 boolean isMissing(int index)
          Return whether the value associated with the attribute field at the supplied index is missing.
 boolean isMissing(java.lang.String attribute)
          Get whether the value associated with the named attribute field is "missing".
 int numEdges()
          Gets the total number of edges for this node.
 int numEdges(java.lang.String edgeTypeName)
          Gets the number of Edges to neighboring Nodes with the supplied EdgeType name.
 Edge removeEdge(java.lang.String edgeTypeName, Node destNode)
          Removes the Edge to the supplied destination Node via the supplied EdgeType name.
 void removeValue(int index)
          Removes the value from the values array at the specified index and shrinks the values array accordingly.
 void setValue(int index, double v)
          Sets the value associated with the attribute field at the supplied index.
 void setValue(java.lang.String attribute, double v)
          Sets the value associated with the named attribute field.
 void setValues(double[] v)
          Sets all of the values for this node.
 java.lang.String toString()
          Returns a String representation for this object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Node

public Node(java.lang.String name,
            Attributes attributes,
            int index)
The constructor must be provided with a name, an Attributes container and an index. The combination of the node name and the Attributes name must be unique, however Nodes with different Attributes can have the same name. The Node's values all default to "unknown" or NaN.

Parameters:
name - a string value which, in combination with the Attributes, uniquely identifies this node.
attributes - an attribute container which, in combination with the name, uniquely identifies this node.
index - an index counter providing an offset into the Graph Node array for quick lookups. Set by the Graph class when creating new Nodes and adding them to it's own containers. Note, each new Node type should get a separate zero-based index.
Method Detail

copy

public Node copy(int index)
The constructor must be provided with a name, an Attributes container and an index. The combination of the node name and the Attributes name must be unique, however Nodes with different Attributes can have the same name. The Node's values all default to "unknown" or NaN.

Parameters:
name - a string value which, in combination with the Attributes, uniquely identifies this node.
attributes - an attribute container which, in combination with the name, uniquely identifies this node.
index - an index counter providing an offset into the Graph Node array for quick lookups. Set by the Graph class when creating new Nodes and adding them to it's own containers. Note, each new Node type should get a separate zero-based index.

getName

public java.lang.String getName()
Get the name of this node.

Returns:
the name of this node.

getIndex

public int getIndex()
Get the index of this node.

Returns:
the index of this node.

getType

public java.lang.String getType()
Get the type name of this node.

Returns:
the type name of this node.

getAttributes

public Attributes getAttributes()
Get the Attributes container detailing the attributes contained within this node.

Returns:
the Attributes container detailing the attributes contained within this node.

addValues

public void addValues()
If the Attributes container (AKA nodeType) has been increased in size by adding one or more Attribute fields to it, this method will resize the internal values array to match. The new value fields will be initialized to "unknown" or NaN. If the Attributes container is smaller than the values array, this method will throw an exception.

Throws:
java.lang.RuntimeException - if the Attributes container holds less Attribute fields than the current values array size.

removeValue

public void removeValue(int index)
Removes the value from the values array at the specified index and shrinks the values array accordingly. This Node's Attributes container (AKA nodeType) must be exactly one element smaller than this Node's values array.

Parameters:
index - an integer specifying which element of the values array to remove.
Throws:
java.lang.RuntimeException - if the user attemps to remove other than exactly one element, this could happen if the Attributes container is not exactly one element smaller than the values array. Also throws if the index is out of bounds.

getAttributeIndex

public int getAttributeIndex(java.lang.String attribute)
Get the integer index of a single value Attribute within this container.

Parameters:
attribute - the attribute name to lookup in this container.
Returns:
the integer index of a single attribute within this container.
Throws:
java.lang.RuntimeException - if the attribute isn't contained within this node.

getValue

public double getValue(int index)
Get the value associated with the attribute field at the supplied index.

Parameters:
index - the integer index representing the attribute field to lookup.
Returns:
the double value associated with the attribute field at the supplied index.

isMissing

public boolean isMissing(int index)
Return whether the value associated with the attribute field at the supplied index is missing.

Parameters:
index - the integer index representing the attribute field to lookup.
Returns:
whether the value associated with the attribute field at the supplied index is missing.

getValue

public double getValue(java.lang.String fieldName)
Get the value associated with the named attribute field.

Parameters:
fieldName - the String name representing the attribute field to lookup.
Returns:
the double value associated with the named attribute field.
Throws:
java.lang.NullPointerException - if the attribute isn't contained within this node.

isMissing

public boolean isMissing(java.lang.String attribute)
Get whether the value associated with the named attribute field is "missing".

Parameters:
attribute - the String name representing the attribute field to lookup.
Returns:
the whether the value associated with the named attribute field is missing.
Throws:
java.lang.RuntimeException - if the attribute isn't contained within this node.

getValues

public double[] getValues()
Gets all of the values associated with this node.

Returns:
all of the values associated with this node.

setValue

public void setValue(int index,
                     double v)
Sets the value associated with the attribute field at the supplied index. Note you can only change the key field if it's currently unknown, i.e. NaN.

Parameters:
index - the integer index of the attribute field.
v - the double value to set.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index is not within the value array bounds.
java.lang.RuntimeException - if the index is the key index and the key field is not unknown, i.e. NaN.

setValue

public void setValue(java.lang.String attribute,
                     double v)
Sets the value associated with the named attribute field. Note you can only change the key field if it's currently unknown, i.e. NaN.

Parameters:
attribute - the name of the attribute field being set.
v - the double value to set.
Throws:
java.lang.RuntimeException - if the attribute isn't contained within this node.
java.lang.RuntimeException - if the index is the key index and the key field is not unknown, i.e. NaN.

setValues

public void setValues(double[] v)
Sets all of the values for this node. Note you can only change the key field if it's currently unknown, i.e. NaN.

Parameters:
v - an array of double to be copied into this node's values; the supplied array's size must match the number of attributes in this node (not including the key attribute.)
Throws:
java.lang.RuntimeException - if the key field differs and the old key field is not unknown, i.e. NaN.

addEdge

public void addEdge(Edge newEdge)
Adds the supplied edge to this node; the Edge must not already exist.

Parameters:
newEdge - the Edge to be added to this node.
Throws:
java.lang.RuntimeException - if the Edge already exists.
java.lang.RuntimeException - if the Edge's source Node does not equal this.

removeEdge

public Edge removeEdge(java.lang.String edgeTypeName,
                       Node destNode)
Removes the Edge to the supplied destination Node via the supplied EdgeType name. The resulting Edge must exist.

Parameters:
edgeTypeName - a String representing the EdgeType of the connection.
destNode - the destination Node for the Edge to be removed.
Returns:
a reference to the removed Edge.
Throws:
java.lang.RuntimeException - if the Edge does not exist.

getEdges

public Edge[] getEdges()
Get all the edges of this Node irrespective of EdgeType; the order is unspecified.

Returns:
all the edges of this Node irrespective of EdgeType.

getEdgesToNeighbor

public Edge[] getEdgesToNeighbor(Node destinationNode)
Get all the edges of this Node whose destination Node is the supplied node; the order is unspecified.

Parameters:
destinationNode - the destination Node.
Returns:
the edges of this Node in an array whose destination Node is the supplied node.

getEdgesToNeighbor

public Edge[] getEdgesToNeighbor(java.lang.String destinationNodeType)
Get all the edges of this Node whose destination Node is of the supplied type; the order is unspecified.

Parameters:
destinationNodeType - a String representation of the destination Node type.
Returns:
the edges of this Node in an array whose destination Node is of the supplied type.

getEdgesToNeighbor

public Edge[] getEdgesToNeighbor(java.lang.String destinationNodeType,
                                 NodeFilter nf)
Get all the edges of this Node whose destination Node is of the supplied type and which matches the supplied NodeFilter; the order is unspecified.

Parameters:
destinationNodeType - a String representation of the destination Node type.
nf - a NodeFilter to match against.
Returns:
the edges of this Node in an array whose destination Node is of the supplied type and which matches the supplied NodeFilter.

getEdgesByType

public Edge[] getEdgesByType(java.lang.String edgeTypeName)
Get the Edges of this Node whose EdgeType name is the supplied parameter; the order is unspecified.

Parameters:
edgeTypeName - a String representing the Edge's EdgeType name.
Returns:
the Edges of this Node whose EdgeType name is the supplied parameter.

getEdgesByType

public Edge[] getEdgesByType(java.lang.String edgeTypeName,
                             NodeFilter nf)
Get the Edges of this Node whose EdgeType name is the supplied parameter and whose destination Nodes match the supplied NodeFilter; the order is unspecified.

Parameters:
edgeTypeName - a String representing the Edge's EdgeType name.
Returns:
the Edges of this Node whose EdgeType name is the supplied parameter and whose destination Nodes match the supplied NodeFilter.

getUnweightedDegree

public int getUnweightedDegree()
Get the number of outgoing Edges.

Returns:
the number of outgoing Edges.

getUnweightedDegree

public int getUnweightedDegree(java.lang.String nodeType)
Get the number of Edges which connect to a destination Node of the supplied node type.

Parameters:
nodeType - the String type name of the matching destination Nodes.
Returns:
the number of Edges which connect to a destination Node of the supplied node type.

getUnweightedDegree

public int getUnweightedDegree(EdgeType edgeType)
Get the number of outbound Edges of the supplied EdgeType.

Parameters:
edgeType - an EdgeType to match against.
Returns:
the number of outbound Edges of the supplied EdgeType.

getWeightedDegree

public double getWeightedDegree()
Get the sum of all weights of outgoing Edges.

Returns:
the sum of all weights of outgoing Edges.

getWeightedDegree

public double getWeightedDegree(java.lang.String nodeType)
Get the sum of all weights of Edges which connect to a destination Node of the supplied node type.

Parameters:
nodeType - the String type name of the matching destination Nodes.
Returns:
the sum of all weights of Edges which connect to a destination Node of the supplied node type.

getWeightedDegree

public double getWeightedDegree(EdgeType edgeType)
Get the sum of all weights of outbound Edges of the supplied EdgeType.

Parameters:
edgeType - an EdgeType to match against.
Returns:
the sum of all weights of outbound Edges of the supplied EdgeType.

getNeighbors

public Node[] getNeighbors()
Gets the adjacent nodes connected to this node irrespective of the EdgeType; the order is unspecified.

Returns:
the adjacent nodes in an array irrespective of the EdgeType.

getNeighbors

public Node[] getNeighbors(java.lang.String edgeTypeName)
Gets the adjacent nodes connected to this node through an Edge with the supplied EdgeType name; the order is unspecified.

Parameters:
edgeTypeName - the name of the EdgeType connecting neighbor Nodes.
Returns:
the adjacent nodes connected to this node through an Edge with the supplied EdgeType name.

getEdge

public Edge getEdge(java.lang.String edgeTypeName,
                    Node neighbor)
Gets the edge connecting this node to a neighbor node.

Parameters:
edgeTypeName - the String name of the EdgeType to consider.
neighbor - the destination node for the connecting edge.
Returns:
the Edge connecting this node to the supplied node, or null if none exists.

numEdges

public int numEdges()
Gets the total number of edges for this node.

Returns:
the total number of edges for this node.

numEdges

public int numEdges(java.lang.String edgeTypeName)
Gets the number of Edges to neighboring Nodes with the supplied EdgeType name.

Parameters:
edgeTypeName - the EdgeType name Edges must have to be included in this count.
Returns:
the number of Edges to neighboring Nodes with the supplied EdgeType name.

hashCode

public int hashCode()
Returns a hash code value for this object.

Overrides:
hashCode in class java.lang.Object
Returns:
a hash code value for this object.

equals

public boolean equals(java.lang.Object o)
Indicates whether some other object is "equal to" this one; Nodes are equal if they share the same name and type.

Overrides:
equals in class java.lang.Object
Parameters:
o - the reference object with which to compare.
Returns:
true if this object is the same as the argument; false otherwise.

compareTo

public int compareTo(Node n)
Specifies a natural ordering for Nodes; compare node types first, then node names.

Specified by:
compareTo in interface java.lang.Comparable<Node>
Parameters:
n - the Node to compare this object to.
Returns:
a natural ordering for Nodes; compare node types first, then node names.

toString

public java.lang.String toString()
Returns a String representation for this object.

Overrides:
toString in class java.lang.Object
Returns:
a String representation for this object.

getNeighbors

public java.util.List<Edge> getNeighbors(java.util.List<EdgeType> edgeTypePath,
                                         NodeFilter nf)
Gets a List of Edges to neighboring Nodes based on the supplied EdgeType path. Neighbors are defined as Nodes that can be reached by following the supplied EdgeType path through Nodes and connecting Edges with matching respective EdgesTypes. Intervening Nodes will be visited at most once. The returned List of Edges are newly created Edges which directly connect the initial and final Nodes in the path and whose weight is calculated by multiplying the weights of the actual intervening Edges. If a particular neighbor can be reached by more than one Edge path, those paths are collapsed by adding the weights of the respective Edges. In all cases, these new Edges are not contained in the associated Graph object.

Parameters:
edgeTypePath - a List which specifies the EdgeTypes that the intervening Edges along the path must contain.
nf - a NodeFilter which is applied against the final Node (or neighbor); this Node must be accepted by the NodeFilter before the neighbor is accepted into the returned list.
Returns:
a List where each Edge is a newly created Edge that directly connects the initial and final Nodes in the path and whose weight is calculated by multiplying the weights of the actual intervening Edges.
Throws:
java.lang.RuntimeException - if the supplied EdgeType path's first element's source nodetype does not match the nodetype of this Node.

getNeighbors

public java.util.List<Edge> getNeighbors(java.util.List<EdgeType> edgeTypePath)
Same as getNeighbors(List,NodeFilter) except that the NodeFilter always accepts Nodes, nothing is filtered out.