2016-07-13 11:59:44 +02:00
|
|
|
package net.osmand.search;
|
2016-07-08 14:32:02 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
2016-07-12 20:16:42 +02:00
|
|
|
import net.osmand.OsmAndCollator;
|
2016-07-08 14:32:02 +02:00
|
|
|
import net.osmand.PlatformUtil;
|
|
|
|
import net.osmand.ResultMatcher;
|
|
|
|
import net.osmand.binary.BinaryMapIndexReader;
|
|
|
|
import net.osmand.data.LatLon;
|
2016-07-10 11:46:39 +02:00
|
|
|
import net.osmand.osm.MapPoiTypes;
|
2016-07-13 11:59:44 +02:00
|
|
|
import net.osmand.search.core.SearchCoreAPI;
|
|
|
|
import net.osmand.search.core.SearchCoreFactory;
|
|
|
|
import net.osmand.search.core.SearchPhrase;
|
|
|
|
import net.osmand.search.core.SearchResult;
|
|
|
|
import net.osmand.search.core.SearchSettings;
|
|
|
|
import net.osmand.search.core.SearchWord;
|
|
|
|
import net.osmand.search.core.SearchPhrase.NameStringMatcher;
|
2016-07-08 14:32:02 +02:00
|
|
|
import net.osmand.util.Algorithms;
|
|
|
|
|
2016-07-11 10:01:25 +02:00
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
2016-07-08 14:32:02 +02:00
|
|
|
public class SearchUICore {
|
|
|
|
|
|
|
|
private static final Log LOG = PlatformUtil.getLog(SearchUICore.class);
|
|
|
|
private SearchPhrase phrase;
|
2016-07-11 10:01:25 +02:00
|
|
|
private SearchResultCollection currentSearchResult = new SearchResultCollection();
|
2016-07-08 14:32:02 +02:00
|
|
|
|
|
|
|
private ThreadPoolExecutor singleThreadedExecutor;
|
|
|
|
private LinkedBlockingQueue<Runnable> taskQueue;
|
|
|
|
private Runnable onResultsComplete = null;
|
|
|
|
private AtomicInteger requestNumber = new AtomicInteger();
|
2016-07-10 11:46:39 +02:00
|
|
|
private int totalLimit = -1; // -1 unlimited - not used
|
2016-07-08 14:32:02 +02:00
|
|
|
|
|
|
|
List<SearchCoreAPI> apis = new ArrayList<>();
|
|
|
|
private SearchSettings searchSettings;
|
2016-07-10 11:46:39 +02:00
|
|
|
private MapPoiTypes poiTypes;
|
2016-07-08 14:32:02 +02:00
|
|
|
|
|
|
|
|
2016-07-10 11:46:39 +02:00
|
|
|
public SearchUICore(MapPoiTypes poiTypes, String locale, BinaryMapIndexReader[] searchIndexes) {
|
|
|
|
this.poiTypes = poiTypes;
|
2016-07-08 14:32:02 +02:00
|
|
|
List<BinaryMapIndexReader> searchIndexesList = Arrays.asList(searchIndexes);
|
|
|
|
taskQueue = new LinkedBlockingQueue<Runnable>();
|
|
|
|
searchSettings = new SearchSettings(searchIndexesList);
|
2016-07-10 11:46:39 +02:00
|
|
|
searchSettings = searchSettings.setLang(locale);
|
2016-07-08 14:32:02 +02:00
|
|
|
phrase = new SearchPhrase(searchSettings);
|
|
|
|
singleThreadedExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, taskQueue);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2016-07-11 10:01:25 +02:00
|
|
|
public static class SearchResultCollection {
|
|
|
|
private List<SearchResult> searchResults;
|
|
|
|
private SearchPhrase phrase;
|
|
|
|
|
|
|
|
public SearchResultCollection(List<SearchResult> requestResults, SearchPhrase phrase) {
|
|
|
|
searchResults = requestResults;
|
|
|
|
this.phrase = phrase;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SearchResultCollection() {
|
|
|
|
searchResults = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<SearchResult> getCurrentSearchResults() {
|
|
|
|
return searchResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SearchPhrase getPhrase() {
|
|
|
|
return phrase;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 14:32:02 +02:00
|
|
|
public int getTotalLimit() {
|
|
|
|
return totalLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTotalLimit(int totalLimit) {
|
|
|
|
this.totalLimit = totalLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void init() {
|
2016-07-10 11:46:39 +02:00
|
|
|
apis.add(new SearchCoreFactory.SearchAmenityTypesAPI(poiTypes));
|
2016-07-10 12:20:18 +02:00
|
|
|
apis.add(new SearchCoreFactory.SearchAmenityByTypeAPI(poiTypes));
|
2016-07-10 11:46:39 +02:00
|
|
|
apis.add(new SearchCoreFactory.SearchAmenityByNameAPI());
|
|
|
|
apis.add(new SearchCoreFactory.SearchStreetByCityAPI());
|
|
|
|
apis.add(new SearchCoreFactory.SearchBuildingAndIntersectionsByStreetAPI());
|
2016-07-12 21:06:57 +02:00
|
|
|
apis.add(new SearchCoreFactory.SearchLocationAndUrlAPI());
|
2016-07-08 14:32:02 +02:00
|
|
|
apis.add(new SearchCoreFactory.SearchAddressByNameAPI());
|
|
|
|
}
|
|
|
|
|
2016-07-12 15:47:34 +02:00
|
|
|
public void registerAPI(SearchCoreAPI api) {
|
|
|
|
apis.add(api);
|
|
|
|
}
|
|
|
|
|
2016-07-11 10:01:25 +02:00
|
|
|
|
|
|
|
public SearchResultCollection getCurrentSearchResult() {
|
|
|
|
return currentSearchResult;
|
2016-07-08 14:32:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public SearchPhrase getPhrase() {
|
|
|
|
return phrase;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOnResultsComplete(Runnable onResultsComplete) {
|
|
|
|
this.onResultsComplete = onResultsComplete;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-10 11:46:39 +02:00
|
|
|
public void updateSettings(SearchSettings settings) {
|
|
|
|
searchSettings = settings;
|
2016-07-08 14:32:02 +02:00
|
|
|
}
|
|
|
|
|
2016-07-11 10:01:25 +02:00
|
|
|
private List<SearchResult> filterCurrentResults(List<SearchResult> rr, SearchPhrase phrase) {
|
|
|
|
List<SearchResult> l = currentSearchResult.searchResults;
|
2016-07-08 14:32:02 +02:00
|
|
|
for(SearchResult r : l) {
|
|
|
|
if(filterOneResult(r, phrase)) {
|
|
|
|
rr.add(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean filterOneResult(SearchResult object, SearchPhrase phrase) {
|
2016-07-10 11:46:39 +02:00
|
|
|
NameStringMatcher nameStringMatcher = phrase.getNameStringMatcher();
|
|
|
|
return nameStringMatcher.matches(object.localeName) || nameStringMatcher.matches(object.otherNames);
|
2016-07-08 14:32:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean selectSearchResult(SearchResult r) {
|
|
|
|
this.phrase = this.phrase.selectWord(r);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-11 10:01:25 +02:00
|
|
|
public SearchResultCollection search(final String text, final ResultMatcher<SearchResult> matcher) {
|
|
|
|
SearchResultCollection quickRes = new SearchResultCollection();
|
2016-07-08 14:32:02 +02:00
|
|
|
final int request = requestNumber.incrementAndGet();
|
|
|
|
final SearchPhrase phrase = this.phrase.generateNewPhrase(text, searchSettings);
|
|
|
|
this.phrase = phrase;
|
2016-07-11 10:01:25 +02:00
|
|
|
quickRes.phrase = phrase;
|
|
|
|
filterCurrentResults(quickRes.searchResults, phrase);
|
2016-07-12 20:16:42 +02:00
|
|
|
LOG.info("> Search phrase " + phrase + " " + quickRes.searchResults.size());
|
2016-07-08 14:32:02 +02:00
|
|
|
singleThreadedExecutor.submit(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2016-07-13 11:59:44 +02:00
|
|
|
SearchResultMatcher rm = new SearchResultMatcher(matcher, request, requestNumber, totalLimit);
|
2016-07-08 14:32:02 +02:00
|
|
|
if(rm.isCancelled()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-10 11:46:39 +02:00
|
|
|
Thread.sleep(200); // FIXME remove timeout
|
2016-07-08 14:32:02 +02:00
|
|
|
searchInBackground(phrase, rm);
|
|
|
|
if (!rm.isCancelled()) {
|
|
|
|
sortSearchResults(phrase, rm.getRequestResults());
|
2016-07-12 20:16:42 +02:00
|
|
|
LOG.info(">> Search phrase " + phrase + " " + rm.getRequestResults().size());
|
2016-07-11 10:01:25 +02:00
|
|
|
SearchResultCollection collection = new SearchResultCollection(rm.getRequestResults(),
|
|
|
|
phrase);
|
|
|
|
currentSearchResult = collection;
|
2016-07-08 14:32:02 +02:00
|
|
|
if (onResultsComplete != null) {
|
|
|
|
onResultsComplete.run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
});
|
2016-07-11 10:01:25 +02:00
|
|
|
return quickRes;
|
2016-07-08 14:32:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void searchInBackground(final SearchPhrase phrase, SearchResultMatcher matcher) {
|
|
|
|
for (SearchWord sw : phrase.getWords()) {
|
2016-07-12 13:45:42 +02:00
|
|
|
if(sw.getResult() != null && sw.getResult().file != null) {
|
|
|
|
phrase.selectFile(sw.getResult().file);
|
2016-07-08 14:32:02 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-10 11:46:39 +02:00
|
|
|
phrase.sortFiles();
|
2016-07-08 14:32:02 +02:00
|
|
|
ArrayList<SearchCoreAPI> lst = new ArrayList<>(apis);
|
|
|
|
Collections.sort(lst, new Comparator<SearchCoreAPI>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compare(SearchCoreAPI o1, SearchCoreAPI o2) {
|
|
|
|
return Algorithms.compare(o1.getSearchPriority(phrase),
|
|
|
|
o2.getSearchPriority(phrase));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for(SearchCoreAPI api : lst) {
|
|
|
|
if(matcher.isCancelled()) {
|
|
|
|
break;
|
|
|
|
}
|
2016-07-10 11:46:39 +02:00
|
|
|
if(api.getSearchPriority(phrase) == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-08 14:32:02 +02:00
|
|
|
try {
|
|
|
|
api.search(phrase, matcher);
|
2016-07-12 20:16:42 +02:00
|
|
|
} catch (Throwable e) {
|
2016-07-08 14:32:02 +02:00
|
|
|
e.printStackTrace();
|
2016-07-12 20:16:42 +02:00
|
|
|
LOG.error(e.getMessage(), e);
|
2016-07-08 14:32:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void sortSearchResults(SearchPhrase sp, List<SearchResult> searchResults) {
|
|
|
|
// sort SearchResult by 1. searchDistance 2. Name
|
|
|
|
final LatLon loc = sp.getLastTokenLocation();
|
2016-07-12 20:16:42 +02:00
|
|
|
final net.osmand.Collator clt = OsmAndCollator.primaryCollator();
|
2016-07-08 14:32:02 +02:00
|
|
|
Collections.sort(searchResults, new Comparator<SearchResult>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compare(SearchResult o1, SearchResult o2) {
|
|
|
|
double s1 = o1.getSearchDistance(loc);
|
|
|
|
double s2 = o2.getSearchDistance(loc);
|
|
|
|
int cmp = Double.compare(s1, s2);
|
|
|
|
if(cmp != 0) {
|
|
|
|
return cmp;
|
|
|
|
}
|
2016-07-12 13:22:47 +02:00
|
|
|
int st1 = Algorithms.extractFirstIntegerNumber(o1.localeName);
|
|
|
|
int st2 = Algorithms.extractFirstIntegerNumber(o2.localeName);
|
|
|
|
if(st1 != st2) {
|
|
|
|
return Algorithms.compare(st1, st2);
|
|
|
|
}
|
2016-07-10 11:46:39 +02:00
|
|
|
return clt.compare(o1.localeName, o2.localeName);
|
2016-07-08 14:32:02 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-13 11:59:44 +02:00
|
|
|
public static class SearchResultMatcher implements ResultMatcher<SearchResult>{
|
2016-07-08 14:32:02 +02:00
|
|
|
private final List<SearchResult> requestResults = new ArrayList<>();
|
2016-07-13 11:59:44 +02:00
|
|
|
private final ResultMatcher<SearchResult> matcher;
|
2016-07-08 14:32:02 +02:00
|
|
|
private final int request;
|
2016-07-13 11:59:44 +02:00
|
|
|
private final int totalLimit;
|
|
|
|
private final AtomicInteger requestNumber;
|
2016-07-08 14:32:02 +02:00
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
2016-07-13 11:59:44 +02:00
|
|
|
public SearchResultMatcher(ResultMatcher<SearchResult> matcher, int request,
|
|
|
|
AtomicInteger requestNumber, int totalLimit) {
|
2016-07-08 14:32:02 +02:00
|
|
|
this.matcher = matcher;
|
|
|
|
this.request = request;
|
2016-07-13 11:59:44 +02:00
|
|
|
this.requestNumber = requestNumber;
|
|
|
|
this.totalLimit = totalLimit;
|
2016-07-08 14:32:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<SearchResult> getRequestResults() {
|
|
|
|
return requestResults;
|
|
|
|
}
|
2016-07-12 20:16:42 +02:00
|
|
|
|
2016-07-08 14:32:02 +02:00
|
|
|
@Override
|
|
|
|
public boolean publish(SearchResult object) {
|
|
|
|
if(matcher == null || matcher.publish(object)) {
|
|
|
|
count++;
|
|
|
|
if(totalLimit == -1 || count < totalLimit) {
|
|
|
|
requestResults.add(object);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public boolean isCancelled() {
|
|
|
|
boolean cancelled = request != requestNumber.get();
|
|
|
|
return cancelled || (matcher != null && matcher.isCancelled());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|