use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class InformationContentServiceTest method createWordIDF.
@Test
public void createWordIDF() {
String ontologyIri = "http://www.molgenis.org";
Ontology ontology = ontologyFactory.create();
ontology.setOntologyIri(ontologyIri);
when(dataService.findOne(ONTOLOGY, new QueryImpl<>().eq(OntologyMetaData.ONTOLOGY_IRI, ontologyIri))).thenReturn(ontology);
when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>().eq(OntologyTermMetaData.ONTOLOGY, ontology))).thenReturn((long) 100);
QueryRule queryRule = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, Operator.FUZZY_MATCH, "hear")));
queryRule.setOperator(Operator.DIS_MAX);
QueryRule finalQuery = new QueryRule(asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, Operator.EQUALS, ontology), new QueryRule(Operator.AND), queryRule));
when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>(finalQuery))).thenReturn((long) 30);
QueryRule queryRule2 = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, Operator.FUZZY_MATCH, "impair")));
queryRule2.setOperator(Operator.DIS_MAX);
QueryRule finalQuery2 = new QueryRule(asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, Operator.EQUALS, ontology), new QueryRule(Operator.AND), queryRule2));
when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>(finalQuery2))).thenReturn((long) 10);
Map<String, Double> expectedWordIDF = informationContentService.createWordIDF("hearing impairment", ontologyIri);
Assert.assertEquals(expectedWordIDF.get("hear").intValue(), 2);
Assert.assertEquals(expectedWordIDF.get("impair").intValue(), 3);
}
use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class PostgreSqlRepositoryTest method countQueryOneToManyEquals.
// TODO test all query operators for one-to-many
@Test
public void countQueryOneToManyEquals() throws Exception {
String oneToManyAttrName = "oneToManyAttr";
String refIdAttrName = "refEntityId";
Attribute refIdAttr = mock(Attribute.class);
when(refIdAttr.getName()).thenReturn(refIdAttrName);
when(refIdAttr.getDataType()).thenReturn(INT);
String xrefAttrName = "xrefAttr";
Attribute xrefAttr = mock(Attribute.class);
when(xrefAttr.getName()).thenReturn(xrefAttrName);
EntityType refEntityMeta = mock(EntityType.class);
when(refEntityMeta.getId()).thenReturn("refEntityId");
when(refEntityMeta.getIdAttribute()).thenReturn(refIdAttr);
String idAttrName = "entityId";
Attribute idAttr = mock(Attribute.class);
when(idAttr.getName()).thenReturn(idAttrName);
Attribute oneToManyAttr = mock(Attribute.class);
when(oneToManyAttr.getName()).thenReturn(oneToManyAttrName);
when(oneToManyAttr.getDataType()).thenReturn(ONE_TO_MANY);
when(oneToManyAttr.getRefEntity()).thenReturn(refEntityMeta);
when(oneToManyAttr.isMappedBy()).thenReturn(true);
when(oneToManyAttr.getMappedBy()).thenReturn(xrefAttr);
when(entityType.getId()).thenReturn("entityId");
when(entityType.getIdAttribute()).thenReturn(idAttr);
doReturn(oneToManyAttr).when(entityType).getAttribute(oneToManyAttrName);
int queryValue = 2;
QueryRule queryRule = new QueryRule(oneToManyAttrName, EQUALS, queryValue);
when(query.getRules()).thenReturn(singletonList(queryRule));
String sql = "SELECT COUNT(DISTINCT this.\"entityId\") FROM \"entityId#fc2928f6\" AS this LEFT JOIN \"refEntityId#07f902bf\" AS \"oneToManyAttr_filter1\" ON (this.\"entityId\" = \"oneToManyAttr_filter1\".\"xrefAttr\") WHERE \"oneToManyAttr_filter1\".\"refEntityId\" = ?";
long count = 123L;
when(jdbcTemplate.queryForObject(sql, new Object[] { queryValue }, Long.class)).thenReturn(count);
assertEquals(postgreSqlRepo.count(query), count);
}
use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class SemanticSearchServiceHelper method createDisMaxQueryRuleForAttribute.
/**
* Create a disMaxJunc query rule based on the given search terms as well as the information from given ontology
* terms
*
* @return disMaxJunc queryRule
*/
public QueryRule createDisMaxQueryRuleForAttribute(Set<String> searchTerms, Collection<OntologyTerm> ontologyTerms) {
List<String> queryTerms = new ArrayList<>();
if (searchTerms != null) {
queryTerms.addAll(searchTerms.stream().filter(StringUtils::isNotBlank).map(this::processQueryString).collect(Collectors.toList()));
}
// Handle tags with only one ontologyterm
ontologyTerms.stream().filter(ontologyTerm -> !ontologyTerm.getIRI().contains(COMMA_CHAR)).forEach(ot -> queryTerms.addAll(parseOntologyTermQueries(ot)));
QueryRule disMaxQueryRule = createDisMaxQueryRuleForTerms(queryTerms);
// Handle tags with multiple ontologyterms
ontologyTerms.stream().filter(ontologyTerm -> ontologyTerm.getIRI().contains(COMMA_CHAR)).forEach(ot -> disMaxQueryRule.getNestedRules().add(createShouldQueryRule(ot.getIRI())));
return disMaxQueryRule;
}
use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class ExplainServiceHelperTest method testFindQuery.
@Test
public void testFindQuery() {
QueryRule queryRule_1 = new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, "hypertension");
QueryRule queryRule_2 = new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, "hypertensive disorder");
QueryRule queryRule_3 = new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, "high blood pressure");
QueryRule queryRule_4 = new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, "drug");
QueryRule queryRule_5 = new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, "medication");
QueryRule queryRule_6 = new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, "pill");
QueryRule disMaxQueryRule_1 = new QueryRule(Arrays.asList(queryRule_1, queryRule_2, queryRule_3));
disMaxQueryRule_1.setOperator(Operator.DIS_MAX);
QueryRule disMaxQueryRule_2 = new QueryRule(Arrays.asList(queryRule_4, queryRule_5, queryRule_6));
disMaxQueryRule_2.setOperator(Operator.DIS_MAX);
QueryRule shouldQueryRule = new QueryRule(Arrays.asList(disMaxQueryRule_1, disMaxQueryRule_2));
shouldQueryRule.setOperator(Operator.SHOULD);
QueryRule finalDisMaxQueryRule = new QueryRule(Arrays.asList(shouldQueryRule));
finalDisMaxQueryRule.setOperator(Operator.DIS_MAX);
Map<String, String> expanedQueryMap = new HashMap<>();
expanedQueryMap.put("hypertension", "hypertension");
expanedQueryMap.put("hypertensive disorder", "hypertension");
expanedQueryMap.put("high blood pressure", "hypertension");
expanedQueryMap.put("medication", "medication");
expanedQueryMap.put("drug", "medication");
expanedQueryMap.put("pill", "medication");
Map<String, Double> findMatchQueries = explainServiceHelper.findMatchQueries("blood high", expanedQueryMap);
assertEquals(findMatchQueries.get("high blood pressure"), 73.333, 0.001);
}
use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class SemanticSearchServiceImpl method findAttributes.
@Override
public Map<Attribute, ExplainedAttribute> findAttributes(EntityType sourceEntityType, Set<String> queryTerms, Collection<OntologyTerm> ontologyTerms) {
Iterable<String> attributeIdentifiers = semanticSearchServiceHelper.getAttributeIdentifiers(sourceEntityType);
QueryRule disMaxQueryRule = semanticSearchServiceHelper.createDisMaxQueryRuleForAttribute(queryTerms, ontologyTerms);
List<QueryRule> finalQueryRules = Lists.newArrayList(new QueryRule(AttributeMetadata.ID, Operator.IN, attributeIdentifiers));
if (disMaxQueryRule.getNestedRules().size() > 0) {
finalQueryRules.addAll(Arrays.asList(new QueryRule(Operator.AND), disMaxQueryRule));
}
Stream<Entity> attributeEntities = dataService.findAll(ATTRIBUTE_META_DATA, new QueryImpl<>(finalQueryRules));
Map<String, String> collectExpanedQueryMap = semanticSearchServiceHelper.collectExpandedQueryMap(queryTerms, ontologyTerms);
// Because the explain-API can be computationally expensive we limit the explanation to the top 10 attributes
Map<Attribute, ExplainedAttribute> explainedAttributes = new LinkedHashMap<>();
AtomicInteger count = new AtomicInteger(0);
attributeEntities.forEach(attributeEntity -> {
Attribute attribute = sourceEntityType.getAttribute(attributeEntity.getString(AttributeMetadata.NAME));
if (count.get() < MAX_NUMBER_EXPLAINED_ATTRIBUTES) {
Set<ExplainedQueryString> explanations = convertAttributeToExplainedAttribute(attribute, collectExpanedQueryMap, new QueryImpl<>(finalQueryRules));
boolean singleMatchHighQuality = isSingleMatchHighQuality(queryTerms, Sets.newHashSet(collectExpanedQueryMap.values()), explanations);
explainedAttributes.put(attribute, ExplainedAttribute.create(attribute, explanations, singleMatchHighQuality));
} else {
explainedAttributes.put(attribute, ExplainedAttribute.create(attribute));
}
count.incrementAndGet();
});
return explainedAttributes;
}
Aggregations