Skip to content
Snippets Groups Projects
Commit 4712ace6 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

search by coordinates takes into account size when few elements of the same size are found

parent 89054aab
No related branches found
No related tags found
1 merge request!35Resolve "Search by coordinates in complex"
......@@ -10,18 +10,22 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.model.map.AnnotatedObject;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.services.SecurityException;
import lcsb.mapviewer.services.UserAccessException;
import lcsb.mapviewer.services.interfaces.IModelService;
import lcsb.mapviewer.services.interfaces.ISearchService;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.services.view.AnnotationViewFactory;
@Transactional(value = "txManager")
public class BioEntitiesRestImpl extends BaseRestImpl {
Logger logger = Logger.getLogger(BioEntitiesRestImpl.class);
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(BioEntitiesRestImpl.class);
@Autowired
private IUserService userService;
......@@ -32,9 +36,6 @@ public class BioEntitiesRestImpl extends BaseRestImpl {
@Autowired
private ISearchService searchService;
@Autowired
AnnotationViewFactory annotationViewFactory;
/**
* @return the userService
* @see #userService
......@@ -60,7 +61,7 @@ public class BioEntitiesRestImpl extends BaseRestImpl {
Model submodel = model.getSubmodelById(modelId);
List<Object> elements = searchService.getClosestElements(submodel, coordinates, count, perfectMatch.equalsIgnoreCase("true"));
List<AnnotatedObject> elements = searchService.getClosestElements(submodel, coordinates, count, perfectMatch.equalsIgnoreCase("true"));
for (Object object : elements) {
Map<String, Object> result = createMinifiedSearchResult(object);
resultMap.add(result);
......
......@@ -527,8 +527,8 @@ public class SearchService implements ISearchService {
}
@Override
public List<Object> getClosestElements(Model model, Point2D point, int numberOfElements, boolean perfectHit) {
List<Object> result = new ArrayList<>();
public List<AnnotatedObject> getClosestElements(Model model, Point2D point, int numberOfElements, boolean perfectHit) {
List<AnnotatedObject> result = new ArrayList<>();
// probably this could be improved algorithmitically, right now all objects
// are sorted by distance, and numberOfElements closest are chosen as a list
......@@ -566,12 +566,18 @@ public class SearchService implements ISearchService {
/**
* Reference to the object.
*/
private Object reference;
private AnnotatedObject reference;
/**
* Distance between the object and some point.
*/
private double distance;
/**
* Size of the object.
*/
private double size;
/**
* Constructor for reaction objects.
*
......@@ -583,6 +589,7 @@ public class SearchService implements ISearchService {
DistanceToObject(Reaction reaction, Point2D point) {
reference = reaction;
distance = reaction.getDistanceFromPoint(point);
size = 0;
}
/**
......@@ -596,6 +603,7 @@ public class SearchService implements ISearchService {
DistanceToObject(Element alias, Point2D point) {
reference = alias;
distance = alias.getDistanceFromPoint(point);
size = alias.getSize();
}
......@@ -606,7 +614,13 @@ public class SearchService implements ISearchService {
} else if (arg0.getDistance() > getDistance()) {
return -1;
} else {
return 0;
if (arg0.getSize() < getSize()) {
return 1;
} else if (arg0.getSize() > getSize()) {
return -1;
} else {
return 0;
}
}
}
......@@ -614,7 +628,7 @@ public class SearchService implements ISearchService {
* @return the reference
* @see #reference
*/
public Object getReference() {
public AnnotatedObject getReference() {
return reference;
}
......@@ -626,6 +640,13 @@ public class SearchService implements ISearchService {
return distance;
}
/**
* @return the size
* @see #size
*/
public double getSize() {
return size;
}
}
/**
......
......@@ -5,6 +5,7 @@ import java.util.List;
import org.apache.log4j.Logger;
import lcsb.mapviewer.model.map.AnnotatedObject;
import lcsb.mapviewer.model.map.layout.Layout;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelSubmodelConnection;
......@@ -295,7 +296,7 @@ public interface ISearchService {
* how many closest elements should be returned
* @return list of the closest elements
*/
List<Object> getClosestElements(Model model, Point2D point, int numberOfElements, boolean perfectHit);
List<AnnotatedObject> getClosestElements(Model model, Point2D point, int numberOfElements, boolean perfectHit);
/**
* Returns list of autocomplete strings for the partial query.
......
......@@ -18,6 +18,7 @@ import org.junit.Test;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.AnnotatedObject;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamRelationType;
import lcsb.mapviewer.model.map.MiriamType;
......@@ -45,7 +46,7 @@ import lcsb.mapviewer.services.search.data.SearchElementResult;
import lcsb.mapviewer.services.utils.SearchIndexer;
public class SearchServiceTest extends ServiceTestFunctions {
static Logger logger = Logger.getLogger(SearchService.class);
static Logger logger = Logger.getLogger(SearchServiceTest.class);
private Model model = null;
SearchIndexer indexer = new SearchIndexer();
......@@ -231,7 +232,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
public void testSearchClosest() throws Exception {
try {
model = getModelForFile("testFiles/graph_path_example3.xml", true);
List<Object> elements = searchService.getClosestElements(model, new Point2D.Double(0, 0), 5, false);
List<AnnotatedObject> elements = searchService.getClosestElements(model, new Point2D.Double(0, 0), 5, false);
assertNotNull(elements);
assertEquals(5, elements.size());
assertTrue(elements.get(0) instanceof Species);
......@@ -256,11 +257,50 @@ public class SearchServiceTest extends ServiceTestFunctions {
}
}
@Test
public void testSearchClosest2() throws Exception {
try {
model = new ModelFullIndexed(null);
GenericProtein protein1 = new GenericProtein("s1");
protein1.setWidth(20);
protein1.setHeight(20);
protein1.setX(5);
protein1.setY(5);
model.addElement(protein1);
GenericProtein protein2 = new GenericProtein("s2");
protein2.setWidth(10);
protein2.setHeight(10);
protein2.setX(5);
protein2.setY(5);
model.addElement(protein2);
GenericProtein protein3 = new GenericProtein("s3");
protein3.setWidth(30);
protein3.setHeight(30);
protein3.setX(0);
protein3.setY(0);
model.addElement(protein3);
List<AnnotatedObject> elements = searchService.getClosestElements(model, new Point2D.Double(10, 10), 5, false);
assertNotNull(elements);
assertEquals(3, elements.size());
AnnotatedObject sAlias = elements.get(0);
assertEquals("s2", sAlias.getName());
AnnotatedObject reaction = elements.get(1);
assertEquals("s1", reaction.getName());
sAlias = elements.get(2);
assertEquals("s3", sAlias.getName());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testSearchClosestWithEmptyModel() throws Exception {
try {
model = new ModelFullIndexed(null);
List<Object> elements = searchService.getClosestElements(model, new Point2D.Double(0, 0), 5, false);
List<AnnotatedObject> elements = searchService.getClosestElements(model, new Point2D.Double(0, 0), 5, false);
assertNotNull(elements);
assertEquals(0, elements.size());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment