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();");
}
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;
}
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;
}
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));
}
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);
}
Aggregations