swooprdfsreasoner.java

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

JAVA
1,175
字号
					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);					}				}			}		}		}		private void addInverse(OWLObjectProperty p1, OWLObjectProperty p2) {		Set invSet = new HashSet();		if (this.inverses.get(p1)!=null) invSet.addAll((HashSet) inverses.get(p1));		invSet.add(p2);		this.inverses.put(p1, invSet);	}		/**	 * 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);			equ.add(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;						}					}										// finally check for alternate paths					for (Iterator iter4 = others.iterator(); iter4.hasNext();) 					{						OWLClass remCla = (OWLClass) iter4.next();						if (this.isSubClassOf(supCla, remCla)) 						{							// check to see if cla and remCla are on a circle (if true, don't remove it)														Set sub1 = this.descendantClassesOf( cla );							Set sub2 = this.descendantClassesOf( remCla );							sub1.retainAll( sub2 );							if ( sub1.size() > 0 )								continue;							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 superClass 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) {		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		OWLClass thing = ontology.getOWLDataFactory().getOWLThing();		// add owl:Thing as a default type of every individual		Set allindSet = new HashSet();		allindSet.addAll(individuals);		classInds.put(thing, allindSet);				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 "RDFS-like";	}	/**	 * 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();) {			OWLDescription desc = (OWLDescription) iter.next();			if (!(desc instanceof OWLClass)) types.remove(desc);		}		return this.getSetofEquivalentSets(types);	}	public String getExpressivity() throws OWLException {		if (indices!=null) {			String expressivity = indices.getExpressivity(ontology);			return ExpressivityChecker.getExplanation(expressivity);		}		return "Unable to determine";	}	public Set allTypesOf(OWLIndividual ind) throws OWLException {		Set types = ind.getTypes(ontologies);		Set copy = new HashSet(types);		for (Iterator iter = copy.iterator(); iter.hasNext();) {			OWLClass cla = (OWLClass) iter.next();			List classTracker = new ArrayList();			Set resultSet = this.getClassHierarchy(cla, classTracker, "SUPER");			types.addAll(resultSet);		}		// return set of sets		return this.getSetofEquivalentSets(types);	}	public Set disjointClassesOf(OWLClass c) throws OWLException {		if (disjoints.containsKey(c)) 			return this.getSetofEquivalentSets((HashSet) disjoints.get(c));		else return new HashSet();	}	public Set complementClassesOf(OWLClass c) throws OWLException {		if (complements.containsKey(c)) 			return this.getSetofEquivalentSets((HashSet) complements.get(c));		else return new HashSet();	}	public Set getOntologies() {		return this.ontologies;	}	public Set getClasses() {		return new HashSet(classes);	}	public Set getProperties() {		return new HashSet(properties);	}	public Set getObjectProperties() {		Set set = new HashSet();		for(int i = 0; i < properties.size(); i++) 			if(properties.get(i) instanceof OWLObjectProperty)				set.add(properties.get(i));

⌨️ 快捷键说明

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