This sample is a preview of VelocityGraph, help us define the best possible graph api! The start of this api is inspired by Dex,
the sample here mimics what the dex sample is doing. Dex is considered by some to have the best performing graph api currently available. Unlike Dex, which is a C++/C implemented api,
VelocityGraph is all implemented in C# and enable any type of Element values. VelocityGraph is provided as open source on GitHub, https://github.com/VelocityDB/VelocityGraph.
Anyone is welcome to contribute! VelocityGraph is built on top of VelocityDB.
You will need a VelocityDB license to use VelocityGraph. Eventually VelocityGraph may have its own web site, http://www.VelocityGraph.com/
1: using System;
2: using System.IO;
3: using System.Linq;
4: using VelocityDb.Session;
5: using VelocityGraph;
6: using Element = System.Int64;
7: using PropertyTypeId = System.Int32;
8: using PropertyId = System.Int32;
9: using TypeId = System.Int32;
10:
11: namespace VelocityGraphSample
12: {
13: class VelocityGraphSample
14: {
15: static readonly string systemDir = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
16: "VelocityDB" + Path.DirectorySeparatorChar + "Databases" + Path.DirectorySeparatorChar + "VelocityGraphSample");
17:
18: static void Main(string[] args)
19: {
20: if (Directory.Exists(systemDir))
21: Directory.Delete(systemDir, true); // remove systemDir from prior runs and all its databases.
22: UInt64 graphId;
23: using (SessionNoServer session = new SessionNoServer(systemDir, false))
24: {
25: session.BeginUpdate();
26: Graph g = new Graph(session);
27:
28: // SCHEMA
29: // Add a node type for the movies, with a unique identifier and two indexed Propertys
30: TypeId movieType = g.NewNodeType("MOVIE");
31: TypeId movieIdType = g.NewProperty(movieType, "ID", DataType.Long, PropertyKind.Unique);
32: PropertyId movieTitleType = g.NewProperty(movieType, "TITLE", DataType.String, PropertyKind.Indexed);
33: PropertyId movieYearType = g.NewProperty(movieType, "YEAR", DataType.Integer, PropertyKind.Indexed);
34:
35: // Add a node type for the people, with a unique identifier and an indexed Property
36: TypeId peopleType = g.NewNodeType("PEOPLE");
37: PropertyId peopleIdType = g.NewProperty(peopleType, "ID", DataType.Long, PropertyKind.Unique);
38: PropertyId peopleNameType = g.NewProperty(peopleType, "NAME", DataType.String, PropertyKind.Indexed);
39:
40: // Add an undirected edge type with an Property for the cast of a movie
41: TypeId castType = g.NewEdgeType("CAST", false, false);
42: PropertyId castCharacterType = g.NewProperty(castType, "CHARACTER", DataType.String, PropertyKind.Basic);
43:
44: // Add a directed edge type restricted to go from people to movie for the director of a movie
45: TypeId directsType = g.NewRestrictedEdgeType("DIRECTS", peopleType, movieType, false);
46:
47: // DATA
48: // Add some MOVIE nodes
49:
50: Element mLostInTranslation = g.NewNode(movieType);
51: g.SetProperty(mLostInTranslation, movieIdType, (long) 1);
52: g.SetProperty(mLostInTranslation, movieTitleType, "Lost in Translation");
53: g.SetProperty(mLostInTranslation, movieYearType, (int) 2003);
54:
55: Element mVickyCB = g.NewNode(movieType);
56: g.SetProperty(mVickyCB, movieIdType, (long) 2);
57: g.SetProperty(mVickyCB, movieTitleType, "Vicky Cristina Barcelona");
58: g.SetProperty(mVickyCB, movieYearType, (int) 2008);
59:
60: Element mManhattan = g.NewNode(movieType);
61: g.SetProperty(mManhattan, movieIdType, (long) 3);
62: g.SetProperty(mManhattan, movieTitleType, "Manhattan");
63: g.SetProperty(mManhattan, movieYearType, (int) 1979);
64:
65:
66: // Add some PEOPLE nodes
67: Element pScarlett = g.NewNode(peopleType);
68: g.SetProperty(pScarlett, peopleIdType, (long) 1);
69: g.SetProperty(pScarlett, peopleNameType, "Scarlett Johansson");
70:
71: Element pBill = g.NewNode(peopleType);
72: g.SetProperty(pBill, peopleIdType, (long) 2);
73: g.SetProperty(pBill, peopleNameType, "Bill Murray");
74:
75: Element pSofia = g.NewNode(peopleType);
76: g.SetProperty(pSofia, peopleIdType, (long) 3);
77: g.SetProperty(pSofia, peopleNameType, "Sofia Coppola");
78:
79: Element pWoody = g.NewNode(peopleType);
80: g.SetProperty(pWoody, peopleIdType, (long) 4);
81: g.SetProperty(pWoody, peopleNameType, "Woody Allen");
82:
83: Element pPenelope = g.NewNode(peopleType);
84: g.SetProperty(pPenelope, peopleIdType, (long) 5);
85: g.SetProperty(pPenelope, peopleNameType, "Penélope Cruz");
86:
87: Element pDiane = g.NewNode(peopleType);
88: g.SetProperty(pDiane, peopleIdType, (long) 6);
89: g.SetProperty(pDiane, peopleNameType, "Diane Keaton");
90:
91: // Add some CAST edges
92: Element anEdge;
93: anEdge = g.NewEdge(castType, mLostInTranslation, pScarlett);
94: g.SetProperty(anEdge, castCharacterType, "Charlotte");
95:
96: anEdge = g.NewEdge(castType, mLostInTranslation, pBill);
97: g.SetProperty(anEdge, castCharacterType, "Bob Harris");
98:
99: anEdge = g.NewEdge(castType, mVickyCB, pScarlett);
100: g.SetProperty(anEdge, castCharacterType, "Cristina");
101:
102: anEdge = g.NewEdge(castType, mVickyCB, pPenelope);
103: g.SetProperty(anEdge, castCharacterType, "Maria Elena");
104:
105: anEdge = g.NewEdge(castType, mManhattan, pDiane);
106: g.SetProperty(anEdge, castCharacterType, "Mary");
107:
108: anEdge = g.NewEdge(castType, mManhattan, pWoody);
109: g.SetProperty(anEdge, castCharacterType, "Isaac");
110:
111: // Add some DIRECTS edges
112: anEdge = g.NewEdge(directsType, pSofia, mLostInTranslation);
113: anEdge = g.NewEdge(directsType, pWoody, mVickyCB);
114: anEdge = g.NewEdge(directsType, pWoody, mManhattan);
115:
116: // QUERIES
117: // Get the movies directed by Woody Allen
118: Elements directedByWoody = g.Neighbors(pWoody, directsType, EdgesDirection.Outgoing);
119:
120: // Get the cast of the movies directed by Woody Allen
121: Elements castDirectedByWoody = g.Neighbors(directedByWoody, castType, EdgesDirection.Any);
122:
123:
124: // Get the movies directed by Sofia Coppola
125: Elements directedBySofia = g.Neighbors(pSofia, directsType, EdgesDirection.Outgoing);
126:
127: // Get the cast of the movies directed by Sofia Coppola
128: Elements castDirectedBySofia = g.Neighbors(directedBySofia, castType, EdgesDirection.Any);
129:
130: // We want to know the people that acted in movies directed by Woody AND in movies directed by Sofia.
131: Elements castFromBoth = Elements.CombineIntersection(castDirectedByWoody, castDirectedBySofia);
132:
133: // Say hello to the people found
134: foreach (Element peopleOid in castFromBoth)
135: {
136: object value = g.GetProperty(peopleOid, peopleNameType);
137: System.Console.WriteLine("Hello " + value);
138: }
139: graphId = session.Persist(g);
140: session.Commit();
141: }
142:
143: using (SessionNoServer session = new SessionNoServer(systemDir, false))
144: {
145: session.BeginRead();
146: Graph g = (Graph) session.Open(graphId);
147: TypeId movieType = g.FindType("MOVIE");
148: PropertyId movieTitleProperty = g.FindProperty(movieType, "TITLE");
149: Element obj = g.FindElement(movieTitleProperty, "Manhattan");
150: session.Commit();
151: }
152: }
153: }
154: }