use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class QueryImplTest method nest.
@Test
public void nest() {
Query<Entity> q = new QueryImpl<>().nest().eq("field", "value").unnest();
QueryRule expectedRule = new QueryRule(Arrays.asList(new QueryRule("field", Operator.EQUALS, "value")));
assertEquals(q.getRules(), Arrays.asList(expectedRule));
}
use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class QueryImplTest method rng.
@Test
public void rng() {
Query<Entity> q = new QueryImpl<>().rng("field", "min", "max");
QueryRule expectedRule = new QueryRule("field", Operator.RANGE, Arrays.asList("min", "max"));
assertEquals(q.getRules(), Arrays.asList(expectedRule));
}
use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class QueryStringParser method parseQueryString.
public Query<Entity> parseQueryString(Map<String, String[]> parameterMap) {
QueryImpl<Entity> q = new QueryImpl<>();
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
String paramName = entry.getKey();
String[] paramValues = entry.getValue();
if ((paramValues != null) && (paramValues.length > 0) && (paramValues[0] != null)) {
if (paramName.equalsIgnoreCase("num")) {
q.pageSize(DataConverter.toInt(paramValues[0]));
} else if (paramName.equalsIgnoreCase("start")) {
q.offset(DataConverter.toInt(paramValues[0]));
} else if (paramName.equalsIgnoreCase("q")) {
Query<Entity> query = molgenisRSQL.createQuery(paramValues[0], entityType);
for (QueryRule rule : query.getRules()) {
q.addRule(rule);
}
}
}
}
return q;
}
use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class SortaServiceImpl method findOntologyTermEntities.
@Override
public Iterable<Entity> findOntologyTermEntities(String ontologyIri, Entity inputEntity) {
Entity ontologyEntity = getOntologyEntity(ontologyIri);
if (ontologyEntity == null)
throw new IllegalArgumentException("Ontology IRI " + ontologyIri + " does not exist in the database!");
// a list to store most relevant entities
List<Entity> relevantEntities = new ArrayList<>();
// query rules for ontology anntations, e.g. OMIM:124343
List<QueryRule> rulesForOtherFields = new ArrayList<>();
// query rules for ontology name and synonyms, e.g. name = proptosis, sysnonym = protruding eye
List<QueryRule> rulesForOntologyTermFields = new ArrayList<>();
List<QueryRule> rulesForOntologyTermFieldsNGram = new ArrayList<>();
for (String attributeName : inputEntity.getAttributeNames()) {
if (StringUtils.isNotEmpty(inputEntity.getString(attributeName)) && !attributeName.equalsIgnoreCase(DEFAULT_MATCHING_IDENTIFIER)) {
// The attribute name is either equal to 'Name' or starts with string 'Synonym'
if (isAttrNameValidForLexicalMatch(attributeName)) {
String stemmedQueryString = stemQuery(inputEntity.getString(attributeName));
if (StringUtils.isNotEmpty(stemmedQueryString)) {
rulesForOntologyTermFields.add(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, FUZZY_MATCH, fuzzyMatchQuerySyntax(stemmedQueryString)));
rulesForOntologyTermFieldsNGram.add(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, FUZZY_MATCH_NGRAM, stemmedQueryString));
}
} else {
QueryRule queryAnnotationName = new QueryRule(OntologyTermDynamicAnnotationMetaData.NAME, EQUALS, attributeName);
QueryRule queryAnnotationValue = new QueryRule(OntologyTermDynamicAnnotationMetaData.VALUE, EQUALS, inputEntity.getString(attributeName));
// ((name=OMIM Operator.AND value=124325) Operator.OR (name=HPO Operator.AND value=hp12435))
if (!rulesForOtherFields.isEmpty())
rulesForOtherFields.add(new QueryRule(OR));
rulesForOtherFields.add(new QueryRule(Arrays.asList(queryAnnotationName, new QueryRule(AND), queryAnnotationValue)));
}
}
}
// Find the ontology terms that have the same annotations as the input ontology annotations
if (!rulesForOtherFields.isEmpty()) {
annotationMatchOntologyTerms(inputEntity, ontologyEntity, relevantEntities, rulesForOtherFields);
}
// Find the ontology terms based on the lexical similarities
if (!rulesForOntologyTermFields.isEmpty()) {
int pageSize = MAX_NUMBER_MATCHES - relevantEntities.size();
lexicalMatchOntologyTerms(ontologyIri, inputEntity, ontologyEntity, pageSize, rulesForOntologyTermFields, relevantEntities);
}
if (!rulesForOntologyTermFieldsNGram.isEmpty()) {
lexicalMatchOntologyTerms(ontologyIri, inputEntity, ontologyEntity, NUMBER_NGRAM_MATCHES, rulesForOntologyTermFieldsNGram, relevantEntities);
}
relevantEntities.sort((entity_1, entity_2) -> entity_2.getDouble(COMBINED_SCORE).compareTo(entity_1.getDouble(COMBINED_SCORE)));
return relevantEntities;
}
use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class SortaServiceImpl method lexicalMatchOntologyTerms.
private void lexicalMatchOntologyTerms(String ontologyIri, Entity inputEntity, Entity ontologyEntity, int pageSize, List<QueryRule> rulesForOntologyTermFields, List<Entity> relevantEntities) {
QueryRule disMaxQueryRule = new QueryRule(rulesForOntologyTermFields);
disMaxQueryRule.setOperator(DIS_MAX);
List<QueryRule> finalQueryRules = Arrays.asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, EQUALS, ontologyEntity), new QueryRule(AND), disMaxQueryRule);
Stream<Entity> lexicalMatchedOntologyTermEntities = dataService.findAll(ONTOLOGY_TERM, new QueryImpl<>(finalQueryRules).pageSize(pageSize)).map(ontologyTerm -> addLexicalScoreToMatchedEntity(inputEntity, ontologyTerm, // TODO use findAll(ONTOLOGY_TERM, ..., OntologyTerm.class)
ontologyIri));
lexicalMatchedOntologyTermEntities.forEach(matchedEntity -> {
if (!relevantEntities.contains(matchedEntity)) {
relevantEntities.add(matchedEntity);
}
});
}
Aggregations