use of org.molgenis.semanticsearch.explain.bean.ExplainedAttribute 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.semanticsearch.explain.bean.ExplainedAttribute in project molgenis by molgenis.
the class AlgorithmTemplateServiceImplTest method find.
@Test
public void find() {
String sourceAttr0Name = "sourceAttr0";
String sourceAttr1Name = "sourceAttr1";
EntityType sourceEntityMeta = entityTypeFactory.create("source");
Attribute sourceAttr0 = attrMetaFactory.create().setName(sourceAttr0Name);
Attribute sourceAttr1 = attrMetaFactory.create().setName(sourceAttr1Name);
sourceEntityMeta.addAttribute(sourceAttr0);
sourceEntityMeta.addAttribute(sourceAttr1);
ExplainedQueryString sourceAttr0Explain = ExplainedQueryString.create("a", "b", param0Name, 1.0);
ExplainedQueryString sourceAttr1Explain = ExplainedQueryString.create("a", "b", param1Name, 0.5);
Map<Attribute, ExplainedAttribute> attrResults = Maps.newHashMap();
attrResults.put(sourceAttr0, ExplainedAttribute.create(sourceAttr0, singletonList(sourceAttr0Explain), false));
attrResults.put(sourceAttr1, ExplainedAttribute.create(sourceAttr1, singletonList(sourceAttr1Explain), false));
Stream<AlgorithmTemplate> templateStream = algorithmTemplateServiceImpl.find(attrResults);
Map<String, String> model = Maps.newHashMap();
model.put(param0Name, sourceAttr0Name);
model.put(param1Name, sourceAttr1Name);
AlgorithmTemplate expectedAlgorithmTemplate = new AlgorithmTemplate(script0, model);
assertEquals(templateStream.collect(Collectors.toList()), Stream.of(expectedAlgorithmTemplate).collect(Collectors.toList()));
}
use of org.molgenis.semanticsearch.explain.bean.ExplainedAttribute in project molgenis by molgenis.
the class AlgorithmGeneratorServiceImpl method generate.
@Override
public GeneratedAlgorithm generate(Attribute targetAttribute, Map<Attribute, ExplainedAttribute> sourceAttributes, EntityType targetEntityType, EntityType sourceEntityType) {
String algorithm = StringUtils.EMPTY;
AlgorithmState algorithmState = null;
Set<Attribute> mappedSourceAttributes = null;
if (sourceAttributes.size() > 0) {
AlgorithmTemplate algorithmTemplate = algorithmTemplateService.find(sourceAttributes).findFirst().orElse(null);
if (algorithmTemplate != null) {
algorithm = algorithmTemplate.render();
mappedSourceAttributes = AlgorithmGeneratorHelper.extractSourceAttributesFromAlgorithm(algorithm, sourceEntityType);
algorithm = convertUnitForTemplateAlgorithm(algorithm, targetAttribute, targetEntityType, mappedSourceAttributes, sourceEntityType);
algorithmState = GENERATED_HIGH;
} else {
Entry<Attribute, ExplainedAttribute> firstEntry = sourceAttributes.entrySet().stream().findFirst().get();
Attribute sourceAttribute = firstEntry.getKey();
algorithm = generate(targetAttribute, Arrays.asList(sourceAttribute), targetEntityType, sourceEntityType);
mappedSourceAttributes = AlgorithmGeneratorHelper.extractSourceAttributesFromAlgorithm(algorithm, sourceEntityType);
algorithmState = firstEntry.getValue().isHighQuality() ? GENERATED_HIGH : GENERATED_LOW;
}
}
return GeneratedAlgorithm.create(algorithm, mappedSourceAttributes, algorithmState);
}
use of org.molgenis.semanticsearch.explain.bean.ExplainedAttribute 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.semanticsearch.explain.bean.ExplainedAttribute in project molgenis by molgenis.
the class SemanticSearchServiceImpl method findAttributes.
@Override
public Map<Attribute, ExplainedAttribute> findAttributes(EntityType sourceEntityType, Set<String> queryTerms, Collection<OntologyTerm> ontologyTerms) {
Iterable<String> attributeIdentifiers = semanticSearchServiceHelper.getAttributeIdentifiers(sourceEntityType);
QueryRule disMaxQueryRule = semanticSearchServiceHelper.createDisMaxQueryRuleForAttribute(queryTerms, ontologyTerms);
List<QueryRule> finalQueryRules = Lists.newArrayList(new QueryRule(AttributeMetadata.ID, Operator.IN, attributeIdentifiers));
if (disMaxQueryRule.getNestedRules().size() > 0) {
finalQueryRules.addAll(Arrays.asList(new QueryRule(Operator.AND), disMaxQueryRule));
}
Stream<Entity> attributeEntities = dataService.findAll(ATTRIBUTE_META_DATA, new QueryImpl<>(finalQueryRules));
Map<String, String> collectExpanedQueryMap = semanticSearchServiceHelper.collectExpandedQueryMap(queryTerms, ontologyTerms);
// Because the explain-API can be computationally expensive we limit the explanation to the top 10 attributes
Map<Attribute, ExplainedAttribute> explainedAttributes = new LinkedHashMap<>();
AtomicInteger count = new AtomicInteger(0);
attributeEntities.forEach(attributeEntity -> {
Attribute attribute = sourceEntityType.getAttribute(attributeEntity.getString(AttributeMetadata.NAME));
if (count.get() < MAX_NUMBER_EXPLAINED_ATTRIBUTES) {
Set<ExplainedQueryString> explanations = convertAttributeToExplainedAttribute(attribute, collectExpanedQueryMap, new QueryImpl<>(finalQueryRules));
boolean singleMatchHighQuality = isSingleMatchHighQuality(queryTerms, Sets.newHashSet(collectExpanedQueryMap.values()), explanations);
explainedAttributes.put(attribute, ExplainedAttribute.create(attribute, explanations, singleMatchHighQuality));
} else {
explainedAttributes.put(attribute, ExplainedAttribute.create(attribute));
}
count.incrementAndGet();
});
return explainedAttributes;
}
Aggregations