Search in sources :

Example 11 with Relation

use of org.molgenis.data.semantic.Relation in project molgenis by molgenis.

the class OntologyTagServiceImpl method addAttributeTag.

@Override
public OntologyTag addAttributeTag(String entity, String attribute, String relationIRI, List<String> ontologyTermIRIs) {
    boolean added = false;
    Entity attributeEntity = findAttributeEntity(entity, attribute);
    Tag tag = new Tag(tagMetadata);
    Stream<OntologyTerm> terms = ontologyTermIRIs.stream().map(ontologyService::getOntologyTerm);
    OntologyTerm combinedOntologyTerm = OntologyTerm.and(terms.toArray(OntologyTerm[]::new));
    Relation relation = Relation.forIRI(relationIRI);
    tag.setId(idGenerator.generateId());
    tag.setCodeSystem(null);
    tag.setRelationIri(relation.getIRI());
    tag.setRelationLabel(relation.getLabel());
    tag.setLabel(combinedOntologyTerm.getLabel());
    tag.setObjectIri(combinedOntologyTerm.getIRI());
    dataService.add(TAG, tag);
    Map<String, Entity> tags = Maps.newHashMap();
    for (Entity attrTag : attributeEntity.getEntities(AttributeMetadata.TAGS)) {
        tags.put(attrTag.get(TagMetadata.OBJECT_IRI).toString(), attrTag);
    }
    if (!tags.containsKey(tag.get(TagMetadata.OBJECT_IRI).toString())) {
        tags.put(tag.get(TagMetadata.OBJECT_IRI).toString(), tag);
        added = true;
    }
    attributeEntity.set(AttributeMetadata.TAGS, tags.values());
    dataService.update(ATTRIBUTE_META_DATA, attributeEntity);
    updateEntityTypeEntityWithNewAttributeEntity(entity, attribute, attributeEntity);
    return added ? OntologyTag.create(combinedOntologyTerm, relation) : null;
}
Also used : Entity(org.molgenis.data.Entity) Relation(org.molgenis.data.semantic.Relation) SemanticTag(org.molgenis.data.semantic.SemanticTag) OntologyTag(org.molgenis.semanticsearch.semantic.OntologyTag) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm)

Example 12 with Relation

use of org.molgenis.data.semantic.Relation 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 13 with Relation

use of org.molgenis.data.semantic.Relation 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 14 with Relation

use of org.molgenis.data.semantic.Relation in project molgenis by molgenis.

the class MappingServiceController method viewAttributeMapping.

/**
 * Displays an {@link AttributeMapping}
 *
 * @param mappingProjectId ID of the {@link MappingProject}
 * @param target           name of the target entity
 * @param source           name of the source entity
 * @param targetAttribute  name of the target attribute
 */
@GetMapping("/attributeMapping")
public String viewAttributeMapping(@RequestParam() String mappingProjectId, @RequestParam() String target, @RequestParam() String source, @RequestParam() String targetAttribute, Model model) {
    MappingProject project = mappingService.getMappingProject(mappingProjectId);
    MappingTarget mappingTarget = project.getMappingTarget(target);
    EntityMapping entityMapping = mappingTarget.getMappingForSource(source);
    AttributeMapping attributeMapping = entityMapping.getAttributeMapping(targetAttribute);
    if (attributeMapping == null) {
        attributeMapping = entityMapping.addAttributeMapping(targetAttribute);
    }
    EntityType refEntityType = attributeMapping.getTargetAttribute().getRefEntity();
    if (refEntityType != null) {
        Iterable<Entity> refEntities = () -> dataService.findAll(refEntityType.getId()).iterator();
        model.addAttribute("categories", refEntities);
    }
    Multimap<Relation, OntologyTerm> tagsForAttribute = ontologyTagService.getTagsForAttribute(entityMapping.getTargetEntityType(), attributeMapping.getTargetAttribute());
    model.addAttribute("tags", tagsForAttribute.values());
    model.addAttribute("dataExplorerUri", menuReaderService.getMenu().findMenuItemPath(DataExplorerController.ID));
    model.addAttribute("mappingProject", project);
    model.addAttribute("entityMapping", entityMapping);
    model.addAttribute("sourceAttributesSize", Iterables.size(entityMapping.getSourceEntityType().getAtomicAttributes()));
    model.addAttribute("attributeMapping", attributeMapping);
    model.addAttribute("attributes", newArrayList(dataService.getEntityType(source).getAtomicAttributes()));
    model.addAttribute("hasWritePermission", hasWritePermission(project, false));
    return VIEW_ATTRIBUTE_MAPPING;
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) ResponseEntity(org.springframework.http.ResponseEntity) Relation(org.molgenis.data.semantic.Relation) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm)

