コード例 #1
0
  @Override
  public boolean conformes(Product data) {
    String[] argParts = this.argument.split(" ");
    String mathSymbol = argParts[0];

    // in g or ml
    double polylAmount = -1.0;

    for (EnergyNutrientsVitamins nutrient :
        data.getIngredientsAndAllergens().getEnergyNutrientsVitamins()) {
      if ((nutrient.getNutritionalValueReference() == 100.0)
          && ((((nutrient.getNutritionalValueReferenceUnit() == ServingSizeUnitOfMeasureCode.GR)
              || (nutrient.getNutritionalValueReferenceUnit()
                  == ServingSizeUnitOfMeasureCode.ML))))) {

        for (NutritionalContentItem content : nutrient.getNutrients()) {
          if (content.getNutritionalContent() == NutritionalContentCode.POLYL) {
            polylAmount = content.getNutritionalContentQuantityContained();
            if (content.getNutritionalContentQuantityContain() == ServingSizeUnitOfMeasureCode.EMPTY
                || content.getNutritionalContentQuantityContain() == PTN) {
              return false;
            } else {
              break;
            }
          }
        }
        break;
      }
    }

    if (polylAmount < 0) {
      // something went wrong, or there are simply no POLYL contained
      return false;
    }

    double amount = -1.0;
    if (argParts[1].contains("%")) {
      amount = Double.parseDouble(argParts[1].replace("%", ""));
      // here we get the percentage of polyl in the product
      // EDIT: if we use the polyl per 100 gr, then we don't need this
      // polylAmount = (polylAmount / convertNetWeight(data)) * 100;
    } else {
      amount = Double.parseDouble(argParts[1].replace("g", ""));
    }

    if (mathSymbol.equals("<")) {
      return polylAmount < amount;
    } else if (mathSymbol.equals("=")) {
      return polylAmount == amount;
    } else if (mathSymbol.equals(">")) {
      return polylAmount > amount;
    } else {
      Logger.getLogger(Logger.GLOBAL_LOGGER_NAME)
          .log(Level.WARNING, "Allowed operators: < OR > OR =. Chosen operator: {0}", mathSymbol);
      return false;
    }
  }
  /** Test of analyze method, of class NutrientComparisonAnalysis. */
  @Test
  public void testAnalyze() {
    MeanDeviationBasedNutrientComparisonAnalysis analysis =
        new MeanDeviationBasedNutrientComparisonAnalysis(measures);

    ArticleDescription desc = new ArticleDescription();
    GPCEntity gpc = new GPCEntity();
    gpc.setClazz("50131900");
    gpc.setBrick("10000168");
    desc.setClassificationCategoryCode(gpc);

    IngredientsAndAllergens ingredients = new IngredientsAndAllergens();
    EnergyNutrientsVitamins energyNutrients = new EnergyNutrientsVitamins();
    /* First test: kcal value is too high */
    // fill in data needed for analysis
    energyNutrients.setCalorificValueKcal(10000);
    energyNutrients.setCalorificValueKj(770);
    energyNutrients.setCalorificValueMeasurementPrecision(MeasurementPrecisionCode.APPROXIMATELY);
    energyNutrients.setNutritionalValueReference(100);
    energyNutrients.setNutritionalValueReferenceUnit(GR);

    List<NutritionalContentItem> nutrients = new ArrayList<>();
    NutritionalContentItem fat = new NutritionalContentItem();
    fat.setNutritionalContent(NutritionalContentCode.FAT);
    fat.setNutritionalContentMeasurementPrecision(MeasurementPrecisionCode.APPROXIMATELY);
    fat.setNutritionalContentQuantityContain(ServingSizeUnitOfMeasureCode.GR);
    fat.setNutritionalContentQuantityContained(10);
    nutrients.add(fat);

    NutritionalContentItem fasat = new NutritionalContentItem();
    fasat.setNutritionalContent(NutritionalContentCode.FASAT);
    fasat.setNutritionalContentMeasurementPrecision(MeasurementPrecisionCode.APPROXIMATELY);
    fasat.setNutritionalContentQuantityContain(ServingSizeUnitOfMeasureCode.GR);
    fasat.setNutritionalContentQuantityContained(2);
    nutrients.add(fasat);

    NutritionalContentItem carbs = new NutritionalContentItem();
    carbs.setNutritionalContent(NutritionalContentCode.CHOAVL);
    carbs.setNutritionalContentMeasurementPrecision(MeasurementPrecisionCode.APPROXIMATELY);
    carbs.setNutritionalContentQuantityContain(ServingSizeUnitOfMeasureCode.GR);
    carbs.setNutritionalContentQuantityContained(2.5);
    nutrients.add(carbs);

    NutritionalContentItem sugar = new NutritionalContentItem();
    sugar.setNutritionalContent(NutritionalContentCode.SUGAR);
    sugar.setNutritionalContentMeasurementPrecision(MeasurementPrecisionCode.APPROXIMATELY);
    sugar.setNutritionalContentQuantityContain(ServingSizeUnitOfMeasureCode.GR);
    sugar.setNutritionalContentQuantityContained(0.0);
    nutrients.add(sugar);

    NutritionalContentItem protein = new NutritionalContentItem();
    protein.setNutritionalContent(NutritionalContentCode.PRO);
    protein.setNutritionalContentMeasurementPrecision(MeasurementPrecisionCode.APPROXIMATELY);
    protein.setNutritionalContentQuantityContain(ServingSizeUnitOfMeasureCode.GR);
    protein.setNutritionalContentQuantityContained(23);
    nutrients.add(protein);

    NutritionalContentItem salt = new NutritionalContentItem();
    salt.setNutritionalContent(NutritionalContentCode.SALTEQ);
    salt.setNutritionalContentMeasurementPrecision(MeasurementPrecisionCode.APPROXIMATELY);
    salt.setNutritionalContentQuantityContain(ServingSizeUnitOfMeasureCode.GR);
    salt.setNutritionalContentQuantityContained(16);
    nutrients.add(salt);

    energyNutrients.setNutrients(nutrients);
    ingredients.setEnergyNutrientsVitamins(Arrays.asList(energyNutrients));

    Product product = ProductUtils.createProduct(desc, ingredients);
    boolean expResult = false;
    boolean result = analysis.analyze(product);
    assertEquals(expResult, result);
  }