Search in sources :

Example 6 with AttributeMapping

use of org.molgenis.semanticmapper.mapping.model.AttributeMapping in project molgenis by molgenis.

the class AlgorithmServiceImplIT method testApplyMrefNillable.

@Test
public void testApplyMrefNillable() throws ParseException {
    String refEntityName = "refEntity";
    String refEntityIdAttrName = "id";
    String refEntityLabelAttrName = "label";
    String sourceEntityName = "source";
    String sourceEntityAttrName = "mref-source";
    String targetEntityAttrName = "mref-target";
    // ref entities
    EntityType refEntityType = entityTypeFactory.create(refEntityName);
    refEntityType.addAttribute(attrMetaFactory.create().setName(refEntityIdAttrName), ROLE_ID);
    refEntityType.addAttribute(attrMetaFactory.create().setName(refEntityLabelAttrName), ROLE_LABEL);
    // mapping
    Attribute targetAttribute = attrMetaFactory.create().setName(targetEntityAttrName);
    targetAttribute.setDataType(MREF).setNillable(true).setRefEntity(refEntityType);
    AttributeMapping attributeMapping = new AttributeMapping(targetAttribute);
    attributeMapping.setAlgorithm("$('" + sourceEntityAttrName + "').value()");
    // source Entity
    EntityType entityTypeSource = entityTypeFactory.create(sourceEntityName);
    entityTypeSource.addAttribute(attrMetaFactory.create().setName(refEntityIdAttrName).setDataType(INT).setAuto(true), ROLE_ID);
    entityTypeSource.addAttribute(attrMetaFactory.create().setName(sourceEntityAttrName).setDataType(MREF).setNillable(true).setRefEntity(refEntityType));
    Entity source = new DynamicEntity(entityTypeSource);
    source.set(sourceEntityAttrName, emptyList());
    Object result = algorithmService.apply(attributeMapping, source, entityTypeSource);
    assertEquals(result, emptyList());
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) DynamicEntity(org.molgenis.data.support.DynamicEntity) AttributeMapping(org.molgenis.semanticmapper.mapping.model.AttributeMapping) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 7 with AttributeMapping

use of org.molgenis.semanticmapper.mapping.model.AttributeMapping in project molgenis by molgenis.

the class AlgorithmServiceImplIT method testApplyMref.

@Test
public void testApplyMref() throws ParseException {
    String refEntityName = "refEntity";
    String refEntityIdAttrName = "id";
    String refEntityLabelAttrName = "label";
    String refEntityId0 = "id0";
    String refEntityId1 = "id1";
    String sourceEntityName = "source";
    String sourceEntityAttrName = "mref-source";
    String targetEntityAttrName = "mref-target";
    // ref entities
    EntityType refEntityType = entityTypeFactory.create(refEntityName);
    refEntityType.addAttribute(attrMetaFactory.create().setName(refEntityIdAttrName), ROLE_ID);
    refEntityType.addAttribute(attrMetaFactory.create().setName(refEntityLabelAttrName).setDataType(STRING), ROLE_LABEL);
    Entity refEntity0 = new DynamicEntity(refEntityType);
    refEntity0.set(refEntityIdAttrName, refEntityId0);
    refEntity0.set(refEntityLabelAttrName, "label0");
    Entity refEntity1 = new DynamicEntity(refEntityType);
    refEntity1.set(refEntityIdAttrName, refEntityId1);
    refEntity1.set(refEntityLabelAttrName, "label1");
    // mapping
    Attribute targetAttribute = attrMetaFactory.create().setName(targetEntityAttrName);
    targetAttribute.setDataType(MREF).setNillable(false).setRefEntity(refEntityType);
    AttributeMapping attributeMapping = new AttributeMapping(targetAttribute);
    attributeMapping.setAlgorithm("$('" + sourceEntityAttrName + "').value()");
    when(entityManager.getReference(refEntityType, refEntityId0)).thenReturn(refEntity0);
    when(entityManager.getReference(refEntityType, refEntityId1)).thenReturn(refEntity1);
    // source Entity
    EntityType entityTypeSource = entityTypeFactory.create(sourceEntityName);
    entityTypeSource.addAttribute(attrMetaFactory.create().setName(refEntityIdAttrName).setDataType(INT).setAuto(true), ROLE_ID);
    entityTypeSource.addAttribute(attrMetaFactory.create().setName(sourceEntityAttrName).setDataType(MREF).setNillable(false).setRefEntity(refEntityType));
    Entity source = new DynamicEntity(entityTypeSource);
    source.set(sourceEntityAttrName, Arrays.asList(refEntity0, refEntity1));
    Object result = algorithmService.apply(attributeMapping, source, entityTypeSource);
    assertEquals(result, Arrays.asList(refEntity0, refEntity1));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) DynamicEntity(org.molgenis.data.support.DynamicEntity) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) AttributeMapping(org.molgenis.semanticmapper.mapping.model.AttributeMapping) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 8 with AttributeMapping

