negativediff.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 548 行 · 第 1/2 页
JAVA
548 行
package org.mindswap.swoop.utils.owlapi.diff;import java.util.Collections;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.Vector;import org.semanticweb.owl.model.OWLAnnotationInstance;import org.semanticweb.owl.model.OWLAnnotationProperty;import org.semanticweb.owl.model.OWLClass;import org.semanticweb.owl.model.OWLClassAxiom;import org.semanticweb.owl.model.OWLClassAxiomVisitor;import org.semanticweb.owl.model.OWLDataFactory;import org.semanticweb.owl.model.OWLDataProperty;import org.semanticweb.owl.model.OWLDataPropertyRangeAxiom;import org.semanticweb.owl.model.OWLDataRange;import org.semanticweb.owl.model.OWLDataType;import org.semanticweb.owl.model.OWLDataValue;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.OWLEntityVisitor;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.OWLFunctionalPropertyAxiom;import org.semanticweb.owl.model.OWLIndividual;import org.semanticweb.owl.model.OWLIndividualAxiom;import org.semanticweb.owl.model.OWLIndividualAxiomVisitor;import org.semanticweb.owl.model.OWLInverseFunctionalPropertyAxiom;import org.semanticweb.owl.model.OWLInversePropertyAxiom;import org.semanticweb.owl.model.OWLObject;import org.semanticweb.owl.model.OWLObjectProperty;import org.semanticweb.owl.model.OWLObjectPropertyRangeAxiom;import org.semanticweb.owl.model.OWLOntology;import org.semanticweb.owl.model.OWLProperty;import org.semanticweb.owl.model.OWLPropertyAxiom;import org.semanticweb.owl.model.OWLPropertyAxiomVisitor;import org.semanticweb.owl.model.OWLPropertyDomainAxiom;import org.semanticweb.owl.model.OWLSameIndividualsAxiom;import org.semanticweb.owl.model.OWLSubClassAxiom;import org.semanticweb.owl.model.OWLSubPropertyAxiom;import org.semanticweb.owl.model.OWLSymmetricPropertyAxiom;import org.semanticweb.owl.model.OWLTransitivePropertyAxiom;import org.semanticweb.owl.model.change.RemoveAnnotationInstance;import org.semanticweb.owl.model.change.RemoveClassAxiom;import org.semanticweb.owl.model.change.RemoveDataPropertyInstance;import org.semanticweb.owl.model.change.RemoveDataPropertyRange;import org.semanticweb.owl.model.change.RemoveDataType;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.RemoveIndividualAxiom;import org.semanticweb.owl.model.change.RemoveIndividualClass;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.SetDeprecated;import org.semanticweb.owl.model.change.SetFunctional;import org.semanticweb.owl.model.change.SetInverseFunctional;import org.semanticweb.owl.model.change.SetOneToOne;import org.semanticweb.owl.model.change.SetSymmetric;import org.semanticweb.owl.model.change.SetTransitive;public class NegativeDiff implements OWLEntityVisitor, OWLClassAxiomVisitor, OWLPropertyAxiomVisitor, OWLIndividualAxiomVisitor { /** * Get the differences as a set of OWLChanges, ready to apply to the * target. * @param source The source ontology * @param destination The destination ontology * @param target The OWLOntology the changes should be applied to. * @return * @throws OWLException */ public static List getChanges(OWLOntology source, OWLOntology destination, OWLOntology target) throws OWLException { NegativeDiff ndiff = new NegativeDiff(source, destination); return ndiff.getChanges(target); } private List changes; private OWLCopy copier; private OWLOntology destination; private OWLDataFactory factory; private OWLOntology source; private OWLOntology target; public NegativeDiff(OWLOntology source, OWLOntology destination) { this.source = source; this.destination = destination; changes = new Vector(); } /** * Remove any excess annotations an object might have. * @param src * @param dst * @param tgt * @throws OWLException */ protected void annotationRemover(OWLObject src, OWLObject dst, OWLObject tgt) throws OWLException { Set srcAnnotations = src.getAnnotations(source); Set dstAnnotations = dst.getAnnotations(destination); for (Iterator iter = srcAnnotations.iterator(); iter.hasNext();) { OWLAnnotationInstance annotation = (OWLAnnotationInstance) iter.next(); if (!dstAnnotations.contains(annotation)) { OWLAnnotationProperty tgtProp = (OWLAnnotationProperty) copier.copy(annotation.getProperty()); changes.add(new RemoveAnnotationInstance(target, tgt, tgtProp, annotation.getContent(), null)); } } } /** * Adds changes between OWLProperties (not Data or Object property specific changes) * @param src * @param dst * @param tgt * @throws OWLException */ private void genericPropertyChanges(OWLProperty entity, OWLProperty dstEntity, OWLProperty tgtEntity) throws OWLException { annotationRemover(entity, dstEntity, tgtEntity); if (entity.isDeprecated(source) && !dstEntity.isDeprecated(destination)) { changes.add(new SetDeprecated(target, tgtEntity, false, null)); } if (entity.isFunctional(source) && !dstEntity.isFunctional(destination)) { changes.add(new SetFunctional(target, tgtEntity, false, null)); } for (Iterator iter = entity.getDomains(source).iterator(); iter.hasNext(); ) { OWLDescription desc = (OWLDescription) iter.next(); if (!dstEntity.getDomains(destination).contains(desc)) { OWLDescription tgtDesc = copier.copy(desc); changes.add(new RemoveDomain(target, tgtEntity, tgtDesc, null)); } } for (Iterator iter = entity.getSuperProperties(source).iterator(); iter.hasNext();) { OWLProperty prop = (OWLProperty) iter.next(); if (!dstEntity.getSuperProperties(destination).contains(prop)) { OWLProperty tgtProp = (OWLProperty) copier.copy(prop); changes.add(new RemoveSuperProperty(target, tgtEntity, tgtProp, null)); } } } /** * Get the differences as a set of OWLChanges, ready to apply to the * target. * @param target The OWLOntology the changes should be applied to. * @return * @throws OWLException */ public List getChanges(OWLOntology target) throws OWLException { changes = new Vector(); this.target = target; factory = target.getOWLDataFactory(); copier = new OWLCopy(source, target); Set entities = new HashSet(); entities.addAll(source.getClasses()); entities.addAll(source.getIndividuals()); entities.addAll(source.getAnnotationProperties()); entities.addAll(source.getDataProperties()); entities.addAll(source.getObjectProperties()); for (Iterator iter = entities.iterator(); iter.hasNext();) { ((OWLEntity) iter.next()).accept(this); } for (Iterator iter = source.getClassAxioms().iterator(); iter.hasNext();) { ((OWLClassAxiom) iter.next()).accept(this); } for (Iterator iter = source.getPropertyAxioms().iterator(); iter.hasNext();) { ((OWLPropertyAxiom) iter.next()).accept(this); } for (Iterator iter = source.getIndividualAxioms().iterator(); iter.hasNext();) { ((OWLIndividualAxiom) iter.next()).accept(this); } // Data types don't have a specific visitor, don't want all of OWLObjectVisitor for (Iterator iter = source.getDatatypes().iterator(); iter.hasNext();) { OWLDataType srcDT = (OWLDataType) iter.next(); if (!destination.getDatatypes().contains(srcDT)) { OWLDataType tgtDT = factory.getOWLConcreteDataType(srcDT.getURI()); changes.add(new RemoveDataType(target, tgtDT, null)); } } return changes; } public void visit(OWLAnnotationProperty entity) throws OWLException { OWLAnnotationProperty tgtEntity = factory.getOWLAnnotationProperty(entity.getURI()); OWLAnnotationProperty dstEntity = destination.getAnnotationProperty(entity.getURI()); if (dstEntity == null) { changes.add(new RemoveEntity(target, tgtEntity, null)); dstEntity = destination.getOWLDataFactory().getOWLAnnotationProperty(entity.getURI()); } annotationRemover(entity, dstEntity, tgtEntity); } public void visit(OWLClass entity) throws OWLException { OWLClass tgtEntity = factory.getOWLClass(entity.getURI()); OWLClass dstEntity = destination.getClass(entity.getURI()); if (dstEntity == null) { changes.add(new RemoveEntity(target, tgtEntity, null)); dstEntity = destination.getOWLDataFactory().getOWLClass(entity.getURI()); } annotationRemover(entity, dstEntity, tgtEntity); for (Iterator iter = entity.getEnumerations(source).iterator(); iter.hasNext();) { OWLEnumeration srcEnum = (OWLEnumeration) iter.next(); if (!dstEntity.getEnumerations(destination).contains(srcEnum)) { changes.add(new RemoveEnumeration(target, tgtEntity, (OWLEnumeration) copier.copy(srcEnum), null)); } } for (Iterator iter = entity.getSuperClasses(source).iterator(); iter.hasNext();) { OWLDescription desc = (OWLDescription) iter.next(); if (!dstEntity.getSuperClasses(destination).contains(desc)) { changes.add(new RemoveSuperClass(target, tgtEntity, copier.copy(desc), null)); } } } public void visit(OWLDataProperty entity) throws OWLException { OWLDataProperty tgtEntity = factory.getOWLDataProperty(entity.getURI()); OWLDataProperty dstEntity = destination.getDataProperty(entity.getURI()); if (dstEntity == null) { changes.add(new RemoveEntity(target, tgtEntity, null)); dstEntity = destination.getOWLDataFactory().getOWLDataProperty(entity.getURI()); } genericPropertyChanges(entity, dstEntity, tgtEntity); for (Iterator iter = entity.getRanges(source).iterator(); iter.hasNext(); ) { OWLDataRange range = (OWLDataRange) iter.next(); if (!dstEntity.getRanges(destination).contains(range)) { OWLDataRange tgtRange = copier.copyDataRange(range); changes.add(new RemoveDataPropertyRange(target, tgtEntity, tgtRange, null)); } } } public void visit(OWLDataPropertyRangeAxiom axiom) throws OWLException { if (!destination.getPropertyAxioms().contains(axiom)) { OWLDataProperty tgtProperty = (OWLDataProperty) copier.copy(axiom.getProperty()); OWLDataRange tgtRange = copier.copyDataRange(axiom.getRange()); OWLPropertyAxiom tgtAxiom = factory.getOWLDataPropertyRangeAxiom(tgtProperty, tgtRange); changes.add(new RemovePropertyAxiom(target, tgtAxiom, null)); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?