Search in sources :

Example 1 with QueryRule

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));
}
Also used : Entity(org.molgenis.data.Entity) QueryRule(org.molgenis.data.QueryRule) Test(org.testng.annotations.Test)

Example 2 with QueryRule

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));
}
Also used : Entity(org.molgenis.data.Entity) QueryRule(org.molgenis.data.QueryRule) Test(org.testng.annotations.Test)

Example 3 with QueryRule

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;
}
Also used : Entity(org.molgenis.data.Entity) QueryImpl(org.molgenis.data.support.QueryImpl) QueryRule(org.molgenis.data.QueryRule) Map(java.util.Map)

Example 4 with QueryRule

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;
}
Also used : OntologyTermHitEntity(org.molgenis.ontology.sorta.bean.OntologyTermHitEntity) Entity(org.molgenis.data.Entity) QueryRule(org.molgenis.data.QueryRule)

Example 5 with QueryRule

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);
        }
    });
}
Also used : OntologyTermHitEntity(org.molgenis.ontology.sorta.bean.OntologyTermHitEntity) Entity(org.molgenis.data.Entity) QueryRule(org.molgenis.data.QueryRule)

Aggregations

QueryRule (org.molgenis.data.QueryRule)24 Entity (org.molgenis.data.Entity)16 Test (org.testng.annotations.Test)15 QueryImpl (org.molgenis.data.support.QueryImpl)7 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)5 Attribute (org.molgenis.data.meta.model.Attribute)4 EntityType (org.molgenis.data.meta.model.EntityType)4 OntologyTerm (org.molgenis.ontology.core.model.OntologyTerm)4 StringUtils (org.apache.commons.lang3.StringUtils)3 Operator (org.molgenis.data.QueryRule.Operator)3 OntologyTermHitEntity (org.molgenis.ontology.sorta.bean.OntologyTermHitEntity)3 Sets (com.google.common.collect.Sets)2 java.util (java.util)2 List (java.util.List)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Collectors (java.util.stream.Collectors)2 DataService (org.molgenis.data.DataService)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 FluentIterable (com.google.common.collect.FluentIterable)1 Iterables (com.google.common.collect.Iterables)1