use of eu.etaxonomy.cdm.model.description.PolytomousKeyNode in project cdmlib by cybertaxonomy.
the class MarkupKeyImport method handleKey.
public void handleKey(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
// attributes
Map<String, Attribute> attributes = getAttributes(parentEvent);
String isSpotcharacters = getAndRemoveAttributeValue(attributes, IS_SPOTCHARACTERS);
if (isNotBlank(isSpotcharacters)) {
// TODO isSpotcharacters
String message = "Attribute isSpotcharacters not yet implemented for <key>";
fireWarningEvent(message, parentEvent, 4);
}
boolean onlyNumberedTaxaExist = checkAndRemoveAttributeValue(attributes, ONLY_NUMBERED_TAXA_EXIST, "true");
state.setOnlyNumberedTaxaExist(onlyNumberedTaxaExist);
PolytomousKey key = PolytomousKey.NewInstance();
key.addPrimaryTaxonomicSource(state.getConfig().getSourceReference(), null);
key.addTaxonomicScope(state.getCurrentTaxon());
state.setCurrentKey(key);
boolean isFirstCouplet = true;
while (reader.hasNext()) {
XMLEvent next = readNoWhitespace(reader);
if (isMyEndingElement(next, parentEvent)) {
save(key, state);
// reset state
state.setCurrentKey(null);
state.setOnlyNumberedTaxaExist(false);
return;
} else if (isEndingElement(next, KEYNOTES)) {
popUnimplemented(next.asEndElement());
} else if (isStartingElement(next, KEY_TITLE)) {
handleKeyTitle(state, reader, next);
} else if (isStartingElement(next, KEYNOTES)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, COUPLET)) {
PolytomousKeyNode node = null;
if (isFirstCouplet) {
node = key.getRoot();
isFirstCouplet = false;
}
handleCouplet(state, reader, next, node);
} else {
handleUnexpectedElement(next);
}
}
throw new IllegalStateException("<key> has no closing tag");
}
use of eu.etaxonomy.cdm.model.description.PolytomousKeyNode in project cdmlib by cybertaxonomy.
the class MarkupKeyImport method handleQuestion.
private void handleQuestion(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, List<PolytomousKeyNode> nodesList) throws XMLStreamException {
// attributes
Map<String, Attribute> attributes = getAttributes(parentEvent);
// TODO needed only for data lineage
String questionNum = getAndRemoveRequiredAttributeValue(parentEvent, attributes, NUM);
PolytomousKeyNode myNode = PolytomousKeyNode.NewInstance();
// to avoid NPE while computing num in PolytomousKeyNode in case this node is not matched correctly with a parent
myNode.setKey(state.getCurrentKey());
nodesList.add(myNode);
int countToTaxon = 0;
while (reader.hasNext()) {
XMLEvent next = readNoWhitespace(reader);
if (isMyEndingElement(next, parentEvent)) {
return;
} else if (isStartingElement(next, TEXT)) {
String text = getCData(state, reader, next);
KeyStatement statement = KeyStatement.NewInstance(getDefaultLanguage(state), text);
myNode.setStatement(statement);
} else if (isStartingElement(next, COUPLET)) {
fireWarningEvent("Check if toCouplet in question is implemented correctly", next, 4);
handleCouplet(state, reader, next, myNode);
} else if (isStartingElement(next, TO_COUPLET)) {
handleToCouplet(state, reader, next, myNode);
} else if (isStartingElement(next, TO_TAXON)) {
if (countToTaxon == 0) {
handleToTaxon(state, reader, next, myNode);
} else {
if (countToTaxon == 1) {
// rearrange first taxon //similar to PKNode.setOrAddTaxon(taxon) but the later can't be used here
// TODO this does not yet move the unmatched taxon keys to the new node
fireWarningEvent("Multiple toTaxon requires manual adjustment", next, 6);
Taxon firstTaxon = myNode.removeTaxon();
PolytomousKeyNode firstChildNode = PolytomousKeyNode.NewInstance();
firstChildNode.setTaxon(firstTaxon);
myNode.addChild(firstChildNode);
}
PolytomousKeyNode childNode = PolytomousKeyNode.NewInstance();
myNode.addChild(childNode);
handleToTaxon(state, reader, next, childNode);
}
countToTaxon++;
} else if (isStartingElement(next, TO_KEY)) {
// TODO
handleNotYetImplementedElement(next);
} else if (isStartingElement(next, KEYNOTES)) {
handleAmbigousManually(state, reader, next.asStartElement());
} else {
handleUnexpectedElement(next);
}
}
throw new IllegalStateException("<question> has no closing tag");
}
use of eu.etaxonomy.cdm.model.description.PolytomousKeyNode in project cdmlib by cybertaxonomy.
the class MarkupKeyImport method handleMatchingNodes.
private Set<PolytomousKeyNode> handleMatchingNodes(MarkupImportState state, XMLEvent event, Taxon taxon, UnmatchedLeadsKey leadsKey) {
Set<PolytomousKeyNode> matchingNodes = state.getUnmatchedLeads().getNodes(leadsKey);
for (PolytomousKeyNode matchingNode : matchingNodes) {
state.getUnmatchedLeads().removeNode(leadsKey, matchingNode);
matchingNode.setTaxon(taxon);
// just to be on the save side
try {
matchingNode.refreshNodeNumbering();
} catch (Exception e) {
String message = "An exception occurred when trying to referesh the node numbering: " + e.getMessage();
fireWarningEvent(message, event, 6);
}
state.getPolytomousKeyNodesToSave().add(matchingNode);
}
return matchingNodes;
}
use of eu.etaxonomy.cdm.model.description.PolytomousKeyNode in project cdmlib by cybertaxonomy.
the class FullCoverageDataGenerator method createDescriptions.
private void createDescriptions(List<CdmBase> cdmBases) {
TermVocabulary<AnnotationType> voc = TermVocabulary.NewInstance(TermType.AnnotationType, AnnotationType.class, "my termVoc desc", "myTerm voc", "mtv", URI.create("http://www.abc.de"));
handleIdentifiableEntity(voc);
cdmBases.add(voc);
Representation rep = voc.getRepresentations().iterator().next();
handleAnnotatableEntity(rep);
// Representation engRep = Language.ENGLISH().getRepresentations().iterator().next();
// handleAnnotatableEntity(engRep);
// cdmBases.add(engRep); //needed?
// Categorical data
State state = State.NewInstance("Test state", "state", "st.");
state.addMedia(Media.NewInstance());
cdmBases.add(state);
CategoricalData categoricalData = CategoricalData.NewInstance(state, Feature.CONSERVATION());
StateData stateData = categoricalData.getStateData().get(0);
stateData.addModifier(DefinedTerm.SEX_FEMALE());
handleAnnotatableEntity(categoricalData);
State nextState = State.NewInstance();
cdmBases.add(nextState);
StateData stateData2 = StateData.NewInstance(nextState);
stateData2.setCount(3);
stateData2.putModifyingText(Language.ENGLISH(), "State2 modifying text");
categoricalData.addStateData(stateData2);
categoricalData.setOrderRelevant(true);
// Quantitative data
Feature leaveLength = Feature.NewInstance("Leave length description", "leave length", "l.l.");
cdmBases.add(leaveLength);
leaveLength.setSupportsQuantitativeData(true);
QuantitativeData quantitativeData = QuantitativeData.NewInstance(leaveLength);
MeasurementUnit measurementUnit = MeasurementUnit.NewInstance("Measurement Unit", "munit", null);
cdmBases.add(measurementUnit);
quantitativeData.setUnit(measurementUnit);
quantitativeData.setUuid(UUID.fromString("920fce5e-4913-4a3f-89bf-1611f5081869"));
StatisticalMeasurementValue statisticalMeasurementValue = quantitativeData.setAverage(new BigDecimal("22.9215"), null);
handleAnnotatableEntity(quantitativeData);
handleIdentifiableEntity(measurementUnit);
DefinedTerm valueModifier = DefinedTerm.NewModifierInstance("about", "about", null);
statisticalMeasurementValue.addModifier(valueModifier);
cdmBases.add(valueModifier);
// Feature
TermVocabulary<DefinedTerm> recommendedModifierEnumeration = TermVocabulary.NewInstance(TermType.Modifier, DefinedTerm.class);
leaveLength.addRecommendedModifierEnumeration(recommendedModifierEnumeration);
cdmBases.add(recommendedModifierEnumeration);
TermVocabulary<State> supportedCategoricalEnumeration = TermVocabulary.NewInstance(TermType.State, State.class);
leaveLength.addSupportedCategoricalEnumeration(supportedCategoricalEnumeration);
cdmBases.add(supportedCategoricalEnumeration);
leaveLength.addRecommendedMeasurementUnit(measurementUnit);
leaveLength.addRecommendedStatisticalMeasure(StatisticalMeasure.AVERAGE());
// CommonTaxonName
CommonTaxonName commonTaxonName = CommonTaxonName.NewInstance("common name", Language.ENGLISH(), Country.UNITEDSTATESOFAMERICA());
handleAnnotatableEntity(commonTaxonName);
// TextData
TextData textData = TextData.NewInstance(Feature.DIAGNOSIS());
Language eng = Language.ENGLISH();
textData.putText(eng, "My text data");
LanguageString languageString = textData.getLanguageText(eng);
Taxon referencedTaxon = getTaxon();
cdmBases.add(referencedTaxon);
languageString.addIntextReference(IntextReference.NewInstance(referencedTaxon, languageString, 2, 5));
textData.putModifyingText(eng, "nice diagnosis");
handleAnnotatableEntity(textData);
handleAnnotatableEntity(languageString);
TextFormat format = TextFormat.NewInstance("format", "format", null);
textData.setFormat(format);
cdmBases.add(format);
handleAnnotatableEntity(format);
// IndividualsAssociation
DerivedUnit specimen = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
IndividualsAssociation indAssoc = IndividualsAssociation.NewInstance(specimen);
indAssoc.putDescription(Language.ENGLISH(), "description for individuals association");
handleAnnotatableEntity(indAssoc);
// TaxonInteraction
TaxonInteraction taxonInteraction = TaxonInteraction.NewInstance(Feature.HOSTPLANT());
taxonInteraction.putDescription(Language.ENGLISH(), "interaction description");
handleAnnotatableEntity(taxonInteraction);
// Distribution
NamedArea inCountryArea = NamedArea.NewInstance("My area in a country", "my area", "ma");
inCountryArea.addCountry(Country.TURKEYREPUBLICOF());
cdmBases.add(inCountryArea);
Distribution distribution = Distribution.NewInstance(inCountryArea, PresenceAbsenceTerm.CULTIVATED());
handleAnnotatableEntity(distribution);
// TemporalData
Feature floweringSeason = Feature.FLOWERING_PERIOD();
TemporalData temporalData = TemporalData.NewInstance(ExtendedTimePeriod.NewExtendedMonthInstance(5, 8, 4, 9));
temporalData.setFeature(floweringSeason);
temporalData.getPeriod().setFreeText("My temporal text");
handleAnnotatableEntity(temporalData);
temporalData.setUuid(UUID.fromString("9a1c91c0-fc58-4310-94cb-8c26115985d3"));
Taxon taxon = getTaxon();
TaxonDescription taxonDescription = TaxonDescription.NewInstance(taxon);
taxonDescription.addElements(categoricalData, quantitativeData, textData, commonTaxonName, taxonInteraction, indAssoc, distribution, temporalData);
DerivedUnit describedSpecimenOrObservation = DerivedUnit.NewInstance(SpecimenOrObservationType.DerivedUnit);
taxonDescription.setDescribedSpecimenOrObservation(describedSpecimenOrObservation);
taxonDescription.addScope(DefinedTerm.SEX_FEMALE());
taxonDescription.addGeoScope(Country.GERMANY());
handleIdentifiableEntity(taxonDescription);
taxon.addAggregationSource(taxonDescription);
cdmBases.add(taxon);
// DescriptionElmenetBase + source
textData.addMedia(Media.NewInstance());
textData.addModifier(DefinedTerm.SEX_HERMAPHRODITE());
textData.putModifyingText(Language.ENGLISH(), "no modification");
textData.setTimeperiod(TimePeriodParser.parseString("1970-1980"));
Reference ref = ReferenceFactory.newArticle();
DescriptionElementSource source = textData.addSource(OriginalSourceType.Import, "22", "taxon description table", ref, "detail");
source.setNameUsedInSource(TaxonNameFactory.NewBotanicalInstance(Rank.GENUS()));
ExternalLink link = ExternalLink.NewInstance(ExternalLinkType.WebSite, URI.create("http://wwww.abd.de"), "Somehow useful link", 445);
source.addLink(link);
handleAnnotatableEntity(source);
// as long as it still exists
taxonDescription.addDescriptionSource(ref);
// Specimen description
SpecimenOrObservationBase<?> describedSpecimen = getSpecimen();
SpecimenDescription specDesc = SpecimenDescription.NewInstance(specimen);
cdmBases.add(describedSpecimen);
handleAnnotatableEntity(specDesc);
// Name description
TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
TaxonNameDescription nameDesc = TaxonNameDescription.NewInstance(name);
cdmBases.add(name);
handleAnnotatableEntity(nameDesc);
// Feature Tree
TermTree<Feature> featureTree = TermTree.NewFeatureInstance();
// featureTree
TermNode<Feature> descriptionTermNode = featureTree.getRoot().addChild(Feature.DESCRIPTION());
TermNode<Feature> leaveLengthNode = descriptionTermNode.addChild(leaveLength);
handleIdentifiableEntity(featureTree);
State inapplicableState = State.NewInstance("inapplicableState", "inapplicableState", null);
State applicableState = State.NewInstance("only applicable state", "only applicable state", null);
cdmBases.add(applicableState);
cdmBases.add(inapplicableState);
// this is semantically not correct, should be a parent feature
leaveLengthNode.addInapplicableState(leaveLength, inapplicableState);
leaveLengthNode.addApplicableState(leaveLength, applicableState);
cdmBases.add(featureTree);
cdmBases.add(leaveLengthNode);
// DescriptiveDataSet
DescriptiveDataSet descriptiveDataSet = DescriptiveDataSet.NewInstance();
descriptiveDataSet.addDescription(taxonDescription);
descriptiveDataSet.setLabel("My Descriptive Dataset");
descriptiveDataSet.getDescriptiveSystem();
handleAnnotatableEntity(descriptiveDataSet);
descriptiveDataSet.addGeoFilterArea(Country.GERMANY());
Classification classification = Classification.NewInstance("DescriptiveDataSet subtree classification");
Taxon subTreeTaxon = getTaxon();
TaxonNode subtree = classification.addChildTaxon(subTreeTaxon, null, null);
descriptiveDataSet.addTaxonSubtree(subtree);
cdmBases.add(classification);
cdmBases.add(subtree);
// polytomous keys
Taxon coveredTaxon = Taxon.NewInstance(name, null);
PolytomousKey key = PolytomousKey.NewTitledInstance("My Polykey");
handleIdentificationKey(key, taxon, coveredTaxon);
key.setStartNumber(10);
PolytomousKeyNode firstChildNode = PolytomousKeyNode.NewInstance("Green", "What is the leave length?", coveredTaxon, leaveLength);
key.getRoot().addChild(firstChildNode);
PolytomousKeyNode secondChildNode = PolytomousKeyNode.NewInstance("234");
firstChildNode.addChild(secondChildNode);
PolytomousKey subkey = PolytomousKey.NewTitledInstance("Sub-key");
firstChildNode.setSubkey(subkey);
PolytomousKeyNode subKeyNode = PolytomousKeyNode.NewInstance("sub key couplet");
subkey.getRoot().addChild(subKeyNode);
secondChildNode.setOtherNode(subKeyNode);
secondChildNode.putModifyingText(Language.GERMAN(), "manchmal");
cdmBases.add(key);
cdmBases.add(subkey);
MediaKey mediaKey = MediaKey.NewInstance();
mediaKey.addKeyRepresentation(Representation.NewInstance("Media Key Representation", "media key", null, Language.ENGLISH()));
handleIdentificationKey(mediaKey, taxon, coveredTaxon);
MultiAccessKey multiAccessKey = MultiAccessKey.NewInstance();
handleIdentificationKey(multiAccessKey, taxon, coveredTaxon);
cdmBases.add(mediaKey);
cdmBases.add(multiAccessKey);
}
use of eu.etaxonomy.cdm.model.description.PolytomousKeyNode in project cdmlib by cybertaxonomy.
the class PolytomousKeyNodeServiceTest method testDelete.
@Test
public final // @DataSet(value="CommonServiceImplTest.xml")
void testDelete() {
PolytomousKey key = PolytomousKey.NewTitledInstance("TestPolytomousKey");
UUID uuidKey = keyService.save(key).getUuid();
PolytomousKeyNode node = PolytomousKeyNode.NewInstance("Test statement");
key.setRoot(node);
key.setStartNumber(0);
PolytomousKeyNode child = PolytomousKeyNode.NewInstance("Test statement Nr 2");
// child.setKey(key);
node.addChild(child, 0);
UUID uuidNode = service.save(node).getUuid();
PolytomousKeyNode child1 = PolytomousKeyNode.NewInstance("Test statement Nr 3");
// child.setKey(key);
child.addChild(child1, 0);
UUID uuidChild = service.save(child).getUuid();
PolytomousKeyNode child2 = PolytomousKeyNode.NewInstance("Test statement Nr 4");
// child.setKey(key);
child1.addChild(child2, 0);
UUID uuidChild1 = service.save(child1).getUuid();
node = service.load(uuidChild1);
UUID uuidChild2 = node.getChildAt(0).getUuid();
assertNotNull(node);
service.delete(uuidChild1, false);
node = service.load(uuidChild1);
assertNull(node);
node = service.load(uuidChild2);
assertNotNull(node);
node = service.load(uuidChild);
assertNotNull(node);
service.delete(uuidChild, true);
node = service.load(uuidChild);
assertNull(node);
node = service.load(uuidChild2);
assertNull(node);
}
Aggregations