Search in sources :

Example 16 with OntologyTerm

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

the class OntologyTermRepositoryTest method testGetOntologyTerm.

@Test
public void testGetOntologyTerm() {
    when(dataService.findOne(ONTOLOGY_TERM, QueryImpl.EQ(ONTOLOGY_TERM_IRI, "http://www.test.nl/iri"))).thenReturn(ontologyTermEntity);
    String[] iris = { "http://www.test.nl/iri" };
    OntologyTerm ontologyTerm = ontologyTermRepository.getOntologyTerm(iris);
    assertEquals(ontologyTerm, OntologyTerm.create("http://www.test.nl/iri", "Ontology term", singletonList("Ontology term")));
}
Also used : OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 17 with OntologyTerm

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

the class UnitResolverImpl method resolveUnit.

@Override
public Unit<? extends Quantity> resolveUnit(Attribute attr, EntityType entityType) {
    Set<String> tokens = tokenize(attr.getLabel(), attr.getDescription());
    // Option 1: Check if a term matches a unit
    Unit<? extends Quantity> unit = null;
    if (!tokens.isEmpty()) {
        for (String term : tokens) {
            try {
                unit = Unit.valueOf(term);
                break;
            } catch (IllegalArgumentException e) {
            // noop
            }
        }
        if (isUnitEmpty(unit)) {
            // Option 2: Search unit ontology for a match
            OntologyTerm unitOntologyTerm = resolveUnitOntologyTerm(tokens.stream().map(this::convertNumberToOntologyTermStyle).collect(Collectors.toSet()));
            if (unitOntologyTerm != null) {
                // try label + synonym labels until hit
                for (String synonymLabel : unitOntologyTerm.getSynonyms()) {
                    try {
                        unit = Unit.valueOf(synonymLabel);
                        break;
                    } catch (IllegalArgumentException e) {
                    // noop
                    }
                }
            }
        }
    }
    if (isUnitEmpty(unit)) {
        unit = null;
    }
    return unit;
}
Also used : OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm)

Example 18 with OntologyTerm

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

the class UnitResolverImpl method resolveUnitOntologyTerm.

private OntologyTerm resolveUnitOntologyTerm(Set<String> tokens) {
    OntologyTerm unitOntologyTerm;
    Ontology unitOntology = ontologyService.getOntology(UNIT_ONTOLOGY_IRI);
    if (unitOntology != null) {
        if (!tokens.isEmpty()) {
            List<String> ontologyIds = Arrays.asList(unitOntology.getId());
            List<OntologyTerm> ontologyTerms = ontologyService.findExcatOntologyTerms(ontologyIds, tokens, Integer.MAX_VALUE);
            if (ontologyTerms != null && !ontologyTerms.isEmpty()) {
                if (ontologyTerms.size() == 1) {
                    unitOntologyTerm = ontologyTerms.get(0);
                } else {
                    // multiple unit ontology terms detected, pick first
                    unitOntologyTerm = ontologyTerms.get(0);
                }
            } else {
                unitOntologyTerm = null;
            }
        } else {
            unitOntologyTerm = null;
        }
    } else {
        LOG.warn("Unit resolver is missing required unit ontology [" + UNIT_ONTOLOGY_IRI + "]");
        unitOntologyTerm = null;
    }
    return unitOntologyTerm;
}
Also used : Ontology(org.molgenis.ontology.core.model.Ontology) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm)

Example 19 with OntologyTerm

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

the class AlgorithmServiceImpl method autoGenerateAlgorithm.

