package owltools.graph;

import java.util.HashSet;
import java.util.Set;

import org.semanticweb.owlapi.model.OWLObject;

/**
 * general static methods for additional graph operations
 *
 */
public class OWLGraphUtil {
	
	/**
	 * @param g
	 * @param x
	 * @param y
	 * @return set of edges
	 */
	public static Set<OWLGraphEdge> findCommonSubsumersAsEdges(OWLGraphWrapper g,
			OWLObject x, OWLObject y) {
		Set<OWLGraphEdge> xs = g.getOutgoingEdgesClosure(x);
		Set<OWLGraphEdge> ys = g.getOutgoingEdgesClosure(y);
		xs.retainAll(ys);
		return xs;
	}
	
	public static Set<OWLGraphEdge> findLeastCommonSubsumersAsEdges(OWLGraphWrapper g,
			OWLObject x, OWLObject y) {
		Set<OWLGraphEdge> cses = findCommonSubsumersAsEdges(g,x,y);
		Set<OWLGraphEdge> lcses = new HashSet<OWLGraphEdge>();
		lcses.addAll(cses);
		for (OWLGraphEdge cse: cses) {
			Set<OWLObject> zs = g.getAncestors(cse.getSource());
			for (OWLObject z : zs) {
				for (OWLGraphEdge lcse: lcses) {
					if (lcse.getSource().equals(z))
						lcses.remove(lcse);
				}
			}
		}
		return lcses;
	}
	
	public static Set<OWLObject> findCommonAncestors(OWLGraphWrapper g,
				OWLObject x, OWLObject y) {
		Set<OWLObject> xs = g.getAncestors(x);
		Set<OWLObject> ys = g.getAncestors(y);
		xs.retainAll(ys);
		return xs;
	}
	public static Set<OWLObject> findLeastCommonAncestors(OWLGraphWrapper g,
			OWLObject x, OWLObject y) {
		Set<OWLObject> cas = findCommonAncestors(g,x,y);
		Set<OWLObject> lcas = new HashSet<OWLObject>();
		lcas.addAll(cas);
		for (OWLObject z : cas) {
			lcas.removeAll(g.getAncestors(z));
		}
		return lcas;
	}

}

@@ -113,16 +113,75 @@
 LObject;
+%0Aimport org.semanticweb.owlapi.model.OWLPropertyExpression;
 %0A%0A/**%0A *
@@ -1209,32 +1209,616 @@
 WLObject y) %7B%0A%09%09
+return findCommonAncestors(g, x, y, null);%0A%09%7D%0A%09public static Set%3COWLObject%3E findLeastCommonAncestors(OWLGraphWrapper g,%0A%09%09%09OWLObject x, OWLObject y) %7B%0A%09%09return findLeastCommonAncestors(g, x, y, null);%0A%09%7D%0A%0A%09/**%0A%09 * Find common ancestors to %7B@code x%7D and %7B@code y%7D that can be reached %0A%09 * over the specified set of relations. %0A%09 * %0A%09 * @param g  %0A%09 * @param x            %0A%09 * @param y%0A%09 * @param overProps%0A%09 * @return%0A%09 */%0A    public static Set%3COWLObject%3E findCommonAncestors(OWLGraphWrapper g,%0A                OWLObject x, OWLObject y, Set%3COWLPropertyExpression%3E overProps) %7B%0A        
 Set%3COWLObject%3E x
@@ -1837,21 +1837,38 @@
 estors(x
-);%0A%09%09
+, overProps);%0A        
 Set%3COWLO
@@ -1895,21 +1895,38 @@
 estors(y
-);%0A%09%09
+, overProps);%0A        
 xs.retai
@@ -1927,34 +1927,40 @@
 .retainAll(ys);%0A
-%09%09
+        
 return xs;%0A%09%7D%0A%09p
@@ -1954,20 +1954,256 @@
 urn xs;%0A
-%09%7D%0A%09
+    %7D%0A    /**%0A     * Find lest common ancestors to %7B@code x%7D and %7B@code y%7D that can be reached %0A     * over the specified set of relations. %0A     * @param g%0A     * @param x%0A     * @param y%0A     * @param overProps%0A     * @return%0A     */%0A    
 public s
@@ -2259,35 +2259,44 @@
 GraphWrapper g,%0A
-%09%09%09
+            
 OWLObject x, OWL
@@ -2299,30 +2299,74 @@
  OWLObject y
-) %7B%0A%09%09
+, Set%3COWLPropertyExpression%3E overProps) %7B%0A        
 Set%3COWLObjec
@@ -2395,29 +2395,46 @@
 estors(g,x,y
-);%0A%09%09
+, overProps);%0A        
 Set%3COWLObjec
@@ -2469,18 +2469,24 @@
 ect%3E();%0A
-%09%09
+        
 lcas.add
@@ -2491,26 +2491,32 @@
 ddAll(cas);%0A
-%09%09
+        
 for (OWLObje
@@ -2529,19 +2529,28 @@
  cas) %7B%0A
-%09%09%09
+            
 lcas.rem
@@ -2576,18 +2576,41 @@
 rs(z
-));%0A%09%09%7D%0A%09%09
+, overProps));%0A        %7D%0A        
 retu
@@ -2618,14 +2618,17 @@
 n lcas;%0A
-%09
+    
 %7D%0A%0A%7D%0A

add method to find common ancestors over properties