Esempio n. 1
0
 public static VehicleData GetVehicle( Api api, UserData user, RoutingProblemData problem )
 {
     TestData.CreateDemoData( problem, api );
     var vehicles = api.Navigate<VehicleDataSet>( problem.GetLink( "list-vehicles" ) );
     var vehicleLink = vehicles.Items.Find( v => v.Id == 1 );
     var vehicle = api.Navigate<VehicleData>(vehicleLink.GetLink("self"));
     return vehicle;
 }
Esempio n. 2
0
 internal static UserData GetOrCreateUser( Api api )
 {
     var users = api.Navigate<UserDataSet>( api.Root.GetLink( "list-users" ) );
     var userExists = users.Items.Exists( u => u.Id == 1 );
     UserData user = null;
     if ( !userExists )
     {
         var createdUser = api.Navigate( api.Root.GetLink( "create-user" ), new UserData() );
         user = api.Navigate<UserData>( createdUser.Location );
     }
     else
     {
         var entityLink = users.Items.Find(u => u.Id == 1);
         user = api.Navigate<UserData>( entityLink.GetLink( "self" ) );
     }            
     return user;
 }
Esempio n. 3
0
        public void Cleanup()
        {
            string url = ConfigurationManager.AppSettings["url"];
            string clientKey = ConfigurationManager.AppSettings["client-key"];
            string clientSecret = ConfigurationManager.AppSettings["client-secret"];
            string responsePath = ConfigurationManager.AppSettings["response-path"];

            var api = new Api( url, clientKey, clientSecret );
            //##BEGIN EXAMPLE oauth##
            var tokenResponse = api.Authenticate();
            //##END EXAMPLE##
            var rootLinks = api.Root;

            var users = api.Navigate<UserDataSet>( rootLinks.GetLink( "list-users" ) );

            foreach ( var user in users.Items )
            {
                var u = api.Navigate<UserData>( user.GetLink( "self" ) );
                api.Navigate<ResponseData>( u.GetLink( "delete-user" ) );
            }

            ResponseWriter.WriteAll(responsePath, responsePath+"/nfleet-responses/nfleet-responses.txt", ".dat");
        }
Esempio n. 4
0
        internal static TaskData GetTask( Api api, RoutingProblemData problem )
        {
            var tasks = api.Navigate<TaskDataSet>( problem.GetLink( "list-tasks" ) );
            var newTask = new TaskUpdateRequest { Name = "test name", RelocationType = "None", ActivityState = "Active" };
            var capacity = new CapacityData { Name = "Weight", Amount = 20 };

            var pickup = new TaskEventUpdateRequest
            {
                Type = "Pickup",
                Location = new LocationData
                {
                    Coordinate = new CoordinateData
                    {
                        Latitude = 62.244958,
                        Longitude = 25.747143,
                        System = "Euclidian"
                    }
                },
                TimeWindows = { new TimeWindowData { Start = new DateTime( 2013, 5, 14, 8, 0, 0 ), End = new DateTime( 2013, 5, 14, 12, 0, 0 ) } }
            };
            pickup.Capacities.Add( capacity );
            newTask.TaskEvents.Add( pickup );

            var delivery = new TaskEventUpdateRequest
            {
                Type = "Delivery",
                Location = new LocationData
                {
                    Coordinate = new CoordinateData
                    {
                        Latitude = 62.244589,
                        Longitude = 25.74892,
                        System = "Euclidian"
                    }
                },
                TimeWindows = { new TimeWindowData { Start = new DateTime( 2013, 5, 14, 8, 0, 0 ), End = new DateTime( 2013, 5, 14, 12, 0, 0 ) } }
            };
            delivery.Capacities.Add( capacity );
            newTask.TaskEvents.Add( delivery );

            var taskCreationResult = api.Navigate<ResponseData>( problem.GetLink( "create-task" ), newTask );
            var task = api.Navigate<TaskData>( taskCreationResult.Location );
            return task;
        }
Esempio n. 5
0
 public static RoutingProblemData CreateProblem( Api api, UserData user, string problemName="test" )
 {
     var created = api.Navigate(user.GetLink("create-problem"), new RoutingProblemUpdateRequest { Name = problemName });
     var problem = api.Navigate<RoutingProblemData>( created.Location );
     return problem;
 }