@Override
@RunAsSystem
public void autoGenerateAlgorithm(EntityType sourceEntityType, EntityType targetEntityType, EntityMapping mapping, Attribute targetAttribute) {
    LOG.debug("createAttributeMappingIfOnlyOneMatch: target= " + targetAttribute.getName());
    Multimap<Relation, OntologyTerm> tagsForAttribute = ontologyTagService.getTagsForAttribute(targetEntityType, targetAttribute);
    Map<Attribute, ExplainedAttribute> relevantAttributes = semanticSearchService.decisionTreeToFindRelevantAttributes(sourceEntityType, targetAttribute, tagsForAttribute.values(), null);
    GeneratedAlgorithm generatedAlgorithm = algorithmGeneratorService.generate(targetAttribute, relevantAttributes, targetEntityType, sourceEntityType);
    if (StringUtils.isNotBlank(generatedAlgorithm.getAlgorithm())) {
        AttributeMapping attributeMapping = mapping.addAttributeMapping(targetAttribute.getName());
        attributeMapping.setAlgorithm(generatedAlgorithm.getAlgorithm());
        attributeMapping.getSourceAttributes().addAll(generatedAlgorithm.getSourceAttributes());
        attributeMapping.setAlgorithmState(generatedAlgorithm.getAlgorithmState());
        LOG.debug("Creating attribute mapping: " + targetAttribute.getName() + " = " + generatedAlgorithm.getAlgorithm());
    }
}
Also used : Relation(org.molgenis.data.semantic.Relation) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) AttributeMapping(org.molgenis.semanticmapper.mapping.model.AttributeMapping) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) GeneratedAlgorithm(org.molgenis.semanticmapper.algorithmgenerator.bean.GeneratedAlgorithm) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 20 with OntologyTerm

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

the class MappingServiceController method getSemanticSearchAttributeMapping.

/**
 * This controller will first of all check if the user-defined search terms exist. If so, the searchTerms will be
 * used directly in the SemanticSearchService. If the searchTerms are not defined by users, it will use the
 * ontologyTermTags in the SemantiSearchService. If neither of the searchTerms and the OntologyTermTags exist, it
 * will use the information from the targetAttribute in the SemanticSearchService
 * <p>
 * If string terms are sent to the SemanticSearchService, they will be first of all converted to the ontologyTerms
 * using findTag method
 */
@PostMapping(value = "/attributeMapping/semanticsearch", consumes = APPLICATION_JSON_VALUE)
@ResponseBody
public List<ExplainedAttribute> getSemanticSearchAttributeMapping(@RequestBody Map<String, String> requestBody) {
    String mappingProjectId = requestBody.get("mappingProjectId");
    String target = requestBody.get("target");
    String source = requestBody.get("source");
    String targetAttributeName = requestBody.get("targetAttribute");
    String searchTermsString = requestBody.get("searchTerms");
    Set<String> searchTerms = new HashSet<>();
    if (StringUtils.isNotBlank(searchTermsString)) {
        searchTerms.addAll(Sets.newHashSet(searchTermsString.toLowerCase().split("\\s+or\\s+")).stream().filter(StringUtils::isNotBlank).map(String::trim).collect(Collectors.toSet()));
    }
    MappingProject project = mappingService.getMappingProject(mappingProjectId);
    MappingTarget mappingTarget = project.getMappingTarget(target);
    EntityMapping entityMapping = mappingTarget.getMappingForSource(source);
    Attribute targetAttribute = entityMapping.getTargetEntityType().getAttribute(targetAttributeName);
    // Find relevant attributes base on tags
    Multimap<Relation, OntologyTerm> tagsForAttribute = ontologyTagService.getTagsForAttribute(entityMapping.getTargetEntityType(), targetAttribute);
    Map<Attribute, ExplainedAttribute> relevantAttributes = semanticSearchService.decisionTreeToFindRelevantAttributes(entityMapping.getSourceEntityType(), targetAttribute, tagsForAttribute.values(), searchTerms);
    // If no relevant attributes are found, return all source attributes
    if (relevantAttributes.isEmpty()) {
        return stream(entityMapping.getSourceEntityType().getAllAttributes()).map(ExplainedAttribute::create).collect(toList());
    }
    return newArrayList(relevantAttributes.values());
}
Also used : Relation(org.molgenis.data.semantic.Relation) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) 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