Search in sources :

Example 6 with ExplainedAttribute

use of org.molgenis.semanticsearch.explain.bean.ExplainedAttribute in project molgenis by molgenis.

the class AlgorithmTemplateServiceImpl method toAlgorithmTemplate.

private Stream<AlgorithmTemplate> toAlgorithmTemplate(Script script, Map<Attribute, ExplainedAttribute> attrMatches) {
    // find attribute for each parameter
    boolean paramMatch = true;
    Map<String, String> model = new HashMap<>();
    for (ScriptParameter param : script.getParameters()) {
        Attribute attr = mapParamToAttribute(param, attrMatches);
        if (attr != null) {
            model.put(param.getName(), attr.getName());
        } else {
            paramMatch = false;
            break;
        }
    }
    // create algorithm template if an attribute was found for all parameters
    AlgorithmTemplate algorithmTemplate = new AlgorithmTemplate(script, model);
    return paramMatch ? Stream.of(algorithmTemplate) : Stream.empty();
}
Also used : HashMap(java.util.HashMap) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) ScriptParameter(org.molgenis.script.core.ScriptParameter)

Example 7 with ExplainedAttribute

use of org.molgenis.semanticsearch.explain.bean.ExplainedAttribute 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 8 with ExplainedAttribute

use of org.molgenis.semanticsearch.explain.bean.ExplainedAttribute 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)

Example 9 with ExplainedAttribute

use of org.molgenis.semanticsearch.explain.bean.ExplainedAttribute in project molgenis by molgenis.

the class AlgorithmGeneratorServiceImplTest method testGenerateTemplateBasedAlgorithm.

@Test
public void testGenerateTemplateBasedAlgorithm() {
    EntityType targetEntityType = entityTypeFactory.create("target");
    Attribute targetBMIAttribute = attrMetaFactory.create().setName("targetHeight");
    targetBMIAttribute.setLabel("BMI kg/m²");
    targetBMIAttribute.setDataType(DECIMAL);
    targetEntityType.addAttribute(targetBMIAttribute);
    EntityType sourceEntityType = entityTypeFactory.create("source");
    Attribute heightSourceAttribute = attrMetaFactory.create().setName("sourceHeight");
    heightSourceAttribute.setDataType(DECIMAL);
    heightSourceAttribute.setLabel("body length in cm");
    Attribute weightSourceAttribute = attrMetaFactory.create().setName("sourceWeight");
    weightSourceAttribute.setDataType(DECIMAL);
    weightSourceAttribute.setLabel("weight in kg");
    sourceEntityType.addAttribute(heightSourceAttribute);
    sourceEntityType.addAttribute(weightSourceAttribute);
    Map<Attribute, ExplainedAttribute> sourceAttributes = ImmutableMap.of(heightSourceAttribute, ExplainedAttribute.create(heightSourceAttribute, singletonList(ExplainedQueryString.create("height", "height", "height", 100)), true), weightSourceAttribute, ExplainedAttribute.create(heightSourceAttribute, Collections.singletonList(ExplainedQueryString.create("weight", "weight", "weight", 100)), true));
    Script script = mock(Script.class);
    ScriptParameter heightParameter = mock(ScriptParameter.class);
    when(heightParameter.getName()).thenReturn("height");
    ScriptParameter weightParameter = mock(ScriptParameter.class);
    when(weightParameter.getName()).thenReturn("weight");
    when(script.getParameters()).thenReturn(asList(heightParameter, weightParameter));
    when(script.getContent()).thenReturn("$('weight').div($('height').pow(2)).value()");
    when(dataService.findAll(SCRIPT, new QueryImpl<Script>().eq(TYPE, JsMagmaScriptRunner.NAME), Script.class)).thenReturn(Stream.of(script));
    GeneratedAlgorithm generate = algorithmGeneratorService.generate(targetBMIAttribute, sourceAttributes, targetEntityType, sourceEntityType);
    assertEquals(generate.getAlgorithm(), "$('sourceWeight').div($('sourceHeight').div(100.0).pow(2)).value()");
    assertEquals(generate.getAlgorithmState(), AttributeMapping.AlgorithmState.GENERATED_HIGH);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Script(org.molgenis.script.core.Script) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) QueryImpl(org.molgenis.data.support.QueryImpl) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) ScriptParameter(org.molgenis.script.core.ScriptParameter) GeneratedAlgorithm(org.molgenis.semanticmapper.algorithmgenerator.bean.GeneratedAlgorithm) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 10 with ExplainedAttribute

use of org.molgenis.semanticsearch.explain.bean.ExplainedAttribute in project molgenis by molgenis.

the class AlgorithmServiceImplIT method testWhenSourceHasMultipleMatchesThenFirstMappingGetsCreated.

@Test
public void testWhenSourceHasMultipleMatchesThenFirstMappingGetsCreated() {
    EntityType targetEntityType = entityTypeFactory.create("target");
    Attribute targetAttribute = attrMetaFactory.create().setName("targetHeight");
    targetAttribute.setDescription("height");
    targetEntityType.addAttribute(targetAttribute);
    EntityType sourceEntityType = entityTypeFactory.create("source");
    Attribute sourceAttribute1 = attrMetaFactory.create().setName("sourceHeight1");
    sourceAttribute1.setDescription("height");
    Attribute sourceAttribute2 = attrMetaFactory.create().setName("sourceHeight2");
    sourceAttribute2.setDescription("height");
    sourceEntityType.addAttributes(Arrays.asList(sourceAttribute1, sourceAttribute2));
    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> mappings = ImmutableMap.of(sourceAttribute1, ExplainedAttribute.create(sourceAttribute1), sourceAttribute2, ExplainedAttribute.create(sourceAttribute2));
    LinkedHashMultimap<Relation, OntologyTerm> ontologyTermTags = LinkedHashMultimap.create();
    when(semanticSearchService.decisionTreeToFindRelevantAttributes(sourceEntityType, targetAttribute, ontologyTermTags.values(), null)).thenReturn(mappings);
    when(ontologyTagService.getTagsForAttribute(targetEntityType, targetAttribute)).thenReturn(ontologyTermTags);
    algorithmService.autoGenerateAlgorithm(sourceEntityType, targetEntityType, mapping, targetAttribute);
    assertEquals(mapping.getAttributeMapping("targetHeight").getSourceAttributes().get(0), sourceAttribute1);
}
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)

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