/// <summary> /// Find the node at the end of edge edgeId, that has node nodeId at the other end /// Returns null if nothing found /// </summary> /// <param name="nodeId">node at one end of edge edgeId</param> /// <param name="edgeId"></param> /// <returns>node at the end of edge edgeId, that has node nodeId at the other end</returns> public virtual CdmNode FindNodeAtEndOfEdge(string nodeId, string edgeId) { CdmEdge e = _edges.Find(c => (c.EdgeId == edgeId)); if (e != null) { if (nodeId == e.NodeSourceId) { // return opposite end return(FindNodeById(e.NodeTargetId)); } else if (nodeId == e.NodeTargetId) { // return opposite end return(FindNodeById(e.NodeSourceId)); } else { return(null); } } else { return(null); } }
/// <summary> /// Adding an edge to a Btc graph can do the following if the edge already exists: /// 1) not add the new edge, instead upgrade existing edge to type mixed ( == input and output) /// 2) not add the new edge, instead update the existing edge's edge counts if any are zero (means unknown) /// </summary> public override void AddEdge(CdmEdge e) { #region Data Quality Checks CdmEdgeBtc ebNew = e as CdmEdgeBtc; if (ebNew == null) { Msg.LogError("CdmGraphBtc.AddEdge cannot cast edge to CdmEdgeBtc"); return; } // Check edge has been handed to us in correct format, that is Transaction in the source slot CdmNode n = FindNodeAtEndOfEdge(ebNew.NodeTargetId, ebNew.EdgeId); if (n != null) { // Above may seem a bit lax, but we dont know order things will be added to the graph, maybe node is not known yet if (n.NodeType != NodeType.Tx) { Msg.LogError("CdmGraphBtc.AddEdge all edges must have a transaction as their source"); return; } } #endregion CdmEdgeBtc ebExisting = FindEdgeByNodeSourceAndTarget(ebNew.NodeSourceId, ebNew.NodeTargetId) as CdmEdgeBtc; if (ebExisting == null) { // New edge, just add to list _edges.Add(ebNew); } else { // Edge exists in graph already, upgrade it to type mixed if needed MergeExistingEdge(ebNew, ebExisting); } }
/// <summary> /// If the CdmPool can fulfill the request, return the graph fragment required, return null otherwise /// </summary> /// <param name="r">The request we want to fulfill, which is a node and a range from-to of edge numbers</param> /// <param name="isPerfectMatchRequired">When true, the pool must perfectly match request, when false will return whatever pool has, even if incomplete</param> /// <returns>Graph fragment that fully or partially fulfills request, or null</returns> private CdmGraph CanCdmPoolFulfillRequest(CdmRequest r, bool isPerfectMatchRequired) { if (r == null) { return(null); } CdmNode n = CdmPool.FindNodeById(r.NodeId); if (n == null) { return(null); } int edgesAskedForCount = r.EdgeCountTo - r.EdgeCountFrom + 1; int edgesFoundCount = 0; int endOfEdgeNodesAskedForCount = edgesAskedForCount; int endOfEdgeNodesFoundCount = 0; // Add requested node to graph fragment we will send CdmGraph gFragment = CreateNewGraph(_adaptorSelector.GetChosenAdaptor().GetFamily()); gFragment.AddNode(n); // Get requisite edges for (int i = r.EdgeCountFrom; i <= r.EdgeCountTo; i++) { // This searches both ways around, sender and receiver CdmEdge e = CdmPool.FindEdgeByNodeAndNumber(n.NodeId, i); if (e != null) { // Store the edge edgesFoundCount++; gFragment.AddEdge(e); // Store the node at the end of the edge CdmNode eoen = CdmPool.FindNodeAtEndOfEdge(n.NodeId, e.EdgeId); gFragment.AddNode(eoen); endOfEdgeNodesFoundCount++; } } if (isPerfectMatchRequired) { if (edgesFoundCount >= edgesAskedForCount && endOfEdgeNodesFoundCount >= endOfEdgeNodesAskedForCount) { return(gFragment); } else { return(null); } } else { return(gFragment); } }
public virtual void AddEdge(CdmEdge e) { // Only add if not already there CdmEdge existing = FindEdgeById(e.EdgeId); if (existing == null) { _edges.Add(e); } }
public virtual void GetNodesAtEitherEndofEdge(string edgeId, out CdmNode n1, out CdmNode n2) { CdmEdge e = _edges.Find(c => (c.EdgeId == edgeId)); if (e != null) { n1 = FindNodeById(e.NodeTargetId); n2 = FindNodeById(e.NodeSourceId); } else { n1 = null; n2 = null; } }
public virtual CdmEdge FindEdgeByNodeSourceAndTarget(string nodeIdSource, string nodeIdTarget) { // Edge can be stored in the Cdm either way around CdmEdge e1 = _edges.Find(c => (c.NodeSourceId == nodeIdSource && c.NodeTargetId == nodeIdTarget)); if (e1 != null) { return(e1); } else { CdmEdge e2 = _edges.Find(c => (c.NodeTargetId == nodeIdSource && c.NodeSourceId == nodeIdTarget)); if (e2 != null) { return(e2); } else { return(null); } } }
public virtual CdmEdge FindEdgeByNodeAndNumber(string nodeId, int edgeNumber) { // Edge can be stored in the Cdm either way around CdmEdge e1 = _edges.Find(c => (c.NodeSourceId == nodeId && c.EdgeNumberInSource == edgeNumber)); if (e1 != null) { return(e1); } else { CdmEdge e2 = _edges.Find(c => (c.NodeTargetId == nodeId && c.EdgeNumberInTarget == edgeNumber)); if (e2 != null) { return(e2); } else { return(null); } } }