Exemplo n.º 1
0
        private static String GetPrettyPrintedPath(PathGenerics <Road, City, WeightDeterminedByRoadLengthAndQuality> path)
        {
            // Note that the code you now are looking at corresponds to client code, i.e. code you could have written yourself.
            // The Path interface is included in the library, but not WeightDeterminedByRoadLengthAndQuality
            // which should be thought of as your own defined type (subtype of Weight in the library).
            // Still the path will return it strongly typed below thanks to usage of Generics.
            // It should also be noted that reflection is NOT used for instantiating the object with the total weight
            // but instead the create method of the Weight interface will be used for creating the instance
            // from within the class PathFinderBase.
            WeightDeterminedByRoadLengthAndQuality totalWeightForPath = path.TotalWeightForPath;

            Console.WriteLine("totalWeightForPath " + totalWeightForPath);
            Console.WriteLine("totalWeightForPath.getLengthInMeters() " + totalWeightForPath.GetLengthInMeters());
            Console.WriteLine("totalWeightForPath.getLengthInKiloMeters() " + totalWeightForPath.GetLengthInKiloMeters());
            // The reason that the above code works is that a prototypical instance of WeightDeterminedByRoadLengthAndQuality
            // will be used for creating an instance of itself.

            IList <Road>  roadsForPath = path.EdgesForPath;
            StringBuilder sb           = new StringBuilder();

            sb.Append("Total weight: " + path.TotalWeightForPath.WeightValue + " | ");
            for (int i = 0; i < roadsForPath.Count; i++)
            {
                Road road           = roadsForPath[i];
                City cityFrom       = road.GetCityFrom();
                City cityFromVertex = road.StartVertex;
                Console.WriteLine("cityFrom getCityName : " + cityFrom.CityName);
                if (i > 0)
                {
                    sb.Append(" ---> ");
                }
                sb.Append("[" + road.RoadName + "](" + road.GetEdgeWeight().WeightValue + ")");
            }
            return(sb.ToString());
        }
Exemplo n.º 2
0
        public P CreatePath(W totalWeight, IList <E> edges)
        {
            PathGenerics <E, V, W> path = PathGenericsImpl <E, V, W> .CreatePathGenerics <E, V, W>(totalWeight, edges);

            P p = (P)path;

            return(p);
        }