Skip to content

Commit

Permalink
Paths for all derived locations added.
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjoy-sust committed Oct 13, 2017
1 parent 8d0257e commit 6d13779
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 39 deletions.
2 changes: 1 addition & 1 deletion sample/AllPathFind/10-error/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"containerSize": 20,
"durationFrom": 20,
"durationTo": 250,
"costFrom": 3000,/*cost not match*/
"costFrom": 3000,
"costTo": 4000
}
44 changes: 42 additions & 2 deletions sample/AllPathFind/2-success/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"route": [
{
"from": "Coxs Bazar",
"from": "Cox's Bazar",
"to": "Comilla",
"transportType": "Road",
"cost": 85,
Expand All @@ -23,7 +23,7 @@
{
"route": [
{
"from": "Coxs Bazar",
"from": "Cox's Bazar",
"to": "Chittagong",
"transportType": "Road",
"cost": 50,
Expand All @@ -39,6 +39,46 @@
],
"totalCost": 115,
"totalDuration": 2
},
{
"route": [
{
"from": "Cox's Bazar",
"to": "Comilla",
"transportType": "Road",
"cost": 85,
"duration": 1
},
{
"from": "Comilla",
"to": "Mirpur",
"transportType": "Road",
"cost": 32,
"duration": 1
}
],
"totalCost": 117,
"totalDuration": 2
},
{
"route": [
{
"from": "Cox's Bazar",
"to": "Chittagong",
"transportType": "Road",
"cost": 50,
"duration": 1
},
{
"from": "Chittagong",
"to": "Mirpur",
"transportType": "Road",
"cost": 62,
"duration": 1
}
],
"totalCost": 112,
"totalDuration": 2
}
]
}
44 changes: 42 additions & 2 deletions sample/AllPathFind/3-success/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"route": [
{
"from": "Coxs Bazar",
"from": "Cox's Bazar",
"to": "Comilla",
"transportType": "Road",
"cost": 85,
Expand All @@ -23,7 +23,7 @@
{
"route": [
{
"from": "Coxs Bazar",
"from": "Cox's Bazar",
"to": "Chittagong",
"transportType": "Road",
"cost": 50,
Expand All @@ -39,6 +39,46 @@
],
"totalCost": 115,
"totalDuration": 2
},
{
"route": [
{
"from": "Cox's Bazar",
"to": "Comilla",
"transportType": "Road",
"cost": 85,
"duration": 1
},
{
"from": "Comilla",
"to": "Mirpur",
"transportType": "Road",
"cost": 32,
"duration": 1
}
],
"totalCost": 117,
"totalDuration": 2
},
{
"route": [
{
"from": "Cox's Bazar",
"to": "Chittagong",
"transportType": "Road",
"cost": 50,
"duration": 1
},
{
"from": "Chittagong",
"to": "Mirpur",
"transportType": "Road",
"cost": 62,
"duration": 1
}
],
"totalCost": 112,
"totalDuration": 2
}
]
}
4 changes: 3 additions & 1 deletion sample/AllPathFind/5-error/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
"destination": "Uttara",
"modeOfTransports": ["Road"],
"containerSize": 40
}
}