Esempio n. 6
0
        private static void Run()
        {


            var api1 = new Api( url, clientKey, clientSecret );

            var tokenResponse = api1.Authenticate();

            var apiData = api1.Root;
            // create a new instance of Api and reuse previously received token
            var api2 = new Api( url, clientKey, clientSecret );

            tokenResponse = api2.Authorize( tokenResponse );
            var createdUser = api2.Navigate( apiData.GetLink( "create-user" ), new UserData() );
            var user = api2.Navigate<UserData>( createdUser.Location );
            var problems = api2.Navigate<RoutingProblemDataSet>( user.GetLink( "list-problems" ) );
            var created = api2.Navigate( user.GetLink( "create-problem" ), new RoutingProblemUpdateRequest { Name = "test" } );
            var problem = api2.Navigate<RoutingProblemData>( created.Location );
            
            CreateDemoData( problem, api2 );

            // refresh to get up to date set of operations
            problem = api2.Navigate<RoutingProblemData>( problem.GetLink( "self" ) );

            var res = api2.Navigate<ResponseData>( problem.GetLink( "toggle-optimization" ), new RoutingProblemUpdateRequest { Name = problem.Name, State = "Running" } );
            RoutingProblemData rb = null;
            while ( true )
            {
                Thread.Sleep( 1000 );
                rb = api2.Navigate<RoutingProblemData>( problem.GetLink( "self" ) );
                Console.WriteLine( "State: {0}", rb.State );
                if ( rb.State == "Running" || rb.Progress == 100 ) break;
            }

            while ( true )
            {
                Thread.Sleep( 1000 );

                int start = 0;
                int end = 1;
                var routingProblem = api2.Navigate<RoutingProblemData>( problem.GetLink( "self" ) );
                var queryParameters = new Dictionary<string, string>
                                              {
                                                  {"Start", start.ToString() },
                                                  {"End", end.ToString() }
                                              };
                var objectiveValues = api2.Navigate<ObjectiveValueDataSet>( problem.GetLink( "objective-values" ), queryParameters );



                Console.WriteLine( routingProblem.State + " (" + routingProblem.Progress + "%)" );
                Console.WriteLine( "---------------" );
                foreach ( var obj in objectiveValues.Items )
                {

                    Console.WriteLine( "Objective values from {0} to {1}: [{2}] {3}", start, end, obj.TimeStamp, obj.Value );
                }
                Console.WriteLine( "---------------" );


                if ( routingProblem.State == "Stopped" )
                {
                    var resultVehicles = api2.Navigate<VehicleDataSet>( routingProblem.GetLink( "list-vehicles" ) );
                    var resultTasks = api2.Navigate<TaskDataSet>( routingProblem.GetLink( "list-tasks" ) );

                    foreach ( var vehicleData in resultVehicles.Items )
                    {
                        var veh = api2.Navigate<VehicleData>( vehicleData.GetLink( "self" ) );
                        Console.Write( "Vehicle {0}({1}): ", vehicleData.Id, vehicleData.Name );
                        var routeEvents = api2.Navigate<RouteEventDataSet>( veh.GetLink( "list-events" ) );
                        var sequence = api2.Navigate<RouteData>( veh.GetLink( "get-route" ) );

                        sequence.Items.Insert( 0, veh.StartLocation.Id );
                        sequence.Items.Add( veh.EndLocation.Id );

                        for ( int i = 0; i < routeEvents.Items.Count; i++ )
                        {
                            var point = sequence.Items[i];
                            var routeEvent = routeEvents.Items[i];

                            Console.WriteLine( "{0}: {1}-{2} ", point, routeEvent.ArrivalTime, routeEvent.DepartureTime );
                        }
                        Console.WriteLine();
                    }
                    break;
                }
            }
        }
