sparqlGraphLibrary/src/main/java/com/ge/research/semtk/ontologyTools/OntologyInfo.java
public String findLowestCommonSuperclass(String subClassName1, String subClassName2) {
		HashMap<String, Integer> hops1 = new HashMap<String, Integer>();
		HashMap<String, Integer> hops2 = new HashMap<String, Integer>();
		boolean newHops1;
		boolean newHops2;
		hops1.put(subClassName1, 0);
		hops2.put(subClassName2, 0);
		
		if (subClassName1.equals(subClassName2)) {
			return subClassName1;
		}
		Integer hops = 	1;
		while (true) {
			newHops1 = false;
			newHops2 = false;
			
			// get classes previous iteration (i.e. hops-1)
			HashSet<String> lastRound1 = new HashSet<String>();
			for (String c : hops1.keySet()) {
				if (hops1.get(c) == hops - 1) {
					lastRound1.add(c);
				}
			}
			// get superclasses for that last round
			for (String c : lastRound1) {
				for (String s : this.getDirectSuperclasses(c)) {
					newHops1 = true;
					hops1.put(s, hops);
				}
			}
			// get classes previous iteration (i.e. hops-1)
			HashSet<String> lastRound2 = new HashSet<String>();
			for (String c : hops2.keySet()) {
				if (hops2.get(c) == hops - 1) {
					lastRound2.add(c);
				}
			}
			// get superclasses for that last round
			for (String c : lastRound2) {
				for (String s : this.getDirectSuperclasses(c)) {
					newHops2 = true;
					hops2.put(s, hops);
				}
			}
			if (!newHops1 && !newHops2) {
				return null;

			} else {
				// are any of hops1 also in hops2
				HashMap<String, Integer> match = new HashMap<String, Integer>();
				for (String c : hops1.keySet()) {
					if (hops2.containsKey(c)) {
						Integer combinedHops = hops1.get(c) + hops2.get(c);
						if (! match.containsKey(c) || match.get(c) > combinedHops) {
							// add to match if new or shorter hops
							match.put(c, combinedHops);
						}
					}
				}
				// are any of hops2 also in hops1
				for (String c : hops2.keySet()) {
					if (hops1.containsKey(c)) {
						Integer combinedHops = hops1.get(c) + hops2.get(c);
						if (! match.containsKey(c) || match.get(c) > combinedHops) {
							// add to match if new or shorter hops
							match.put(c, combinedHops);
						}
					}
				}
				// found matches: find the lowest hop one and return
				if (match.size() > 0) {
					String ret = null;
					Integer retHops = 0;
					for (String matchClass : match.keySet()) {
						if (ret == null || match.get(matchClass) < retHops) {
							ret = matchClass;
							retHops = match.get(matchClass);
						}
					}
					return ret;
				}
			}
			hops += 1;
		}
	}
/**
	 * Find superclass that is the least number of hops combined upward from the two classes.
	 * Ties are broken randomly.
	 * @param subClassName1
	 * @param subClassName2
	 * @return class name or null
	 */
