Getting Started with VelocityGraph (Friday, June 21, 2019)

This guide compliments the sample programs, the User’s Guide and the API reference provided on our site.  Developers should review this in order to better understand how to quickly build a VelocityGraph application.

We made a getting started video as well. To see it click here.

1)  Getting assemblies

The simplest way to get started is by using the nuget package.

You can download the entire product package setup from the VelocityGraph website. After you download it, run it to install it on your PC.

2)Register as User, Create & Download license database (4.odb)

·         Register as user

·         Create Trial License

·         Download License

3)Add required using Statements

As a minimum you will need the following using statements:

using VelocityDb.Session;

using VelocityGraph;

4)Choose a database directory and start an update transaction

class QuickStartVelocityGraph

{

  static readonly string systemDir = "QuickStartVelocityGraph"; // appended to SessionBase.BaseDatabasePath

 

  static void CreateGraph()

  {

    using (SessionNoServer session = new SessionNoServer(systemDir))

    {

      if (Directory.Exists(session.SystemDirectory))

        Directory.Delete(session.SystemDirectory, true); // remove systemDir from prior runs and all its databases.

      // Start an update transaction

      session.BeginUpdate();

5)Create a Graph

Graph g = new Graph(session);

1.     Persist the graph – give it a persistent id

session.Persist(g);

2.     Define some Vertex, Edge and Property types

VertexType movieType = g.NewVertexType("Movie");

PropertyType movieTitleType = g.NewVertexProperty(movieType, "title", DataType.String, PropertyKind.Indexed);

PropertyType movieYearType = g.NewVertexProperty(movieType, "year", DataType.Integer, PropertyKind.Indexed);

 

VertexType actorType = g.NewVertexType("Actor");

PropertyType actorNameType = g.NewVertexProperty(actorType, "name", DataType.String, PropertyKind.Indexed);

 

EdgeType castType = g.NewEdgeType("ACTS_IN", false);

PropertyType castCharacterType = g.NewEdgeProperty(castType, "role", DataType.String, PropertyKind.Indexed);

3.     Add some vertices (nodes)

// Add some Movies

Vertex matrix1 = movieType.NewVertex();

matrix1.SetProperty(movieTitleType, "The Matrix");

matrix1.SetProperty(movieYearType, (int)1999);

 

Vertex matrix2 = movieType.NewVertex();

matrix2.SetProperty(movieTitleType, "The Matrix Reloaded");

matrix2.SetProperty(movieYearType, (int)2003);

 

Vertex matrix3 = movieType.NewVertex();

matrix3.SetProperty(movieTitleType, "The Matrix  Revolutions");

matrix3.SetProperty(movieYearType, (int)2003);

 

// Add some Actors

Vertex keanu = actorType.NewVertex();

keanu.SetProperty(actorNameType, "Keanu Reeves");

 

Vertex laurence = actorType.NewVertex();

laurence.SetProperty(actorNameType, "Laurence Fishburne");

 

Vertex carrieanne = actorType.NewVertex();

carrieanne.SetProperty(actorNameType, "Carrie-Anne Moss");

4.     Add some edges (relations)

Edge keanuAsNeo = castType.NewEdge(keanu, matrix1);

keanuAsNeo.SetProperty(castCharacterType, "Neo");

keanuAsNeo = castType.NewEdge(keanu, matrix2);

keanuAsNeo.SetProperty(castCharacterType, "Neo");

keanuAsNeo = castType.NewEdge(keanu, matrix3);

keanuAsNeo.SetProperty(castCharacterType, "Neo");

 

Edge laurenceAsMorpheus = castType.NewEdge(laurence, matrix1);

laurenceAsMorpheus.SetProperty(castCharacterType, "Morpheus");

laurenceAsMorpheus = castType.NewEdge(laurence, matrix2);

laurenceAsMorpheus.SetProperty(castCharacterType, "Morpheus");

laurenceAsMorpheus = castType.NewEdge(laurence, matrix3);

laurenceAsMorpheus.SetProperty(castCharacterType, "Morpheus");

 

Edge carrieanneAsTrinity = castType.NewEdge(carrieanne, matrix1);

carrieanneAsTrinity.SetProperty(castCharacterType, "Trinity");

carrieanneAsTrinity = castType.NewEdge(carrieanne, matrix2);

carrieanneAsTrinity.SetProperty(castCharacterType, "Trinity");

carrieanneAsTrinity = castType.NewEdge(carrieanne, matrix3);

carrieanneAsTrinity.SetProperty(castCharacterType, "Trinity");

6)Commit the transaction

session.Commit();

7)Query the graph

static void QueryGraph()

{

  using (SessionNoServer session = new SessionNoServer(systemDir))

  {

    // Start a read only transaction

    session.BeginRead();

    Graph g = Graph.Open(session);

    // Cache SCHEMA

    VertexType movieType = g.FindVertexType("Movie");

    PropertyType movieTitleType = movieType.FindProperty("title");

    VertexType actorType = g.FindVertexType("Actor");

    PropertyType actorNameType = actorType.FindProperty("name");

 

    // How many vertices do we have?

    Console.WriteLine("Number of Vertices: " + g.CountVertices());

 

    // Find a movie by name

    Vertex movie = movieTitleType.GetPropertyVertex("The Matrix");

 

    // Get all actors

    var actors = actorType.GetVertices();

 

    // Count the actors

    int actorCount = actors.Count();

    Console.WriteLine("Number of Actors: " + actorCount);

 

    // Get only the actors whose names end with “s”

    foreach (Vertex vertex in actors)

    {

      string actorName = (string) actorNameType.GetPropertyValue(vertex.VertexId);

      if (actorName.EndsWith("s"))

        Console.WriteLine("Found actor with name ending with \"s\" " + actorName);

    }

 

    // All vertices and their edges

    var edges = g.GetEdges();

    int edgeCount = edges.Count();

 

    session.Commit();

  }

}

For more examples, download our setup installer and take a look at our many sample projects here or in your installed VelocityDB (see %USERPROFILE%\My Documents\VelocityDB\VelocityDB.sln)