internal void AttachPassByConduitToClosure(Guid conduitId, ConduitClosureInfoSide incommingSide, ConduitClosureInfoSide outgoingSide, int incommingPortPosition, int outgoingPortPosition, int incommingTerminalPosition, int outgoingTerminalPosition, IRouteNetworkState routeNetworkQueryService, IConduitNetworkQueryService conduitNetworkQueryService, IConduitClosureRepository conduitClosureRepository) { // Check if multi conduit is passing by closure var conduit = conduitNetworkQueryService.GetMultiConduitInfo(conduitId); var walkOfInterest = routeNetworkQueryService.GetWalkOfInterestInfo(conduit.WalkOfInterestId); if (!walkOfInterest.AllNodeIds.Contains(_pointOfInterestId)) { throw new ArgumentException("Conduit: " + conduitId + " is not related to the point of interest: " + _pointOfInterestId + " where conduit closure: " + Id + " is placed at all."); } if (walkOfInterest.StartNodeId == _pointOfInterestId || walkOfInterest.EndNodeId == _pointOfInterestId) { throw new ArgumentException("Conduit: " + conduitId + " is ending in point of interest: " + _pointOfInterestId + " - but not pasing through it. Please use AttachConduitEndToClosure instead. The AttachPassByConduitToClosure can only be used on conduits that are passing through the point of interest (route node) where the conduit closure is placed."); } if (incommingSide == outgoingSide && incommingPortPosition == outgoingPortPosition) { throw new ArgumentException("A conduit is not allowed to enter and exit the same port on the same side."); } var conduitClosureInfo = conduitClosureRepository.GetConduitClosureInfo(Id); // Check if conduit is already attached to closure if (conduitClosureInfo.Sides.Exists(s => s.Ports.Exists(p => p.MultiConduitId == conduitId))) { throw new ArgumentException("The conduit: " + conduitId + " is already attached to the closure: " + this.Id); } if (conduitClosureInfo.Sides.Exists(s => s.Ports.Exists(p => p.Terminals.Exists(t => t.LineId == conduitId)))) { throw new ArgumentException("The conduit: " + conduitId + " is already attached to the closure: " + this.Id); } // If ports already exists on the side and outgoing port argument not set, find next available port if (incommingPortPosition == 0 && conduitClosureInfo.Sides.Find(s => s.Position == incommingSide).Ports.Count != 0) { incommingPortPosition = conduitClosureInfo.Sides.Find(s => s.Position == incommingSide).Ports.OrderByDescending(p => p.Position).First().Position + 1; } else { incommingPortPosition = 1; // No ports yet, so we put it on port 1 } // If ports already exists on the side and outgoing port argument not set, find next available port if (outgoingPortPosition == 0 && conduitClosureInfo.Sides.Find(s => s.Position == outgoingSide).Ports.Count != 0) { outgoingPortPosition = conduitClosureInfo.Sides.Find(s => s.Position == outgoingSide).Ports.OrderByDescending(p => p.Position).First().Position + 1; } else { outgoingPortPosition = 1; // No ports yet, so we put it on port 1 } // Check if port is the next in sequence. if (conduitClosureInfo.Sides.Find(s => s.Position == incommingSide).Ports.Count != 0) { if (conduitClosureInfo.Sides.Find(s => s.Position == incommingSide).Ports.OrderByDescending(p => p.Position).First().Position != (incommingPortPosition - 1)) { throw new ArgumentException("Incomming port: " + incommingPortPosition + "on side: " + incommingSide + " is not the next number in sequence. Last port number used on side: " + incommingSide + " is: " + conduitClosureInfo.Sides.Find(s => s.Position == incommingSide).Ports.OrderByDescending(p => p.Position).First().Position); } } else { if (incommingPortPosition != 1) { throw new ArgumentException("Incomming port: " + incommingPortPosition + "on side: " + incommingSide + " is not the next number in sequence. No ports currently added to side: " + incommingSide + ". Therefor port number = 1 was expected."); } } if (conduitClosureInfo.Sides.Find(s => s.Position == outgoingSide).Ports.Count != 0) { if (conduitClosureInfo.Sides.Find(s => s.Position == outgoingSide).Ports.OrderByDescending(p => p.Position).First().Position != (outgoingPortPosition - 1)) { throw new ArgumentException("Outgoing port: " + outgoingPortPosition + "on side: " + outgoingSide + " is not the next number in sequence. Last port number used on side: " + outgoingSide + " is: " + conduitClosureInfo.Sides.Find(s => s.Position == outgoingSide).Ports.OrderByDescending(p => p.Position).First().Position); } } else { if (outgoingPortPosition != 1) { throw new ArgumentException("Outgoing port: " + outgoingPortPosition + "on side: " + outgoingSide + " is not the next number in sequence. No ports currently added to side: " + outgoingSide + ". Therefor port number = 1 was expected."); } } /////////////////////////////////////////////////////////////// /// Finish checking. Now created the domain events var conduitAttached = new ConduitClosurePassingByConduitAttached() { ConduitClosureId = this.Id, ConduitId = conduitId, IncommingSide = incommingSide, OutgoingSide = outgoingSide, IncommingPortPosition = incommingPortPosition, OutgoingPortPosition = outgoingPortPosition, IncommingTerminalPosition = incommingTerminalPosition, OutgoingTerminalPosition = outgoingTerminalPosition }; RaiseEvent(conduitAttached); }
private void OnConduitClosurePassingByConduitAttached(ConduitClosureInfo conduitClosureInfo, ConduitClosurePassingByConduitAttached @event) { // Get conduit var conduit = conduitNetworkQueryService.GetConduitInfo(@event.ConduitId); if (conduit is MultiConduitInfo) { // Incomming side AttachMultiConduitToPort(conduitClosureInfo, @event.ConduitId, @event.IncommingSide, @event.IncommingSide, @event.IncommingPortPosition, @event.OutgoingSide, @event.OutgoingPortPosition); AttachInnerConduitsToTerminals(conduitClosureInfo, @event, @event.IncommingSide); // Outgoing side AttachMultiConduitToPort(conduitClosureInfo, @event.ConduitId, @event.OutgoingSide, @event.IncommingSide, @event.IncommingPortPosition, @event.OutgoingSide, @event.OutgoingPortPosition); AttachInnerConduitsToTerminals(conduitClosureInfo, @event, @event.OutgoingSide); conduitClosureRepository.UpdateConduitClosureInfo(conduitClosureInfo); } }
private void Apply(ConduitClosurePassingByConduitAttached @event) { }
private void AttachInnerConduitsToTerminals(ConduitClosureInfo conduitClosureInfo, ConduitClosurePassingByConduitAttached @event, ConduitClosureInfoSide sidePosition) { // Get side var side = conduitClosureInfo.Sides.Find(s => s.Position == sidePosition); // Get conduit end kind (to be placed on terminal) var endKind = @event.IncommingSide == sidePosition ? ConduitEndKindEnum.Incomming : ConduitEndKindEnum.Outgoing; // Get port position var portPosition = @event.IncommingSide == sidePosition ? @event.IncommingPortPosition : @event.OutgoingPortPosition; // Get port var port = side.Ports.Find(p => p.Position == portPosition); // Get multi conduit var multiConduit = conduitNetworkQueryService.GetConduitInfo(@event.ConduitId); int terminalPosition = 1; foreach (var innerConduit in multiConduit.Children) { // Get related segment info var relatedSegmentInfo = FindRelatedSegmentInfo((ConduitInfo)innerConduit, conduitClosureInfo.PointOfInterestId); // Create terminal var newTerminal = new ConduitClosureTerminalInfo() { Position = terminalPosition, ConnectionKind = ConduitClosureInternalConnectionKindEnum.NotConnected, LineId = innerConduit.Id, LineSegmentId = relatedSegmentInfo.Segment.Id, LineSegmentEndKind = endKind }; // Try find other side var otherEndTerminalInfo = FindRelatedTerminal(conduitClosureInfo, relatedSegmentInfo.Segment.Id); if (otherEndTerminalInfo != null) { newTerminal.ConnectedToSide = otherEndTerminalInfo.Side; newTerminal.ConnectedToPort = otherEndTerminalInfo.Port; newTerminal.ConnectedToTerminal = otherEndTerminalInfo.Terminal; newTerminal.ConnectionKind = otherEndTerminalInfo.ConnectionKind; // Update the other end as well var otherEndPort = conduitClosureInfo.Sides.Find(s => s.Position == otherEndTerminalInfo.Side).Ports.Find(p => p.Position == otherEndTerminalInfo.Port).Terminals.Find(t => t.Position == otherEndTerminalInfo.Terminal); otherEndPort.ConnectedToSide = sidePosition; otherEndPort.ConnectedToPort = portPosition; otherEndPort.ConnectedToTerminal = terminalPosition; otherEndPort.ConnectionKind = otherEndTerminalInfo.ConnectionKind; } port.Terminals.Add(newTerminal); terminalPosition++; } }