package eu.etaxonomy.cdm.io.descriptive.owl.in;
import java.net.URI;
+import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
-import eu.etaxonomy.cdm.api.service.ITermService;
+import eu.etaxonomy.cdm.api.application.ICdmRepository;
import eu.etaxonomy.cdm.common.CdmUtils;
import eu.etaxonomy.cdm.io.descriptive.owl.OwlUtil;
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
import eu.etaxonomy.cdm.model.common.Language;
import eu.etaxonomy.cdm.model.description.Character;
import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.cdm.model.description.MeasurementUnit;
+import eu.etaxonomy.cdm.model.description.State;
+import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
import eu.etaxonomy.cdm.model.media.Media;
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
import eu.etaxonomy.cdm.model.reference.Reference;
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
import eu.etaxonomy.cdm.model.term.DefinedTerm;
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
+import eu.etaxonomy.cdm.model.term.FeatureNode;
import eu.etaxonomy.cdm.model.term.Representation;
import eu.etaxonomy.cdm.model.term.TermType;
import eu.etaxonomy.cdm.model.term.TermVocabulary;
static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(OwlImportUtil.class);
- static Feature createFeature(Resource termResource, ITermService termService, Model model, StructureTreeOwlImportState state){
- Feature feature = Feature.NewInstance();
+ 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<DefinedTermBase> measurementUnits = new HashSet<>();
-// List<Statement> measurementUnitStatements = termResource.listProperties(OwlUtil.propFeatureHasRecommendedMeasurementUnit).toList();
-// for (Statement statement : measurementUnitStatements) {
-// Resource measurementUnitResource = model.createResource(statement.getObject().toString());
-// measurementUnits.add(OwlImportUtil.createTerm(measurementUnitResource, termService, model, state));
-// }
-// measurementUnits.forEach(unit->feature.addRecommendedMeasurementUnit(unit));
-
- return feature;
- }
-
- static Character createCharacter(Resource termResource, ITermService termService, Model model, StructureTreeOwlImportState state){
- Character character = Character.NewInstance();
- return character;
+ // 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));
}
- static DefinedTermBase createTerm(Resource termResource, ITermService termService, Model model, StructureTreeOwlImportState state){
- TermType termType = TermType.getByKey(termResource.getProperty(OwlUtil.propType).getString());
- DefinedTermBase term;
- // create new term
- if(termType.equals(TermType.Feature)){
- term = createFeature(termResource, termService, model, state);
+ private static void addCharacterProperties(Character character, Resource termResource, ICdmRepository repo, Model model, StructureTreeOwlImportState state){
+ addFeatureProperties(character, termResource, repo, model, state);
+ // TODO import/export of complete term tree of structures and properties belonging to a character is necessary
+ // import structure
+ Statement structureStatement = termResource.getProperty(OwlUtil.propCharacterHasStructure);
+ Resource structureResource = model.createResource(structureStatement.getObject().toString());
+ FeatureNode structureNode = findNode(structureResource, repo, model, state);
+ if(structureNode!=null){
+ character.setStructure(structureNode);
}
- else if(termType.equals(TermType.Character)){
- term = createCharacter(termResource, termService, model, state);
+ // import property
+ Statement propertyStatement = termResource.getProperty(OwlUtil.propCharacterHasProperty);
+ Resource propertyResource = model.createResource(propertyStatement.getObject().toString());
+ FeatureNode propertyNode = findNode(propertyResource, repo, model, state);
+ if(propertyNode!=null){
+ character.setProperty(propertyNode);
}
- else{
- term = DefinedTerm.NewInstance(termType);
+ // import structure modifier
+ Statement structureModifierStatement = termResource.getProperty(OwlUtil.propCharacterHasStructureModfier);
+ Resource structureModifierResource = model.createResource(structureModifierStatement.getObject().toString());
+ DefinedTerm structureModifier = findTerm(DefinedTerm.class, structureModifierResource, repo, model, state);
+ if(structureModifier!=null){
+ character.setStructureModifier(structureModifier);
}
+ }
+
+ private static void addTermProperties(DefinedTermBase term, Resource termResource, ICdmRepository repo, Model model, StructureTreeOwlImportState state){
term.setUuid(UUID.fromString(termResource.getProperty(OwlUtil.propUuid).getString()));
// term URI
// import representations
Set<Representation> representations = new HashSet<>();
- termResource.listProperties(OwlUtil.propHasRepresentation).forEachRemaining(r->representations.add(OwlImportUtil.createRepresentation(termService, r, model)));
+ termResource.listProperties(OwlUtil.propHasRepresentation).forEachRemaining(r->representations.add(OwlImportUtil.createRepresentation(repo, r, model)));
if(representations.isEmpty()){
logger.error("No representations found for term: "+termResource.getProperty(OwlUtil.propUuid));
}
IdentifiableSource importSource = IdentifiableSource.NewDataImportInstance(termResource.getURI());
importSource.setCitation(state.getConfig().getSourceReference());
term.addSource(importSource);
+ }
+
+ private static <T extends DefinedTermBase> T findTerm(Class<T> clazz, Resource termResource, ICdmRepository repo, Model model, StructureTreeOwlImportState state){
+ UUID termUuid = UUID.fromString(termResource.getProperty(OwlUtil.propUuid).getString());
+ List<T> terms = repo.getTermService().find(clazz, Collections.singleton(termUuid));
+ if(!terms.isEmpty()){
+ return terms.iterator().next();
+ }
+ return null;
+ }
+ private static TermVocabulary findVocabulary(Resource termResource, ICdmRepository repo, Model model, StructureTreeOwlImportState state){
+ UUID termUuid = UUID.fromString(termResource.getProperty(OwlUtil.propUuid).getString());
+ return repo.getVocabularyService().find(termUuid);
+ }
+
+ private static FeatureNode findNode(Resource termResource, ICdmRepository repo, Model model, StructureTreeOwlImportState state){
+ UUID uuid = UUID.fromString(termResource.getProperty(OwlUtil.propUuid).getString());
+ return repo.getFeatureNodeService().find(uuid);
+ }
+
+ static Feature createFeature(Resource termResource, ICdmRepository repo, Model model, StructureTreeOwlImportState state){
+ Feature feature = findTerm(Feature.class, termResource, repo, model, state);
+ if(feature==null){
+ feature = Feature.NewInstance();
+ addFeatureProperties(feature, termResource, repo, model, state);
+ }
+ return feature;
+ }
+
+ private static Character createCharacter(Resource termResource, ICdmRepository repo, Model model, StructureTreeOwlImportState state){
+ Character character = findTerm(Character.class, termResource, repo, model, state);
+ if(character==null){
+ character = Character.NewInstance();
+ addCharacterProperties(character, termResource, repo, model, state);
+ }
+ return character;
+ }
+
+ static DefinedTermBase createTerm(Resource termResource, ICdmRepository repo, Model model, StructureTreeOwlImportState state){
+ TermType termType = TermType.getByKey(termResource.getProperty(OwlUtil.propType).getString());
+ DefinedTermBase term = null;
+ // create new term
+ if(termType.equals(TermType.Feature)){
+ term = createFeature(termResource, repo, model, state);
+ }
+ else if(termType.equals(TermType.Character)){
+ term = createCharacter(termResource, repo, model, state);
+ }
+ else{
+ term = DefinedTerm.NewInstance(termType);
+ }
+ addTermProperties(term, termResource, repo, model, state);
return term;
}
return citation;
}
- static TermVocabulary createVocabulary(Resource vocabularyResource, ITermService termService, Model model, StructureTreeOwlImportState state){
+ static TermVocabulary createVocabulary(Resource vocabularyResource, ICdmRepository repo, Model model, StructureTreeOwlImportState state){
TermType termType = TermType.getByKey(vocabularyResource.getProperty(OwlUtil.propType).getString());
// create new vocabulary
TermVocabulary vocabulary = TermVocabulary.NewInstance(termType);
// voc representations
Set<Representation> vocRepresentations = new HashSet<>();
- vocabularyResource.listProperties(OwlUtil.propHasRepresentation).forEachRemaining(r->vocRepresentations.add(OwlImportUtil.createRepresentation(termService, r, model)));
+ vocabularyResource.listProperties(OwlUtil.propHasRepresentation).forEachRemaining(r->vocRepresentations.add(OwlImportUtil.createRepresentation(repo, r, model)));
if(vocRepresentations.isEmpty()){
logger.error("No representations found for vocabulary: "+vocabularyResource.getProperty(OwlUtil.propUuid));
}
return media;
}
- static Representation createRepresentation(ITermService termService, Statement repr, Model model) {
+ static Representation createRepresentation(ICdmRepository repo, Statement repr, Model model) {
Resource repsentationResource = model.createResource(repr.getObject().toString());
String languageLabel = repsentationResource.getProperty(OwlUtil.propLanguage).getString();
UUID languageUuid = UUID.fromString(repsentationResource.getProperty(OwlUtil.propLanguageUuid).getString());
Language language = Language.getLanguageFromUuid(languageUuid);
if(language==null){
- language = termService.getLanguageByLabel(languageLabel);
+ language = repo.getTermService().getLanguageByLabel(languageLabel);
}
if(language==null){
language = Language.getDefaultLanguage();