swooprdfsreasoner.java

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

JAVA
1,175
字号
package org.mindswap.swoop.reasoner;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.mindswap.swoop.utils.ExpressivityChecker;import org.mindswap.swoop.utils.SetUtils;import org.mindswap.swoop.utils.owlapi.OntologyIndices;import org.mindswap.swoop.utils.owlapi.QNameShortFormProvider;import org.semanticweb.owl.io.ShortFormProvider;import org.semanticweb.owl.model.OWLAnd;import org.semanticweb.owl.model.OWLAnnotationProperty;import org.semanticweb.owl.model.OWLClass;import org.semanticweb.owl.model.OWLClassAxiom;import org.semanticweb.owl.model.OWLDataProperty;import org.semanticweb.owl.model.OWLDescription;import org.semanticweb.owl.model.OWLDisjointClassesAxiom;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.OWLObjectProperty;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.OWLSameIndividualsAxiom;import org.semanticweb.owl.model.OWLSubClassAxiom;import org.semanticweb.owl.model.OWLSubPropertyAxiom;import org.semanticweb.owl.model.helper.OntologyHelper;/** * @author Aditya Kalyanpur */public class SwoopRDFSReasoner implements SwoopReasoner {		public static boolean DEBUG = false; //	 for debugging purposes only	boolean loadImports = true;	private Set ontologies;	private OWLOntology ontology;	private List classes, properties, individuals;	private List classAxioms, propAxioms, indAxioms;	private ShortFormProvider shortForms = new QNameShortFormProvider();	private Map incLinks, outLinks, equivalents, complements, disjoints;	private Map classInds, sameAs, differentFrom;	private Map inverses;	private OntologyIndices indices;		public void setOntology(OWLOntology ont) throws OWLException {		ontology = ont;				if(loadImports)			ontologies = OntologyHelper.importClosure(ontology);		else			ontologies = Collections.singleton(ontology);				classify();		buildIndices();	}		/**	 * Check if a subclass relationship between two OWL descriptions subClass(subC, supC)	 * needs to be added to the incoming/outgoing links hashmaps. Check	 * 1. If both subC, supC are OWL Classes	 * 2. If subC is a OWL Class and supC is an OWLAnd	 * 3. If subC is an OWLOr and supC is a OWL Class	 * @param subC	 * @param supC	 * @throws OWLException	 */	public void subClassCheck(OWLDescription subC, OWLDescription supC) throws OWLException {			if (subC instanceof OWLClass) {			if (supC instanceof OWLClass) this.addSubSuperClass((OWLClass) subC, (OWLClass) supC);			else				// if A subCof (B AND C) -> (A subCOf B) and (A subCOf C) 				if (supC instanceof OWLAnd) {				for (Iterator iter = ((OWLAnd) supC).getOperands().iterator(); iter.hasNext(); ) {					OWLDescription desc = (OWLDescription) iter.next();					if (desc instanceof OWLClass) addSubSuperClass((OWLClass) subC, (OWLClass) desc);				}			}		}		else 		if (subC instanceof OWLOr && supC instanceof OWLClass) {			// if (A OR B) subCOf C -> (A subCOf C) and (B subCOf C)			for (Iterator iter = ((OWLOr) subC).getOperands().iterator(); iter.hasNext(); ) {				OWLDescription desc = (OWLDescription) iter.next();				if (desc instanceof OWLClass) addSubSuperClass((OWLClass) desc, (OWLClass) supC);			}		}	}		/**	 * Compute subclass, equivalence, complement and disjoint relations 	 * for all classes in ontology and add it to corresponding HashMaps	 * 1. check direct assertions (sub/equ)	 * 2. check class axioms (GCIS) (sub/equ/dis)	 * @throws OWLException	 */	private void computeClassRelations() throws OWLException {				// handle class assertions		// superClass and equivalentClass		for(int i = 0; i < classes.size(); i++) {			OWLClass c = (OWLClass) classes.get(i);			Iterator supers = c.getSuperClasses(ontologies).iterator();			while(supers.hasNext()) {				OWLDescription sup = (OWLDescription) supers.next();				this.subClassCheck(c, sup);											}						Iterator equs = c.getEquivalentClasses(ontologies).iterator();			while(equs.hasNext()) {				OWLDescription equ = (OWLDescription) equs.next();				this.addEquivalentClass(c, equ);						}					}				// handle class axioms		// OWLSubClass, OWLEquivalentClasses and OWLDisjointClasses		for (int i=0; i<classAxioms.size(); i++) {			OWLClassAxiom axiom = (OWLClassAxiom) classAxioms.get(i);			if (axiom instanceof OWLSubClassAxiom) {				OWLSubClassAxiom subAxiom = (OWLSubClassAxiom) axiom;				this.subClassCheck(subAxiom.getSubClass(), subAxiom.getSuperClass());											}			else if (axiom instanceof OWLEquivalentClassesAxiom) {				OWLEquivalentClassesAxiom equAxiom = (OWLEquivalentClassesAxiom) axiom;				Set equs = equAxiom.getEquivalentClasses();				for (Iterator iter = equs.iterator(); iter.hasNext(); ) {					OWLDescription desc = (OWLDescription) iter.next();					if (desc instanceof OWLClass) {						Set copyEqus = new HashSet(equs);						copyEqus.remove(desc);						for (Iterator iter2 = copyEqus.iterator(); iter2.hasNext(); ) {							OWLDescription desc2 = (OWLDescription) iter2.next();							this.addEquivalentClass((OWLClass) desc, desc2);						}					}				}			}			else if (axiom instanceof OWLDisjointClassesAxiom) {				OWLDisjointClassesAxiom disAxiom = (OWLDisjointClassesAxiom) axiom;				Set dis = disAxiom.getDisjointClasses();				for (Iterator iter = dis.iterator(); iter.hasNext(); ) {					OWLDescription desc = (OWLDescription) iter.next();					if (desc instanceof OWLClass) {						Set copyDis = new HashSet(dis);						copyDis.remove(desc);						for (Iterator iter2 = copyDis.iterator(); iter2.hasNext(); ) {							OWLDescription desc2 = (OWLDescription) iter2.next();							if (desc2 instanceof OWLClass) {								// add disjoint between desc and desc2 when both are classes								this.addDisjointClass((OWLClass) desc, (OWLClass) desc2);							}						}					}				}			}		}			}		/*	 * Add complement assertion between two classes to the corresponding hashmap	 */	private void addComplementClass(OWLClass c1, OWLClass c2) {		Set compSet1 = new HashSet(); // complements of c1		Set compSet2 = new HashSet(); // complements of c2		if (complements.containsKey(c1)) compSet1.addAll((HashSet) complements.get(c1));		if (complements.containsKey(c2)) compSet2.addAll((HashSet) complements.get(c2));		complements.put(c1, compSet1);		complements.put(c2, compSet2);	}		/*	 * Add disjoint assertion between two classes to the corresponding hashmap	 */	private void addDisjointClass(OWLClass c1, OWLClass c2) {		Set disSet1 = new HashSet(); // disjoints of c1		Set disSet2 = new HashSet(); // disjoints of c2		if (disjoints.containsKey(c1)) disSet1.addAll((HashSet) disjoints.get(c1));		if (disjoints.containsKey(c2)) disSet2.addAll((HashSet) disjoints.get(c2));		disjoints.put(c1, disSet1);		disjoints.put(c2, disSet2);	}		/**	 * Add equivalent class relation to the hashmap - equivalents.	 * Also perform special check for: A = (B and C) -> A subCOf B..	 * @param cla - class	 * @param desc - equivalent class description	 * @throws OWLException	 */	private void addEquivalentClass(OWLClass cla, OWLDescription desc) throws OWLException {				Set equMap = new HashSet();			equMap.add(cla); // add class itself to its equivalents map				// get existing map, if any		if (equivalents.containsKey(cla)) equMap.addAll((HashSet) equivalents.get(cla));				// add only class to map (no descriptions)		if (desc instanceof OWLClass) {			equMap.add(desc);			// get equivalents of desc as well			if (equivalents.containsKey(desc)) equMap.addAll((HashSet) equivalents.get(desc));		}		else if (desc instanceof OWLAnd) {			// if A = (B AND C); A subCOf B, A subCOf C			for (Iterator iter = ((OWLAnd) desc).getOperands().iterator(); iter.hasNext(); ) {				OWLDescription equ = (OWLDescription) iter.next();				if (equ instanceof OWLClass) {					this.addSubSuperClass(cla, (OWLClass) equ);				}				else if (equ instanceof OWLAnd) {					// check for nested intersections!					this.addEquivalentClass(cla, equ);				}			}					}		else if (desc instanceof OWLNot) {			// if complement, put in separate hashmap			OWLNot not = (OWLNot) desc;			if (not.getOperand() instanceof OWLClass)				this.addComplementClass(cla, (OWLClass) not.getOperand());		}				// put the equivalents for each class in equMap		for (Iterator iter = equMap.iterator(); iter.hasNext();) {			OWLClass equCla = (OWLClass) iter.next();			equivalents.put(equCla, equMap);		}	}		private void addSubSuperProperty(OWLProperty subProp, OWLProperty superProp) {				// get existing superset if any		Set superSet = new HashSet();		if (outLinks.containsKey(subProp)) superSet = (HashSet) outLinks.get(subProp);		superSet.add(superProp);		outLinks.put(subProp, superSet);				// get existing subset if any		Set subSet = new HashSet();		if (incLinks.containsKey(superProp)) subSet = (HashSet) incLinks.get(superProp);		subSet.add(subProp);		incLinks.put(superProp, subSet);			}		private void addEquivalentProperty(OWLProperty prop1, OWLProperty prop2) throws OWLException {		if (prop1.equals(prop2)) return;				Set equSet = new HashSet();		equSet.add(prop1); // add property itself to equivalent set				if (equivalents.containsKey(prop1)) equSet.addAll((HashSet) equivalents.get(prop1));		equSet.add(prop2);				// put same equivalents set for each property in hashmap		for (Iterator iter = equSet.iterator(); iter.hasNext();) {			OWLProperty prop = (OWLProperty) iter.next();			equivalents.put(prop, equSet);		}	}		/**	 * Add sub-super class relationship links to the two hashmaps :- 	 * incoming links (incLinks) and outgoing links (outLinks). 	 * 	 * @param subClass - subclass description	 * @param superClass - super class description	 * @throws OWLException	 */	private void addSubSuperClass(OWLClass subClass, OWLClass superClass) throws OWLException {				// get existing superset if any		Set superSet = new HashSet();		if (outLinks.containsKey(subClass)) superSet = (HashSet) outLinks.get(subClass);				// get existing subset if any		Set subSet = new HashSet();		if (incLinks.containsKey(superClass)) subSet = (HashSet) incLinks.get(superClass);				superSet.add(superClass);		subSet.add(subClass);				// add to outgoing and incoming links maps respectively		outLinks.put(subClass, superSet);		incLinks.put(superClass, subSet);	}		private void init() throws OWLException {				Set classSet = new HashSet();		Set propSet = new HashSet();		individuals = new ArrayList();				classAxioms = new ArrayList();		propAxioms = new ArrayList();		indAxioms = new ArrayList();				incLinks = new HashMap();		outLinks = new HashMap();		equivalents = new HashMap();		complements = new HashMap();		disjoints = new HashMap();				classInds = new HashMap();		sameAs = new HashMap();		differentFrom = new HashMap();				inverses = new HashMap();				Iterator ont = ontologies.iterator();		while(ont.hasNext()) {			OWLOntology o = (OWLOntology) ont.next();			classSet.addAll(o.getClasses());			propSet.addAll(o.getObjectProperties());			propSet.addAll(o.getDataProperties());			propSet.addAll(o.getAnnotationProperties());			individuals.addAll(o.getIndividuals());			classAxioms.addAll(o.getClassAxioms());			propAxioms.addAll(o.getPropertyAxioms());			indAxioms.addAll(o.getIndividualAxioms());		}		classes = new ArrayList(classSet.size() + 1);		properties = new ArrayList(propSet);				OWLClass thing = ontology.getOWLDataFactory().getOWLThing();		OWLClass nothing = ontology.getOWLDataFactory().getOWLNothing();		// we want owl:Thing and owl:Nothing to be always at the beginning		classes.add(thing);		classes.add(nothing);		// remove multiple copies if exists		classSet.remove(thing);		classSet.remove(nothing);				classes.addAll(classSet);			}		/**	 * Compute subproperty and equivalence relations for all properties 	 * in ontology and add it to corresponding HashMaps	 * 1. check direct assertions (sub/equ)	 * 2. check property axioms (GCIS) (sub/equ)	 * @throws OWLException	 */	private void computePropertyRelations() throws OWLException {			for(int i = 0; i < properties.size(); i++) {			OWLProperty p = (OWLProperty) properties.get(i);						// handle super properties			Iterator supers = p.getSuperProperties(ontologies).iterator();			while(supers.hasNext()) {				OWLProperty sup = (OWLProperty) supers.next();				this.addSubSuperProperty(p, sup);															}						// handle inverse properties			if (p instanceof OWLObjectProperty) {				for (Iterator invIter = ((OWLObjectProperty) p).getInverses(ontologies).iterator(); invIter.hasNext();) {					OWLObjectProperty invP = (OWLObjectProperty) invIter.next();					// add inverses to set of each property					this.addInverse((OWLObjectProperty) p, invP);					this.addInverse(invP, (OWLObjectProperty) p);				}			}		}				// handle explicit axioms		for (int i=0; i<propAxioms.size(); i++) {			OWLPropertyAxiom axiom = (OWLPropertyAxiom) propAxioms.get(i);			if (axiom instanceof OWLSubPropertyAxiom) {				OWLSubPropertyAxiom subAxiom = (OWLSubPropertyAxiom) axiom;				this.addSubSuperProperty(subAxiom.getSubProperty(), subAxiom.getSuperProperty());			}			else if (axiom instanceof OWLEquivalentPropertiesAxiom) {				OWLEquivalentPropertiesAxiom equAxiom = (OWLEquivalentPropertiesAxiom) axiom;				Set equs = equAxiom.getProperties();				for (Iterator iter = equs.iterator(); iter.hasNext(); ) {

⌨️ 快捷键说明

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