use of eu.etaxonomy.cdm.model.description.StatisticalMeasure in project cdmlib by cybertaxonomy.
the class OwlImportUtil method addFeatureProperties.
private static void addFeatureProperties(Feature feature, Resource termResource, ICdmRepository repo, Model model, StructureTreeOwlImportState state) {
if (termResource.hasProperty(OwlUtil.propFeatureIsCategorical)) {
feature.setSupportsCategoricalData(termResource.getProperty(OwlUtil.propFeatureIsCategorical).getBoolean());
}
if (termResource.hasProperty(OwlUtil.propFeatureIsQuantitative)) {
feature.setSupportsQuantitativeData(termResource.getProperty(OwlUtil.propFeatureIsQuantitative).getBoolean());
}
// import measurement units
Set<MeasurementUnit> measurementUnits = new HashSet<>();
List<Statement> measurementUnitStatements = termResource.listProperties(OwlUtil.propFeatureHasRecommendedMeasurementUnit).toList();
for (Statement statement : measurementUnitStatements) {
Resource measurementUnitResource = model.createResource(statement.getObject().toString());
MeasurementUnit measurementUnit = findTerm(MeasurementUnit.class, measurementUnitResource, repo, model, state);
if (measurementUnit == null) {
measurementUnit = MeasurementUnit.NewInstance();
addTermProperties(measurementUnit, measurementUnitResource, repo, model, state);
}
measurementUnits.add(measurementUnit);
}
measurementUnits.forEach(unit -> feature.addRecommendedMeasurementUnit(unit));
// import modifier TODO: create entire vocabulary if it cannot be found
Set<TermVocabulary<DefinedTerm>> modifierVocs = new HashSet<>();
List<Statement> modifierEnumStatements = termResource.listProperties(OwlUtil.propFeatureHasRecommendedModifierEnumeration).toList();
for (Statement statement : modifierEnumStatements) {
Resource modifierEnumResource = model.createResource(statement.getObject().toString());
TermVocabulary modifierVoc = findVocabulary(modifierEnumResource, repo, model, state);
if (modifierVoc != null) {
modifierVocs.add(modifierVoc);
}
}
modifierVocs.forEach(modiferVoc -> feature.addRecommendedModifierEnumeration(modiferVoc));
// import statistical measures
Set<StatisticalMeasure> statisticalMeasures = new HashSet<>();
List<Statement> statisticalMeasureStatements = termResource.listProperties(OwlUtil.propFeatureHasRecommendedStatisticalMeasure).toList();
for (Statement statement : statisticalMeasureStatements) {
Resource statisticalMeasureResource = model.createResource(statement.getObject().toString());
StatisticalMeasure statisticalMeasure = findTerm(StatisticalMeasure.class, statisticalMeasureResource, repo, model, state);
if (statisticalMeasure == null) {
statisticalMeasure = StatisticalMeasure.NewInstance();
addTermProperties(statisticalMeasure, statisticalMeasureResource, repo, model, state);
}
statisticalMeasures.add(statisticalMeasure);
}
statisticalMeasures.forEach(statisticalMeasure -> feature.addRecommendedStatisticalMeasure(statisticalMeasure));
// import categorical enums TODO: create entire vocabulary if it cannot be found
Set<TermVocabulary<State>> stateVocs = new HashSet<>();
List<Statement> stateVocStatements = termResource.listProperties(OwlUtil.propFeatureHasSupportedCategoricalEnumeration).toList();
for (Statement statement : stateVocStatements) {
Resource stateVocResource = model.createResource(statement.getObject().toString());
TermVocabulary stateVoc = findVocabulary(stateVocResource, repo, model, state);
if (stateVoc != null) {
stateVocs.add(stateVoc);
}
}
stateVocs.forEach(stateVoc -> feature.addSupportedCategoricalEnumeration(stateVoc));
}
use of eu.etaxonomy.cdm.model.description.StatisticalMeasure in project cdmlib by cybertaxonomy.
the class DescriptiveDataSetService method addRowWrapperToDataset.
@Override
@Transactional(readOnly = false)
public UpdateResult addRowWrapperToDataset(Collection<SpecimenRowWrapperDTO> wrappers, UUID datasetUuid, boolean addDatasetSource) {
UpdateResult result = new UpdateResult();
DescriptiveDataSet dataSet = load(datasetUuid);
result.setCdmEntity(dataSet);
List<UUID> taxonUuids = wrappers.stream().map(wrapper -> wrapper.getTaxonNode().getTaxonUuid()).collect(Collectors.toList());
List<TaxonBase> taxa = taxonService.load(taxonUuids, Arrays.asList(new String[] { "descriptions" }));
for (SpecimenRowWrapperDTO wrapper : wrappers) {
Optional<TaxonBase> findAny = taxa.stream().filter(taxon -> taxon.getUuid().equals(wrapper.getTaxonNode().getTaxonUuid())).findAny();
if (!findAny.isPresent()) {
result.addException(new IllegalArgumentException("Could not create wrapper for " + wrapper.getSpecimenDto().getLabel()));
continue;
}
Taxon taxon = (Taxon) findAny.get();
SpecimenOrObservationBase<?> specimen = occurrenceService.load(wrapper.getSpecimenDto().getUuid());
TaxonDescription taxonDescription = taxon.getDescriptions().stream().filter(desc -> desc.getTypes().contains(DescriptionType.INDIVIDUALS_ASSOCIATION)).findFirst().orElseGet(() -> {
TaxonDescription td = TaxonDescription.NewInstance(taxon);
td.addType(DescriptionType.INDIVIDUALS_ASSOCIATION);
td.setTitleCache("Specimens used by " + dataSet.getTitleCache() + " for " + getTaxonLabel(taxon), true);
return td;
});
IndividualsAssociation association = null;
for (DescriptionElementBase el : taxonDescription.getElements()) {
if (el instanceof IndividualsAssociation) {
IndividualsAssociation indAss = (IndividualsAssociation) el;
if (indAss.getAssociatedSpecimenOrObservation().getUuid().equals(specimen.getUuid())) {
association = indAss;
}
}
}
if (association == null) {
association = IndividualsAssociation.NewInstance(specimen);
taxonDescription.addElement(association);
taxonService.saveOrUpdate(taxon);
result.addUpdatedObject(taxon);
}
UUID specimenDescriptionUuid = wrapper.getDescription().getDescriptionUuid();
DescriptionBaseDto descriptionDto = wrapper.getDescription();
DescriptionBase<?> specimenDescription = descriptionService.load(specimenDescriptionUuid);
// if description already exist use the loaded one and add changed data otherwise create a new one and add to specimen
if (specimenDescription == null) {
specimenDescription = SpecimenDescription.NewInstance(specimen);
specimenDescription.setUuid(specimenDescriptionUuid);
List<DescriptionElementDto> elementDtos = descriptionDto.getElements();
for (DescriptionElementDto elementDto : elementDtos) {
if (elementDto instanceof CategoricalDataDto) {
eu.etaxonomy.cdm.model.description.Character feature = DefinedTermBase.getTermByClassAndUUID(eu.etaxonomy.cdm.model.description.Character.class, elementDto.getFeatureUuid());
CategoricalData data = CategoricalData.NewInstance(feature);
for (StateDataDto stateDto : ((CategoricalDataDto) elementDto).getStates()) {
State state = DefinedTermBase.getTermByClassAndUUID(State.class, stateDto.getState().getUuid());
data.addStateData(state);
specimenDescription.addElement(data);
}
}
if (elementDto instanceof QuantitativeDataDto) {
eu.etaxonomy.cdm.model.description.Character feature = DefinedTermBase.getTermByClassAndUUID(eu.etaxonomy.cdm.model.description.Character.class, elementDto.getFeatureUuid());
QuantitativeData data = QuantitativeData.NewInstance(feature);
if (((QuantitativeDataDto) elementDto).getMeasurementUnit() != null) {
MeasurementUnit unit = DefinedTermBase.getTermByClassAndUUID(MeasurementUnit.class, ((QuantitativeDataDto) elementDto).getMeasurementUnit().getUuid());
data.setUnit(unit);
}
for (StatisticalMeasurementValueDto stateDto : ((QuantitativeDataDto) elementDto).getValues()) {
StatisticalMeasure statMeasure = DefinedTermBase.getTermByClassAndUUID(StatisticalMeasure.class, stateDto.getType().getUuid());
StatisticalMeasurementValue value = StatisticalMeasurementValue.NewInstance(statMeasure, stateDto.getValue());
data.addStatisticalValue(value);
specimenDescription.addElement(data);
}
}
}
} else {
List<DescriptionElementDto> elementDtos = descriptionDto.getElements();
for (DescriptionElementDto elementDto : elementDtos) {
if (elementDto instanceof CategoricalDataDto) {
eu.etaxonomy.cdm.model.description.Character feature = DefinedTermBase.getTermByClassAndUUID(eu.etaxonomy.cdm.model.description.Character.class, elementDto.getFeatureUuid());
List<DescriptionElementBase> uniqueElementList = specimenDescription.getElements().stream().filter(element -> element.getUuid().equals(elementDto.getElementUuid())).collect(Collectors.toList());
List<State> allStates = new ArrayList<>();
CategoricalData element = null;
if (uniqueElementList.size() == 1) {
element = HibernateProxyHelper.deproxy(uniqueElementList.get(0), CategoricalData.class);
} else {
element = CategoricalData.NewInstance(feature);
}
for (StateDataDto stateDto : ((CategoricalDataDto) elementDto).getStates()) {
State state = DefinedTermBase.getTermByClassAndUUID(State.class, stateDto.getState().getUuid());
allStates.add(state);
}
element.setStateDataOnly(allStates);
}
if (elementDto instanceof QuantitativeDataDto) {
eu.etaxonomy.cdm.model.description.Character feature = DefinedTermBase.getTermByClassAndUUID(eu.etaxonomy.cdm.model.description.Character.class, elementDto.getFeatureUuid());
QuantitativeData data = QuantitativeData.NewInstance(feature);
if (((QuantitativeDataDto) elementDto).getMeasurementUnit() != null) {
MeasurementUnit unit = DefinedTermBase.getTermByClassAndUUID(MeasurementUnit.class, ((QuantitativeDataDto) elementDto).getMeasurementUnit().getUuid());
data.setUnit(unit);
}
for (StatisticalMeasurementValueDto stateDto : ((QuantitativeDataDto) elementDto).getValues()) {
StatisticalMeasure statMeasure = DefinedTermBase.getTermByClassAndUUID(StatisticalMeasure.class, stateDto.getType().getUuid());
StatisticalMeasurementValue value = StatisticalMeasurementValue.NewInstance(statMeasure, stateDto.getValue());
data.addStatisticalValue(value);
specimenDescription.addElement(data);
}
}
}
}
if (addDatasetSource) {
for (IdentifiableSource source : dataSet.getSources()) {
try {
specimenDescription.addSource(source.clone());
} catch (CloneNotSupportedException e) {
// nothing
}
}
}
// add specimen description to data set
specimenDescription.addDescriptiveDataSet(dataSet);
// add taxon description with IndividualsAssociation to the specimen to data set
taxonDescription.addDescriptiveDataSet(dataSet);
result.addUpdatedObject(specimen);
result.addUpdatedObject(specimenDescription);
result.addUpdatedObject(taxonDescription);
}
saveOrUpdate(dataSet);
return result;
}
use of eu.etaxonomy.cdm.model.description.StatisticalMeasure in project cdmlib by cybertaxonomy.
the class DescriptionServiceImpl method mergeDescriptions.
@Override
@Transactional(readOnly = false)
public UpdateResult mergeDescriptions(Collection<DescriptionBaseDto> descriptions, UUID descriptiveDataSetUuid) {
UpdateResult result = new UpdateResult();
DescriptiveDataSet dataSet = descriptiveDataSetDao.load(descriptiveDataSetUuid);
Set<DescriptionBase> descriptionsOfDataSet = dataSet.getDescriptions();
HashMap<UUID, Set<DescriptionBase>> descriptionSpecimenMap = new HashMap<>();
Set<DescriptionBase> specimenDescriptions;
for (DescriptionBase<?> descriptionBase : descriptionsOfDataSet) {
if (descriptionBase.getDescribedSpecimenOrObservation() != null) {
specimenDescriptions = descriptionSpecimenMap.get(descriptionBase.getDescribedSpecimenOrObservation().getUuid());
if (specimenDescriptions == null) {
specimenDescriptions = new HashSet<>();
}
specimenDescriptions.add(descriptionBase);
descriptionSpecimenMap.put(descriptionBase.getDescribedSpecimenOrObservation().getUuid(), specimenDescriptions);
}
if (descriptionBase instanceof TaxonDescription) {
specimenDescriptions = descriptionSpecimenMap.get(((TaxonDescription) descriptionBase).getTaxon().getUuid());
if (specimenDescriptions == null) {
specimenDescriptions = new HashSet<>();
}
specimenDescriptions.add(descriptionBase);
descriptionSpecimenMap.put(((TaxonDescription) descriptionBase).getTaxon().getUuid(), specimenDescriptions);
}
if (descriptionBase instanceof TaxonNameDescription) {
specimenDescriptions = descriptionSpecimenMap.get(((TaxonNameDescription) descriptionBase).getTaxonName().getUuid());
if (specimenDescriptions == null) {
specimenDescriptions = new HashSet<>();
}
specimenDescriptions.add(descriptionBase);
descriptionSpecimenMap.put(((TaxonNameDescription) descriptionBase).getTaxonName().getUuid(), specimenDescriptions);
}
}
MergeResult<DescriptionBase> mergeResult = null;
for (DescriptionBaseDto descDto : descriptions) {
UUID descriptionUUID = descDto.getDescriptionUuid();
DescriptionBase<?> description = load(descriptionUUID);
UUID describedObjectUuid = null;
if (description instanceof SpecimenDescription) {
describedObjectUuid = descDto.getSpecimenDto().getUuid();
} else if (description instanceof TaxonDescription) {
describedObjectUuid = descDto.getTaxonDto().getUuid();
} else if (description instanceof TaxonNameDescription) {
describedObjectUuid = descDto.getNameDto().getUuid();
}
Set<DescriptionBase> descSpecimen = descriptionSpecimenMap.get(describedObjectUuid);
if (descSpecimen != null) {
// TODO: elements are Dtos now, no cdm entities, needs to get the value and replace or create new description element
Set<DescriptionElementDto> elements = new HashSet<>();
for (Object element : descDto.getElements()) {
elements.add((DescriptionElementDto) element);
}
DescriptionBase<?> desc = null;
for (DescriptionBase<?> tempDesc : descSpecimen) {
if (tempDesc.getUuid().equals(descDto.getDescriptionUuid())) {
desc = tempDesc;
break;
}
}
Set<DescriptionElementBase> removeElements = new HashSet<>();
Set<DescriptionElementBase> descriptionElements = desc.getElements();
for (DescriptionElementBase elementBase : descriptionElements) {
UUID descElementUuid = elementBase.getUuid();
if (descElementUuid != null) {
List<DescriptionElementDto> equalUuidsElements = elements.stream().filter(e -> e != null && e.getElementUuid() != null && e.getElementUuid().equals(descElementUuid)).collect(Collectors.toList());
if (equalUuidsElements.size() == 0 || (equalUuidsElements.size() == 1 && equalUuidsElements.get(0) instanceof QuantitativeDataDto && ((QuantitativeDataDto) equalUuidsElements.get(0)).getValues().isEmpty())) {
removeElements.add(elementBase);
}
}
}
if (!removeElements.isEmpty()) {
for (DescriptionElementBase el : removeElements) {
desc.removeElement(el);
}
}
for (DescriptionElementDto descElement : elements) {
if (descElement == null) {
continue;
}
UUID descElementUuid = descElement.getElementUuid();
if (descElement instanceof CategoricalDataDto && ((CategoricalDataDto) descElement).getStates().isEmpty() || descElement instanceof QuantitativeDataDto && ((QuantitativeDataDto) descElement).getValues().isEmpty()) {
continue;
}
List<DescriptionElementBase> equalUuidsElements = descriptionElements.stream().filter(e -> e.getUuid().equals(descElementUuid)).collect(Collectors.toList());
eu.etaxonomy.cdm.model.description.Feature feature = DefinedTermBase.getTermByClassAndUUID(eu.etaxonomy.cdm.model.description.Feature.class, descElement.getFeatureUuid());
if (feature == null) {
feature = DefinedTermBase.getTermByClassAndUUID(eu.etaxonomy.cdm.model.description.Character.class, descElement.getFeatureUuid());
}
if (equalUuidsElements.size() == 0) {
if (descElement instanceof CategoricalDataDto) {
CategoricalData elementBase = CategoricalData.NewInstance(feature);
List<StateDataDto> stateDtos = ((CategoricalDataDto) descElement).getStates();
for (StateDataDto dataDto : stateDtos) {
// create new statedata
State newState = DefinedTermBase.getTermByClassAndUUID(State.class, dataDto.getState().getUuid());
StateData newStateData = StateData.NewInstance(newState);
elementBase.addStateData(newStateData);
}
desc.addElement(elementBase);
}
if (descElement instanceof QuantitativeDataDto) {
QuantitativeData data = QuantitativeData.NewInstance(feature);
if (((QuantitativeDataDto) descElement).getMeasurementUnit() != null) {
MeasurementUnit unit = DefinedTermBase.getTermByClassAndUUID(MeasurementUnit.class, ((QuantitativeDataDto) descElement).getMeasurementUnit().getUuid());
data.setUnit(unit);
}
Set<StatisticalMeasurementValue> statisticalValues = new HashSet<>();
Set<StatisticalMeasurementValueDto> valueDtos = ((QuantitativeDataDto) descElement).getValues();
data.getStatisticalValues().clear();
for (StatisticalMeasurementValueDto dataDto : valueDtos) {
// create new statedata
StatisticalMeasurementValue newStatisticalMeasurement = StatisticalMeasurementValue.NewInstance(DefinedTermBase.getTermByClassAndUUID(StatisticalMeasure.class, dataDto.getType().getUuid()), dataDto.getValue());
statisticalValues.add(newStatisticalMeasurement);
data.addStatisticalValue(newStatisticalMeasurement);
}
// data.getStatisticalValues().addAll(statisticalValues);
data = StructuredDescriptionAggregation.handleMissingMinOrMax(data, MissingMinimumMode.MinToZero, MissingMaximumMode.MaxToMin);
desc.addElement(data);
}
// create new element
} else {
DescriptionElementBase elementBase = equalUuidsElements.get(0);
if (elementBase.isInstanceOf(CategoricalData.class)) {
CategoricalData data = HibernateProxyHelper.deproxy(elementBase, CategoricalData.class);
List<StateData> states = new ArrayList<>(data.getStateData());
List<StateDataDto> stateDtos = ((CategoricalDataDto) descElement).getStates();
data.getStateData().clear();
if (stateDtos.isEmpty()) {
desc.removeElement(data);
} else {
for (StateDataDto dataDto : stateDtos) {
State newState = DefinedTermBase.getTermByClassAndUUID(State.class, dataDto.getState().getUuid());
StateData newStateData = StateData.NewInstance(newState);
data.addStateData(newStateData);
}
}
} else if (elementBase.isInstanceOf(QuantitativeData.class)) {
QuantitativeData data = HibernateProxyHelper.deproxy(elementBase, QuantitativeData.class);
Set<StatisticalMeasurementValue> statisticalValues = new HashSet<>();
if (((QuantitativeDataDto) descElement).getMeasurementUnit() != null) {
MeasurementUnit unit = DefinedTermBase.getTermByClassAndUUID(MeasurementUnit.class, ((QuantitativeDataDto) descElement).getMeasurementUnit().getUuid());
if (data.getUnit() == null || (data.getUnit() != null && !data.getUnit().equals(unit))) {
data.setUnit(unit);
}
}
Set<StatisticalMeasurementValueDto> valueDtos = ((QuantitativeDataDto) descElement).getValues();
data.getStatisticalValues().clear();
if (valueDtos.isEmpty()) {
desc.removeElement(data);
} else {
for (StatisticalMeasurementValueDto dataDto : valueDtos) {
// create new statedata
StatisticalMeasure statMeasure = DefinedTermBase.getTermByClassAndUUID(StatisticalMeasure.class, dataDto.getType().getUuid());
StatisticalMeasurementValue newStatisticalMeasurement = StatisticalMeasurementValue.NewInstance(statMeasure, dataDto.getValue());
statisticalValues.add(newStatisticalMeasurement);
data.addStatisticalValue(newStatisticalMeasurement);
}
// data.getStatisticalValues().addAll(statisticalValues);
data = StructuredDescriptionAggregation.handleMissingMinOrMax(data, MissingMinimumMode.MinToZero, MissingMaximumMode.MaxToMin);
}
}
}
// remove deleted elements
}
descriptionSpecimenMap.get(describedObjectUuid).add(desc);
description = desc;
}
try {
if (description != null) {
mergeResult = dao.merge(description, true);
result.addUpdatedObject(mergeResult.getMergedEntity());
}
// if (description instanceof SpecimenDescription){
// result.addUpdatedObject(mergeResult.getMergedEntity().getDescribedSpecimenOrObservation());
// }else if (description instanceof TaxonDescription){
// result.addUpdatedObject(((TaxonDescription)mergeResult.getMergedEntity()).getTaxon());
// }else if (description instanceof TaxonNameDescription){
// result.addUpdatedObject(((TaxonNameDescription)mergeResult.getMergedEntity()).getTaxonName());
// }
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
use of eu.etaxonomy.cdm.model.description.StatisticalMeasure in project cdmlib by cybertaxonomy.
the class CdmImportBase method getStatisticalMeasure.
/**
* Returns a {@link StatisticalMeasure} for a given uuid by first checking if the uuid has already been used in this import, if not
* checking if the {@link StatisticalMeasure} exists in the database, if not creating it anew (with vocabulary etc.).
* If label, text and labelAbbrev are all <code>null</code> no {@link StatisticalMeasure} is created.
* @param state
* @param uuid
* @param label
* @param text
* @param labelAbbrev
* @return
*/
protected StatisticalMeasure getStatisticalMeasure(STATE state, UUID uuid, String label, String description, String labelAbbrev, TermVocabulary<StatisticalMeasure> voc) {
if (uuid == null) {
return null;
}
StatisticalMeasure statisticalMeasure = state.getStatisticalMeasure(uuid);
if (statisticalMeasure == null) {
statisticalMeasure = (StatisticalMeasure) getTermService().find(uuid);
if (statisticalMeasure == null && !hasNoLabel(label, description, labelAbbrev)) {
statisticalMeasure = StatisticalMeasure.NewInstance(description, label, labelAbbrev);
statisticalMeasure.setUuid(uuid);
if (voc == null) {
boolean isOrdered = false;
voc = getVocabulary(state, TermType.StatisticalMeasure, uuidUserDefinedStatisticalMeasureVocabulary, "User defined vocabulary for statistical measures", "User Defined Statistical Measures", null, null, isOrdered, statisticalMeasure);
}
voc.addTerm(statisticalMeasure);
getTermService().save(statisticalMeasure);
}
state.putStatisticalMeasure(statisticalMeasure);
}
return statisticalMeasure;
}
use of eu.etaxonomy.cdm.model.description.StatisticalMeasure in project cdmlib by cybertaxonomy.
the class SDDDocumentBuilder method buildMeasure.
/**
* Builds Measure associated with a Quantitative
*/
public void buildMeasure(ElementImpl element, StatisticalMeasurementValue statisticalValue) throws ParseException {
// <Quantitative ref="c2">
// <Measure type="Min" value="2.3"></Measure>
// <Measure type="Mean" value="5.1"/>
// <Measure type="Max" value="7.9"/>
// <Measure type="SD" value="1.3"/>
// <Measure type="N" value="20"/>
// </Quantitative>
ElementImpl measure = new ElementImpl(document, MEASURE);
StatisticalMeasure type = statisticalValue.getType();
String label = type.getLabel();
if (label.equals("Average")) {
measure.setAttribute("type", "Mean");
} else if (label.equals("StandardDeviation")) {
measure.setAttribute("type", "SD");
} else if (label.equals("SampleSize")) {
measure.setAttribute("type", "N");
} else {
measure.setAttribute("type", label);
}
BigDecimal value = statisticalValue.getValue();
measure.setAttribute("value", String.valueOf(value));
element.appendChild(measure);
}
Aggregations