Search in sources :

Example 6 with OntologyTerm

use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.

the class AlgorithmServiceImplIT method testCreateAttributeMappingIfOnlyOneMatch.

@Test
public void testCreateAttributeMappingIfOnlyOneMatch() {
    EntityType targetEntityType = entityTypeFactory.create("target");
    Attribute targetAttribute = attrMetaFactory.create().setName("targetHeight");
    targetAttribute.setDescription("height");
    targetEntityType.addAttribute(targetAttribute);
    EntityType sourceEntityType = entityTypeFactory.create("source");
    Attribute sourceAttribute = attrMetaFactory.create().setName("sourceHeight");
    sourceAttribute.setDescription("height");
    sourceEntityType.addAttribute(sourceAttribute);
    User owner = userFactory.create();
    owner.setUsername("flup");
    owner.setPassword("geheim");
    owner.setId("12345");
    owner.setActive(true);
    owner.setEmail("flup@blah.com");
    owner.setFirstName("Flup");
    owner.setLastName("de Flap");
    MappingProject project = new MappingProject("project", owner);
    project.addTarget(targetEntityType);
    EntityMapping mapping = project.getMappingTarget("target").addSource(sourceEntityType);
    Map<Attribute, ExplainedAttribute> matches = ImmutableMap.of(sourceAttribute, ExplainedAttribute.create(sourceAttribute, singletonList(ExplainedQueryString.create("height", "height", "height", 100)), true));
    LinkedHashMultimap<Relation, OntologyTerm> ontologyTermTags = LinkedHashMultimap.create();
    when(semanticSearchService.decisionTreeToFindRelevantAttributes(sourceEntityType, targetAttribute, ontologyTermTags.values(), null)).thenReturn(matches);
    when(ontologyTagService.getTagsForAttribute(targetEntityType, targetAttribute)).thenReturn(ontologyTermTags);
    algorithmService.autoGenerateAlgorithm(sourceEntityType, targetEntityType, mapping, targetAttribute);
    assertEquals(mapping.getAttributeMapping("targetHeight").getAlgorithm(), "$('sourceHeight').value();");
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityMapping(org.molgenis.semanticmapper.mapping.model.EntityMapping) MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) Relation(org.molgenis.data.semantic.Relation) User(org.molgenis.data.security.auth.User) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 7 with OntologyTerm

use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.

the class SemanticSearchServiceHelper method parseOntologyTermQueries.

/**
 * Create a list of string queries based on the information collected from current ontologyterm including label,
 * synonyms and child ontologyterms
 */
public List<String> parseOntologyTermQueries(OntologyTerm ontologyTerm) {
    List<String> queryTerms = getOtLabelAndSynonyms(ontologyTerm).stream().map(this::processQueryString).collect(Collectors.toList());
    for (OntologyTerm childOt : ontologyService.getChildren(ontologyTerm)) {
        double boostedNumber = Math.pow(0.5, ontologyService.getOntologyTermDistance(ontologyTerm, childOt));
        getOtLabelAndSynonyms(childOt).forEach(synonym -> queryTerms.add(parseBoostQueryString(synonym, boostedNumber)));
    }
    return queryTerms;
}
Also used : OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm)

Example 8 with OntologyTerm

use of org.molgenis.ontology.core.model.OntologyTerm 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;
}
Also used : NGramDistanceAlgorithm(org.molgenis.semanticsearch.string.NGramDistanceAlgorithm) Stemmer(org.molgenis.semanticsearch.string.Stemmer) java.util(java.util) EntityTypeMetadata(org.molgenis.data.meta.model.EntityTypeMetadata) TermFrequencyService(org.molgenis.ontology.core.ic.TermFrequencyService) Operator(org.molgenis.data.QueryRule.Operator) QueryImpl(org.molgenis.data.support.QueryImpl) StringUtils(org.apache.commons.lang3.StringUtils) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ENTITY_TYPE_META_DATA(org.molgenis.data.meta.model.EntityTypeMetadata.ENTITY_TYPE_META_DATA) MolgenisDataAccessException(org.molgenis.data.MolgenisDataAccessException) COMPOUND(org.molgenis.data.meta.AttributeType.COMPOUND) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Objects.requireNonNull(java.util.Objects.requireNonNull) DataService(org.molgenis.data.DataService) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) OntologyService(org.molgenis.ontology.core.service.OntologyService) Arrays.stream(java.util.Arrays.stream) QueryRule(org.molgenis.data.QueryRule) Entity(org.molgenis.data.Entity) QueryRule(org.molgenis.data.QueryRule)

Example 9 with OntologyTerm

use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.

the class SemanticSearchServiceImplTest method testFindAttributes.

