use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class SemanticSearchServiceHelperTest method testCreateDisMaxQueryRule.
@Test
public void testCreateDisMaxQueryRule() {
List<String> createdTargetAttributeQueries = asList("Height", "Standing height in cm", "body_length", "Sitting height", "sitting_length", "Height", "sature");
QueryRule actualRule = semanticSearchServiceHelper.createDisMaxQueryRuleForTerms(createdTargetAttributeQueries);
String expectedQueryRuleToString = "DIS_MAX ('label' FUZZY_MATCH 'Height', 'description' FUZZY_MATCH 'Height', 'label' FUZZY_MATCH 'Standing height in cm', 'description' FUZZY_MATCH 'Standing height in cm', 'label' FUZZY_MATCH 'body_length', 'description' FUZZY_MATCH 'body_length', 'label' FUZZY_MATCH 'Sitting height', 'description' FUZZY_MATCH 'Sitting height', 'label' FUZZY_MATCH 'sitting_length', 'description' FUZZY_MATCH 'sitting_length', 'label' FUZZY_MATCH 'Height', 'description' FUZZY_MATCH 'Height', 'label' FUZZY_MATCH 'sature', 'description' FUZZY_MATCH 'sature')";
assertEquals(actualRule.getOperator(), QueryRule.Operator.DIS_MAX);
assertEquals(actualRule.toString(), expectedQueryRuleToString);
List<String> createdTargetAttributeQueries2 = singletonList("(Height) [stand^~]");
QueryRule actualRule2 = semanticSearchServiceHelper.createDisMaxQueryRuleForTerms(createdTargetAttributeQueries2);
String expectedQueryRuleToString2 = "DIS_MAX ('label' FUZZY_MATCH '\\(Height\\) \\[stand^\\~\\]', 'description' FUZZY_MATCH '\\(Height\\) \\[stand^\\~\\]')";
assertEquals(actualRule2.getOperator(), QueryRule.Operator.DIS_MAX);
assertEquals(actualRule2.toString(), expectedQueryRuleToString2);
}
use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class SortaServiceImplTest method beforeMethod.
@BeforeMethod
public void beforeMethod() {
// Mock ontology entity
Ontology ontology = ontologyFactory.create();
ontology.setOntologyIri(ONTOLOGY_IRI);
// define dataService actions for test one
when(dataService.findOne(ONTOLOGY, new QueryImpl<>().eq(OntologyMetaData.ONTOLOGY_IRI, ONTOLOGY_IRI))).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, FUZZY_MATCH, "hear")));
queryRule.setOperator(DIS_MAX);
when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>(queryRule))).thenReturn((long) 50);
QueryRule queryRule2 = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, FUZZY_MATCH, "impair")));
queryRule2.setOperator(DIS_MAX);
when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>(queryRule2))).thenReturn((long) 50);
when(dataService.findAll(ONTOLOGY)).thenReturn(Collections.<Entity>singletonList(ontology).stream());
// ########################### TEST ONE ###########################
// Mock the first ontology term entity only with name
OntologyTermSynonym ontologyTermSynonym0 = ontologyTermSynonymFactory.create();
ontologyTermSynonym0.setOntologyTermSynonym("hearing impairment");
OntologyTerm ontologyTerm0 = ontologyTermFactory.create();
ontologyTerm0.setId("1");
ontologyTerm0.setOntology(ontology);
ontologyTerm0.setOntologyTermName("hearing impairment");
ontologyTerm0.setOntologyTermIri(ONTOLOGY_IRI + '1');
ontologyTerm0.setOntologyTermSynonyms(singletonList(ontologyTermSynonym0));
ontologyTerm0.setOntologyTermDynamicAnnotations(emptyList());
// Mock the second ontology term entity only with name
OntologyTermSynonym ontologyTermSynonym1 = ontologyTermSynonymFactory.create();
ontologyTermSynonym1.setOntologyTermSynonym("mixed hearing impairment");
OntologyTerm ontologyTerm1 = ontologyTermFactory.create();
ontologyTerm1.setId("2");
ontologyTerm1.setOntology(ontology);
ontologyTerm1.setOntologyTermName("mixed hearing impairment");
ontologyTerm1.setOntologyTermIri(ONTOLOGY_IRI + '2');
ontologyTerm1.setOntologyTermSynonyms(singletonList(ontologyTermSynonym1));
ontologyTerm1.setOntologyTermDynamicAnnotations(emptyList());
// DataService action for regular matching ontology term synonyms
QueryRule disMaxRegularQueryRule = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, FUZZY_MATCH, "hear~0.8 impair~0.8")));
disMaxRegularQueryRule.setOperator(DIS_MAX);
List<QueryRule> finalQueryRules = asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, EQUALS, ontology), new QueryRule(AND), disMaxRegularQueryRule);
when(dataService.findAll(ONTOLOGY_TERM, new QueryImpl<>(finalQueryRules).pageSize(50))).thenReturn(Arrays.<Entity>asList(ontologyTerm0, ontologyTerm1).stream());
// DataService action for n-gram matching ontology term synonyms
QueryRule disMaxNGramQueryRule = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, FUZZY_MATCH_NGRAM, "hear impair")));
disMaxNGramQueryRule.setOperator(DIS_MAX);
when(dataService.findAll(ONTOLOGY_TERM, new QueryImpl<>(asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, EQUALS, ontology), new QueryRule(AND), disMaxNGramQueryRule)).pageSize(10))).thenReturn(Arrays.<Entity>asList(ontologyTerm0, ontologyTerm1).stream());
// DataService action for querying specific ontology term based on ontologyIRI and ontologyTermIRI
when(dataService.findOne(ONTOLOGY_TERM, new QueryImpl<>().eq(OntologyTermMetaData.ONTOLOGY_TERM_IRI, ONTOLOGY_IRI + '1').and().eq(OntologyTermMetaData.ONTOLOGY, ontology))).thenReturn(ontologyTerm0);
when(dataService.findOne(ONTOLOGY_TERM, new QueryImpl<>().eq(OntologyTermMetaData.ONTOLOGY_TERM_IRI, ONTOLOGY_IRI + '2').and().eq(OntologyTermMetaData.ONTOLOGY, ontology))).thenReturn(ontologyTerm1);
// ########################### TEST TWO ###########################
OntologyTermSynonym ontologyTermSynonym2 = ontologyTermSynonymFactory.create();
ontologyTermSynonym2.setOntologyTermSynonym("ot_3");
// Mock ontologyTermDynamicAnnotation entities
OntologyTermDynamicAnnotation ontologyTermDynamicAnnotation_3_1 = ontologyTermDynamicAnnotationFactory.create();
ontologyTermDynamicAnnotation_3_1.setName("OMIM");
ontologyTermDynamicAnnotation_3_1.setValue("123456");
ontologyTermDynamicAnnotation_3_1.setLabel("OMIM:123456");
// Mock ontologyTerm entity based on the previous entities defined
OntologyTerm ontologyTermEntity_3 = ontologyTermFactory.create();
ontologyTermEntity_3.setId("3");
ontologyTermEntity_3.setOntology(ontology);
ontologyTermEntity_3.setOntologyTermName("ot_3");
ontologyTermEntity_3.setOntologyTermIri(ONTOLOGY_IRI + '3');
ontologyTermEntity_3.setOntologyTermSynonyms(// self reference intended? Arrays.asList(ontologyTermEntity_3)
singletonList(ontologyTermSynonym2));
ontologyTermEntity_3.set(OntologyTermMetaData.ONTOLOGY_TERM_DYNAMIC_ANNOTATION, singletonList(ontologyTermDynamicAnnotation_3_1));
// DataService action for matching ontology term annotation
QueryRule annotationQueryRule = new QueryRule(asList(new QueryRule(OntologyTermDynamicAnnotationMetaData.NAME, EQUALS, "OMIM"), new QueryRule(AND), new QueryRule(OntologyTermDynamicAnnotationMetaData.VALUE, EQUALS, "123456")));
when(dataService.findAll(ONTOLOGY_TERM_DYNAMIC_ANNOTATION, new QueryImpl<>(singletonList(annotationQueryRule)).pageSize(Integer.MAX_VALUE))).thenReturn(Collections.<Entity>singletonList(ontologyTermDynamicAnnotation_3_1).stream());
when(dataService.findAll(ONTOLOGY_TERM, new QueryImpl<>(asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, EQUALS, ontology), new QueryRule(AND), new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_DYNAMIC_ANNOTATION, IN, singletonList(ontologyTermDynamicAnnotation_3_1)))).pageSize(Integer.MAX_VALUE))).thenReturn(Collections.<Entity>singletonList(ontologyTermEntity_3).stream());
// DataService action for elasticsearch regular matching ontology term synonyms
QueryRule disMaxRegularQueryRule_2 = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, FUZZY_MATCH, "input~0.8")));
disMaxRegularQueryRule_2.setOperator(DIS_MAX);
when(dataService.findAll(ONTOLOGY_TERM, new QueryImpl<>(asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, EQUALS, ontology), new QueryRule(AND), disMaxRegularQueryRule_2)).pageSize(49))).thenReturn(Stream.empty());
// DataService action for n-gram matching ontology term synonyms
QueryRule disMaxNGramQueryRule_2 = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, FUZZY_MATCH_NGRAM, "input")));
disMaxNGramQueryRule_2.setOperator(DIS_MAX);
when(dataService.findAll(ONTOLOGY_TERM, new QueryImpl<>(asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, EQUALS, ontology), new QueryRule(AND), disMaxNGramQueryRule_2)).pageSize(10))).thenReturn(Stream.empty());
// ########################### TEST THREE ###########################
// Define the input for test three
OntologyTermSynonym ontologyTermSynonym_4_1 = ontologyTermSynonymFactory.create();
ontologyTermSynonym_4_1.setOntologyTermSynonym("protruding eye");
OntologyTermSynonym ontologyTermSynonym_4_2 = ontologyTermSynonymFactory.create();
ontologyTermSynonym_4_2.setOntologyTermSynonym("proptosis");
OntologyTermSynonym ontologyTermSynonym_4_3 = ontologyTermSynonymFactory.create();
ontologyTermSynonym_4_3.setOntologyTermSynonym("Exophthalmos");
// Mock ontologyTerm entity based on the previous entities defined
OntologyTerm ontologyTermEntity_4 = ontologyTermFactory.create();
ontologyTermEntity_4.setId("4");
ontologyTermEntity_4.setOntology(ontology);
ontologyTermEntity_4.setOntologyTermName("protruding eye");
ontologyTermEntity_4.setOntologyTermIri(ONTOLOGY_IRI + '4');
ontologyTermEntity_4.setOntologyTermSynonyms(asList(ontologyTermSynonym_4_1, ontologyTermSynonym_4_2, ontologyTermSynonym_4_3));
ontologyTermEntity_4.setOntologyTermDynamicAnnotations(emptyList());
// DataService action for elasticsearch regular matching ontology term synonyms
QueryRule disMaxRegularQueryRule_3 = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, FUZZY_MATCH, "proptosi~0.8 protrud~0.8 ey~0.8 exophthalmo~0.8")));
disMaxRegularQueryRule_3.setOperator(DIS_MAX);
when(dataService.findAll(ONTOLOGY_TERM, new QueryImpl<>(asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, EQUALS, ontology), new QueryRule(AND), disMaxRegularQueryRule_3)).pageSize(50))).thenReturn(Collections.<Entity>singletonList(ontologyTermEntity_4).stream());
// DataService action for elasticsearch ngram matching ontology term synonyms
QueryRule disMaxNGramQueryRule_3 = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, FUZZY_MATCH_NGRAM, "proptosi protrud ey exophthalmo")));
disMaxNGramQueryRule_3.setOperator(QueryRule.Operator.DIS_MAX);
when(dataService.findAll(ONTOLOGY_TERM, new QueryImpl<>(asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, EQUALS, ontology), new QueryRule(AND), disMaxNGramQueryRule_3)).pageSize(10))).thenReturn(Collections.<Entity>singletonList(ontologyTermEntity_4).stream());
}
use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class SortaServiceImpl method annotationMatchOntologyTerms.
private void annotationMatchOntologyTerms(Entity inputEntity, Entity ontologyEntity, List<Entity> relevantEntities, List<QueryRule> rulesForOtherFields) {
List<Entity> ontologyTermAnnotationEntities = dataService.findAll(ONTOLOGY_TERM_DYNAMIC_ANNOTATION, new QueryImpl<>(rulesForOtherFields).pageSize(Integer.MAX_VALUE)).collect(Collectors.toList());
if (!ontologyTermAnnotationEntities.isEmpty()) {
List<QueryRule> rules = Arrays.asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, EQUALS, ontologyEntity), new QueryRule(AND), new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_DYNAMIC_ANNOTATION, IN, ontologyTermAnnotationEntities));
Stream<Entity> ontologyTermEntities = dataService.findAll(ONTOLOGY_TERM, new QueryImpl<>(rules).pageSize(Integer.MAX_VALUE));
List<Entity> relevantOntologyTermEntities = ontologyTermEntities.map(ontologyTermEntity -> calculateNGromOTAnnotations(inputEntity, ontologyTermEntity)).collect(Collectors.toList());
relevantEntities.addAll(relevantOntologyTermEntities);
}
}
use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class InformationContentServiceTest method redistributedNGramScore.
@Test
public void redistributedNGramScore() {
String ontologyIri = "http://www.molgenis.org";
Entity ontologyEntity = ontologyFactory.create();
ontologyEntity.set(OntologyMetaData.ONTOLOGY_IRI, ontologyIri);
when(dataService.findOne(ONTOLOGY, new QueryImpl<>().eq(OntologyMetaData.ONTOLOGY_IRI, ontologyIri))).thenReturn(ontologyEntity);
when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>().eq(OntologyTermMetaData.ONTOLOGY, ontologyEntity))).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, ontologyEntity), 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, ontologyEntity), new QueryRule(Operator.AND), queryRule2));
when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>(finalQuery2))).thenReturn((long) 10);
Map<String, Double> redistributedNGramScore = informationContentService.redistributedNGramScore("hearing impairment", ontologyIri);
Assert.assertEquals(redistributedNGramScore.get("hear").intValue(), -7);
Assert.assertEquals(redistributedNGramScore.get("impair").intValue(), 7);
}
use of org.molgenis.data.QueryRule in project molgenis by molgenis.
the class QueryValidatorTest method validateValidProvider.
@DataProvider(name = "validateValidProvider")
public static Iterator<Object[]> validateValidProvider() {
List<Object[]> queries = new ArrayList<>(256);
EnumSet.of(EQUALS).forEach(operator -> {
// BOOL
Entity boolEntityType = createEntityType(BOOL);
asList(Boolean.TRUE, Boolean.FALSE, null, "true", "false", "True", "False").forEach(value -> queries.add(new Object[] { boolEntityType, new QueryImpl<>().eq("attr", value) }));
// CATEGORICAL, XREF, CATEGORICAL_MREF, MREF, ONE_TO_MANY
EnumSet.of(STRING, INT, LONG, EMAIL, HYPERLINK).forEach(refIdAttrType -> EnumSet.of(CATEGORICAL, XREF, CATEGORICAL_MREF, MREF, ONE_TO_MANY).forEach(refAttrType -> {
Entity refEntityType = createEntityType(refAttrType, refIdAttrType);
asList("1", 1, 1L, null).forEach(idValue -> queries.add(new Object[] { refEntityType, new QueryImpl<>().eq("attr", idValue) }));
Entity refEntity = when(mock(Entity.class).getIdValue()).thenReturn("1").getMock();
queries.add(new Object[] { refEntityType, new QueryImpl<>().eq("attr", refEntity) });
}));
// DATE
Entity dateEntityType = createEntityType(DATE);
asList(LocalDate.now(), "2016-11-25", null).forEach(value -> queries.add(new Object[] { dateEntityType, new QueryImpl<>().eq("attr", value) }));
// DATE_TIME
Entity dateTimeEntityType = createEntityType(DATE_TIME);
asList(Instant.now(), "1985-08-12T11:12:13+0500", null).forEach(value -> queries.add(new Object[] { dateTimeEntityType, new QueryImpl<>().eq("attr", value) }));
// DECIMAL
Entity decimalEntityType = createEntityType(DECIMAL);
asList(1.23, "1.23", 1, 1L, null).forEach(value -> queries.add(new Object[] { decimalEntityType, new QueryImpl<>().eq("attr", value) }));
// EMAIL, HTML, HYPERLINK, SCRIPT, STRING, TEXT
EnumSet.of(EMAIL, HTML, HYPERLINK, SCRIPT, STRING, TEXT).forEach(attrType -> {
Entity entityType = createEntityType(attrType);
asList("abc", 1, 1L, 1.23, null).forEach(value -> queries.add(new Object[] { entityType, new QueryImpl<>().eq("attr", value) }));
});
// INT, LONG
EnumSet.of(INT, LONG).forEach(attrType -> {
Entity entityType = createEntityType(attrType);
asList(1, 1L, "1", null).forEach(value -> queries.add(new Object[] { entityType, new QueryImpl<>().eq("attr", value) }));
});
// FILE
Entity fileEntityType = createEntityType(FILE, STRING);
asList("file0", mock(FileMeta.class), null).forEach(idValue -> queries.add(new Object[] { fileEntityType, new QueryImpl<>().eq("attr", idValue) }));
// ENUM
Entity enumEntityType = createEntityType(ENUM);
asList(TestEnum.ENUM0, TestEnum.ENUM1, "ENUM0", "ENUM1", null).forEach(value -> queries.add(new Object[] { enumEntityType, new QueryImpl<>().eq("attr", value) }));
});
EnumSet.of(GREATER, GREATER_EQUAL, LESS, LESS_EQUAL).forEach(operator -> {
Entity entityType = createEntityType(INT);
QueryImpl<Entity> query = new QueryImpl<>();
query.addRule(new QueryRule("attr", operator, 1));
queries.add(new Object[] { entityType, query });
});
EnumSet.of(FUZZY_MATCH, FUZZY_MATCH_NGRAM, LIKE).forEach(operator -> {
Entity entityType = createEntityType(STRING);
QueryImpl<Entity> query = new QueryImpl<>();
query.addRule(new QueryRule("attr", operator, "abc"));
queries.add(new Object[] { entityType, query });
});
EnumSet.of(IN, RANGE).forEach(operator -> {
Entity entityType = createEntityType(INT);
QueryImpl<Entity> query = new QueryImpl<>();
query.addRule(new QueryRule("attr", operator, asList(1, 2)));
queries.add(new Object[] { entityType, query });
});
return queries.iterator();
}
Aggregations