/// <summary> /// Add an edge from this Vertex to inVertex of edge type. /// </summary> /// <param name="edgeType">The type of edge to add</param> /// <param name="head">The head of the new edge</param> /// <returns>the new edge</returns> public Edge AddEdge(EdgeType edgeType, Vertex head) { return(edgeType.NewEdge(this, head)); }
/// <summary> /// Selects all edges from or to this vertex and for the given edge type. /// </summary> /// <param name="edgeType">the id of an EdgeType</param> /// <param name="dir">direction, one of: Out, In, Both</param> /// <returns>a set of Edge</returns> public IEnumerable <IEdge> GetEdges(EdgeType edgeType, Direction dir) { return(m_vertexType.GetEdges(edgeType, this, dir)); }
/// <summary> /// Selects all neighbor Vertices from or to this vertex and for the given edge type. /// </summary> /// <param name="etype">Edge type identifier.</param> /// <param name="dir">Direction to traverse edges</param> /// <returns>Dictionary of vertex key with edge path(s) to vertex</returns> public Dictionary <Vertex, HashSet <Edge> > Traverse(EdgeType etype, Direction dir) { return(m_vertexType.Traverse(this, etype, dir)); }
internal Edge(Graph g, EdgeType eType, EdgeId eId, Vertex h, Vertex t) : base(eId, g) { edgeType = eType; tail = t; head = h; }
/// <summary> /// Gets the number of edges from or to this vertex for the given edge type and the given other Vertex. /// </summary> /// <param name="edgeType">an EdgeType</param> /// <param name="headVertex">Vertex at other end of the edge</param> /// <param name="dir">direction, one of: Out, In, Both</param> /// <returns>The number of edges.</returns> public long GetNumberOfEdges(EdgeType edgeType, Vertex headVertex, Direction dir) { return(m_vertexType.GetNumberOfEdges(edgeType, this.VertexId, headVertex.VertexId, dir)); }
/// <summary> /// Creates a new edge instance. /// The tail of the edge will be any node having the given tailV Value for the given tailAttr Property identifier, /// and the head of the edge will be any node having the given headV Value for the given headAttr Property identifier. /// </summary> /// <param name="type">Node or edge type identifier.</param> /// <param name="tailAttr">Property identifier.</param> /// <param name="tailV">Tail value</param> /// <param name="headAttr">Property identifier.</param> /// <param name="headV">Head value</param> /// <returns>Unique edge instance.</returns> public IEdge NewEdge(EdgeType edgeType, PropertyType tailAttr, object tailV, PropertyType headAttr, object headV) { return(edgeType.NewEdgeX(propertyType, tailAttr, tailV, headAttr, headV, Session)); }
/// <summary> /// Selects all neighbor Vertices from or to each of the node OID in the given collection and for the given edge type. /// </summary> /// <param name="Vertices">Vertex collection.</param> /// <param name="etype">Edge type identifier.</param> /// <param name="dir">Direction.</param> /// <returns>Dictionary of vertex keys with edges path to vertex</returns> public Dictionary <Vertex, HashSet <Edge> > Traverse(Dictionary <Vertex, Edge> vertices, EdgeType etype, Direction dir) { Dictionary <Vertex, HashSet <Edge> > result = new Dictionary <Vertex, HashSet <Edge> >(); foreach (KeyValuePair <Vertex, Edge> p in vertices) { Dictionary <Vertex, Edge> t = p.Key.Traverse(etype, dir); foreach (KeyValuePair <Vertex, Edge> p2 in t) { HashSet <Edge> edges; if (result.TryGetValue(p2.Key, out edges)) { edges.Add(p2.Value); } else { edges = new HashSet <Edge>(); edges.Add(p2.Value); result[p2.Key] = edges; } } } return(result); }
/// <summary> /// Creates a new Property. /// </summary> /// <param name="type">Node or edge type identifier.</param> /// <param name="name">Unique name for the new Property.</param> /// <param name="dt">Data type for the new Property.</param> /// <param name="kind">Property kind.</param> /// <returns>Unique Property identifier.</returns> public PropertyType NewEdgeProperty(EdgeType edgeType, string name, DataType dt, PropertyKind kind) { return(edgeType.NewProperty(name, dt, kind)); }
/// <summary> /// Creates a new edge instance. /// </summary> /// <param name="type">Edge type identifier.</param> /// <param name="tail">Source OID.</param> /// <param name="head">Target OID. </param> /// <returns>Unique OID of the new edge instance.</returns> public Edge NewEdge(EdgeType edgeType, Vertex tail, Vertex head) { return(edgeType.NewEdge(tail, head)); }
/// <summary> /// Creates a new edge type. /// </summary> /// <param name="name">Unique name for the new edge type.</param> /// <param name="biderectional">If true, this creates a biderectional edge type, otherwise this creates a unidirectional edge type.</param> /// <returns>Unique edge type.</returns> /// <param name="tailType">a fixed tail VertexType</param> /// <param name="headType">a fixed head VertexType</param> /// <returns>a new edge type</returns> public EdgeType NewEdgeType(string name, bool biderectional, VertexType tailType, VertexType headType, EdgeType baseType = null) { EdgeType aType; if (stringToEdgeType.TryGetValue(name, out aType) == false) { int pos = edgeTypeCt; Update(); Array.Resize(ref edgeType, ++edgeTypeCt); aType = new EdgeType(pos, name, tailType, headType, biderectional, baseType, this); edgeType[pos] = aType; stringToEdgeType.AddFast(name, aType); } return(aType); }
/// <summary> /// Enumerates all the edges of the given type between two given nodes (tail and head). /// </summary> /// <param name="etype">Type of Edge</param> /// <param name="tail">Outgoing Vertex</param> /// <param name="head">Incoming Vertex</param> /// <returns>Enumeration of Edge</returns> public IEnumerable <IEdge> Edges(EdgeType etype, Vertex tail, Vertex head) { VertexType vertexType = tail.VertexType; return(vertexType.GetEdges(etype, tail, Direction.Out, head)); }
/// <summary> /// Traverses graph from this Vertex to a target Vertex using Breadth-first search like in Dijkstra's algorithm /// </summary> /// <param name="toVertex">the goal Vertex</param> /// <param name="et">the type of edges to follow</param> /// <param name="maxHops">maximum number of hops between this Vertex and to Vertex</param> /// <param name="all">find or not find all paths to goal Vertex</param> /// <param name="includedVertices">one or more Vertex instances that MUST be in the path for the path to be traversed i.e. if a path does exist /// to the specified toVertex, but does not include all the instances in includedVertices set, the Traverse method will exclude that path</param> /// <param name="excludedVertices">one or more Vertex instances that MUST NOT be in the path for the path to be traversed i.e. if a path does exist /// to the specified toVertex, but does include any of the instances in includedVertices set, the Traverse method will exclude that path</param> /// <param name="includedEdges">one or more Edge instances that MUST be in the path for the path to be traversed i.e. if a path does exist /// to the specified toVertex, but does not include all the instances in includedEdges set, the Traverse method will exclude that path</param> /// <param name="excludedEdges">one or more Edge instances that MUST NOT be in the path for the path to be traversed i.e. if a path does exist /// to the specified toVertex, but does include any of the instances in includedEdges set, the Traverse method will exclude that path</param> /// <param name="includedVertexProperty">One or more Vertex property types that MUST be in the path for the path to be accepted i.e. if a path does exist /// to the specified toVertex, but does not include all of the Vertex properties in the set, the Traverse method will exclude that path</param> /// <param name="excludedVertexProperty">One or more Vertex property types that MUST NOT be in the path for the path to be accepted i.e. if a path does exist /// to the specified toVertex, but does include any of the Vertex properties in the set, the Traverse method will exclude that path</param> /// <param name="includedEdgeProperty">One or more Vertex property types that MUST be in the path for the path to be accepted i.e. if a path does exist /// to the specified toVertex, but does not include all of the Vertex properties in the set, the Traverse method will exclude that path</param> /// <param name="excludedEdgeProperty">One or more Edge property types that MUST NOT be in the path for the path to be accepted i.e. if a path does exist /// to the specified toVertex, but does include any of the Edge properties in the set, the Traverse method will exclude that path</param> /// <param name="validateVertex">A function that will be called before accepting a Vertex in path to toVertex. If function returns true then this vertex is accepted in path; otherwise vertex is rejected</param> /// <param name="validateEdge">A function that will be called before accepting an Edge in path to toVertex. If function returns true then this Edge is accepted in path; otherwise edge is rejected</param> /// <param name="validateEdges">A function that will be called before accepting a candidate Edges list in path to toVertex. If function returns true then this Edge list is accepted in path; otherwise edge list is rejected</param> /// <returns>List of paths to goal Vertex</returns> public List <List <Edge> > Traverse(Vertex toVertex, EdgeType et, int maxHops, bool all, ISet <Vertex> includedVertices = null, ISet <Vertex> excludedVertices = null, ISet <Edge> includedEdges = null, ISet <Edge> excludedEdges = null, ISet <PropertyType> includedVertexProperty = null, ISet <PropertyType> excludedVertexProperty = null, ISet <PropertyType> includedEdgeProperty = null, ISet <PropertyType> excludedEdgeProperty = null, Func <Vertex, bool> validateVertex = null, Func <Edge, bool> validateEdge = null, Func <List <Edge>, bool> validateEdges = null) { Queue <PathInfo> q = new Queue <PathInfo>(); HashSet <Vertex> visited = new HashSet <Vertex>(); HashSet <PropertyType> vertexPropertyTypesToFind = null; HashSet <PropertyType> edgePropertyTypesToFind = null; visited.Add(this); visited.Add(toVertex); // don't pass through end vertex when finding paths (only end a path with toVertex) HashSet <Edge> edgeSet; List <Edge> path = new List <Edge>(); List <List <Edge> > resultPaths = new List <List <Edge> >(); if (excludedVertexProperty != null) { foreach (PropertyType pt in excludedVertexProperty) { if (pt.HasPropertyValue(VertexId)) { return(resultPaths); } if (pt.HasPropertyValue(toVertex.VertexId)) { return(resultPaths); } } } int includedVerticesSize; if (includedVertices != null) { // these will always be included so remove from list includedVertices.Remove(this); includedVertices.Remove(toVertex); includedVerticesSize = includedVertices.Count; } else { includedVerticesSize = 0; } int includedVertexPropertySize; if (includedVertexProperty != null) { vertexPropertyTypesToFind = new HashSet <PropertyType>(includedVertexProperty); foreach (PropertyType pt in vertexPropertyTypesToFind.ToArray()) { if (pt.HasPropertyValue(VertexId)) { vertexPropertyTypesToFind.Remove(pt); } else if (pt.HasPropertyValue(toVertex.VertexId)) { vertexPropertyTypesToFind.Remove(pt); } } includedVertexPropertySize = vertexPropertyTypesToFind.Count; } else { includedVertexPropertySize = 0; } int includedEdgePropertySize; if (includedEdgeProperty != null) { edgePropertyTypesToFind = new HashSet <PropertyType>(includedEdgeProperty); includedEdgePropertySize = edgePropertyTypesToFind.Count; } else { includedEdgePropertySize = 0; } if (excludedVertices != null) { visited.UnionWith(excludedVertices); } PathInfo pathInfo = new PathInfo(this, path); q.Enqueue(pathInfo); while (q.Count > 0) { pathInfo = q.Dequeue(); Dictionary <Vertex, HashSet <Edge> > friends = pathInfo.node.Traverse(et, Direction.Out); if (friends.TryGetValue(toVertex, out edgeSet)) { foreach (Edge edge in edgeSet) { if ((excludedEdges == null || excludedEdges.Contains(edge) == false) && (validateEdge == null || validateEdge(edge))) { //Console.WriteLine(this + " and " + toVertex + " have a friendship link"); List <Edge> edgePath = pathInfo.edgePath; edgePath.Add(edge); if (validateEdges == null || validateEdges(edgePath)) { bool foundVerticesToInclude = includedVerticesSize == 0; if (includedVerticesSize > 0) { HashSet <Vertex> verticesToFind = new HashSet <Vertex>(includedVertices); foreach (Edge edgeInPath in edgePath) { if (verticesToFind.Contains(edgeInPath.Tail)) { verticesToFind.Remove(edgeInPath.Tail); if (verticesToFind.Count == 0) { break; } } } foundVerticesToInclude = verticesToFind.Count == 0; } bool foundVertexPropertyTypesToInclude = includedVertexPropertySize == 0; if (includedVertexPropertySize > 0) { foreach (Edge edgeInPath in edgePath) { foreach (PropertyType pt in vertexPropertyTypesToFind.ToArray()) { if (pt.HasPropertyValue(edgeInPath.Tail.VertexId)) { vertexPropertyTypesToFind.Remove(pt); } } if (vertexPropertyTypesToFind.Count == 0) { break; } } foundVertexPropertyTypesToInclude = vertexPropertyTypesToFind.Count == 0; } bool foundEdgePropertyTypesToInclude = includedEdgePropertySize == 0; if (includedEdgePropertySize > 0) { foreach (Edge edgeInPath in edgePath) { foreach (PropertyType pt in edgePropertyTypesToFind.ToArray()) { if (pt.HasPropertyValue(edgeInPath.EdgeId)) { edgePropertyTypesToFind.Remove(pt); } } if (edgePropertyTypesToFind.Count == 0) { break; } } foundEdgePropertyTypesToInclude = edgePropertyTypesToFind.Count == 0; } if (foundVerticesToInclude && foundVertexPropertyTypesToInclude && foundEdgePropertyTypesToInclude) { if (includedEdges == null || includedEdges.IsSubsetOf(edgePath)) { resultPaths.Add(edgePath); if (!all) { return(resultPaths); } } } } } } } if (pathInfo.edgePath.Count < maxHops) { foreach (KeyValuePair <Vertex, HashSet <Edge> > v in friends) { if (visited.Contains(v.Key) == false) { foreach (Edge edge in v.Value) { if (excludedEdges == null || excludedEdges.Contains(edge) == false) { bool doExclude = false; if (excludedVertexProperty != null) { foreach (PropertyType pt in excludedVertexProperty) { if (pt.HasPropertyValue(v.Key.VertexId)) { visited.Add(v.Key); doExclude = true; break; } } } if (excludedEdgeProperty != null) { foreach (PropertyType pt in excludedEdgeProperty) { if (pt.HasPropertyValue(edge.EdgeId)) { doExclude = true; break; } } } if (!doExclude) { visited.Add(v.Key); path = new List <Edge>(pathInfo.edgePath); path.Add(edge); PathInfo newPath = new PathInfo(v.Key, path); if (validateEdges == null || validateEdges(path)) { if (validateVertex == null || validateVertex(v.Key)) { q.Enqueue(newPath); } } } } } } } } } //if (all && resultPaths.Count == 0) // Console.WriteLine(this + " and " + toVertex + " may not be connected by indirect frienship"); return(resultPaths); }
/// <summary> /// Gets the number of edges from or to this vertex and for the given edge type. /// </summary> /// <param name="edgeType">an EdgeType</param> /// <param name="dir">direction, one of: Out, In, Both</param> /// <returns>The number of edges.</returns> public long GetNumberOfEdges(EdgeType edgeType, Direction dir) { return(vertexType.GetNumberOfEdges(edgeType, this.VertexId, dir)); }
/// <summary> /// Selects all neighbor Vertices from or to this vertex and for the given edge type. /// </summary> /// <param name="etype">Edge type identifier.</param> /// <param name="dir">Direction</param> /// <returns>Dictionary of vertex key with edge path to vertex</returns> public Dictionary <Vertex, Edge> Traverse(EdgeType etype, Direction dir) { return(vertexType.Traverse(Graph, this, etype, dir)); }