using com.sparsity.sparksee.gdb; namespace HelloSparksee { class Program { static void Main(string[] args) { // // Create a sample database // SparkseeConfig cfg = new SparkseeConfig(); Sparksee sparksee = new Sparksee(cfg); Database db = sparksee.Create("HelloSparksee.gdb", "HelloSparksee"); Session sess = db.NewSession(); Graph g = sess.GetGraph(); // // SCHEMA // // Add a node type for the movies, with a unique identifier and two indexed attributes int movieType = g.NewNodeType("MOVIE"); int movieIdType = g.NewAttribute(movieType, "ID", DataType.Long, AttributeKind.Unique); int movieTitleType = g.NewAttribute(movieType, "TITLE", DataType.String, AttributeKind.Indexed); int movieYearType = g.NewAttribute(movieType, "YEAR", DataType.Integer, AttributeKind.Indexed); // Add a node type for the people, with a unique identifier and an indexed attribute int peopleType = g.NewNodeType("PEOPLE"); int peopleIdType = g.NewAttribute(peopleType, "ID", DataType.Long, AttributeKind.Unique); int peopleNameType = g.NewAttribute(peopleType, "NAME", DataType.String, AttributeKind.Indexed); // Add an undirected edge type with an attribute for the cast of a movie int castType = g.NewEdgeType("CAST", false, false); int castCharacterType = g.NewAttribute(castType, "CHARACTER", DataType.String, AttributeKind.Basic); // Add a directed edge type restricted to go from people to movie for the director of a movie int directsType = g.NewRestrictedEdgeType("DIRECTS", peopleType, movieType, false); // // DATA // // Add some MOVIE nodes Value value = new Value(); long mLostInTranslation = g.NewNode(movieType); g.SetAttribute(mLostInTranslation, movieIdType, value.SetLong(1)); g.SetAttribute(mLostInTranslation, movieTitleType, value.SetString("Lost in Translation")); g.SetAttribute(mLostInTranslation, movieYearType, value.SetInteger(2003)); long mVickyCB = g.NewNode(movieType); g.SetAttribute(mVickyCB, movieIdType, value.SetLong(2)); g.SetAttribute(mVickyCB, movieTitleType, value.SetString("Vicky Cristina Barcelona")); g.SetAttribute(mVickyCB, movieYearType, value.SetInteger(2008)); long mManhattan = g.NewNode(movieType); g.SetAttribute(mManhattan, movieIdType, value.SetLong(3)); g.SetAttribute(mManhattan, movieTitleType, value.SetString("Manhattan")); g.SetAttribute(mManhattan, movieYearType, value.SetInteger(1979)); // Add some PEOPLE nodes long pScarlett = g.NewNode(peopleType); g.SetAttribute(pScarlett, peopleIdType, value.SetLong(1)); g.SetAttribute(pScarlett, peopleNameType, value.SetString("Scarlett Johansson")); long pBill = g.NewNode(peopleType); g.SetAttribute(pBill, peopleIdType, value.SetLong(2)); g.SetAttribute(pBill, peopleNameType, value.SetString("Bill Murray")); long pSofia = g.NewNode(peopleType); g.SetAttribute(pSofia, peopleIdType, value.SetLong(3)); g.SetAttribute(pSofia, peopleNameType, value.SetString("Sofia Coppola")); long pWoody = g.NewNode(peopleType); g.SetAttribute(pWoody, peopleIdType, value.SetLong(4)); g.SetAttribute(pWoody, peopleNameType, value.SetString("Woody Allen")); long pPenelope = g.NewNode(peopleType); g.SetAttribute(pPenelope, peopleIdType, value.SetLong(5)); g.SetAttribute(pPenelope, peopleNameType, value.SetString("Penélope Cruz")); long pDiane = g.NewNode(peopleType); g.SetAttribute(pDiane, peopleIdType, value.SetLong(6)); g.SetAttribute(pDiane, peopleNameType, value.SetString("Diane Keaton")); // Add some CAST edges long anEdge; anEdge = g.NewEdge(castType, mLostInTranslation, pScarlett); g.SetAttribute(anEdge, castCharacterType, value.SetString("Charlotte")); anEdge = g.NewEdge(castType, mLostInTranslation, pBill); g.SetAttribute(anEdge, castCharacterType, value.SetString("Bob Harris")); anEdge = g.NewEdge(castType, mVickyCB, pScarlett); g.SetAttribute(anEdge, castCharacterType, value.SetString("Cristina")); anEdge = g.NewEdge(castType, mVickyCB, pPenelope); g.SetAttribute(anEdge, castCharacterType, value.SetString("Maria Elena")); anEdge = g.NewEdge(castType, mManhattan, pDiane); g.SetAttribute(anEdge, castCharacterType, value.SetString("Mary")); anEdge = g.NewEdge(castType, mManhattan, pWoody); g.SetAttribute(anEdge, castCharacterType, value.SetString("Isaac")); // Add some DIRECTS edges anEdge = g.NewEdge(directsType, pSofia, mLostInTranslation); anEdge = g.NewEdge(directsType, pWoody, mVickyCB); anEdge = g.NewEdge(directsType, pWoody, mManhattan); // // QUERIES // // Get the movies directed by Woody Allen Objects directedByWoody = g.Neighbors(pWoody, directsType, EdgesDirection.Outgoing); // Get the cast of the movies directed by Woody Allen Objects castDirectedByWoody = g.Neighbors(directedByWoody, castType, EdgesDirection.Any); // We don't need the directedByWoody collection anymore, so we should close it directedByWoody.Close(); // Get the movies directed by Sofia Coppola Objects directedBySofia = g.Neighbors(pSofia, directsType, EdgesDirection.Outgoing); // Get the cast of the movies directed by Sofia Coppola Objects castDirectedBySofia = g.Neighbors(directedBySofia, castType, EdgesDirection.Any); // We don't need the directedBySofia collection anymore, so we should close it directedBySofia.Close(); // We want to know the people that acted in movies directed by Woody AND in movies directed by Sofia. Objects castFromBoth = Objects.CombineIntersection(castDirectedByWoody, castDirectedBySofia); // We don't need the other collections anymore castDirectedByWoody.Close(); castDirectedBySofia.Close(); // Say hello to the people found ObjectsIterator it = castFromBoth.Iterator(); while (it.HasNext()) { long peopleOid = it.Next(); g.GetAttribute(peopleOid, peopleNameType, value); System.Console.WriteLine("Hello " + value.GetString()); } // The ObjectsIterator must be closed it.Close(); // The Objects must be closed castFromBoth.Close(); // // Close the database // sess.Close(); db.Close(); sparksee.Close(); } } }