コード例 #1
0
        // Queries
        public void doQueries()
        {
            using (SessionNoServer session = new SessionNoServer(s_systemDir))
            {
                session.BeginRead();
                Graph        g                       = Graph.Open(session); // it takes a while to open graph fresh from databases
                VertexType   userType                = g.FindVertexType("User");
                EdgeType     friendEdgeType          = g.FindEdgeType("Friend");
                PropertyType countryProperty         = userType.FindProperty("country");
                PropertyType incomeProperty          = userType.FindProperty("income");
                PropertyType friendshipStartProperty = friendEdgeType.FindProperty("start");

                Vertex someUser  = userType.GetVertex(1);
                Vertex someUser2 = userType.GetVertex(12282);

                var someUserFriends = from Edge e in someUser.GetEdges(friendEdgeType, Direction.Out)
                                      select e.Head;
                var            someUser3LevelNetwork = someUser.Traverse(3, true, Direction.Out);
                HashSet <Edge> edges = new HashSet <Edge>();
                Edge           edge  = (Edge)someUser.GetEdges(friendEdgeType, Direction.Out).Skip(1).First();
                edges.Add(edge);
                HashSet <EdgeType> edgeTypesToTraverse = new HashSet <EdgeType>();
                edgeTypesToTraverse.Add(friendEdgeType);
                // Find Shortest path between two given user ids
                List <List <Edge> > path = someUser.Traverse(4, false, Direction.Out, someUser2, edgeTypesToTraverse);
                Debug.Assert(path.Count > 0);
                var path2 = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(41), edgeTypesToTraverse);
                HashSet <Vertex> vertices = new HashSet <Vertex>();
                vertices.Add(someUser2);
                var path3  = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(9465810), edgeTypesToTraverse, null, null, vertices);                // path must include vertices
                var path3b = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(9465810), edgeTypesToTraverse, null, null, null, vertices);          // path must NOT include vertices
                var path3c = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(9465810), edgeTypesToTraverse, null, null, null, null, edges);       // path must include edges
                var path3d = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(9465810), edgeTypesToTraverse, null, null, null, null, null, edges); // path must NOT include edges
                HashSet <PropertyType> vertexPropertyTypes = new HashSet <PropertyType>();
                vertexPropertyTypes.Add(incomeProperty);
                vertexPropertyTypes.Add(countryProperty);
                var path3e = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(9465810), edgeTypesToTraverse, null, null, null, null, null, edges, vertexPropertyTypes);       // path must NOT include edges and at least one vertex in path must have property in propertyTypes
                var path3f = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(9465810), edgeTypesToTraverse, null, null, null, null, null, edges, null, vertexPropertyTypes); // path must NOT include edges and no vertex in path must have any property in propertyTypes
                HashSet <PropertyType> edgePropertyTypes = new HashSet <PropertyType>();
                edgePropertyTypes.Add(friendshipStartProperty);
                var path3g = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(9465810), edgeTypesToTraverse, null, null, null, null, null, edges, null, null, edgePropertyTypes);
                var path3h = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(9465810), edgeTypesToTraverse, null, null, null, null, null, edges, null, null, null, edgePropertyTypes);
                var path3i = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(9465810), edgeTypesToTraverse, null, null, null, null, null, edges, null, null, null, null, p => { object pv = p.GetProperty(countryProperty); return(pv != null && pv.Equals("Sweden")); });
                var path3j = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(9465810), edgeTypesToTraverse, null, null, null, null, null, edges, null, null, null, null, null, p => { DateTime?pv = (DateTime?)p.GetProperty(friendshipStartProperty); return(pv != null && pv.Value.CompareTo(DateTime.Now) > 0); });
                var path4  = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(2798), edgeTypesToTraverse);
                var path5  = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(175), edgeTypesToTraverse);
                var path6  = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(1531), edgeTypesToTraverse);
                var path7  = someUser.Traverse(4, false, Direction.Out, userType.GetVertex(1537), edgeTypesToTraverse);
                Console.WriteLine();

                // Get all the paths between two user ids
                var path8 = someUser.Traverse(4, true, Direction.Out, someUser2, edgeTypesToTraverse);
                path = someUser.Traverse(4, true, Direction.Out, userType.GetVertex(41), edgeTypesToTraverse);

                // Get the number of unique 2nd level friends a given user has (friends of my friends)

                var someUsers2ndLevelFriends = (from v in someUserFriends
                                                from Edge e in v.GetEdges(friendEdgeType, Direction.Out)
                                                select e.Head).Distinct();

                Console.WriteLine("unique 2nd level friends a given user has");
                int ct = 0;
                foreach (Vertex v in someUsers2ndLevelFriends)
                {
                    if (++ct % 100 == 0)
                    {
                        Console.WriteLine("User id: " + v.VertexId);
                    }
                }
                Console.WriteLine("Some user has: " + ct + " 2nd level friends");
                Console.WriteLine();

                // Get the top 10 users with most friends
                var top10mostFriends = from vertex in userType.GetTopNumberOfEdges(friendEdgeType, 10, Direction.Out)
                                       select vertex;
                Console.WriteLine("top 10 users with most friends");
                foreach (Vertex v in top10mostFriends)
                {
                    long count = v.GetNumberOfEdges(friendEdgeType, Direction.Out);
                    Console.WriteLine("User id: " + v.VertexId + "\t number of friends: " + count);
                }
                Console.WriteLine();
                session.Commit();
            }
        }