Esempio n. 7
0
        private static void CreateDemoData( RoutingProblemData problem, Api api )
        {
            // To build a test case we first need to create a vehicle.
            // We start by defining vehicle capacity.
            var vehicleCapacities = new List<CapacityData> {new CapacityData {Name = "Weight", Amount = 100000}};
            // ...the time window(s)
            var vehicleTimeWindow = new List<TimeWindowData> { new TimeWindowData { Start = new DateTime( 2013, 5, 14, 7, 0, 0 ), End = new DateTime( 2013, 5, 14, 16, 0, 0 ) }};
            // ... the locations for pickup and delivery
            var vehiclePickup = new LocationData {Coordinate = new CoordinateData { Latitude = 62.244588, Longitude = 25.742683, System = "WGS84" }};
            var vehicleDelivery = new LocationData {Coordinate = new CoordinateData { Latitude = 62.244588, Longitude = 25.742683, System = "WGS84" }};
            // And then we wrap these into a single vehicle update request.
            var vehicleUpdateRequest = new VehicleUpdateRequest
                                           {
                                               Name = "Vehicle1",
                                               Capacities = vehicleCapacities,
                                               StartLocation = vehiclePickup,
                                               EndLocation = vehicleDelivery,
                                               TimeWindows = vehicleTimeWindow,
                                               RelocationType = "None"
                                           };


            api.Navigate<ResponseData>( problem.GetLink( "create-vehicle" ), vehicleUpdateRequest );
            // This should have created a vehicle.

            // Next, we will create a pickup and delivery task.
            // The task will consist of two task events that are the pickup and delivery.
            // Define the capacity, location and time window for the pickup.
            var capacity = new CapacityData { Name = "Weight", Amount = 1 };
            var task1PickupLocation = new LocationData
                                          {
                                              Coordinate = new CoordinateData
                                                               {
                                                                   Latitude = 62.247906,
                                                                   Longitude = 25.867395,
                                                                   System = "WGS84"
                                                               }
                                          };
            var task1PickupTimeWindows = new List<TimeWindowData> { new TimeWindowData { Start = new DateTime(2013, 5, 14, 7, 0, 0), End = new DateTime(2013, 5, 14, 16, 0, 0) } };
            //... and wrap it in a task event update request.
            var pickup = new TaskEventUpdateRequest
            {
                Type = "Pickup",
                Location = task1PickupLocation,
                TimeWindows = task1PickupTimeWindows,
                Capacities = new List<CapacityData> {capacity}
            };


            // Then we do the same for the delivery.
            var task1DeliveryLocation = new LocationData
                                            {
                                                Coordinate = new CoordinateData
                                                                 {
                                                                     Latitude = 61.386909,
                                                                     Longitude = 24.654106,
                                                                     System = "WGS84"
                                                                 }
                                            };
            var task1DeliveryTimeWindows = new List<TimeWindowData> { new TimeWindowData { Start = new DateTime(2013, 5, 14, 7, 0, 0), End = new DateTime(2013, 5, 14, 16, 0, 0) } };

            var delivery = new TaskEventUpdateRequest
            {
                Type = "Delivery",
                Location = task1DeliveryLocation,
                TimeWindows = task1DeliveryTimeWindows,
                Capacities = new List<CapacityData> { capacity }
            };


            // And finally we contain the pickup and delivery in a task update request and send it.
            var newTask = new TaskUpdateRequest {Name = "Task1", RelocationType = "None"};
            newTask.TaskEvents.Add( pickup );
            newTask.TaskEvents.Add( delivery );

            api.Navigate<ResponseData>( problem.GetLink( "create-task" ), newTask );

            // And this is how we can create optimization cases.
        }
Esempio n. 8
0
        public static void CreateDemoData(RoutingProblemData problem, Api api)
        {
            api.Navigate<ResponseData>(problem.GetLink("create-vehicle"), new VehicleUpdateRequest
            {
                Name = "TestVehicle-" + GenerateRandomString(8),
                Capacities = new List<CapacityData>
                {
                    new CapacityData { Name = "Weight", Amount = 5000 }
                },
                StartLocation = new LocationData
                {
                    Coordinate = new CoordinateData
                    {
                        Latitude = 62.244958,
                        Longitude = 25.747143,
                        System = "Euclidian"
                    }
                },
                EndLocation = new LocationData
                {
                    Coordinate = new CoordinateData
                    {
                        Latitude = 62.244958,
                        Longitude = 25.747143,
                        System = "Euclidian"
                    }
                },
                TimeWindows = { new TimeWindowData { Start = new DateTime(2013, 5, 14, 8, 0, 0), End = new DateTime(2013, 5, 14, 12, 0, 0) } },
                RelocationType = "None",
            });

            var newTask = new TaskUpdateRequest { Name = "task", RelocationType = "None", ActivityState = "Active" };
            var capacity = new CapacityData { Name = "Weight", Amount = 20 };

            var pickup = new TaskEventUpdateRequest
            {
                Type = "Pickup",
                Location = new LocationData
                {
                    Coordinate = new CoordinateData
                    {
                        Latitude = 62.282617,
                        Longitude = 25.797272,
                        System = "Euclidian"
                    }
                },
                TimeWindows = { new TimeWindowData { Start = new DateTime(2013, 5, 14, 8, 0, 0), End = new DateTime(2013, 5, 14, 12, 0, 0) } }
            };
            pickup.Capacities.Add(capacity);
            newTask.TaskEvents.Add(pickup);

            var delivery = new TaskEventUpdateRequest
            {
                Type = "Delivery",
                Location = new LocationData
                {
                    Coordinate = new CoordinateData
                    {
                        Latitude = 62.373658,
                        Longitude = 25.885506,
                        System = "Euclidian"
                    }
                },
                TimeWindows = { new TimeWindowData { Start = new DateTime(2013, 5, 14, 8, 0, 0), End = new DateTime(2013, 5, 14, 12, 0, 0) } }
            };
            delivery.Capacities.Add(capacity);
            newTask.TaskEvents.Add(delivery);

            api.Navigate<ResponseData>(problem.GetLink("create-task"), newTask);
        }