public void AddDisconnectedSegment(SpanEquipment spanEquipment, UInt16 structureIndex, UInt16 segmentIndex) { var spanSegment = spanEquipment.SpanStructures[structureIndex].SpanSegments[segmentIndex]; var disconnectedGraphSegment = new UtilityGraphDisconnectedSegment(spanEquipment.Id, structureIndex, segmentIndex); if (!_graphElementsById.TryAdd(spanSegment.Id, disconnectedGraphSegment)) { throw new ArgumentException($"A span segment with id: {spanSegment.Id} already exists in the graph."); } }
public void ApplySegmentCut(SpanEquipment spanEquipment, SpanSegmentCutInfo spanSegmentCutInfo) { var version = _objectManager.GetLatestCommitedVersion(); var trans = _objectManager.CreateTransaction(); try { if (!_graphElementsById.TryGetValue(spanSegmentCutInfo.OldSpanSegmentId, out var oldSegmentGraphElement)) { throw new ApplicationException($"Cannot find span segment graph element with id: {spanSegmentCutInfo.OldSpanSegmentId} processing segment cut."); } if (!spanEquipment.TryGetSpanSegment(spanSegmentCutInfo.NewSpanSegmentId1, out var segment1withIndexInfo)) { throw new ApplicationException($"Cannot find span segment with id: {spanSegmentCutInfo.OldSpanSegmentId} in span equipment: {spanEquipment.Id} "); } if (!spanEquipment.TryGetSpanSegment(spanSegmentCutInfo.NewSpanSegmentId2, out var segment2withIndexInfo)) { throw new ApplicationException($"Cannot find span segment with id: {spanSegmentCutInfo.OldSpanSegmentId} in span equipment: {spanEquipment.Id} "); } // We need to go through all new span segments in the structure, and check if we need update the graph element due to segment index shift for (int newSegmentIndex = 0; newSegmentIndex < spanEquipment.SpanStructures[segment1withIndexInfo.StructureIndex].SpanSegments.Length; newSegmentIndex++) { var newSegment = spanEquipment.SpanStructures[segment1withIndexInfo.StructureIndex].SpanSegments[newSegmentIndex]; if (TryGetGraphElement <IUtilityGraphSegmentRef>(newSegment.Id, out var existingUtilityGraphSegmentRef)) { if (existingUtilityGraphSegmentRef.SegmentIndex != newSegmentIndex) { var newUtilityGraphElement = existingUtilityGraphSegmentRef.CreateWithNewSegmentIndex((ushort)newSegmentIndex); if (newUtilityGraphElement is UtilityGraphConnectedSegment) { trans.Update((UtilityGraphConnectedSegment)newUtilityGraphElement); } UpdateDict(newSegment.Id, newUtilityGraphElement); } } } // If existing segment is disconnected, it's not in the graph either if (oldSegmentGraphElement is UtilityGraphDisconnectedSegment) { AddDisconnectedSegment(spanEquipment, segment1withIndexInfo.StructureIndex, segment1withIndexInfo.SegmentIndex); AddDisconnectedSegment(spanEquipment, segment2withIndexInfo.StructureIndex, segment2withIndexInfo.SegmentIndex); RemoveFromDict(spanSegmentCutInfo.OldSpanSegmentId); } else { var oldConnectedGraphElement = (UtilityGraphConnectedSegment)oldSegmentGraphElement; // Create first/left segment if (oldConnectedGraphElement.InV(version) != null) { var newSegment1GraphElement = new UtilityGraphConnectedSegment(spanSegmentCutInfo.NewSpanSegmentId1, (UtilityGraphConnectedTerminal)oldConnectedGraphElement.InV(version), null, spanEquipment.Id, segment1withIndexInfo.StructureIndex, segment1withIndexInfo.SegmentIndex); trans.Add(newSegment1GraphElement); AddToDict(newSegment1GraphElement.Id, newSegment1GraphElement); } // If InV is null, then we end with a disconnected segment else { var newSegment1DisconnectedGraphElement = new UtilityGraphDisconnectedSegment(spanEquipment.Id, segment1withIndexInfo.StructureIndex, segment1withIndexInfo.SegmentIndex); AddToDict(spanSegmentCutInfo.NewSpanSegmentId1, newSegment1DisconnectedGraphElement); } // Create second/right segment if (oldConnectedGraphElement.OutV(version) != null) { var newSegment2GraphElement = new UtilityGraphConnectedSegment(spanSegmentCutInfo.NewSpanSegmentId2, null, (UtilityGraphConnectedTerminal)oldConnectedGraphElement.OutV(version), spanEquipment.Id, segment2withIndexInfo.StructureIndex, segment2withIndexInfo.SegmentIndex); trans.Add(newSegment2GraphElement); AddToDict(newSegment2GraphElement.Id, newSegment2GraphElement); } // If OutV is null, then we end with a disconnected segment else { var newSegment2DisconnectedGraphElement = new UtilityGraphDisconnectedSegment(spanEquipment.Id, segment2withIndexInfo.StructureIndex, segment2withIndexInfo.SegmentIndex); AddToDict(spanSegmentCutInfo.NewSpanSegmentId2, newSegment2DisconnectedGraphElement); } // Remove old segment trans.Delete(oldConnectedGraphElement.Id); RemoveFromDict(oldConnectedGraphElement.Id); } } finally { trans.Commit(); } }