owlentityremover.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 957 行 · 第 1/3 页

JAVA
957
字号
//The MIT License//// Copyright (c) 2004 Mindswap Research Group, University of Maryland, College Park//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions://// The above copyright notice and this permission notice shall be included in// all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS// IN THE SOFTWARE./* * Author: Aditya Kalyanpur */package org.mindswap.swoop.utils.owlapi;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.semanticweb.owl.model.OWLAnd;import org.semanticweb.owl.model.OWLAnnotationInstance;import org.semanticweb.owl.model.OWLClass;import org.semanticweb.owl.model.OWLClassAxiom;import org.semanticweb.owl.model.OWLDataAllRestriction;import org.semanticweb.owl.model.OWLDataCardinalityRestriction;import org.semanticweb.owl.model.OWLDataFactory;import org.semanticweb.owl.model.OWLDataProperty;import org.semanticweb.owl.model.OWLDataSomeRestriction;import org.semanticweb.owl.model.OWLDataType;import org.semanticweb.owl.model.OWLDataValue;import org.semanticweb.owl.model.OWLDataValueRestriction;import org.semanticweb.owl.model.OWLDescription;import org.semanticweb.owl.model.OWLDifferentIndividualsAxiom;import org.semanticweb.owl.model.OWLDisjointClassesAxiom;import org.semanticweb.owl.model.OWLEntity;import org.semanticweb.owl.model.OWLEnumeration;import org.semanticweb.owl.model.OWLEquivalentClassesAxiom;import org.semanticweb.owl.model.OWLEquivalentPropertiesAxiom;import org.semanticweb.owl.model.OWLException;import org.semanticweb.owl.model.OWLIndividual;import org.semanticweb.owl.model.OWLIndividualAxiom;import org.semanticweb.owl.model.OWLNot;import org.semanticweb.owl.model.OWLObjectAllRestriction;import org.semanticweb.owl.model.OWLObjectCardinalityRestriction;import org.semanticweb.owl.model.OWLObjectProperty;import org.semanticweb.owl.model.OWLObjectQuantifiedRestriction;import org.semanticweb.owl.model.OWLObjectSomeRestriction;import org.semanticweb.owl.model.OWLObjectValueRestriction;import org.semanticweb.owl.model.OWLOntology;import org.semanticweb.owl.model.OWLOr;import org.semanticweb.owl.model.OWLProperty;import org.semanticweb.owl.model.OWLPropertyAxiom;import org.semanticweb.owl.model.OWLRestriction;import org.semanticweb.owl.model.OWLSameIndividualsAxiom;import org.semanticweb.owl.model.OWLSubClassAxiom;import org.semanticweb.owl.model.OWLSubPropertyAxiom;import org.semanticweb.owl.model.change.AddAnnotationInstance;import org.semanticweb.owl.model.change.AddClassAxiom;import org.semanticweb.owl.model.change.AddDataPropertyInstance;import org.semanticweb.owl.model.change.AddDataPropertyRange;import org.semanticweb.owl.model.change.AddDomain;import org.semanticweb.owl.model.change.AddEnumeration;import org.semanticweb.owl.model.change.AddEquivalentClass;import org.semanticweb.owl.model.change.AddIndividualAxiom;import org.semanticweb.owl.model.change.AddIndividualClass;import org.semanticweb.owl.model.change.AddInverse;import org.semanticweb.owl.model.change.AddObjectPropertyInstance;import org.semanticweb.owl.model.change.AddObjectPropertyRange;import org.semanticweb.owl.model.change.AddPropertyAxiom;import org.semanticweb.owl.model.change.AddSuperClass;import org.semanticweb.owl.model.change.AddSuperProperty;import org.semanticweb.owl.model.change.ChangeVisitor;import org.semanticweb.owl.model.change.OntologyChange;import org.semanticweb.owl.model.change.RemoveClassAxiom;import org.semanticweb.owl.model.change.RemoveDataPropertyInstance;import org.semanticweb.owl.model.change.RemoveDomain;import org.semanticweb.owl.model.change.RemoveEntity;import org.semanticweb.owl.model.change.RemoveEnumeration;import org.semanticweb.owl.model.change.RemoveEquivalentClass;import org.semanticweb.owl.model.change.RemoveIndividualAxiom;import org.semanticweb.owl.model.change.RemoveIndividualClass;import org.semanticweb.owl.model.change.RemoveInverse;import org.semanticweb.owl.model.change.RemoveObjectPropertyInstance;import org.semanticweb.owl.model.change.RemoveObjectPropertyRange;import org.semanticweb.owl.model.change.RemovePropertyAxiom;import org.semanticweb.owl.model.change.RemoveSuperClass;import org.semanticweb.owl.model.change.RemoveSuperProperty;import org.semanticweb.owl.model.change.SetFunctional;import org.semanticweb.owl.model.change.SetInverseFunctional;import org.semanticweb.owl.model.change.SetSymmetric;import org.semanticweb.owl.model.change.SetTransitive;/** * @author Aditya Kalyanpur * */public class OWLEntityRemover {	private OWLOntology ontology;	private OWLEntity remEntity;	private OWLEntity replEntity;		final static int ANNOTATION = -1;	final static int SUPERCLASS = 0;	final static int EQUIVALENTCLASS = 1;	final static int ENUMERATION = 2;	final static int SUBCLASSAXIOM = 3;	final static int EQUCLASSAXIOM = 4;	final static int DISJOINTAXIOM = 5;	final static int SUPERPROPERTY = 6;	final static int EQUIVALENTPROPERTY = 7;	final static int SUBPROPAXIOM = 8;	final static int EQUPROPAXIOM = 9;	final static int DOMAIN = 10;	final static int RANGE = 11; // object property range	final static int INVERSE = 12;	final static int INDIVIDUALCLASS = 13;	final static int INDDATAPROP = 14;	final static int INDOBJPROP = 15;	final static int SAMEAS = 16;	final static int DIFFFROM = 17;	final static String DISCARD = "DISCARD";		private List changes;		public OWLEntityRemover(OWLOntology ontology) {		this.ontology = ontology;		this.changes = new ArrayList();	}		/**	 * Replace an OWL Entity with another OWL Entity of the same type	 * @param removeEntity	 * @param replaceEntity	 */	public void replaceEntity(OWLEntity removeEntity, OWLEntity replaceEntity) {		this.replEntity = replaceEntity; // set this field		this.removeEntity(removeEntity); // now call this method which will remove and replace when necessary	}		/**	 * Removes all references of an entity from an ontology	 * @param removeEntity	 */	public void removeEntity(OWLEntity removeEntity) {				this.remEntity = removeEntity;		try {			// check all classes			for (Iterator iter = ontology.getClasses().iterator(); iter.hasNext();) {								OWLClass cla = (OWLClass) iter.next();								// check all basic stuff that would get removed automatically				// by using RemoveEntity on class				if (cla.equals(remEntity) && replEntity!=null) {					// move superclasses, equivalentclasses, enumerations, annotations					for (Iterator iter2 = cla.getSuperClasses(ontology).iterator(); iter2.hasNext();) {						OWLDescription sup = (OWLDescription) iter2.next();						this.addChange(replEntity, sup, this.SUPERCLASS);					}					for (Iterator iter2 = cla.getEquivalentClasses(ontology).iterator(); iter2.hasNext();) {						OWLDescription equ = (OWLDescription) iter2.next();						this.addChange(replEntity, equ, this.EQUIVALENTCLASS);					}					for (Iterator iter2 = cla.getEnumerations(ontology).iterator(); iter2.hasNext();) {						OWLEnumeration enu = (OWLEnumeration) iter2.next();						this.addChange(replEntity, enu, this.ENUMERATION);					}					for (Iterator iter2 = cla.getAnnotations(ontology).iterator(); iter2.hasNext();) {						OWLAnnotationInstance oai = (OWLAnnotationInstance) iter2.next();						this.addChange(replEntity, oai, this.ANNOTATION);					}				}								// check super classes				Set sup = cla.getSuperClasses(ontology);				for (Iterator iter2= sup.iterator(); iter2.hasNext();) {					OWLDescription desc = (OWLDescription) iter2.next();					this.checkClassRelation(cla, desc, SUPERCLASS);				}								// check equivalent classes				Set equ = cla.getEquivalentClasses(ontology);				for (Iterator iter2= equ.iterator(); iter2.hasNext();) {					OWLDescription desc = (OWLDescription) iter2.next();					this.checkClassRelation(cla, desc, EQUIVALENTCLASS);				}								// check enumerations				Set enuSet = cla.getEnumerations(ontology);				for (Iterator iter2= enuSet.iterator(); iter2.hasNext();) {					OWLEnumeration enu = (OWLEnumeration) iter2.next();					Set ops = enu.getIndividuals();					boolean changed = false;					for (Iterator iter3=new HashSet(ops).iterator(); iter3.hasNext();) {						OWLIndividual ind = (OWLIndividual) iter3.next();						if (ind.equals(remEntity)) {							changed = true;							ops.remove(ind);							if (replEntity!=null) ops.add(replEntity);						}					}					if (changed) {						// remove current enumeration and add new enum						this.removeChange(cla, enu, ENUMERATION);						if (ops.size()>0) {							OWLEnumeration newEnu = ontology.getOWLDataFactory().getOWLEnumeration(ops);							this.addChange(cla, newEnu, ENUMERATION);						}					}				}							}						// check class axioms			for (Iterator iter = ontology.getClassAxioms().iterator(); iter.hasNext();) {				OWLClassAxiom axiom = (OWLClassAxiom) iter.next();								// check sub class axioms				if (axiom instanceof OWLSubClassAxiom) {					OWLSubClassAxiom subAxiom = (OWLSubClassAxiom) axiom;					this.checkClassRelation(subAxiom.getSubClass(), subAxiom.getSuperClass(), SUBCLASSAXIOM);				}								// check equivalent class axioms				else if (axiom instanceof OWLEquivalentClassesAxiom) {					OWLEquivalentClassesAxiom equAxiom = (OWLEquivalentClassesAxiom) axiom;					Set equOps = equAxiom.getEquivalentClasses();					boolean changed = false;					for (Iterator iter2 = new HashSet(equOps).iterator(); iter2.hasNext();) {						OWLDescription desc = (OWLDescription) iter2.next();						Object check = this.traceDescription(desc);						if (check!=null) {							changed = true;							equOps.remove(desc);							if (check instanceof OWLDescription) {								equOps.add((OWLDescription) check);							}						}											}					if (changed) {						this.removeChange(equAxiom.getEquivalentClasses(), null, EQUCLASSAXIOM);						this.addChange(equOps, null, EQUCLASSAXIOM);					}				}								// check disjoint class axioms				else if (axiom instanceof OWLDisjointClassesAxiom) {					OWLDisjointClassesAxiom disAxiom = (OWLDisjointClassesAxiom) axiom;					Set disOps = disAxiom.getDisjointClasses();					boolean changed = false;					for (Iterator iter2 = new HashSet(disOps).iterator(); iter2.hasNext();) {						OWLDescription desc = (OWLDescription) iter2.next();						Object check = this.traceDescription(desc);						if (check!=null) {							changed = true;							disOps.remove(desc);							if (check instanceof OWLDescription) {								disOps.add((OWLDescription) check);							}						}											}					if (changed) {						this.removeChange(disAxiom.getDisjointClasses(), null, DISJOINTAXIOM);						this.addChange(disOps, null, DISJOINTAXIOM);					}				}							}						// check all data/object properties (annotation?)			Set properties = ontology.getDataProperties();			properties.addAll(ontology.getObjectProperties());			for (Iterator iter = properties.iterator(); iter.hasNext();) {								OWLProperty prop = (OWLProperty) iter.next();								// check all basic stuff that would get removed automatically				// by using RemoveEntity on property				if (prop.equals(remEntity) && replEntity!=null) {					// add domain of remEntity to replEntity					for (Iterator iter2 = prop.getDomains(ontology).iterator(); iter2.hasNext();) {						OWLDescription dom = (OWLDescription) iter2.next();						this.addChange(replEntity, dom, this.DOMAIN);					}										// add super-props, functional and annotations					for (Iterator iter2 = prop.getSuperProperties(ontology).iterator(); iter2.hasNext();) {						OWLProperty sup = (OWLProperty) iter2.next();						this.addChange(replEntity, sup, this.SUPERPROPERTY);					}					if (prop.isFunctional(ontology)) {						SetFunctional sf = new SetFunctional(ontology, (OWLProperty) replEntity, true, null);						changes.add(sf);												}					for (Iterator iter2 = prop.getAnnotations(ontology).iterator(); iter2.hasNext();) {						OWLAnnotationInstance oai = (OWLAnnotationInstance) iter2.next();						this.addChange(replEntity, oai, this.ANNOTATION);					}										if (prop instanceof OWLDataProperty) {						OWLDataProperty dp = (OWLDataProperty) prop;						// add data property range						for (Iterator iter2 = dp.getRanges(ontology).iterator(); iter2.hasNext();) {							OWLDataType dt = (OWLDataType) iter2.next();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?