All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.protege.editor.owl.model.util.OWLObjectRemover Maven / Gradle / Ivy

Go to download

OWL ontology editing infrastructure used by the Protege desktop application.

The newest version!
package org.protege.editor.owl.model.util;

import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.util.OWLObjectVisitorExAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Matthew Horridge
 * Stanford Center for Biomedical Informatics Research
 * 08/03/15
 */
public class OWLObjectRemover {

    /**
     * Gets the changes to remove the specified entity from the specified ontology.
     *
     * @param object  The entity.  Not {@code null}.
     * @return The changes.
     */
    public List getChangesToRemoveObject(OWLEntity object, final OWLOntology ontology) {
        return getChangesToRemoveObject((OWLObject) object, ontology);
    }



    /**
     * Gets the changes to remove the specified individual from the specified ontology.
     *
     * @param object  The individual.  Not {@code null}.
     * @return The changes.
     */
    public List getChangesToRemoveObject(OWLAnonymousIndividual object, final OWLOntology ontology) {
        return getChangesToRemoveObject((OWLObject) object, ontology);
    }


    private List getChangesToRemoveObject(OWLObject object, final OWLOntology ontology) {
        return object.accept(new OWLObjectVisitorExAdapter>(Collections.emptyList()) {
            @Override
            public List visit(OWLDatatype datatype) {
                return getChangesForEntity(datatype, ontology);
            }

            @Override
            public List visit(OWLDataProperty property) {
                return getChangesForEntity(property, ontology);
            }

            @Override
            public List visit(OWLObjectProperty property) {
                return getChangesForEntity(property, ontology);
            }

            @Override
            public List visit(OWLNamedIndividual individual) {
                return getChangesForEntity(individual, ontology);
            }

            @Override
            public List visit(OWLClass desc) {
                return getChangesForEntity(desc, ontology);
            }

            @Override
            public List visit(OWLAnonymousIndividual individual) {
                return getChangesForAnonymousIndividual(individual, ontology);
            }

            @Override
            public List visit(OWLAnnotationProperty property) {
                List changes = getChangesForEntity(property, ontology);
                changes.addAll(getChangesForOntologyAnnotations(property, ontology));
                return changes;
            }
        });
    }

    private List getChangesForAnonymousIndividual(OWLAnonymousIndividual individual, OWLOntology ont) {
        List changes = new ArrayList<>();
        for (OWLAxiom ax : ont.getReferencingAxioms(individual)) {
            changes.add(new RemoveAxiom(ont, ax));
        }
        changes.addAll(getChangesForAnnotationSubject(individual, ont));
        changes.addAll(getChangesForAnnotationValue(individual, ont));
        return changes;
    }

    private List getChangesForEntity(OWLEntity entity, OWLOntology ont) {
        List changes = new ArrayList<>();
        for (OWLAxiom ax : ont.getReferencingAxioms(entity)) {
            changes.add(new RemoveAxiom(ont, ax));
        }
        IRI entityIRI = entity.getIRI();
        changes.addAll(getChangesForAnnotationSubject(entityIRI, ont));
        changes.addAll(getChangesForAnnotationValue(entityIRI, ont));
        changes.addAll(getChangesForOntologyAnnotations(entityIRI, ont));
        return changes;
    }


    private List getChangesForAnnotationSubject(OWLAnnotationSubject subject, OWLOntology ont) {
        List changes = new ArrayList<>();
        for (OWLAnnotationAssertionAxiom ax : ont.getAnnotationAssertionAxioms(subject)) {
            changes.add(new RemoveAxiom(ont, ax));
        }
        return changes;
    }

    private List getChangesForAnnotationValue(OWLAnnotationValue object, OWLOntology ont) {
        List changes = new ArrayList<>();
        for (OWLAnnotationAssertionAxiom ax : ont.getAxioms(AxiomType.ANNOTATION_ASSERTION)) {
            if (ax.getValue().equals(object)) {
                changes.add(new RemoveAxiom(ont, ax));
            }
        }
        return changes;
    }

    private List getChangesForOntologyAnnotations(OWLAnnotationProperty annotationProperty, OWLOntology ont) {
        List changes = new ArrayList<>();
        for (OWLAnnotation annotation : ont.getAnnotations()) {
            if (annotation.getProperty().equals(annotationProperty)) {
                changes.add(new RemoveOntologyAnnotation(ont, annotation));
            }
        }
        return changes;
    }

    private List getChangesForOntologyAnnotations(OWLAnnotationValue value, OWLOntology ont) {
        List changes = new ArrayList<>();
        for (OWLAnnotation annotation : ont.getAnnotations()) {
            if (annotation.getValue().equals(value)) {
                changes.add(new RemoveOntologyAnnotation(ont, annotation));
            }
        }
        return changes;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy