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