Example 15 with Relation

use of org.molgenis.data.semantic.Relation in project molgenis by molgenis.

the class EntityModelWriterTest method testCreateRfdModelSTRINGKeywords.

@Test
public void testCreateRfdModelSTRINGKeywords() {
    Entity objectEntity = mock(Entity.class);
    EntityType entityType = mock(EntityType.class);
    Attribute attribute = mock(Attribute.class);
    List<Attribute> attributeList = singletonList(attribute);
    when(objectEntity.getEntityType()).thenReturn(entityType);
    String value = "molgenis,genetics,fair";
    when(objectEntity.get("attributeName")).thenReturn(value);
    when(objectEntity.getString("attributeName")).thenReturn(value);
    when(entityType.getAtomicAttributes()).thenReturn(attributeList);
    when(attribute.getName()).thenReturn("attributeName");
    when(attribute.getDataType()).thenReturn(AttributeType.STRING);
    LabeledResource tag = new LabeledResource("http://www.w3.org/ns/dcat#keyword", "keywords");
    Multimap<Relation, LabeledResource> tags = ImmutableMultimap.of(Relation.isAssociatedWith, tag);
    when(tagService.getTagsForAttribute(entityType, attribute)).thenReturn(tags);
    Model result = writer.createRdfModel("http://molgenis01.gcc.rug.nl/fdp/catolog/test/this", objectEntity);
    assertEquals(result.size(), 3);
    List<String> statements = result.stream().map(Statement::toString).collect(toList());
    assertEquals(statements, Arrays.asList("(http://molgenis01.gcc.rug.nl/fdp/catolog/test/this, http://www.w3.org/ns/dcat#keyword, \"molgenis\"^^<http://www.w3.org/2001/XMLSchema#string>) [null]", "(http://molgenis01.gcc.rug.nl/fdp/catolog/test/this, http://www.w3.org/ns/dcat#keyword, \"genetics\"^^<http://www.w3.org/2001/XMLSchema#string>) [null]", "(http://molgenis01.gcc.rug.nl/fdp/catolog/test/this, http://www.w3.org/ns/dcat#keyword, \"fair\"^^<http://www.w3.org/2001/XMLSchema#string>) [null]"));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) Relation(org.molgenis.data.semantic.Relation) LabeledResource(org.molgenis.data.semantic.LabeledResource) Attribute(org.molgenis.data.meta.model.Attribute) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Aggregations

Relation (org.molgenis.data.semantic.Relation)22 Attribute (org.molgenis.data.meta.model.Attribute)15 Test (org.testng.annotations.Test)15 LabeledResource (org.molgenis.data.semantic.LabeledResource)12 Model (org.eclipse.rdf4j.model.Model)11 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)11 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)11 Iterator (java.util.Iterator)10 EntityType (org.molgenis.data.meta.model.EntityType)10 Entity (org.molgenis.data.Entity)9 OntologyTerm (org.molgenis.ontology.core.model.OntologyTerm)9 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)4 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)4 User (org.molgenis.data.security.auth.User)2 SemanticTag (org.molgenis.data.semantic.SemanticTag)2 Ontology (org.molgenis.ontology.core.model.Ontology)2 RunAsSystem (org.molgenis.security.core.runas.RunAsSystem)2 EntityMapping (org.molgenis.semanticmapper.mapping.model.EntityMapping)2 MappingProject (org.molgenis.semanticmapper.mapping.model.MappingProject)2 Instant (java.time.Instant)1