Exemple #1
0
        public virtual IRouteGraphBuilder Add(IStatement statement)
        {
            if (statement == null)
            {
                throw new ArgumentNullException(nameof(statement));
            }

            switch (statement.Statement)
            {
            case Statement.From:
                _root        = _factory.Create(statement);
                _currentNode = _root;
                break;

            case Statement.Process:
            case Statement.To:
                if (_currentNode == null)
                {
                    throw new DromedaryRouteGraphBuilderException("From node was not add", statement);
                }

                var node = _factory.Create(statement);
                _currentNode.Add(node);
                _currentNode = node;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(this);
        }
        public void Add_Should_Work_When_FromOnly()
        {
            var from      = Substitute.For <IRouteNode>();
            var statement = Substitute.For <IStatement>();

            statement.Statement.Returns(Statement.From);

            _factory.Create(statement)
            .Returns(from);

            var graph = _builder
                        .Add(statement)
                        .Build();

            graph.Should().NotBeNull();
            graph.Root.Should().NotBeNull();
            graph.Root.Should().Be(from);

            _factory
            .Received(1)
            .Create(statement);
        }
        private List <INotification> HandleExistingRouteSegmentSplitted(int intersectingSegmentsCount, int intersectingNodesCount, Point point, RouteSegment routeSegment)
        {
            var notifications = new List <INotification>();

            if (intersectingSegmentsCount == 1 && intersectingNodesCount == 0)
            {
                var node = _routeNodeFactory.Create(point);

                var routeSegmentSplitted = CreateExistingRouteSegmentSplitted(routeSegment, node, true);
                notifications.Add(routeSegmentSplitted);
            }

            return(notifications);
        }
Exemple #4
0
        public async Task Handle(RouteSegmentConnectivityChanged request, CancellationToken token)
        {
            _logger.LogInformation($"Starting {nameof(RouteSegmentConnectivityChangedHandler)}");

            var startNode = (await _geoDatabase.GetIntersectingStartRouteNodes(request.After)).FirstOrDefault();
            var endNode   = (await _geoDatabase.GetIntersectingEndRouteNodes(request.After)).FirstOrDefault();

            if ((!(startNode is null) && !(endNode is null)) && startNode.Mrid == endNode.Mrid)
            {
                _logger.LogWarning($"Reverting RouteSegment with mrid '{request.After.Mrid}', because of both ends intersecting with the same RouteNode with mrid '{startNode.Mrid}'");
                await _geoDatabase.UpdateRouteSegment(request.Before);

                return;
            }

            var routeNetworkEvents = new List <RouteNetworkEvent>();

            if (startNode is null)
            {
                startNode = _routeNodeFactory.Create(request.After.FindStartPoint());
                var insertRouteNodeEvent = await InsertRouteNode(startNode);

                routeNetworkEvents.Add(insertRouteNodeEvent);
            }
            else if (_applicationSettings.EnableSegmentEndsAutoSnappingToRouteNode)
            {
                var lineString = request.After.GetLineString();
                lineString.Coordinates[0] = new Coordinate(startNode.GetPoint().Coordinate);
                request.After.Coord       = lineString.AsBinary();
                await _geoDatabase.UpdateRouteSegment(request.After);
            }

            if (endNode is null)
            {
                endNode = _routeNodeFactory.Create(request.After.FindEndPoint());
                var insertRouteNodeEvent = await InsertRouteNode(endNode);

                routeNetworkEvents.Add(insertRouteNodeEvent);
            }
            else if (_applicationSettings.EnableSegmentEndsAutoSnappingToRouteNode)
            {
                var lineString = request.After.GetLineString();
                lineString.Coordinates[lineString.Coordinates.Count() - 1] = new Coordinate(endNode.GetPoint().Coordinate);
                request.After.Coord = lineString.AsBinary();
                await _geoDatabase.UpdateRouteSegment(request.After);
            }

            var(routeSegmentClone, routeSegmentAddedEvent) = await InsertRouteSegmentClone(request.After);

            routeNetworkEvents.Add(routeSegmentAddedEvent);

            var routeSegmentMarkedForDeletionEvent = await MarkRouteSegmentForDeletion(request.Before);

            routeNetworkEvents.Add(routeSegmentMarkedForDeletionEvent);

            var beforeStartNode             = (await _geoDatabase.GetIntersectingStartRouteNodes(request.Before)).FirstOrDefault();
            var beforeEndNode               = (await _geoDatabase.GetIntersectingEndRouteNodes(request.Before)).FirstOrDefault();
            var isBeforeStartNodeDeleteable = await IsRouteNodeDeleteable(beforeStartNode);

            var isBeforeEndNodeDeletable = await IsRouteNodeDeleteable(beforeEndNode);

            if (isBeforeStartNodeDeleteable)
            {
                var routeNodeMarkedForDeletionEvent = await MarkDeleteRouteNode(beforeStartNode);

                routeNetworkEvents.Add(routeNodeMarkedForDeletionEvent);
            }

            if (isBeforeEndNodeDeletable)
            {
                var routeNodeMarkedForDeletionEvent = await MarkDeleteRouteNode(beforeEndNode);

                routeNetworkEvents.Add(routeNodeMarkedForDeletionEvent);
            }

            var cmdId = Guid.NewGuid();
            var routeSegmentConnectivityChangedEvent = new RouteNetworkCommand(nameof(RouteSegmentConnectivityChanged), cmdId, routeNetworkEvents.ToArray());

            _eventStore.Insert(routeSegmentConnectivityChangedEvent);
        }
        public async Task Handle(NewRouteSegmentDigitized request, CancellationToken token)
        {
            _logger.LogInformation($"Starting {nameof(NewRouteSegmentDigitizedHandler)}");

            if (request.RouteSegment is null)
            {
                throw new ArgumentNullException($"{nameof(RouteSegment)} cannot be null.");
            }

            var routeSegment = request.RouteSegment;
            var startNode    = (await _geoDatabase.GetIntersectingStartRouteNodes(routeSegment)).FirstOrDefault();
            var endNode      = (await _geoDatabase.GetIntersectingEndRouteNodes(routeSegment)).FirstOrDefault();

            if ((!(startNode is null) && !(endNode is null)) && startNode.Mrid == endNode.Mrid)
            {
                _logger.LogWarning($"Deleting RouteSegment with mrid '{routeSegment.Mrid}', because of both ends intersecting with the same RouteNode with mrid '{startNode.Mrid}'");
                await _geoDatabase.DeleteRouteSegment(routeSegment.Mrid);

                return;
            }

            var routeNetworkEvents = new List <RouteNetworkEvent>();

            if (startNode is null)
            {
                var startPoint = routeSegment.FindStartPoint();
                startNode              = _routeNodeFactory.Create(startPoint);
                startNode.Username     = routeSegment.Username;
                startNode.WorkTaskMrid = routeSegment.WorkTaskMrid;

                await _geoDatabase.InsertRouteNode(startNode);

                var startRouteNodeAddedEvent = _routeNodeEventFactory.CreateAdded(startNode);
                routeNetworkEvents.Add(startRouteNodeAddedEvent);
            }
            else if (_applicationSettings.EnableSegmentEndsAutoSnappingToRouteNode)
            {
                var lineString = routeSegment.GetLineString();
                lineString.Coordinates[0] = new Coordinate(startNode.GetPoint().Coordinate);
                routeSegment.Coord        = lineString.AsBinary();
                await _geoDatabase.UpdateRouteSegment(routeSegment);
            }

            if (endNode is null)
            {
                var endPoint = routeSegment.FindEndPoint();
                endNode              = _routeNodeFactory.Create(endPoint);
                endNode.Username     = routeSegment.Username;
                endNode.WorkTaskMrid = routeSegment.WorkTaskMrid;

                await _geoDatabase.InsertRouteNode(endNode);

                var endRouteNodeAddedEvent = _routeNodeEventFactory.CreateAdded(endNode);
                routeNetworkEvents.Add(endRouteNodeAddedEvent);
            }
            else if (_applicationSettings.EnableSegmentEndsAutoSnappingToRouteNode)
            {
                var lineString = routeSegment.GetLineString();
                lineString.Coordinates[lineString.Coordinates.Count() - 1] = new Coordinate(endNode.GetPoint().Coordinate);
                routeSegment.Coord = lineString.AsBinary();
                await _geoDatabase.UpdateRouteSegment(routeSegment);
            }

            var routeSegmentAddedEvent = _routeSegmentEventFactory.CreateAdded(routeSegment, startNode, endNode);

            routeNetworkEvents.Add(routeSegmentAddedEvent);

            var cmdId = Guid.NewGuid();
            var newRouteSegmentDigitizedCommand = new RouteNetworkCommand(nameof(NewRouteSegmentDigitized), cmdId, routeNetworkEvents.ToArray());

            _eventStore.Insert(newRouteSegmentDigitizedCommand);
        }