@Test
public void testFindAttributes() {
    EntityType sourceEntityType = entityTypeFactory.create("sourceEntityType");
    // Mock the id's of the attribute entities that should be searched
    List<String> attributeIdentifiers = asList("1", "2");
    when(semanticSearchServiceHelper.getAttributeIdentifiers(sourceEntityType)).thenReturn(attributeIdentifiers);
    // Mock the createDisMaxQueryRule method
    List<QueryRule> rules = newArrayList();
    QueryRule targetQueryRuleLabel = new QueryRule(AttributeMetadata.LABEL, QueryRule.Operator.FUZZY_MATCH, "height");
    rules.add(targetQueryRuleLabel);
    QueryRule targetQueryRuleOntologyTermTag = new QueryRule(AttributeMetadata.LABEL, QueryRule.Operator.FUZZY_MATCH, "standing height");
    rules.add(targetQueryRuleOntologyTermTag);
    QueryRule targetQueryRuleOntologyTermTagSyn = new QueryRule(AttributeMetadata.LABEL, QueryRule.Operator.FUZZY_MATCH, "length");
    rules.add(targetQueryRuleOntologyTermTagSyn);
    QueryRule disMaxQueryRule = new QueryRule(rules);
    disMaxQueryRule.setOperator(QueryRule.Operator.DIS_MAX);
    when(semanticSearchServiceHelper.createDisMaxQueryRuleForAttribute(newHashSet("targetAttribute"), emptyList())).thenReturn(disMaxQueryRule);
    Entity entity1 = mock(Entity.class);
    when(entity1.getString(AttributeMetadata.NAME)).thenReturn("height_0");
    when(entity1.getString(AttributeMetadata.LABEL)).thenReturn("height");
    when(entity1.getString(AttributeMetadata.DESCRIPTION)).thenReturn("this is a height measurement in m!");
    List<QueryRule> disMaxQueryRules = newArrayList(new QueryRule(AttributeMetadata.ID, QueryRule.Operator.IN, attributeIdentifiers), new QueryRule(QueryRule.Operator.AND), disMaxQueryRule);
    Attribute attributeHeight = attrMetaDataFactory.create().setName("height_0");
    Attribute attributeWeight = attrMetaDataFactory.create().setName("weight_0");
    sourceEntityType.addAttribute(attributeHeight);
    sourceEntityType.addAttribute(attributeWeight);
    // Case 1
    when(dataService.findAll(ATTRIBUTE_META_DATA, new QueryImpl<>(disMaxQueryRules))).thenReturn(Stream.of(entity1));
    Map<Attribute, ExplainedAttribute> termsActual1 = semanticSearchService.findAttributes(sourceEntityType, newHashSet("targetAttribute"), emptyList());
    Map<Attribute, ExplainedAttribute> termsExpected1 = ImmutableMap.of(attributeHeight, ExplainedAttribute.create(attributeHeight));
    assertEquals(termsActual1.toString(), termsExpected1.toString());
    // Case 2
    when(dataService.findAll(ATTRIBUTE_META_DATA, new QueryImpl<>(disMaxQueryRules))).thenReturn(Stream.empty());
    Map<Attribute, ExplainedAttribute> termsActual2 = semanticSearchService.findAttributes(sourceEntityType, newHashSet("targetAttribute"), emptyList());
    Map<Attribute, ExplainedAttribute> termsExpected2 = ImmutableMap.of();
    assertEquals(termsActual2, termsExpected2);
    Mockito.reset(ontologyService);
    attribute.setDescription("Standing height (Ångstrøm)");
    when(ontologyService.findOntologyTerms(ontologies, ImmutableSet.of("standing", "height", "ångstrøm"), 100)).thenReturn(ontologyTerms);
    Hit<OntologyTerm> result = semanticSearchService.findTags(attribute, ontologies);
    assertEquals(result, Hit.create(standingHeight, 0.76471f));
}
Also used : ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Test(org.testng.annotations.Test)

Example 10 with OntologyTerm

use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.

the class OntologyTagServiceImpl method removeAttributeTag.

@Override
public void removeAttributeTag(EntityType entityType, SemanticTag<Attribute, OntologyTerm, Ontology> removeTag) {
    Attribute attribute = removeTag.getSubject();
    Entity attributeEntity = findAttributeEntity(entityType.getId(), attribute.getName());
    List<Entity> tags = new ArrayList<>();
    for (Entity tagEntity : attributeEntity.getEntities(AttributeMetadata.TAGS)) {
        SemanticTag<Attribute, OntologyTerm, Ontology> tag = asTag(attribute, tagEntity);
        if (!removeTag.equals(tag)) {
            tags.add(tagEntity);
        }
    }
    attributeEntity.set(AttributeMetadata.TAGS, tags);
    dataService.update(ATTRIBUTE_META_DATA, attributeEntity);
}
Also used : Entity(org.molgenis.data.Entity) Ontology(org.molgenis.ontology.core.model.Ontology) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm)

Aggregations

OntologyTerm (org.molgenis.ontology.core.model.OntologyTerm)30 Test (org.testng.annotations.Test)13 Relation (org.molgenis.data.semantic.Relation)9 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)8 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)8 Attribute (org.molgenis.data.meta.model.Attribute)7 EntityType (org.molgenis.data.meta.model.EntityType)7 Ontology (org.molgenis.ontology.core.model.Ontology)7 QueryRule (org.molgenis.data.QueryRule)6 Entity (org.molgenis.data.Entity)5 Sets (com.google.common.collect.Sets)3 java.util (java.util)3 Objects.requireNonNull (java.util.Objects.requireNonNull)3 Collectors (java.util.stream.Collectors)3 StringUtils (org.apache.commons.lang3.StringUtils)3 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)3 DataService (org.molgenis.data.DataService)3 Operator (org.molgenis.data.QueryRule.Operator)3 AttributeMetadata (org.molgenis.data.meta.model.AttributeMetadata)3 SemanticTag (org.molgenis.data.semantic.SemanticTag)3