use of org.molgenis.semanticmapper.algorithmgenerator.bean.GeneratedAlgorithm 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.semanticmapper.algorithmgenerator.bean.GeneratedAlgorithm 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);
}
Aggregations