Find superclass that is the least number of hops combined upward from the two classes.
Ties are broken randomly.
@param subClassName1
@param subClassName2
@return class name or null
Find superclass that is the least number of hops combined upward from the two classes . Ties are broken randomly . @param subClassName1 @param subClassName2 @return class name or null
public String findLowestCommonSuperclass(String subClassName1, String subClassName2) {
		HashMap<String, Integer> hops1 = new HashMap<String, Integer>();
		HashMap<String, Integer> hops2 = new HashMap<String, Integer>();
		boolean newHops1;
		boolean newHops2;
		hops1.put(subClassName1, 0);
		hops2.put(subClassName2, 0);
		if (subClassName1.equals(subClassName2)) {
			return subClassName1;
		}
		Integer hops = 	1;
		while (true) {
			newHops1 = false;
			newHops2 = false;
			HashSet<String> lastRound1 = new HashSet<String>();
			for (String c : hops1.keySet()) {
				if (hops1.get(c) == hops - 1) {
					lastRound1.add(c);
				}
			}
			for (String c : lastRound1) {
				for (String s : this.getDirectSuperclasses(c)) {
					newHops1 = true;
					hops1.put(s, hops);
				}
			}
			HashSet<String> lastRound2 = new HashSet<String>();
			for (String c : hops2.keySet()) {
				if (hops2.get(c) == hops - 1) {
					lastRound2.add(c);
				}
			}
			for (String c : lastRound2) {
				for (String s : this.getDirectSuperclasses(c)) {
					newHops2 = true;
					hops2.put(s, hops);
				}
			}
			if (!newHops1 && !newHops2) {
				return null;
			} else {
				HashMap<String, Integer> match = new HashMap<String, Integer>();
				for (String c : hops1.keySet()) {
					if (hops2.containsKey(c)) {
						Integer combinedHops = hops1.get(c) + hops2.get(c);
						if (! match.containsKey(c) || match.get(c) > combinedHops) {
							match.put(c, combinedHops);
						}
					}
				}
				for (String c : hops2.keySet()) {
					if (hops1.containsKey(c)) {
						Integer combinedHops = hops1.get(c) + hops2.get(c);
						if (! match.containsKey(c) || match.get(c) > combinedHops) {
							match.put(c, combinedHops);
						}
					}
				}
				if (match.size() > 0) {
					String ret = null;
					Integer retHops = 0;
					for (String matchClass : match.keySet()) {
						if (ret == null || match.get(matchClass) < retHops) {
							ret = matchClass;
							retHops = match.get(matchClass);
						}
					}
					return ret;
				}
			}
			hops += 1;
		}
	}
public String findLowestCommonSuperclass ( String subClassName1 , String subClassName2 ) { HashMap < String , Integer > hops1 = new HashMap < String , Integer > ( ) ; HashMap < String , Integer > hops2 = new HashMap < String , Integer > ( ) ; boolean newHops1 ; boolean newHops2 ; hops1 . put ( subClassName1 , 0 ) ; hops2 . put ( subClassName2 , 0 ) ; if ( subClassName1 . equals ( subClassName2 ) ) { return subClassName1 ; } Integer hops = 1 ; while ( true ) { newHops1 = false ; newHops2 = false ; HashSet < String > lastRound1 = new HashSet < String > ( ) ; for ( String c : hops1 . keySet ( ) ) { if ( hops1 . get ( c ) == hops - 1 ) { lastRound1 . add ( c ) ; } } for ( String c : lastRound1 ) { for ( String s : this . getDirectSuperclasses ( c ) ) { newHops1 = true ; hops1 . put ( s , hops ) ; } } HashSet < String > lastRound2 = new HashSet < String > ( ) ; for ( String c : hops2 . keySet ( ) ) { if ( hops2 . get ( c ) == hops - 1 ) { lastRound2 . add ( c ) ; } } for ( String c : lastRound2 ) { for ( String s : this . getDirectSuperclasses ( c ) ) { newHops2 = true ; hops2 . put ( s , hops ) ; } } if ( ! newHops1 && ! newHops2 ) { return null ; } else { HashMap < String , Integer > match = new HashMap < String , Integer > ( ) ; for ( String c : hops1 . keySet ( ) ) { if ( hops2 . containsKey ( c ) ) { Integer combinedHops = hops1 . get ( c ) + hops2 . get ( c ) ; if ( ! match . containsKey ( c ) || match . get ( c ) > combinedHops ) { match . put ( c , combinedHops ) ; } } } for ( String c : hops2 . keySet ( ) ) { if ( hops1 . containsKey ( c ) ) { Integer combinedHops = hops1 . get ( c ) + hops2 . get ( c ) ; if ( ! match . containsKey ( c ) || match . get ( c ) > combinedHops ) { match . put ( c , combinedHops ) ; } } } if ( match . size ( ) > 0 ) { String ret = null ; Integer retHops = 0 ; for ( String matchClass : match . keySet ( ) ) { if ( ret == null || match . get ( matchClass ) < retHops ) { ret = matchClass ; retHops = match . get ( matchClass ) ; } } return ret ; } } hops += 1 ; } }
Find superclass that is the least number of hops combined upward from the two classes.
Find superclass that is the least number of hops combined upward from the two classes .
// get classes previous iteration (i.e. hops-1) // get superclasses for that last round // get classes previous iteration (i.e. hops-1) // get superclasses for that last round // are any of hops1 also in hops2 // add to match if new or shorter hops // are any of hops2 also in hops1 // add to match if new or shorter hops // found matches: find the lowest hop one and return