예제 #1
0
        public static int Execute(CostOfTheRouteFilter filter)
        {
            int total  = 0;
            var before = filter.nodes.First();

            foreach (var node in filter.nodes.Skip(1))
            {
                int cost;
                if (filter.cheapestCostOfTheRoute)
                {
                    using (IShortestPathAlgorithm alg = AlgorithmFactory.ShortestPathAlgorithm(filter.graph))
                    {
                        cost = alg.CheapestCost(before, node);
                    }
                }
                else
                {
                    var pathToNode = before.Routes.FirstOrDefault(path => path.End.Name.Equals(node.Name));

                    if (pathToNode == null)
                    {
                        return(int.MaxValue);
                    }

                    cost = pathToNode.Cost;
                }

                total += cost;
                before = node;
            }

            return(total);
        }
        public string CostOfTheRoute(params Node[] nodes)
        {
            var filter = new CostOfTheRouteFilter(this._graph, nodes, this._cheapestCostOfTheRoute);
            var total  = GetCostOfTheRoute.Execute(filter);

            return(total == int.MaxValue ? "NO SUCH ROUTE" : total.ToString());
        }