Skip to content

Commit

Permalink
better cach
Browse files Browse the repository at this point in the history
  • Loading branch information
Vick FOEX committed Jan 23, 2024
1 parent 54b78be commit ebd98e5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.example.BackSimulation.Model.API;

import com.example.BackSimulation.Model.MapObjects.Building;
import com.example.BackSimulation.Model.MouvableObject.CoordBigDecimal;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.aop.scope.ScopedObject;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -19,15 +23,34 @@ public class APIopenRouteService {
//private static final String tocken = "5b3ce3597851110001cf624898c7eb581706416a9ab7df828faeaae3";
private static final String tocken = "5b3ce3597851110001cf624898d73d0c7634461abc58ae5564b8c48d";

private static Map<String, List<List<BigDecimal> >> cachedPath = new HashMap<>();
private static Map<List<CoordBigDecimal>, List<List<BigDecimal> >> cachedPath = new HashMap<>();

public static List<List<BigDecimal>> getPathAPI(Building startBuilding, Building endbuilding){
List<CoordBigDecimal> testKeyCach = new ArrayList<>();
testKeyCach.add(startBuilding.getCoords());
testKeyCach.add(endbuilding.getCoords());

System.out.println(startBuilding);
System.out.println(endbuilding);
System.out.println(cachedPath);

if( cachedPath.containsKey(testKeyCach)){
System.out.println("Cached call");
return cachedPath.get(testKeyCach);
}
System.out.println("Not cached");


cachedPath.put(testKeyCach, getPathAPI(startBuilding.coords.lng.floatValue(), startBuilding.coords.lat.floatValue()
, endbuilding.coords.lng.floatValue(), endbuilding.coords.lat.floatValue()));

return cachedPath.get(testKeyCach);
}

public static List<List<BigDecimal>> getPathAPI(float startLong, float startLat, float endLong, float endLat){

String tailURL = "&start="+ startLong + "," + startLat + "&end=" + endLong + "," + endLat;

if (cachedPath.containsKey(tailURL)){
return cachedPath.get(tailURL);
}

String URL = domainName
+ "?api_key=" + tocken
Expand All @@ -46,17 +69,29 @@ public static List<List<BigDecimal>> getPathAPI(float startLong, float startLat,
e.printStackTrace();
}

List<List<BigDecimal>> cordsList;
JSONObject json = new JSONObject(response.body());

JSONArray a = (JSONArray) json.get("features");
JSONObject b = (JSONObject) a.get(0);
JSONObject c = (JSONObject) b.get("geometry");
JSONArray d = (JSONArray) c.get("coordinates");
if (json.has("features")) {
JSONArray a = (JSONArray) json.get("features");
JSONObject b = (JSONObject) a.get(0);
JSONObject c = (JSONObject) b.get("geometry");
JSONArray d = (JSONArray) c.get("coordinates");

List<List<BigDecimal>> cordsList = (List<List<BigDecimal>>)(List<?>)d.toList();
cordsList = (List<List<BigDecimal>>) (List<?>) d.toList();

// Add path to cach :
cachedPath.put( tailURL , cordsList);
}else{
// This part cheats a little !
// if the API is overloaded will give some dummy coords to the agents
System.out.println("No Feature ! ");

List<BigDecimal> coords = new ArrayList<>();
coords.add(BigDecimal.valueOf(startLong));
coords.add( BigDecimal.valueOf(startLat));

cordsList = new ArrayList<>();
cordsList.add( coords );
}

return cordsList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class MouvableAgent extends MapObject implements Mouvable{
private List<List<BigDecimal>> path;
private int indexPath = 0;
private boolean hasArrived = false;
private Building lastPostion;

private final BigDecimal deplacementMax = APIopenRouteService.deltaPoints.divide( BigDecimal.valueOf(1),10, RoundingMode.HALF_UP ) ;
private final BigDecimal sightRadius= APIopenRouteService.deltaPoints.divide( BigDecimal.valueOf(0.5), 10, RoundingMode.HALF_UP) ;
Expand All @@ -28,15 +29,18 @@ public MouvableAgent( int id, CoordBigDecimal coords){
// PUBLIC METHODES FROM MOUVABLE OBJECT :

public void setGoal( Building building ){
//System.out.println("API build" + building );
//System.out.println("CONVertion : " + building.coords.lng.doubleValue() + " " + building.coords.lat.doubleValue());

this.path = APIopenRouteService.getPathAPI(this.coords.lng.floatValue(), this.coords.lat.floatValue()
, building.coords.lng.floatValue(), building.coords.lat.floatValue());
if(lastPostion == null) {
System.out.println("NULL");
this.path = APIopenRouteService.getPathAPI(this.coords.lng.floatValue(), this.coords.lat.floatValue()
, building.coords.lng.floatValue(), building.coords.lat.floatValue());

}else {
System.out.println("NOT NULL");
this.path = APIopenRouteService.getPathAPI(this.lastPostion, building);
}
lastPostion = building;
this.hasArrived = false;
this.indexPath = 0;

//System.out.println("Path : " + this.path);
}

@Override
Expand Down Expand Up @@ -66,7 +70,6 @@ public void setMoveToGoal(){

if (this.indexPath >= this.path.size()) {
this.hasArrived = true;
System.out.println("Yay, mission acomplie !");
}

}
Expand Down Expand Up @@ -101,19 +104,9 @@ public void setObstacle(MouvableAgent otherAgent){
this.dCoords.lat = dLatitude.add(dLatitude.divide(distance, 10, RoundingMode.HALF_UP).multiply(push));
}catch(ArithmeticException e ){
System.out.println("Div 0 : " + e);

}

}

/*
BigDecimal multiply = dLongitude.divide(distance, 10, RoundingMode.HALF_UP).multiply(push);
this.dCoords.lng = dLongitude.add(multiply);
this.dCoords.lat = dLatitude.add(multiply);
*/


}

public boolean hasArrived(){
Expand Down

0 comments on commit ebd98e5

Please sign in to comment.