Search in sources :

Example 1 with ScriptParameter

use of org.molgenis.script.core.ScriptParameter 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 2 with ScriptParameter

use of org.molgenis.script.core.ScriptParameter 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)

Aggregations

Attribute (org.molgenis.data.meta.model.Attribute)2 ScriptParameter (org.molgenis.script.core.ScriptParameter)2 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)2 HashMap (java.util.HashMap)1 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)1 EntityType (org.molgenis.data.meta.model.EntityType)1 QueryImpl (org.molgenis.data.support.QueryImpl)1 Script (org.molgenis.script.core.Script)1 GeneratedAlgorithm (org.molgenis.semanticmapper.algorithmgenerator.bean.GeneratedAlgorithm)1 Test (org.testng.annotations.Test)1