Project

General

Profile

« Previous | Next » 

Revision 9b8a8049

Added by Andreas Müller over 2 years ago

ref #9804 improve handling for empty categorical data (empty or null value state data)

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/description/CategoricalData.java
208 208
// ********************* CONVENIENCE ******************************************/
209 209

  
210 210
    /**
211
     * Convenience method which returns only the list of states. Leaving out modifiers and modifying text.
212
     * @return
211
     * Convenience method returning only the list of states. Leaving out modifiers and modifying text.
213 212
     */
214 213
    @Transient
215 214
    public List<State> getStatesOnly(){
216
        List<State> result = new ArrayList<State>();
215
        List<State> result = new ArrayList<>();
217 216
        for (StateData stateData : getStateData()){
218 217
            State state = stateData.getState();
219
            result.add(state);
218
            if (state != null){
219
                result.add(state);
220
            }
220 221
        }
221 222
        return result;
222 223
    }
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/description/StructuredDescriptionAggregation.java
557 557

  
558 558
    private void addToCategorical(CategoricalData cd, StructuredDescriptionResultHolder resultHolder) {
559 559
        CategoricalData aggregatedCategoricalData = resultHolder.categoricalMap.get(cd.getFeature());
560
        if(aggregatedCategoricalData==null){
560
        if(aggregatedCategoricalData == null){
561 561
            // no CategoricalData with this feature in aggregation
562 562
            aggregatedCategoricalData = cd.clone();
563 563
            // set count to 1 if not set
564
            aggregatedCategoricalData.getStateData().stream().filter(sd->sd.getCount()==null).forEach(sd->sd.incrementCount());
565
            resultHolder.categoricalMap.put(aggregatedCategoricalData.getFeature(), aggregatedCategoricalData);
564
            if (!aggregatedCategoricalData.getStatesOnly().isEmpty()){
565
                aggregatedCategoricalData.getStateData().stream().filter(sd->sd.getCount()==null).forEach(sd->sd.incrementCount());
566
                resultHolder.categoricalMap.put(aggregatedCategoricalData.getFeature(), aggregatedCategoricalData);
567
            }
566 568
        }
567 569
        else{
568 570
            // split all StateData into those where the state already exists and those where it doesn't
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/description/StructuredDescriptionAggregationTest.java
308 308
        datasetService.save(dataSet);
309 309

  
310 310
        SpecimenDescription specDescAlpina1 = createSpecimenDescription(dataSet, T_LAPSANA_COMMUNIS_ALPINA_UUID, "alpina specimen1", T_LAPSANA_COMMUNIS_ALPINA_SPEC1_UUID);
311
        //create empty categorical data
311 312
        addCategoricalData(specDescAlpina1, uuidFeatureLeafColor, null);
312 313

  
313 314
        TaxonNode tnLapsana = taxonNodeService.find(TN_LAPSANA_UUID);
......
321 322

  
322 323
        //aggregate
323 324
        StructuredDescriptionAggregationConfiguration config = createConfig(dataSet);
324

  
325 325
        UpdateResult result = engine.invoke(config, repository);
326
        verifyStatusOk(result);
327 326

  
327
        //test aggregation with categorical data without states (empty categorical data)
328
        verifyStatusOk(result);
328 329
        Taxon taxLapsanaCommunisAlpina = (Taxon)taxonService.find(T_LAPSANA_COMMUNIS_ALPINA_UUID);
330
        //if no state at all exists in description even the description is not created (this was different before but changed with xxx)
331
        verifyNumberTaxonDescriptions(taxLapsanaCommunisAlpina, 0);
332

  
333
        //add data for another feature
334
        specDescAlpina1 = (SpecimenDescription)descriptionService.find(specDescAlpina1.getUuid());
335
        addCategoricalData(specDescAlpina1, uuidFeatureLeafPA, State.uuidPresent);
336
        commitAndStartNewTransaction();
337
        result = engine.invoke(config, repository);
338
        verifyStatusOk(result);
339
        taxLapsanaCommunisAlpina = (Taxon)taxonService.find(T_LAPSANA_COMMUNIS_ALPINA_UUID);
329 340
        TaxonDescription aggrDescLapsanaCommunisAlpina = verifyTaxonDescriptions(taxLapsanaCommunisAlpina, 1);
341
        verifyNumberDescriptionElements(aggrDescLapsanaCommunisAlpina, uuidFeatureLeafColor, 0);
342
//        List<StateData> sdAlpinaLeafColor = verifyCategoricalData(uuidFeatureLeafColor, 0, aggrDescLapsanaCommunisAlpina, false);
343
//        verifyState(sdAlpinaLeafColor, uuidLeafColorBlue, 0);
344
//        verifyState(sdAlpinaLeafColor, uuidLeafColorYellow, 0);
345

  
346
        //test duplicates
347
        specDescAlpina1 = (SpecimenDescription)descriptionService.find(specDescAlpina1.getUuid());
348
        addCategoricalData(specDescAlpina1, uuidFeatureLeafColor, uuidLeafColorBlue);
349
        addCategoricalData(specDescAlpina1, uuidFeatureLeafColor, uuidLeafColorBlue);
350
        commitAndStartNewTransaction();
351
        result = engine.invoke(config, repository);
352

  
353
        verifyStatusOk(result);
354
        taxLapsanaCommunisAlpina = (Taxon)taxonService.find(T_LAPSANA_COMMUNIS_ALPINA_UUID);
355
        aggrDescLapsanaCommunisAlpina = verifyTaxonDescriptions(taxLapsanaCommunisAlpina, 2);  //for leafPA and for color
330 356
        List<StateData> sdAlpinaLeafColor = verifyCategoricalData(uuidFeatureLeafColor, 1, aggrDescLapsanaCommunisAlpina, false);
331
        verifyState(sdAlpinaLeafColor, uuidLeafColorBlue, 0);
357
        verifyState(sdAlpinaLeafColor, uuidLeafColorBlue, 2);
332 358
        verifyState(sdAlpinaLeafColor, uuidLeafColorYellow, 0);
359

  
333 360
    }
334 361

  
335 362
    @Test
......
700 727
        return dataSet;
701 728
    }
702 729

  
703
    private TaxonDescription verifyTaxonDescriptions(Taxon taxon, int elementSize){
704
        List<TaxonDescription> taxonDescriptions = taxon.getDescriptions().stream()
730
    private TaxonDescription verifyTaxonDescriptions(Taxon taxon, int elementCount){
731
        List<TaxonDescription> aggrNonCloneTaxonDescriptions = taxon.getDescriptions().stream()
705 732
                .filter(desc->desc.getTypes().contains(DescriptionType.AGGREGATED_STRUC_DESC))
706 733
                .filter(desc->!desc.getTypes().contains(DescriptionType.CLONE_FOR_SOURCE))
707 734
                .collect(Collectors.toList());
708 735

  
709
        Assert.assertEquals(1, taxonDescriptions.size());
710
        TaxonDescription aggrDesc = taxonDescriptions.iterator().next();
736
        Assert.assertEquals(1, aggrNonCloneTaxonDescriptions.size());
737
        TaxonDescription aggrDesc = aggrNonCloneTaxonDescriptions.iterator().next();
711 738
        Set<DescriptionElementBase> elements = aggrDesc.getElements();
712
        Assert.assertEquals(elementSize, elements.size());
739
        Assert.assertEquals(elementCount, elements.size());
713 740
        return aggrDesc;
714 741
    }
715 742

  
743
    private void verifyNumberTaxonDescriptions(Taxon taxon, long descriptionCount){
744
        long n = taxon.getDescriptions().stream()
745
            .filter(desc->desc.getTypes().contains(DescriptionType.AGGREGATED_STRUC_DESC))
746
            .filter(desc->!desc.getTypes().contains(DescriptionType.CLONE_FOR_SOURCE)).count();
747
        Assert.assertEquals(descriptionCount, n);
748
    }
749

  
750
    private void verifyNumberDescriptionElements(DescriptionBase<?> description, UUID featureUuid, long elementCount){
751
        long n = description.getElements().stream()
752
                .filter(element->element.getFeature().getUuid().equals(featureUuid))
753
                .map(catData->CdmBase.deproxy(catData, CategoricalData.class))
754
                .count();
755
        Assert.assertEquals(elementCount, n);
756
    }
757

  
716 758
    private void verifyQuantitativeData(UUID featureUuid, BigDecimal sampleSize, BigDecimal min,
717 759
            BigDecimal max, BigDecimal avg, TaxonDescription aggrDesc) {
718 760
        List<QuantitativeData> quantitativeDatas = aggrDesc.getElements().stream()

Also available in: Unified diff