@Test 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); Hits<ExplainedAttribute> sourceAttributes = Hits.create( Hit.create( ExplainedAttribute.create( heightSourceAttribute, singleton(ExplainedQueryString.create("height", "height", "height", 100)), true), 1f), Hit.create( ExplainedAttribute.create( weightSourceAttribute, singleton(ExplainedQueryString.create("weight", "weight", "weight", 100)), true), 1f)); 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( "$('sourceWeight').div($('sourceHeight').div(100.0).pow(2)).value()", generate.getAlgorithm()); assertEquals(GENERATED_HIGH, generate.getAlgorithmState()); }
@Override public String generate( Attribute targetAttribute, List<Attribute> sourceAttributes, EntityType targetEntityType, EntityType sourceEntityType) { if (targetAttribute.hasExpression()) { throw new AlgorithmException( format( "Algorithm generation for expressed attribute '%s' is not allowed", targetAttribute.getName())); } if (!sourceAttributes.isEmpty()) { for (AlgorithmGenerator generator : generators) { if (generator.isSuitable(targetAttribute, sourceAttributes)) { return generator.generate( targetAttribute, sourceAttributes, targetEntityType, sourceEntityType); } } return generateMixedTypes( targetAttribute, sourceAttributes, targetEntityType, sourceEntityType); } return StringUtils.EMPTY; }
AlgorithmGeneratorServiceImpl implements AlgorithmGeneratorService { @Override public String generate( Attribute targetAttribute, List<Attribute> sourceAttributes, EntityType targetEntityType, EntityType sourceEntityType) { if (targetAttribute.hasExpression()) { throw new AlgorithmException( format( "Algorithm generation for expressed attribute '%s' is not allowed", targetAttribute.getName())); } if (!sourceAttributes.isEmpty()) { for (AlgorithmGenerator generator : generators) { if (generator.isSuitable(targetAttribute, sourceAttributes)) { return generator.generate( targetAttribute, sourceAttributes, targetEntityType, sourceEntityType); } } return generateMixedTypes( targetAttribute, sourceAttributes, targetEntityType, sourceEntityType); } return StringUtils.EMPTY; } }
AlgorithmGeneratorServiceImpl implements AlgorithmGeneratorService { @Override public String generate( Attribute targetAttribute, List<Attribute> sourceAttributes, EntityType targetEntityType, EntityType sourceEntityType) { if (targetAttribute.hasExpression()) { throw new AlgorithmException( format( "Algorithm generation for expressed attribute '%s' is not allowed", targetAttribute.getName())); } if (!sourceAttributes.isEmpty()) { for (AlgorithmGenerator generator : generators) { if (generator.isSuitable(targetAttribute, sourceAttributes)) { return generator.generate( targetAttribute, sourceAttributes, targetEntityType, sourceEntityType); } } return generateMixedTypes( targetAttribute, sourceAttributes, targetEntityType, sourceEntityType); } return StringUtils.EMPTY; } AlgorithmGeneratorServiceImpl(
      DataService dataService,
      UnitResolver unitResolver,
      AlgorithmTemplateService algorithmTemplateService); }
AlgorithmGeneratorServiceImpl implements AlgorithmGeneratorService { @Override public String generate( Attribute targetAttribute, List<Attribute> sourceAttributes, EntityType targetEntityType, EntityType sourceEntityType) { if (targetAttribute.hasExpression()) { throw new AlgorithmException( format( "Algorithm generation for expressed attribute '%s' is not allowed", targetAttribute.getName())); } if (!sourceAttributes.isEmpty()) { for (AlgorithmGenerator generator : generators) { if (generator.isSuitable(targetAttribute, sourceAttributes)) { return generator.generate( targetAttribute, sourceAttributes, targetEntityType, sourceEntityType); } } return generateMixedTypes( targetAttribute, sourceAttributes, targetEntityType, sourceEntityType); } return StringUtils.EMPTY; } AlgorithmGeneratorServiceImpl(
      DataService dataService,
      UnitResolver unitResolver,
      AlgorithmTemplateService algorithmTemplateService); @Override String generate(
      Attribute targetAttribute,
      List<Attribute> sourceAttributes,
      EntityType targetEntityType,
      EntityType sourceEntityType); @Override GeneratedAlgorithm generate(
      Attribute targetAttribute,
      Hits<ExplainedAttribute> sourceAttributes,
      EntityType targetEntityType,
      EntityType sourceEntityType); }
AlgorithmGeneratorServiceImpl implements AlgorithmGeneratorService { @Override public String generate( Attribute targetAttribute, List<Attribute> sourceAttributes, EntityType targetEntityType, EntityType sourceEntityType) { if (targetAttribute.hasExpression()) { throw new AlgorithmException( format( "Algorithm generation for expressed attribute '%s' is not allowed", targetAttribute.getName())); } if (!sourceAttributes.isEmpty()) { for (AlgorithmGenerator generator : generators) { if (generator.isSuitable(targetAttribute, sourceAttributes)) { return generator.generate( targetAttribute, sourceAttributes, targetEntityType, sourceEntityType); } } return generateMixedTypes( targetAttribute, sourceAttributes, targetEntityType, sourceEntityType); } return StringUtils.EMPTY; } AlgorithmGeneratorServiceImpl(
      DataService dataService,
      UnitResolver unitResolver,
      AlgorithmTemplateService algorithmTemplateService); @Override String generate(
      Attribute targetAttribute,
      List<Attribute> sourceAttributes,
      EntityType targetEntityType,
      EntityType sourceEntityType); @Override GeneratedAlgorithm generate(
      Attribute targetAttribute,
      Hits<ExplainedAttribute> sourceAttributes,
      EntityType targetEntityType,
      EntityType sourceEntityType);  }