org.semanticweb.owlapi.util.OWLAnonymousIndividualsWithMultipleOccurrences Maven / Gradle / Ivy
/* This file is part of the OWL API.
* The contents of this file are subject to the LGPL License, Version 3.0.
* Copyright 2014, The University of Manchester
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU 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 General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
*
* Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */
package org.semanticweb.owlapi.util;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.semanticweb.owlapi.model.*;
/**
* A utility class that visits axioms, class expressions etc. and accumulates the anonymous
* individuals objects that are referred to in those axioms, class expressions etc.
*
* @author Matthew Horridge, The University Of Manchester, Bio-Health Informatics Group, Date:
* 13-Nov-2006
*/
public class OWLAnonymousIndividualsWithMultipleOccurrences
implements OWLObjectVisitor, SWRLObjectVisitor, IndividualAppearance {
private final Set singleAppearance = new HashSet<>();
private final Set multipleAppearances = new HashSet<>();
// ////////////////////////////////////////////////////////////////////////////////////////////
//
// Axiom Visitor stuff
//
// ////////////////////////////////////////////////////////////////////////////////////////////
protected void processAxiomAnnotations(OWLAxiom ax) {
// default behavior: iterate over the annotations outside the axiom
for (OWLAnnotation anno : ax.getAnnotations()) {
anno.accept(this);
}
}
@Override
public boolean appearsMultipleTimes(OWLAnonymousIndividual i) {
return multipleAppearances.contains(i);
}
@Override
public void visit(OWLSubClassOfAxiom axiom) {
axiom.getSubClass().accept(this);
axiom.getSuperClass().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLNegativeObjectPropertyAssertionAxiom axiom) {
axiom.getProperty().accept(this);
axiom.getObject().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLAsymmetricObjectPropertyAxiom axiom) {
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLReflexiveObjectPropertyAxiom axiom) {
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLDisjointClassesAxiom axiom) {
for (OWLClassExpression desc : axiom.getClassExpressions()) {
desc.accept(this);
}
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLDataPropertyDomainAxiom axiom) {
axiom.getDomain().accept(this);
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLObjectPropertyDomainAxiom axiom) {
axiom.getDomain().accept(this);
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLEquivalentObjectPropertiesAxiom axiom) {
for (OWLObjectPropertyExpression prop : axiom.getProperties()) {
prop.accept(this);
}
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLNegativeDataPropertyAssertionAxiom axiom) {
axiom.getProperty().accept(this);
axiom.getObject().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLDifferentIndividualsAxiom axiom) {
for (OWLIndividual ind : axiom.getIndividuals()) {
ind.accept(this);
}
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLDisjointDataPropertiesAxiom axiom) {
for (OWLDataPropertyExpression prop : axiom.getProperties()) {
prop.accept(this);
}
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLDisjointObjectPropertiesAxiom axiom) {
for (OWLObjectPropertyExpression prop : axiom.getProperties()) {
prop.accept(this);
}
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLObjectPropertyRangeAxiom axiom) {
axiom.getRange().accept(this);
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLObjectPropertyAssertionAxiom axiom) {
axiom.getProperty().accept(this);
axiom.getSubject().accept(this);
axiom.getObject().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLFunctionalObjectPropertyAxiom axiom) {
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLSubObjectPropertyOfAxiom axiom) {
axiom.getSubProperty().accept(this);
axiom.getSuperProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLDisjointUnionAxiom axiom) {
axiom.getOWLClass().accept((OWLEntityVisitor) this);
for (OWLClassExpression desc : axiom.getClassExpressions()) {
desc.accept(this);
}
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLDeclarationAxiom axiom) {
axiom.getEntity().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLSymmetricObjectPropertyAxiom axiom) {
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLDataPropertyRangeAxiom axiom) {
axiom.getProperty().accept(this);
axiom.getRange().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLFunctionalDataPropertyAxiom axiom) {
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLEquivalentDataPropertiesAxiom axiom) {
for (OWLDataPropertyExpression prop : axiom.getProperties()) {
prop.accept(this);
}
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLClassAssertionAxiom axiom) {
axiom.getClassExpression().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLEquivalentClassesAxiom axiom) {
for (OWLClassExpression desc : axiom.getClassExpressions()) {
desc.accept(this);
}
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLDataPropertyAssertionAxiom axiom) {
axiom.getProperty().accept(this);
axiom.getObject().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLTransitiveObjectPropertyAxiom axiom) {
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLIrreflexiveObjectPropertyAxiom axiom) {
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLSubDataPropertyOfAxiom axiom) {
axiom.getSubProperty().accept(this);
axiom.getSuperProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLInverseFunctionalObjectPropertyAxiom axiom) {
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLSameIndividualAxiom axiom) {
for (OWLIndividual ind : axiom.getIndividuals()) {
ind.accept(this);
}
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLSubPropertyChainOfAxiom axiom) {
for (OWLObjectPropertyExpression prop : axiom.getPropertyChain()) {
prop.accept(this);
}
axiom.getSuperProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLInverseObjectPropertiesAxiom axiom) {
axiom.getFirstProperty().accept(this);
axiom.getSecondProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLHasKeyAxiom axiom) {
axiom.getClassExpression().accept(this);
for (OWLPropertyExpression prop : axiom.getPropertyExpressions()) {
prop.accept(this);
}
processAxiomAnnotations(axiom);
}
// //////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// OWLClassExpressionVisitor
//
// //////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void visit(OWLClass desc) {}
@Override
public void visit(OWLObjectIntersectionOf desc) {
for (OWLClassExpression operand : desc.getOperands()) {
operand.accept(this);
}
}
@Override
public void visit(OWLObjectUnionOf desc) {
for (OWLClassExpression operand : desc.getOperands()) {
operand.accept(this);
}
}
@Override
public void visit(OWLObjectComplementOf desc) {
desc.getOperand().accept(this);
}
@Override
public void visit(OWLObjectSomeValuesFrom desc) {
desc.getProperty().accept(this);
desc.getFiller().accept(this);
}
@Override
public void visit(OWLObjectAllValuesFrom desc) {
desc.getProperty().accept(this);
desc.getFiller().accept(this);
}
@Override
public void visit(OWLObjectHasValue desc) {
desc.getProperty().accept(this);
desc.getFiller().accept(this);
}
@Override
public void visit(OWLObjectMinCardinality desc) {
desc.getProperty().accept(this);
desc.getFiller().accept(this);
}
@Override
public void visit(OWLObjectExactCardinality desc) {
desc.getProperty().accept(this);
desc.getFiller().accept(this);
}
@Override
public void visit(OWLObjectMaxCardinality desc) {
desc.getProperty().accept(this);
desc.getFiller().accept(this);
}
@Override
public void visit(OWLObjectHasSelf desc) {
desc.getProperty().accept(this);
}
@Override
public void visit(OWLObjectOneOf desc) {
for (OWLIndividual ind : desc.getIndividuals()) {
ind.accept(this);
}
}
@Override
public void visit(OWLDataSomeValuesFrom desc) {
desc.getProperty().accept(this);
desc.getFiller().accept(this);
}
@Override
public void visit(OWLDataAllValuesFrom desc) {
desc.getProperty().accept(this);
desc.getFiller().accept(this);
}
@Override
public void visit(OWLDataHasValue desc) {
desc.getProperty().accept(this);
desc.getFiller().accept(this);
}
@Override
public void visit(OWLDataMinCardinality desc) {
desc.getProperty().accept(this);
desc.getFiller().accept(this);
}
@Override
public void visit(OWLDataExactCardinality desc) {
desc.getProperty().accept(this);
desc.getFiller().accept(this);
}
@Override
public void visit(OWLDataMaxCardinality desc) {
desc.getProperty().accept(this);
desc.getFiller().accept(this);
}
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Data visitor
//
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void visit(OWLDataComplementOf node) {
node.getDataRange().accept(this);
}
@Override
public void visit(OWLDataOneOf node) {
for (OWLLiteral val : node.getValues()) {
val.accept(this);
}
}
@Override
public void visit(OWLDataIntersectionOf node) {
for (OWLDataRange dr : node.getOperands()) {
dr.accept(this);
}
}
@Override
public void visit(OWLDataUnionOf node) {
for (OWLDataRange dr : node.getOperands()) {
dr.accept(this);
}
}
@Override
public void visit(OWLDatatypeRestriction node) {
node.getDatatype().accept(this);
for (OWLFacetRestriction facetRestriction : node.getFacetRestrictions()) {
facetRestriction.accept(this);
}
}
@Override
public void visit(OWLFacetRestriction node) {
node.getFacetValue().accept(this);
}
@Override
public void visit(OWLLiteral node) {
node.getDatatype().accept(this);
}
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Property expression visitor
//
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void visit(OWLObjectInverseOf expression) {
expression.getInverse().accept(this);
}
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Entity visitor
//
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void visit(OWLObjectProperty property) {}
@Override
public void visit(OWLDataProperty property) {}
@Override
public void visit(OWLNamedIndividual individual) {}
@Override
public void visit(OWLDatatype datatype) {}
@Override
public void visit(OWLAnnotation annotation) {
annotation.getProperty().accept(this);
annotation.getValue().accept(this);
for (OWLAnnotation anno : annotation.getAnnotations()) {
anno.accept(this);
}
}
@Override
public void visit(OWLAnnotationAssertionAxiom axiom) {
axiom.getProperty().accept(this);
axiom.getValue().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLAnonymousIndividual individual) {
checkAppearance(individual);
}
@Override
public void visit(IRI iri) {}
@Override
public void visit(OWLOntology ontology) {
checkOccurrence(ontology.getAnnotations());
for (AxiomType> t : AxiomType.AXIOM_TYPES) {
for (OWLAxiom ax : ontology.getAxioms(t)) {
checkOccurrence(ax.getAnnotations());
ax.accept(this);
}
}
singleAppearance.clear();
}
private void checkOccurrence(Collection c) {
for (OWLAnnotation a : c) {
if (a.getValue() instanceof OWLAnonymousIndividual) {
checkAppearance((OWLAnonymousIndividual) a.getValue());
}
}
}
protected void checkAppearance(OWLAnonymousIndividual a) {
if (!multipleAppearances.contains(a) && !singleAppearance.add(a)) {
// already seen, move it
singleAppearance.remove(a);
multipleAppearances.add(a);
}
}
@Override
public void visit(OWLAnnotationProperty property) {}
@Override
public void visit(OWLAnnotationPropertyDomainAxiom axiom) {
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLAnnotationPropertyRangeAxiom axiom) {
axiom.getProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLSubAnnotationPropertyOfAxiom axiom) {
axiom.getSubProperty().accept(this);
axiom.getSuperProperty().accept(this);
processAxiomAnnotations(axiom);
}
@Override
public void visit(OWLDatatypeDefinitionAxiom axiom) {
axiom.getDatatype().accept(this);
axiom.getDataRange().accept(this);
processAxiomAnnotations(axiom);
}
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// SWRL Object Visitor
//
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void visit(SWRLRule rule) {
for (SWRLAtom atom : rule.getBody()) {
atom.accept(this);
}
for (SWRLAtom atom : rule.getHead()) {
atom.accept(this);
}
processAxiomAnnotations(rule);
}
@Override
public void visit(SWRLClassAtom node) {
node.getArgument().accept(this);
node.getPredicate().accept(this);
}
@Override
public void visit(SWRLDataRangeAtom node) {
node.getArgument().accept(this);
node.getPredicate().accept(this);
}
@Override
public void visit(SWRLObjectPropertyAtom node) {
node.getPredicate().accept(this);
node.getFirstArgument().accept(this);
node.getSecondArgument().accept(this);
}
@Override
public void visit(SWRLDataPropertyAtom node) {
node.getPredicate().accept(this);
node.getFirstArgument().accept(this);
node.getSecondArgument().accept(this);
}
@Override
public void visit(SWRLBuiltInAtom node) {
for (SWRLArgument obj : node.getAllArguments()) {
obj.accept(this);
}
}
@Override
public void visit(SWRLVariable node) {}
@Override
public void visit(SWRLIndividualArgument node) {
node.getIndividual().accept(this);
}
@Override
public void visit(SWRLLiteralArgument node) {
node.getLiteral().accept(this);
}
@Override
public void visit(SWRLDifferentIndividualsAtom node) {
node.getFirstArgument().accept(this);
}
@Override
public void visit(SWRLSameIndividualAtom node) {
node.getSecondArgument().accept(this);
}
}