/*This locations comes some place in india*/
44 changes: 42 additions & 2 deletions sample/AllPathFind/6-success/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"route": [
{
"from": "Coxs Bazar",
"from": "Cox's Bazar",
"to": "Comilla",
"transportType": "Road",
"cost": 115,
Expand All @@ -23,7 +23,7 @@
{
"route": [
{
"from": "Coxs Bazar",
"from": "Cox's Bazar",
"to": "Chittagong",
"transportType": "Road",
"cost": 80,
Expand All @@ -39,6 +39,46 @@
],
"totalCost": 172,
"totalDuration": 2
},
{
"route": [
{
"from": "Cox's Bazar",
"to": "Comilla",
"transportType": "Road",
"cost": 115,
"duration": 1
},
{
"from": "Comilla",
"to": "Mohakhali",
"transportType": "Road",
"cost": 65,
"duration": 1
}
],
"totalCost": 180,
"totalDuration": 2
},
{
"route": [
{
"from": "Cox's Bazar",
"to": "Chittagong",
"transportType": "Road",
"cost": 80,
"duration": 1
},
{
"from": "Chittagong",
"to": "Mohakhali",
"transportType": "Road",
"cost": 92,
"duration": 1
}
],
"totalCost": 172,
"totalDuration": 2
}
]
}
52 changes: 21 additions & 31 deletions src/main/java/com/fm/assignment/core/service/PathServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
* This class use for path related task.
Expand Down Expand Up @@ -91,18 +92,24 @@ public List<ResultParam> getAllPaths(FindPathParam param) throws ResourceNotFoun
throw new ResourceNotFoundException(ErrorCodes.Feature.PATH_FIND,
ErrorCodes.CODE.SOURCE_NOT_FOUND, ErrorCodes.REASON_MAP.get(ErrorCodes.CODE.SOURCE_NOT_FOUND));
}
/*If destination not found then find near location for destination within 50KM.*/
if (destinationCode == null) {
destinationCode = findNearestLocationAsDestination(param);

List<List<PathEntity>> allPaths = new ArrayList<>();
if(destinationCode != null)
{
/*Find All posible path from source to destination*/
allPaths = getPaths(param, sourceCode, destinationCode);
}
/*If still destination not found within 50KM then send error*/
if (destinationCode == null) {
throw new ResourceNotFoundException(ErrorCodes.Feature.PATH_FIND,
ErrorCodes.CODE.DESTINATION_NOT_FOUND, ErrorCodes.REASON_MAP.get(ErrorCodes.CODE.DESTINATION_NOT_FOUND));
/*If destination not found then find near location for destination within 50KM.*/
else if (destinationCode == null) {
List<PlaceParam> derivedLocationAsDestination = findDerivedLocationAsDestination(param);
for (PlaceParam placeParam: derivedLocationAsDestination)
{
List<List<PathEntity>> paths = getPaths(param, sourceCode, placeParam.getCode());
allPaths.addAll(paths);
}
}

/*Find All posible path from source to destination*/
List<List<PathEntity>> allPaths = getPaths(param, sourceCode, destinationCode);

if (allPaths == null || allPaths.size() == 0 ) {
throw new ResourceNotFoundException(ErrorCodes.Feature.PATH_FIND,
ErrorCodes.CODE.PATH_NOT_FOUND, ErrorCodes.REASON_MAP.get(ErrorCodes.CODE.PATH_NOT_FOUND));
Expand All @@ -125,36 +132,19 @@ public List<ResultParam> getAllPaths(FindPathParam param) throws ResourceNotFoun
* @return
* @throws Exception
*/
private String findNearestLocationAsDestination(FindPathParam param) throws RemoteApiException, ResourceNotFoundException {
String destinationCode;LatLongModel latLongModel = latLongService.getLatLongPositions(param.getDestination());
List<PlaceParam> allNearestPlaces = placeService.getAllNearestPlaces(
private List<PlaceParam> findDerivedLocationAsDestination(FindPathParam param) throws RemoteApiException, ResourceNotFoundException {
LatLongModel latLongModel = latLongService.getLatLongPositions(param.getDestination());
List<PlaceParam> allDerivedLocations = placeService.getAllNearestPlaces(
latLongModel.getLatitude(),
latLongModel.getLongitude(),
Constants.MINIMUM_DISTANCE_TO_NEAR_LOCATION);
if(allNearestPlaces.size() ==0)
if(allDerivedLocations.size() ==0)
{
throw new ResourceNotFoundException(ErrorCodes.Feature.PATH_FIND,
ErrorCodes.CODE.DESTINATION_NOT_FOUND,
ErrorCodes.REASON_MAP.get(ErrorCodes.CODE.DESTINATION_NOT_FOUND));
}

double minimumDistance = Constants.MINIMUM_DISTANCE_TO_NEAR_LOCATION;
PlaceParam targetPlace = null;
for (PlaceParam placeParam : allNearestPlaces) {
double nearestPlaceLatitude = placeParam.getLatitude();
double nearestPlaceLongitude = placeParam.getLongitude();
Double distance = latLongService.distance(
latLongModel.getLatitude(),
latLongModel.getLongitude(),
nearestPlaceLatitude,
nearestPlaceLongitude);
if (distance < minimumDistance) {
minimumDistance = distance;
targetPlace = placeParam;
}
}
destinationCode = targetPlace.getCode();
return destinationCode;
return allDerivedLocations;
}

/**
Expand Down

0 comments on commit 6d13779

Please sign in to comment.