Search in sources :

Example 1 with ExplainedAttribute

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();");
}
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 2 with ExplainedAttribute

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()));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 3 with ExplainedAttribute

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);
}
Also used : ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) AlgorithmState(org.molgenis.semanticmapper.mapping.model.AttributeMapping.AlgorithmState) AlgorithmTemplate(org.molgenis.semanticmapper.service.impl.AlgorithmTemplate)

Example 4 with ExplainedAttribute

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));
}
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 5 with ExplainedAttribute

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;
}
Also used : Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryRule(org.molgenis.data.QueryRule) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString)

Aggregations

ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)10 Attribute (org.molgenis.data.meta.model.Attribute)9 OntologyTerm (org.molgenis.ontology.core.model.OntologyTerm)5 Test (org.testng.annotations.Test)5 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)4 EntityType (org.molgenis.data.meta.model.EntityType)4 Relation (org.molgenis.data.semantic.Relation)4 ExplainedQueryString (org.molgenis.semanticsearch.explain.bean.ExplainedQueryString)3 User (org.molgenis.data.security.auth.User)2 ScriptParameter (org.molgenis.script.core.ScriptParameter)2 GeneratedAlgorithm (org.molgenis.semanticmapper.algorithmgenerator.bean.GeneratedAlgorithm)2 EntityMapping (org.molgenis.semanticmapper.mapping.model.EntityMapping)2 MappingProject (org.molgenis.semanticmapper.mapping.model.MappingProject)2 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Entity (org.molgenis.data.Entity)1 QueryRule (org.molgenis.data.QueryRule)1 QueryImpl (org.molgenis.data.support.QueryImpl)1 Script (org.molgenis.script.core.Script)1 RunAsSystem (org.molgenis.security.core.runas.RunAsSystem)1