org.erasmusmc.data_mining.ontology.impl.CompoundOntologyImpl Maven / Gradle / Ivy
The newest version!
/**
* Peregrine is an indexing engine that can recognize concepts in human readable
* text, based on a database (thesaurus) of known terms.
*
* Copyright 2005-2011 Erasmus University Medical Centre (EMC)
* Copyright 2009-2011 Netherlands Bioinformatics Centre (NBIC)
*
* Contact us at: [email protected]
*
* This file is part of Peregrine. Peregrine is free software: you can
* redistribute it and/or modify it under the terms of the GNU Affero General
* Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package org.erasmusmc.data_mining.ontology.impl;
import java.io.Serializable;
import java.util.Iterator;
import org.erasmusmc.data_mining.ontology.api.Concept;
import org.erasmusmc.data_mining.ontology.api.ModifiableOntology;
import org.erasmusmc.data_mining.ontology.api.Ontology;
import org.erasmusmc.data_mining.ontology.api.OntologyModificationListener;
import org.erasmusmc.data_mining.ontology.api.Term;
/**
* This is an ontology implementation, that combines two ontologies. Useful in
* cases:
*
* - when data should be read from different ontologies
*
- when data should be read from two ontologies, but persisted only into one
* ontology.
*
*
* @author Dmitry Katsubo
*/
public final class CompoundOntologyImpl implements ModifiableOntology {
/**
* The first ontology.
*/
private final Ontology ontology1;
/**
* The second ontology.
*/
private final Ontology ontology2;
/**
* Constructor.
*
* @param first The first ontology.
* @param second The second ontology.
*/
public CompoundOntologyImpl(final Ontology first,
final Ontology second) {
if (first instanceof ModifiableOntology
&& second instanceof ModifiableOntology) {
throw new IllegalArgumentException(
"Can't handle two modifiable ontologies at a time");
}
/*
* Make sure that if there is at least one modifiable ontology, it is
* set to 1st field.
*/
if (second instanceof ModifiableOntology) {
this.ontology1 = second;
this.ontology2 = first;
} else {
this.ontology1 = first;
this.ontology2 = second;
}
}
@Override
public void addConcept(final Concept concept) {
if (ontology1 instanceof ModifiableOntology) {
((ModifiableOntology) ontology1).addConcept(concept);
} else {
throw new UnsupportedOperationException();
}
}
@Override
public boolean removeConcept(final Serializable conceptId) {
if (ontology1 instanceof ModifiableOntology) {
return ((ModifiableOntology) ontology1).removeConcept(conceptId);
} else {
throw new UnsupportedOperationException();
}
}
@Override
public void addListener(final OntologyModificationListener listener) {
if (ontology1 instanceof ModifiableOntology) {
((ModifiableOntology) ontology1).addListener(listener);
} else {
throw new UnsupportedOperationException();
}
}
@Override
public void removeListener(final OntologyModificationListener listener) {
if (ontology1 instanceof ModifiableOntology) {
((ModifiableOntology) ontology1).removeListener(listener);
} else {
throw new UnsupportedOperationException();
}
}
@Override
public Concept getConcept(final Serializable conceptId) {
final Concept concept = ontology1.getConcept(conceptId);
if (concept != null) {
return concept;
}
return ontology2.getConcept(conceptId);
}
@Override
public Term getTerm(final Term.Id termId) {
final Term term = ontology1.getTerm(termId);
if (term != null) {
return term;
}
return ontology2.getTerm(termId);
}
@Override
@SuppressWarnings("unchecked")
public Iterator getConceptsIterator() {
return new CompoundIterator<>(ontology1.getConceptsIterator(),
ontology2.getConceptsIterator());
}
@Override
@SuppressWarnings("unchecked")
public Iterator getTermsIterator() {
return new CompoundIterator<>(ontology1.getTermsIterator(),
ontology2.getTermsIterator());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy