econniterativepartitioning.java

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

JAVA
1,353
字号
	public void init(OWLOntology source, OWLOntology target, Map linksToSource, Map boundedInverses) throws OWLException {		if (DEBUG) System.out.println("Starting initialization.."+swoopModel.getTimeStamp());				// init owl:Thing		owlThing = source.getOWLDataFactory().getOWLClass( URI.create( OWLVocabularyAdapter.INSTANCE.getThing()));				// build indices using SwoopUtils.OntologyIndices class		timers8.start();		indexedLibrary.computeIndices(source);		timers8.stop();		OntologyIndices indices = indexedLibrary.getIndices(source);		Expressivity = indices.getExpressivity(source);				// map external indices to local file data structures		this.descriptionsLeft = new HashSet();		this.descriptionsLeft.addAll(source.getClasses());		for (Iterator iter = indices.descriptions.iterator(); iter.hasNext();) {			this.descriptionsLeft.add((OWLDescription) iter.next());		}		this.Restrictions = indices.restrictionToEntity.keySet();		this.classesAndPropertiesRestriction = indices.classesToPropertiesRestriction;		this.classesInRestriction = indices.classesToPropertiesRestriction.keySet();		this.individualsAndPropertiesRestriction = indices.individualsToPropertiesRestriction;		this.individualsAndPropertiesAssertions = indices.individualsToPropertiesAssertions;		this.individualsInRestriction = indices.individualsToPropertiesRestriction.keySet();		this.individualsInEnumerations = indices.individualsInEnumerations;		this.propToIndividualAssertions = indices.propToIndividualAssertions;		this.propToDescriptionRestrictions = indices.propToDescriptionRestrictions;		this.nestedRestrictions = indices.nestedRestrictions;		this.classesWithDomain = indices.classesToDomain;		this.classesWithRange = indices.classesToRange;		this.equivalents = indices.equivalents;		this.subClasses = indices.subClasses;		this.superClasses = indices.superClasses;		this.subProperties = indices.subProperties;		this.superProperties = indices.superProperties;		this.instancesOf = indices.instancesOf;		this.ClassesInIntersections = indices.classesInIntersections;		this.ClassesInUnions = indices.classesInUnions;		this.Unions = indices.unions;		this.Intersections = indices.intersections;		this.disjointAxioms = indices.disjointAxioms;				if(DEBUG) {			System.out.println("Initialization Complete. "+swoopModel.getTimeStamp());			System.out.println("Stored " + Restrictions.size() + " Restrictions");			System.out.println("Stored " + Unions.size() + " Unions");			System.out.println("Stored " + Intersections.size() + " Intersections");			System.out.println("Stored " + indices.descriptions.size() + " Descriptions");			System.out.println("There are "+  classesWithDomain.keySet().size()+ " classes which are domain of some property" );			System.out.println("There are "+  classesWithRange.keySet().size()+ " classes which are range of some property" );			System.out.println("There are " + linksToSource.keySet().size() + " link properties pointing at the source ontology");		}		System.out.println("Stored " + indices.descriptions.size() + " Descriptions");			} 	public StringWriter getTrace(){		return out;	}		public void RunStateMachine(OWLClass initial) throws OWLException {		int threshold = 3;				if (DEBUG) System.out.println("Starting state machine with Class "+getName(initial)+".."+swoopModel.getTimeStamp());		out.write("Moving INITIALLY class "+getName(initial));		// move the state of the initial class to O2 and then run state machine		this.makeStateChange(initial, STATE_O2, this.DescriptionsO1, this.DescriptionsO2);				//Optimization: We know that the ancestors and descendants of the		//initially moved class must move as well		timers9.start();		Set related2 = new HashSet();		if(DEBUG)System.out.println("Start computing descendants");		related2.addAll(SetUtils.union(reasoner.descendantClassesOf(initial)));		related2.addAll(SetUtils.union(reasoner.ancestorClassesOf(initial)));		related2.remove(owlThing);		if(DEBUG)System.out.println("Stop computing descendants");		for (Iterator iter2 = related2.iterator(); iter2.hasNext();) {			OWLDescription relDesc = (OWLDescription) iter2.next();			if (checkState(relDesc, STATE_O1)) {				if (DEBUG) System.out.println("Moving class "+getName(relDesc) + " because related (ANCESTOR/DESCENDANT) class "+getName(initial)+" is in O2");				makeStateChange(relDesc, STATE_O2, DescriptionsO1, DescriptionsO2);			}							}		timers9.stop();				// iteratively apply state changes for all OWL Objects		// till no more changes can be applied		boolean changed;		do {			timers.start();			changed = false;			boolean cont = false;			boolean copyChanged = false;						// handle state changes for classes/descriptions in O1						timers2.start();		    Set copy = new HashSet(DescriptionsO1);			timers2.stop();			// create a copy and iterate over copy because original set may change inside loop			for (Iterator iter = copy.iterator(); iter.hasNext(); ) {													OWLDescription desc = (OWLDescription) iter.next();												// skip owl:Thing				if (desc.equals(owlThing)) continue;					//				*** if any related (sub/super/equivalent) description is in O2				timers4.start();				Set related = new HashSet();				if (this.subClasses.get(desc)!=null) related.addAll((HashSet) this.subClasses.get(desc));				if (this.superClasses.get(desc)!=null) related.addAll((HashSet) this.superClasses.get(desc));				if (this.equivalents.get(desc)!=null) related.addAll((HashSet) this.equivalents.get(desc));				Set copyRelated = new HashSet(related);				for (Iterator iter2 = related.iterator(); iter2.hasNext();) {					OWLDescription relDesc = (OWLDescription) iter2.next();					if (checkState(relDesc, STATE_O2)) {						if (DEBUG) System.out.println("Moving class "+getName(desc) + " because related (EQU/SUB/SUP) class "+getName(relDesc)+" is in O2");						//out.println("Moving class "+getName(desc) + " because related (EQU/SUB/SUP) class "+getName(relDesc)+" is in O2");						changed = makeStateChange(desc, STATE_O2, DescriptionsO1, DescriptionsO2);						break;											}									}			    timers4.stop();				   				//*** if description instanceof OWLAnd				if (!changed && desc instanceof OWLAnd) {					OWLAnd and = (OWLAnd) desc;					Set operands = and.getOperands();					for (Iterator iter2 = operands.iterator(); iter2.hasNext();) {						OWLDescription andDesc = (OWLDescription) iter2.next();						if (checkState(andDesc, STATE_O2)) {							if (DEBUG) System.out.println("Moving class "+getName(desc) + " because its an INTERSECTION and component "+getName(andDesc)+" is in O2");							out.write("Moving class "+getName(desc) + " because its an INTERSECTION and component "+getName(andDesc)+" is in O2 <br>");							changed = makeStateChange(desc, STATE_O2, DescriptionsO1, DescriptionsO2);							break;												}					}				}								//*** if description instanceof OWLOr				if (!changed && desc instanceof OWLOr) {					OWLOr or = (OWLOr) desc;					for (Iterator iter2 = or.getOperands().iterator(); iter2.hasNext();) {						OWLDescription orDesc = (OWLDescription) iter2.next();						if (checkState(orDesc, STATE_O2)) {							if (DEBUG) System.out.println("Moving class "+getName(desc) + " because its a UNION and component "+getName(orDesc)+" is in O2");							out.write("Moving class "+getName(desc) + " because its a UNION and component "+getName(orDesc)+" is in O2 <br>");							changed = makeStateChange(desc, STATE_O2, DescriptionsO1, DescriptionsO2);							break;												}					}				}								//*** if description instanceof OWLRestriction				//*** and restriction involves P, then..				if (!changed && desc instanceof OWLRestriction) {					OWLProperty prop = ((OWLRestriction) desc).getProperty();					if (this.checkPropertyCausingTransitionA(prop)) {						if (DEBUG) System.out.println("Moving class "+getName(desc) + " because its a restriction on property "+getName(prop)+" and P has moved ");						out.write("Moving class "+getName(desc) + " because its a restriction on property "+getName(prop)+" and P has moved <br>");						changed = makeStateChange(desc, STATE_O2, DescriptionsO1, DescriptionsO2);						//Optimization: If the number of restrictions on prop is greater than the						//threshold, then we move those as well and exit the big loop						if(propToDescriptionRestrictions.containsKey(prop)){						if(((Set)propToDescriptionRestrictions.get(prop)).size()>threshold){							for(Iterator i = ((Set)propToDescriptionRestrictions.get(prop)).iterator(); i.hasNext(); ){							  OWLRestriction res = (OWLRestriction)i.next();							   if(checkState(res,STATE_O1)){							  	  changed = makeStateChange(res, STATE_O2, DescriptionsO1, DescriptionsO2);							  	  cont = true;							 	 }							}						}						}						break;					}				}								if(cont) continue;								//*** if C is Domain of P, then..				if (!changed && this.classesWithDomain.get(desc)!=null) {					for (Iterator iter2 = ((HashSet) classesWithDomain.get(desc)).iterator(); iter2.hasNext(); ) {						OWLProperty prop = (OWLProperty) iter2.next();						if (this.checkPropertyCausingTransitionA(prop)) {							if (DEBUG) System.out.println("Moving class "+getName(desc) + " because its DOMAIN of property "+getName(prop)+" and P has moved ");							out.write("Moving class "+getName(desc) + " because its DOMAIN of property "+getName(prop)+" and P has moved <br>");							changed = makeStateChange(desc, STATE_O2, DescriptionsO1, DescriptionsO2);							//Optimization: If prop has many other domains, move all of them, since all the domains							//of a property must end in the same partition							if(reasoner.domainsOf(prop).size() > threshold){								for(Iterator i = reasoner.domainsOf(prop).iterator(); i.hasNext(); ){									OWLDescription d = (OWLDescription)i.next();									 if(checkState(d,STATE_O1)){									  	  changed = makeStateChange(d, STATE_O2, DescriptionsO1, DescriptionsO2);									  	  if(DEBUG) System.out.println("OPTIMIZATION: Moved Class" + getName(d) + " because it is a domain of" + getName(prop) + "and other of its domains has moved");									  	  cont = true;								 	 }								}							}							break;						}					}				}								if(cont) continue;								//*** if C is range of P, then..				if (!changed && this.classesWithRange.get(desc)!=null) {					for (Iterator iter2 = ((HashSet) classesWithRange.get(desc)).iterator(); iter2.hasNext(); ) {						OWLProperty prop = (OWLProperty) iter2.next();						if (this.checkPropertyCausingTransitionB(prop)) {							if (DEBUG) System.out.println("Moving class "+getName(desc) + " because its RANGE of property "+getName(prop)+" and P has moved ");							out.write("Moving class "+getName(desc) + " because its RANGE of property "+getName(prop)+" and P has moved <br>");							changed = makeStateChange(desc, STATE_O2, DescriptionsO1, DescriptionsO2);//							Optimization: If prop has many other ranges, move all of them, since all the domains							//of a property must end in the same partition							if(reasoner.rangesOf(prop).size() > threshold){								for(Iterator i = reasoner.domainsOf(prop).iterator(); i.hasNext(); ){									OWLDescription d = (OWLDescription)i.next();									 if(checkState(d,STATE_O1)){									  	  changed = makeStateChange(d, STATE_O2, DescriptionsO1, DescriptionsO2);									  	  if(DEBUG) System.out.println("OPTIMIZATION: Moved Class" + getName(d) + " because it is a range of" + getName(prop) + "and other of its domains has moved");									  	  cont = true;								 	 }								}							}							break;						}					}				}				if(cont) continue;										// *** check if C(a) and a in O2				if (!changed && instancesOf.get(desc)!=null) {					for (Iterator iter2 = ((HashSet) instancesOf.get(desc)).iterator(); iter2.hasNext();) {						OWLIndividual ind = (OWLIndividual) iter2.next();						if (checkState(ind, STATE_O2)) {							if (DEBUG) System.out.println("Moving class "+getName(desc) + " because its TYPE of instance "+getName(ind)+" and I has moved ");							out.write("Moving class "+getName(desc) + " because its TYPE of instance "+getName(ind)+" and I has moved <br>");							changed = makeStateChange(desc, STATE_O2, DescriptionsO1, DescriptionsO2);							break;						}					}				}								//*** if C is in a restriction involving prop P, then..				if (!changed && this.classesAndPropertiesRestriction.get(desc)!=null) {					for (Iterator iter2 = ((HashSet) classesAndPropertiesRestriction.get(desc)).iterator(); iter2.hasNext(); ) {						OWLProperty prop = (OWLProperty) iter2.next();						if (!isLinkProperty(prop) && prop instanceof OWLObjectProperty && (checkState(prop, STATE_L12) || checkState(prop, STATE_O2))) {							if (DEBUG) System.out.println("Moving class "+getName(desc) + " because its IN A RESTRICTION involving property "+getName(prop)+" and P is in S2/S4 ");							out.write("Moving class "+getName(desc) + " because its IN A RESTRICTION involving property "+getName(prop)+" and P is in S2/S4 <br>");							changed = makeStateChange(desc, STATE_O2, DescriptionsO1, DescriptionsO2);							//Optimization: if appears in restrictions involving many other classes, move those as well							if(((Set)propToDescriptionRestrictions.get(prop)).size()>threshold){								for(Iterator i = ((Set)propToDescriptionRestrictions.get(prop)).iterator(); i.hasNext(); ){								  OWLRestriction res = (OWLRestriction)i.next();								  if(res instanceof OWLObjectQuantifiedRestriction){								  	 OWLDescription d = ((OWLObjectQuantifiedRestriction)res).getDescription();								 	 if(checkState(d,STATE_O2)){								  	  changed = makeStateChange(d, STATE_O2, DescriptionsO1, DescriptionsO2);								  	  if(DEBUG) System.out.println("I have changed here 2!");

⌨️ 快捷键说明

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