![JAR search and dependency download from the Maven repository](/logo.png)
edu.stanford.protege.webprotege.hierarchy.AnnotationPropertyHierarchyProviderImpl Maven / Gradle / Ivy
The newest version!
package edu.stanford.protege.webprotege.hierarchy;
import com.google.common.base.Stopwatch;
import edu.stanford.protege.webprotege.change.OntologyChange;
import edu.stanford.protege.webprotege.index.*;
import edu.stanford.protege.webprotege.inject.ProjectSingleton;
import edu.stanford.protege.webprotege.common.ProjectId;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.util.OWLAxiomVisitorAdapter;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import java.util.*;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
/**
* Author: drummond
* http://www.cs.man.ac.uk/~drummond/
*
* The University Of Manchester
* Bio Health Informatics Group
* Date: Apr 23, 2009
*/
@ProjectSingleton
public class AnnotationPropertyHierarchyProviderImpl extends AbstractHierarchyProvider implements AnnotationPropertyHierarchyProvider {
private static final Logger logger = LoggerFactory.getLogger(AnnotationPropertyHierarchyProviderImpl.class);
private final ProjectId projectId;
private final Set roots;
private final OWLAnnotationPropertyProvider annotationPropertyProvider;
private final ProjectSignatureByTypeIndex projectSignatureByTypeIndex;
private final ProjectOntologiesIndex projectOntologiesIndex;
private final SubAnnotationPropertyAxiomsBySubPropertyIndex subAnnotationPropertyAxioms;
private final SubAnnotationPropertyAxiomsBySuperPropertyIndex subAnnotationPropertyAxiomsBySuperPropertyIndex;
private final EntitiesInProjectSignatureIndex entitiesInSignature;
private boolean stale = true;
@Inject
public AnnotationPropertyHierarchyProviderImpl(ProjectId projectId,
OWLAnnotationPropertyProvider annotationPropertyProvider,
ProjectSignatureByTypeIndex projectSignatureByTypeIndex,
ProjectOntologiesIndex projectOntologiesIndex,
SubAnnotationPropertyAxiomsBySubPropertyIndex subAnnotationPropertyAxioms,
SubAnnotationPropertyAxiomsBySuperPropertyIndex subAnnotationPropertyAxiomsBySuperPropertyIndex,
EntitiesInProjectSignatureIndex entitiesInSignature) {
this.projectId = projectId;
this.projectSignatureByTypeIndex = projectSignatureByTypeIndex;
this.projectOntologiesIndex = projectOntologiesIndex;
this.subAnnotationPropertyAxioms = subAnnotationPropertyAxioms;
this.subAnnotationPropertyAxiomsBySuperPropertyIndex = subAnnotationPropertyAxiomsBySuperPropertyIndex;
this.entitiesInSignature = entitiesInSignature;
this.roots = new HashSet<>();
this.annotationPropertyProvider = annotationPropertyProvider;
}
private void rebuildIfNecessary() {
if(this.stale) {
rebuildRoots();
}
}
public Collection getRoots() {
rebuildIfNecessary();
return Collections.unmodifiableSet(roots);
}
public boolean containsReference(OWLAnnotationProperty object) {
return entitiesInSignature.containsEntityInSignature(object);
}
public Collection getChildren(OWLAnnotationProperty object) {
rebuildIfNecessary();
return projectOntologiesIndex.getOntologyIds()
.flatMap(ontId -> subAnnotationPropertyAxiomsBySuperPropertyIndex.getAxiomsForSuperProperty(object, ontId))
.map(OWLSubAnnotationPropertyOfAxiom::getSubProperty)
.filter(prop -> !getAncestors(prop).contains(prop))
.collect(toImmutableSet());
}
public Collection getParents(OWLAnnotationProperty object) {
rebuildIfNecessary();
return projectOntologiesIndex.getOntologyIds()
.flatMap(ontId -> subAnnotationPropertyAxioms.getSubPropertyOfAxioms(object, ontId))
.map(OWLSubAnnotationPropertyOfAxiom::getSuperProperty)
.collect(toImmutableSet());
}
public void dispose() {
super.dispose();
}
public void handleChanges(List changes) {
Set properties = new HashSet<>(getPropertiesReferencedInChange(changes));
for (OWLAnnotationProperty prop : properties) {
if (isRoot(prop)) {
roots.add(prop);
}
else {
if (getAncestors(prop).contains(prop)) {
roots.add(prop);
for (OWLAnnotationProperty anc : getAncestors(prop)) {
if (getAncestors(anc).contains(prop)) {
roots.add(anc);
}
}
}
else {
roots.remove(prop);
}
}
}
}
private Set getPropertiesReferencedInChange(List changes){
final Set props = new HashSet<>();
for (OntologyChange chg : changes){
if(chg.isAxiomChange()){
chg.getAxiomOrThrow().accept(new OWLAxiomVisitorAdapter(){
public void visit(OWLSubAnnotationPropertyOfAxiom owlSubAnnotationPropertyOfAxiom) {
props.add(owlSubAnnotationPropertyOfAxiom.getSubProperty());
props.add(owlSubAnnotationPropertyOfAxiom.getSuperProperty());
}
public void visit(OWLDeclarationAxiom owlDeclarationAxiom) {
if (owlDeclarationAxiom.getEntity().isOWLAnnotationProperty()){
props.add(owlDeclarationAxiom.getEntity().asOWLAnnotationProperty());
}
}
});
}
}
return props;
}
private boolean isRoot(OWLAnnotationProperty prop) {
// We deem a property to be a root property if it doesn't have
// any super properties (i.e. it is not on
// the LHS of a subproperty axiom
// Assume the property is a root property to begin with
boolean isRoot = getParents(prop).isEmpty();
// Additional condition: If we have P -> Q and Q -> P, then
// there is no path to the root, so put P and Q as root properties
// Collapse any cycles and force properties that are equivalent
// through cycles to appear at the root.
return isRoot && containsReference(prop) || getAncestors(prop).contains(prop);
}
private void rebuildRoots() {
this.stale = false;
roots.clear();
logger.info("{} Rebuilding annotation property hierarchy", projectId);
Stopwatch stopwatch = Stopwatch.createStarted();
projectSignatureByTypeIndex.getSignature(EntityType.ANNOTATION_PROPERTY)
.filter(this::isRoot)
.forEach(roots::add);
OWLRDFVocabulary.BUILT_IN_ANNOTATION_PROPERTY_IRIS.stream()
.map(annotationPropertyProvider::getOWLAnnotationProperty)
.forEach(roots::add);
logger.info("{} Rebuilt annotation property hierarchy provider in {} ms", projectId, stopwatch.elapsed(MILLISECONDS));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy