swooptoldreasoner.java

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

JAVA
990
字号
				OWLProperty sup = (OWLProperty) supers.next();				this.addSubSuperProperty(p, sup);															}					}				// 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(); ) {					OWLProperty prop = (OWLProperty) iter.next();					Set copyEqus = new HashSet(equs);					copyEqus.remove(prop);					for (Iterator iter2 = copyEqus.iterator(); iter2.hasNext(); ) {						OWLProperty prop2 = (OWLProperty) iter2.next();						this.addEquivalentProperty(prop, prop2);					}				}			}		}		}		/**	 * Compute top-level classes in the ontology and owl:Thing as their superclass	 * @throws OWLException	 */	private void computeTopClasses() throws OWLException {		OWLClass thing = ontology.getOWLDataFactory().getOWLThing();		OWLClass nothing = ontology.getOWLDataFactory().getOWLNothing();		// get set of classes that have atleast one outgoing (subClassOf) link		Set subclasses = outLinks.keySet();				Set topClasses = new HashSet(classes);		// remove subclasses from total set of classes to get top level classes		topClasses.removeAll(subclasses);		topClasses.remove(thing);		topClasses.remove(nothing);				// add owl:Thing as an outgoing link for topClasses alone		for (Iterator iter = topClasses.iterator(); iter.hasNext();) {			OWLClass cla = (OWLClass) iter.next();						// also check if equivalents of topClasses have outgoing links			boolean isTop = true;			Set equ = this.equivalentClassesOf(cla);			for (Iterator iter2 = equ.iterator(); iter2.hasNext();) {				OWLClass equCla = (OWLClass) iter2.next();				// check if outgoing links don't come back to equivalent class (cycle)				if (outLinks.containsKey(equCla)) {					Set out = new HashSet((HashSet) outLinks.get(equCla));					out.add(equCla);					isTop = SetUtils.subset(equ, out);									}			}			if (isTop) this.addSubSuperClass(cla,thing);		}			}		private void removeSubSuperClass(OWLClass cl, OWLClass supCl) throws OWLException {		Set equ = this.equivalentClassesOf(cl);		equ.add(cl);		Set equ2 = this.equivalentClassesOf(supCl);		equ2.add(supCl);		// remove supCl (and all its equivalents) from outLinks		// for key cl (and all its equivalents)		for (Iterator iter = equ.iterator(); iter.hasNext();) {			OWLClass cla = (OWLClass) iter.next();			if (outLinks.containsKey(cla)) {				((HashSet) outLinks.get(cla)).removeAll(equ2);				if (((HashSet) outLinks.get(cla)).size()==0) outLinks.remove(cla);			}		}		// remove cl (and all its equivalents) from incLinks		// for key supCl (and all its equivalents)		for (Iterator iter = equ2.iterator(); iter.hasNext();) {			OWLClass cla = (OWLClass) iter.next();			if (incLinks.containsKey(cla)) {				((HashSet) incLinks.get(cla)).removeAll(equ);				if (((HashSet) incLinks.get(cla)).size()==0) incLinks.remove(cla);			}		}			}		private void removeSubSuperProperty(OWLProperty p, OWLProperty supP) throws OWLException {		Set equ = this.equivalentPropertiesOf(p);		equ.add(p);		Set equ2 = this.equivalentPropertiesOf(supP);		equ2.add(supP);		// remove supP (and all its equivalents) from outLinks		// for key p (and all its equivalents)		for (Iterator iter = equ.iterator(); iter.hasNext();) {			OWLProperty prop = (OWLProperty) iter.next();			if (outLinks.containsKey(prop)) {				((HashSet) outLinks.get(prop)).removeAll(equ2);			}		}		// remove p (and all its equivalents) from incLinks		// for key supP (and all its equivalents)		for (Iterator iter = equ2.iterator(); iter.hasNext();) {			OWLProperty prop = (OWLProperty) iter.next();			if (incLinks.containsKey(prop)) {				((HashSet) incLinks.get(prop)).removeAll(equ);			}		}			}		/*	 * Check for multiple paths in the class/property hierarchy and	 * eliminate redundant path(s). This prevents the class/property node	 * from occuring twice in the class/property tree along the same hierarchical chain  	 */	private void computeMultiplePaths() throws OWLException {		// check multiple subClass hierarchies		// and keep only the lowest hanging class node		// eg. A->B->C and A->C => remove A->C		for (Iterator iter = classes.iterator(); iter.hasNext();) {			// for each class in the ontology			OWLClass cla = (OWLClass) iter.next();			// get set of equivalent superclass sets			Set setOfSets = this.superClassesOf(cla);						for (Iterator iter2=setOfSets.iterator(); iter2.hasNext();) {				// for each superClass set (1)				Set supSet = (HashSet) iter2.next();								// take all *other* superclasses (2)				Set others = new HashSet(setOfSets);				others.remove(supSet);				others = SetUtils.union(others);								// and check for subClass relationship for every class pair (1, 2)				for (Iterator iter3 = supSet.iterator(); iter3.hasNext();) {										Object owlObj = iter3.next();					if (!(owlObj instanceof OWLClass)) continue;					OWLClass supCla = (OWLClass) owlObj;										// check for loops A subCOf B, B subCOf A					if (outLinks.get(supCla)!=null) {						if (((HashSet) outLinks.get(supCla)).contains(cla)) {							this.removeSubSuperClass(cla, supCla);							this.removeSubSuperClass(supCla, cla);							this.addEquivalentClass(cla, supCla);							continue;						}					}										// check for A subCOf B and A=B					if (equivalents.get(cla)!=null) {						if (((HashSet) equivalents.get(cla)).contains(supCla)) {							this.removeSubSuperClass(cla, supCla);							continue;						}					}										// and check for alternate paths					for (Iterator iter4 = others.iterator(); iter4.hasNext();) {						OWLClass remCla = (OWLClass) iter4.next();						if (this.isSubClassOf(supCla, remCla)) {							this.removeSubSuperClass(cla, remCla); 						}										}				}			}		}				// do the same for the property hierarchy		for (Iterator iter = properties.iterator(); iter.hasNext();) {			// for each property in the ontology			OWLProperty prop = (OWLProperty) iter.next();			// get set of equivalent superproperty sets			Set setOfSets = this.superPropertiesOf(prop);						for (Iterator iter2=setOfSets.iterator(); iter2.hasNext();) {				// for each superProperty set (1)				Set supSet = (HashSet) iter2.next();								// take all *other* superproperty sets (2)				Set others = new HashSet(setOfSets);				others.remove(supSet);				others = SetUtils.union(others);								// and check for subProperty relationship for every property pair (1, 2)				for (Iterator iter3 = supSet.iterator(); iter3.hasNext();) {										Object owlObj = iter3.next();					if (!(owlObj instanceof OWLProperty)) continue;					OWLProperty supProp = (OWLProperty) owlObj;										// check for loops A subPOf B, B subPOf A					if (outLinks.get(supProp)!=null) {						if (((HashSet) outLinks.get(supProp)).contains(prop)) {							this.removeSubSuperProperty(prop, supProp);							this.removeSubSuperProperty(supProp, prop);							this.addEquivalentProperty(prop, supProp);						}					}										for (Iterator iter4 = others.iterator(); iter4.hasNext();) {						OWLProperty remProp = (OWLProperty) iter4.next();						if (this.isSubPropertyOf(supProp, remProp)) {							this.removeSubSuperProperty(prop, remProp); 						}										}				}			}		}	}		private void classify() {		try {			init();			this.computeClassRelations();			this.computeMultiplePaths();			this.computeTopClasses();			this.computePropertyRelations();			this.computeIndividualRelations();					}		catch (OWLException e) {			e.printStackTrace();		}	}		public Set getSameAsIndividuals(OWLIndividual ind) {		// TODO Auto-generated method stub		if (sameAs.containsKey(ind)) return (HashSet) sameAs.get(ind);		else return new HashSet();	}	public Set getDifferentFromIndividuals(OWLIndividual ind) {				if (differentFrom.containsKey(ind)) return (HashSet) differentFrom.get(ind);		else return new HashSet();	}		/**	 * Check for assertions/relations involving all individuals in the ontology:	 *  1. instance types (assertions)	 *  2. sameAs or differentFrom (axioms)	 * 	 */	private void computeIndividualRelations() throws OWLException {						// check types of individuals		for (Iterator iter = individuals.iterator(); iter.hasNext();) {			OWLIndividual ind = (OWLIndividual) iter.next();			Set types = ind.getTypes(ontologies);			for (Iterator iter2 = types.iterator(); iter2.hasNext();) {				OWLDescription desc = (OWLDescription) iter2.next();				Set indSet = new HashSet();				if (classInds.containsKey(desc)) indSet = (HashSet) classInds.get(desc);				indSet.add(ind);				classInds.put(desc, indSet);			}		}				// iterate through each ontology		Iterator ont = ontologies.iterator();		// check axioms for sameAs and differentFrom assertions b/w individuals		while(ont.hasNext()) {			OWLOntology o = (OWLOntology) ont.next();			// get individual axioms for each ontology			for (Iterator iter = o.getIndividualAxioms().iterator(); iter.hasNext(); ){				OWLIndividualAxiom indAxiom = (OWLIndividualAxiom) iter.next();				// get the set of individuals participating in each axiom				Set inds = indAxiom.getIndividuals();				Map map = null;								if (indAxiom instanceof OWLSameIndividualsAxiom) map = sameAs;				else map = differentFrom;								// add it to the corresponding map				for (Iterator iter2 = inds.iterator(); iter2.hasNext(); ) {					Set copyInds = new HashSet(inds); // create copy of set					OWLIndividual ind = (OWLIndividual) iter2.next();					copyInds.remove(ind);					if (map.get(ind)==null) {						// put new set						map.put(ind, copyInds);					}					else {						// add to existing set						Set current = (HashSet) map.get(ind);						current.addAll(copyInds);						map.put(ind, current);					}				}							}		}	}	public String getName() {		return "No Reasoner";	}	/**	 * refreshOntology	 * 	 * 	 */	private void refreshOntology() throws OWLException {		if(ontology != null) setOntology(ontology);	}		public void setLoadImports(boolean useImports, boolean refresh) throws OWLException {		this.loadImports = useImports;		if (refresh) this.refreshOntology();	}	public boolean loadImports() {		return this.loadImports;	}	public boolean isConsistent() {		return true;	}	public boolean isConsistent(OWLClass c) throws OWLException {		return true;	}	public Set typesOf(OWLIndividual ind) throws OWLException {		Set types = ind.getTypes(ontologies);		// remove unnamed types		for (Iterator iter = new HashSet(types).iterator(); iter.hasNext();) {

⌨️ 快捷键说明

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