Graph Creation

In this section you will learn how to create an empty Graph and a VisualGraphView instance. Together these classes lay the basic foundations of the Tensegrity Graph Framework.

Graph Concepts

A graph is a container entity consisting of nodes and edges. A node (or vertex) is part of a set of vertices of the graph. Additionally, a graph consists of a set of edges, which describe how the nodes (or vertices) are connected to each other.

A graph is formally defined as:

  • A set of vertices (nodes [1]) V.

  • A set of edges E.

  • Each edge is a pair (V1,V2), where V1 and V2 denote distinct vertices that are both elements of V.

All elements of V and E must be unique as required by sets in the mathematical sense.

The Graph class in the Tensegrity Graph API represents this mathematical data and is often referred to as the “model.” This non-visual component can be used on its own for applying graph algorithms and deriving specialized graph classes. Most notably, we distinguish between directed and undirected graphs. In a directed graph, all edges have a direction or orientation, while an undirected graph has edges with no orientation. Both directed and undirected graphs are represented by classes which implement the Graph interface.

Example 3.1. Creating a Graph instance

// get an instance of the factory
graphModelFactory= GraphModelFactory.newInstance();

// instantiate a graph object
graphModel= graphModelFactory.newGraph();

In the example above, it is important to note that a new Graph instance is created through a method call to a factory object. This technique decouples clients from code that decides which component class should be instantiated, providing a higher level of indirection, reuse and maintainability. Also note that exception handling has been ignored as in most examples in this tutorial. Under normal programming circumstances, exception handling must be part of your client code.

VisualGraphView Concepts

As the name implies, a VisualGraphView defines a “view” for a particular Graph instance to which it is always connected. In other words, a VisualGraphView is always coupled to a model which contains the connectivity and non-visual attribute data.

The VisualGraphView is also a container (see base class VisualGraphObjectContainer) and consists of the visual counterparts contained in a Graph. As a result, it manages instances of classes VisualNode and VisualEdge, each of which visualizes a Node and an Edge from the Graph model respectively. Although these visual elements exist, it is not always necessary to create them explicitly. Depending on the type of GraphConroller you are using (see MVC chapter for more details), visual elements may be created automatically for you in a view when adding elements to a connected Graph model.

Note

A VisualGraphView contains additional data which is required for its representation in a view, including coordinate data, drawing style and formats. Remember, a Graph simply manages the node and edge instances without understanding how these elements should be rendered and positioned in dimensional space.

Example 3.2. Creating a VisualGraphView instance

// Get a factory instance
GraphViewFactory viewfactory= GraphViewFactory.newInstance();

// Create a new visual graph view using a graph controller
graphView= viewfactory.newVisualGraphView(controller);
graphView.enableUndoRedo();

// Add the view to managed list of views
controller.addVisualGraph(graphView);

In the example above, it is important to note that a new VisualGraphView instance is created through a method call to a factory object. This technique is used when creating most if not all of the graph elements (model, view and controller) in this framework.

Also, it is important to note that a new VisualGraphView instance requires a reference to a GraphController which must be advised to manage the newly created view after instantiation. GraphController classes are discussed in more detail in the chapter entitled MVC.



[1] In the Tensegrity Graph API, a vertex is consistently called a “node” and a graph is usually referred to as the “model”.