use of org.molgenis.semanticmapper.mapping.model.AttributeMapping in project molgenis by molgenis.

the class AlgorithmServiceImplIT method testGetAgeScript.

@Test
public void testGetAgeScript() throws ParseException {
    String idAttrName = "id";
    EntityType entityType = entityTypeFactory.create("LL");
    entityType.addAttribute(attrMetaFactory.create().setName(idAttrName).setDataType(INT), ROLE_ID);
    entityType.addAttribute(attrMetaFactory.create().setName("dob").setDataType(DATE));
    Entity source = new DynamicEntity(entityType);
    source.set(idAttrName, 1);
    source.set("dob", LocalDate.of(1973, Month.AUGUST, 28));
    Attribute targetAttribute = attrMetaFactory.create().setName("age");
    targetAttribute.setDataType(INT);
    AttributeMapping attributeMapping = new AttributeMapping(targetAttribute);
    attributeMapping.setAlgorithm("Math.floor((new Date(2015, 2, 12) - $('dob').value())/(365.2425 * 24 * 60 * 60 * 1000))");
    Object result = algorithmService.apply(attributeMapping, source, entityType);
    assertEquals(result, 41);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) DynamicEntity(org.molgenis.data.support.DynamicEntity) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) AttributeMapping(org.molgenis.semanticmapper.mapping.model.AttributeMapping) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 9 with AttributeMapping

use of org.molgenis.semanticmapper.mapping.model.AttributeMapping in project molgenis by molgenis.

the class AlgorithmServiceImplIT method testDotAnnotationWithMref.

@Test
public void testDotAnnotationWithMref() throws ParseException {
    EntityType referenceEntityType = entityTypeFactory.create("reference");
    referenceEntityType.addAttribute(attrMetaFactory.create().setName("id"), ROLE_ID);
    referenceEntityType.addAttribute(attrMetaFactory.create().setName("label"));
    Entity referenceEntity1 = new DynamicEntity(referenceEntityType);
    referenceEntity1.set("id", "1");
    referenceEntity1.set("label", "label 1");
    Entity referenceEntity2 = new DynamicEntity(referenceEntityType);
    referenceEntity2.set("id", "2");
    referenceEntity2.set("label", "label 2");
    EntityType sourceEntityType = entityTypeFactory.create("source");
    sourceEntityType.addAttribute(attrMetaFactory.create().setName("id"), ROLE_ID);
    sourceEntityType.addAttribute(attrMetaFactory.create().setName("source_mref").setDataType(MREF));
    Entity sourceEntity = new DynamicEntity(sourceEntityType);
    sourceEntity.set("id", "1");
    sourceEntity.set("source_mref", newArrayList(referenceEntity1, referenceEntity2));
    Attribute targetAttribute = attrMetaFactory.create().setName("target_label");
    AttributeMapping attributeMapping = new AttributeMapping(targetAttribute);
    attributeMapping.setAlgorithm("var result = [];$('source_mref').map(function(mref){result.push(mref.val.label)});result");
    Object result = algorithmService.apply(attributeMapping, sourceEntity, sourceEntityType);
    assertEquals(result, "[label 1, label 2]");
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) DynamicEntity(org.molgenis.data.support.DynamicEntity) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) AttributeMapping(org.molgenis.semanticmapper.mapping.model.AttributeMapping) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 10 with AttributeMapping

use of org.molgenis.semanticmapper.mapping.model.AttributeMapping in project molgenis by molgenis.

the class EntityMappingRepositoryImplTest method testUpsert.

@Test
public void testUpsert() {
    Attribute targetAttribute = attrMetaFactory.create().setName("targetAttribute");
    List<Attribute> sourceAttributes = Lists.newArrayList();
    EntityType sourceEntityType = entityTypeFactory.create("source");
    EntityType targetEntityType = entityTypeFactory.create("target");
    targetEntityType.addAttribute(targetAttribute);
    List<AttributeMapping> attributeMappings = Lists.newArrayList();
    attributeMappings.add(new AttributeMapping("1", "targetAttribute", targetAttribute, "algorithm", sourceAttributes, CURATED.toString()));
    Collection<EntityMapping> entityMappings = singletonList(new EntityMapping(AUTO_ID, sourceEntityType, targetEntityType, attributeMappings));
    Entity attributeMappingEntity = new DynamicEntity(attrMappingMeta);
    attributeMappingEntity.set(EntityMappingMetaData.IDENTIFIER, AUTO_ID);
    attributeMappingEntity.set(AttributeMappingMetaData.TARGET_ATTRIBUTE, "targetAttribute");
    attributeMappingEntity.set(AttributeMappingMetaData.SOURCE_ATTRIBUTES, "");
    attributeMappingEntity.set(AttributeMappingMetaData.ALGORITHM, "algorithm");
    attributeMappingEntity.set(AttributeMappingMetaData.ALGORITHM_STATE, CURATED.toString());
    List<Entity> attributeMappingEntities = Lists.newArrayList();
    attributeMappingEntities.add(attributeMappingEntity);
    List<Entity> entityMappingEntities = Lists.newArrayList();
    Entity entityMappingEntity = new DynamicEntity(entityMappingMeta);
    entityMappingEntity.set(EntityMappingMetaData.IDENTIFIER, AUTO_ID);
    entityMappingEntity.set(EntityMappingMetaData.SOURCE_ENTITY_TYPE, "source");
    entityMappingEntity.set(EntityMappingMetaData.TARGET_ENTITY_TYPE, "target");
    entityMappingEntity.set(EntityMappingMetaData.ATTRIBUTE_MAPPINGS, attributeMappingEntities);
    entityMappingEntities.add(entityMappingEntity);
    Assert.assertTrue(EntityUtils.equals(entityMappingRepository.upsert(entityMappings).get(0), entityMappingEntities.get(0)));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityMapping(org.molgenis.semanticmapper.mapping.model.EntityMapping) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) DynamicEntity(org.molgenis.data.support.DynamicEntity) AttributeMapping(org.molgenis.semanticmapper.mapping.model.AttributeMapping) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Aggregations

AttributeMapping (org.molgenis.semanticmapper.mapping.model.AttributeMapping)22 Test (org.testng.annotations.Test)16 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)15 Entity (org.molgenis.data.Entity)15 Attribute (org.molgenis.data.meta.model.Attribute)15 DynamicEntity (org.molgenis.data.support.DynamicEntity)13 EntityType (org.molgenis.data.meta.model.EntityType)11 EntityMapping (org.molgenis.semanticmapper.mapping.model.EntityMapping)8 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)8 MappingTarget (org.molgenis.semanticmapper.mapping.model.MappingTarget)5 MappingProject (org.molgenis.semanticmapper.mapping.model.MappingProject)4 ExplainedQueryString (org.molgenis.semanticsearch.explain.bean.ExplainedQueryString)4 Menu (org.molgenis.core.ui.menu.Menu)3 UnknownEntityException (org.molgenis.data.UnknownEntityException)1 User (org.molgenis.data.security.auth.User)1 Relation (org.molgenis.data.semantic.Relation)1 OntologyTerm (org.molgenis.ontology.core.model.OntologyTerm)1 ScriptException (org.molgenis.script.core.ScriptException)1 RunAsSystem (org.molgenis.security.core.runas.RunAsSystem)1 GeneratedAlgorithm (org.molgenis.semanticmapper.algorithmgenerator.bean.GeneratedAlgorithm)1