From 801c35985205f382a73ea4a5c097b5c804aeef96 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Euzenat?= <Jerome.Euzenat@inria.fr>
Date: Wed, 25 Jun 2008 14:23:27 +0000
Subject: [PATCH] - Modified Cell interface so that object URIs can be found in
 a generic way         -> solves a database storage bug

---
 .../exmo/align/impl/OWLAPIAlignment.java      | 10 +++---
 .../exmo/align/impl/ObjectAlignment.java      |  6 ++--
 .../align/impl/eval/PRGraphEvaluator.java     | 18 +++++-----
 .../exmo/align/impl/eval/PRecEvaluator.java   | 25 +++++++++++++
 .../impl/renderer/HTMLRendererVisitor.java    | 16 ++++-----
 .../impl/renderer/SKOSRendererVisitor.java    |  4 +--
 .../impl/renderer/XSLTRendererVisitor.java    |  9 ++---
 .../align/service/AServProtocolManager.java   | 35 ++++++++++++++++++
 .../exmo/align/service/CacheImpl.java         | 36 +++++++++++++++----
 .../exmo/align/service/QueryMediator.java     |  5 +--
 src/org/semanticweb/owl/align/Cell.java       | 10 ++++++
 11 files changed, 134 insertions(+), 40 deletions(-)

diff --git a/src/fr/inrialpes/exmo/align/impl/OWLAPIAlignment.java b/src/fr/inrialpes/exmo/align/impl/OWLAPIAlignment.java
index 928df006..cb92ed2a 100644
--- a/src/fr/inrialpes/exmo/align/impl/OWLAPIAlignment.java
+++ b/src/fr/inrialpes/exmo/align/impl/OWLAPIAlignment.java
@@ -223,7 +223,7 @@ public class OWLAPIAlignment extends ObjectAlignment {
 	for (Enumeration e = getElements(); e.hasMoreElements();) {
 	    OWLAPICell c = (OWLAPICell)e.nextElement(); //[W:Deprecated]
 	    try {
-		align.addAlignCell( c.getId(), c.getObject1AsURI(), c.getObject2AsURI(), c.getRelation(), c.getStrength() );
+		align.addAlignCell( c.getId(), c.getObject1AsURI(this), c.getObject2AsURI(this), c.getRelation(), c.getStrength() );
 	    } catch (AlignmentException aex) {
 		// Sometimes URIs are null, this is ignored
 	    }
@@ -245,11 +245,11 @@ public class OWLAPIAlignment extends ObjectAlignment {
 	//System.err.println( o1 );
 	for (Enumeration e = al.getElements(); e.hasMoreElements();) {
 	    Cell c = (Cell)e.nextElement();
-	    //System.err.println( c.getObject1AsURI() );
-	    //System.err.println( c.getObject2AsURI() );
+	    //System.err.println( c.getObject1AsURI(this) );
+	    //System.err.println( c.getObject2AsURI(this) );
 	    alignment.addAlignCell( c.getId(), 
-				    getEntity( o1, c.getObject1AsURI() ),
-				    getEntity( o2, c.getObject2AsURI() ),
+				    getEntity( o1, c.getObject1AsURI(al) ),
+				    getEntity( o2, c.getObject2AsURI(al) ),
 				    c.getRelation(), 
 				    c.getStrength(),
 				    c.getExtensions() );
diff --git a/src/fr/inrialpes/exmo/align/impl/ObjectAlignment.java b/src/fr/inrialpes/exmo/align/impl/ObjectAlignment.java
index cd0ef1d3..1af66e28 100644
--- a/src/fr/inrialpes/exmo/align/impl/ObjectAlignment.java
+++ b/src/fr/inrialpes/exmo/align/impl/ObjectAlignment.java
@@ -150,7 +150,7 @@ public class ObjectAlignment extends BasicAlignment {
 	for (Enumeration e = getElements(); e.hasMoreElements();) {
 	    Cell c = (Cell)e.nextElement();
 	    try {
-		align.addAlignCell( c.getId(), c.getObject1AsURI(), c.getObject2AsURI(), c.getRelation(), c.getStrength() );
+		align.addAlignCell( c.getId(), c.getObject1AsURI(this), c.getObject2AsURI(this), c.getRelation(), c.getStrength() );
 	    } catch (AlignmentException aex) {
 		// Sometimes URIs are null, this is ignore
 	    }
@@ -171,8 +171,8 @@ public class ObjectAlignment extends BasicAlignment {
 	for (Enumeration e = al.getElements(); e.hasMoreElements();) {
 	    Cell c = (Cell)e.nextElement();
 	    alignment.addAlignCell( c.getId(), 
-				    o1.getEntity( c.getObject1AsURI() ),
-				    o2.getEntity( c.getObject2AsURI() ),
+				    o1.getEntity( c.getObject1AsURI(alignment) ),
+				    o2.getEntity( c.getObject2AsURI(alignment) ),
 				    c.getRelation(), 
 				    c.getStrength(),
 				    c.getExtensions() );
diff --git a/src/fr/inrialpes/exmo/align/impl/eval/PRGraphEvaluator.java b/src/fr/inrialpes/exmo/align/impl/eval/PRGraphEvaluator.java
index 1e7fc6e9..7ff3e81f 100644
--- a/src/fr/inrialpes/exmo/align/impl/eval/PRGraphEvaluator.java
+++ b/src/fr/inrialpes/exmo/align/impl/eval/PRGraphEvaluator.java
@@ -116,17 +116,17 @@ public class PRGraphEvaluator extends BasicEvaluator {
 					} else if ( ((Cell)o1).getStrength() < ((Cell)o2).getStrength() ){
 					    return 1;
 					//The comparator must always tell that things are different!
-					} else if ( (((Cell)o1).getObject1AsURI().getFragment() == null)
-						    || (((Cell)o2).getObject1AsURI().getFragment() == null) ) {
+					} else if ( (((Cell)o1).getObject1AsURI(align1).getFragment() == null)
+						    || (((Cell)o2).getObject1AsURI(align2).getFragment() == null) ) {
 					    return -1;
-					} else if ( ((Cell)o1).getObject1AsURI().getFragment().compareTo(((Cell)o2).getObject1AsURI().getFragment()) > 0) {
+					} else if ( ((Cell)o1).getObject1AsURI(align1).getFragment().compareTo(((Cell)o2).getObject1AsURI(align2).getFragment()) > 0) {
 					    return -1;
-					} else if ( ((Cell)o1).getObject1AsURI().getFragment().compareTo(((Cell)o2).getObject1AsURI().getFragment()) < 0 ) {
+					} else if ( ((Cell)o1).getObject1AsURI(align1).getFragment().compareTo(((Cell)o2).getObject1AsURI(align2).getFragment()) < 0 ) {
 					    return 1;
-					} else if ( (((Cell)o1).getObject2AsURI().getFragment() == null)
-						    || (((Cell)o2).getObject2AsURI().getFragment() == null) ) {
+					} else if ( (((Cell)o1).getObject2AsURI(align1).getFragment() == null)
+						    || (((Cell)o2).getObject2AsURI(align2).getFragment() == null) ) {
 					    return -1;
-					} else if ( ((Cell)o1).getObject2AsURI().getFragment().compareTo(((Cell)o2).getObject2AsURI().getFragment()) > 0) {
+					} else if ( ((Cell)o1).getObject2AsURI(align1).getFragment().compareTo(((Cell)o2).getObject2AsURI(align2).getFragment()) > 0) {
 					    return -1;
 					// We assume that they have different names
 					} else { return 1; }
@@ -151,8 +151,8 @@ public class PRGraphEvaluator extends BasicEvaluator {
 	  if( s1 != null ){ // for all cells matching our first entity
 	      for( Iterator it1 = s1.iterator(); it1.hasNext() && c2 != null; ){
 		  Cell c1 = (Cell)it1.next();
-		  URI uri1 = c1.getObject2AsURI();
-		  URI uri2 = c2.getObject2AsURI();	
+		  URI uri1 = c1.getObject2AsURI(align1);
+		  URI uri2 = c2.getObject2AsURI(align2);	
 		  if (uri1.toString().equals(uri2.toString())) { //This cell matches a correct one
 		      nbcorrect++;
 		      double recall = (double)nbcorrect / (double)nbexpected;
diff --git a/src/fr/inrialpes/exmo/align/impl/eval/PRecEvaluator.java b/src/fr/inrialpes/exmo/align/impl/eval/PRecEvaluator.java
index dd236a66..706b07b2 100644
--- a/src/fr/inrialpes/exmo/align/impl/eval/PRecEvaluator.java
+++ b/src/fr/inrialpes/exmo/align/impl/eval/PRecEvaluator.java
@@ -140,6 +140,31 @@ public class PRecEvaluator extends BasicEvaluator {
 	return (result);
     }
 
+    public String HTMLString (){
+	String result = "";
+	result += "  <div  xmlns:map='http://www.atl.external.lmco.com/projects/ontology/ResultsOntology.n3#' typeof=\"map:output\" href=''>";
+	result += "    <dl>";
+	//if ( ) {
+	//    result += "    <dt>algorithm</dt><dd property=\"map:algorithm\">"+align1.get+"</dd>";
+	//}
+	try {
+	    result += "    <dt>input1</dt><dd rel=\"map:input1\" href=\""+align1.getOntology1URI()+"\">"+align1.getOntology1URI()+"</dd>";
+	    result += "    <dt>input2</dt><dd rel=\"map:input2\" href=\""+align1.getOntology2URI()+"\">"+align1.getOntology2URI()+"</dd>";
+	} catch (AlignmentException e) { e.printStackTrace(); };
+	// Other missing items (easy to get)
+	// result += "    <map:falseNegative>");
+	// result += "    <map:falsePositive>");
+	result += "    <dt>precision</dt><dd property=\"map:precision\">"+precision+"</dd>\n";
+	result += "    <dt>recall</dt><dd property=\"map:recall\">"+recall+"</dd>\n";
+	result += "    <dt>fallout</dt><dd property=\"map:fallout\">"+fallout+"</dd>\n";
+	result += "    <dt>F-measure</dt><dd property=\"map:fMeasure\">"+fmeasure+"</dd>\n";
+	result += "    <dt>O-measure</dt><dd property=\"map:oMeasure\">"+overall+"</dd>\n";
+	if ( time != 0 ) result += "    <dt>time</dt><dd property=\"map:time\">"+time+"</dd>\n";
+    	result += "    <dt>result</dt><dd property=\"map:result\">"+result+"</dd>\n";
+	result += "  </dl>\n  </div>\n";
+return result;
+    }
+
     /**
      * This now output the results in Lockheed format.
      */
diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/HTMLRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/HTMLRendererVisitor.java
index 7e50be42..4c8d2bf5 100644
--- a/src/fr/inrialpes/exmo/align/impl/renderer/HTMLRendererVisitor.java
+++ b/src/fr/inrialpes/exmo/align/impl/renderer/HTMLRendererVisitor.java
@@ -130,14 +130,14 @@ public class HTMLRendererVisitor implements AlignmentVisitor
     public void visit( Cell cell ) throws AlignmentException {
 	this.cell = cell;
 	URI u1, u2;
-	if ( cell instanceof ObjectCell ) {
-	    u1 = ((LoadedOntology)((BasicAlignment)alignment).getOntologyObject1()).getEntityURI( cell.getObject1() );
-	    u2 = ((LoadedOntology)((BasicAlignment)alignment).getOntologyObject2()).getEntityURI( cell.getObject2() );
-	} else {
-	    System.err.println( cell );
-	    u1 = cell.getObject1AsURI();
-	    u2 = cell.getObject2AsURI();
-	}
+	// JE: I think that now these two clauses should be unified (3.4)
+	//if ( cell instanceof ObjectCell ) {
+	//    u1 = ((LoadedOntology)((BasicAlignment)alignment).getOntologyObject1()).getEntityURI( cell.getObject1() );
+	//    u2 = ((LoadedOntology)((BasicAlignment)alignment).getOntologyObject2()).getEntityURI( cell.getObject2() );
+	//} else {
+	    u1 = cell.getObject1AsURI( alignment );
+	    u2 = cell.getObject2AsURI( alignment );
+	    //}
 	writer.print(" <tr typeof=\"align:Cell\">");
 	writer.print("<td rel=\"align:entity1\" href=\""+u1+"\">"+u1+"</td><td property=\"align:relation\">");
 	cell.getRelation().accept( this );
diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/SKOSRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/SKOSRendererVisitor.java
index 691ba69c..0079d490 100644
--- a/src/fr/inrialpes/exmo/align/impl/renderer/SKOSRendererVisitor.java
+++ b/src/fr/inrialpes/exmo/align/impl/renderer/SKOSRendererVisitor.java
@@ -76,7 +76,7 @@ public class SKOSRendererVisitor implements AlignmentVisitor {
 	if ( onto2 != null ) {
 	    return onto2.getEntityURI( cell.getObject2() );
 	} else {
-	    return cell.getObject2AsURI();
+	    return cell.getObject2AsURI( alignment );
 	}
     }
 
@@ -85,7 +85,7 @@ public class SKOSRendererVisitor implements AlignmentVisitor {
 	if ( onto1 != null ) {
 	    writer.print("  <skos:Concept rdf:about=\""+onto1.getEntityURI( cell.getObject1() )+"\">\n");
 	} else {
-	    writer.print("  <skos:Concept rdf:about=\""+cell.getObject1AsURI()+"\">\n");
+	    writer.print("  <skos:Concept rdf:about=\""+cell.getObject1AsURI( alignment )+"\">\n");
 	}
 	cell.getRelation().accept( this );
 	writer.print("  </skos:Concept>\n\n");
diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/XSLTRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/XSLTRendererVisitor.java
index 56b500bc..49c00b26 100644
--- a/src/fr/inrialpes/exmo/align/impl/renderer/XSLTRendererVisitor.java
+++ b/src/fr/inrialpes/exmo/align/impl/renderer/XSLTRendererVisitor.java
@@ -107,12 +107,13 @@ public class XSLTRendererVisitor implements AlignmentVisitor {
 
     private void collectURIs ( Cell cell ) throws AlignmentException {
 	URI entity1URI, entity2URI;
+	// JE: I think that now these two clauses should be unified (3.4)
 	if ( onto1 != null ){
 	    entity1URI = onto1.getEntityURI( cell.getObject1() );
 	    entity2URI = onto2.getEntityURI( cell.getObject2() );
 	} else {
-	    entity1URI = cell.getObject1AsURI();
-	    entity2URI = cell.getObject2AsURI();
+	    entity1URI = cell.getObject1AsURI(alignment);
+	    entity2URI = cell.getObject2AsURI(alignment);
 	}
 	if ( entity1URI != null ) {
 	    String ns1 = entity1URI.getScheme()+":"+entity1URI.getSchemeSpecificPart()+"#";
@@ -134,8 +135,8 @@ public class XSLTRendererVisitor implements AlignmentVisitor {
 	    writer.println("  <xsl:template match=\""+namespacify(onto1.getEntityURI( cell.getObject1() ))+"\">");
 	    writer.println("    <xsl:element name=\""+namespacify(onto2.getEntityURI( cell.getObject2() ))+"\">");
 	} else {
-	    writer.println("  <xsl:template match=\""+namespacify(cell.getObject1AsURI())+"\">");
-	    writer.println("    <xsl:element name=\""+namespacify(cell.getObject2AsURI())+"\">");
+	    writer.println("  <xsl:template match=\""+namespacify(cell.getObject1AsURI(alignment))+"\">");
+	    writer.println("    <xsl:element name=\""+namespacify(cell.getObject2AsURI(alignment))+"\">");
 	}
 	writer.println("      <xsl:apply-templates select=\"*|@*|text()\"/>");
 	writer.println("    </xsl:element>");
diff --git a/src/fr/inrialpes/exmo/align/service/AServProtocolManager.java b/src/fr/inrialpes/exmo/align/service/AServProtocolManager.java
index 7036a887..c0b24288 100644
--- a/src/fr/inrialpes/exmo/align/service/AServProtocolManager.java
+++ b/src/fr/inrialpes/exmo/align/service/AServProtocolManager.java
@@ -450,6 +450,7 @@ public class AServProtocolManager {
 	} catch (Exception e) {
 	    return new UnknownAlignment(newId(),mess,myId,mess.getSender(),"unknown/Alignment/"+id,(Parameters)null);
 	}
+System.err.println( id +" -- "+al);
 	// Invert it
 	try { al = al.inverse(); }
 	catch (AlignmentException e) {
@@ -474,6 +475,40 @@ public class AServProtocolManager {
 	return new AlignmentId(newId(),mess,myId,mess.getSender(),"dummy//",(Parameters)null);
     }
 
+    // It is also possible to try a groupeval ~> with a zipfile containing results
+    //            ~~> But it is more difficut to know where is the reference (non public)
+    // There should also be options for selecting the result display
+    //            ~~> PRGraph (but this may be a Evaluator)
+    //            ~~> Triangle
+    //            ~~> Cross
+    public Message eval( Message mess ){
+	Parameters params = mess.getParameters();
+	// Retrieve the alignment
+	String id = (String)params.getParameter("id");
+	Alignment al = null;
+	try {
+	    al = alignmentCache.getAlignment( id );
+	} catch (Exception e) {
+	    return new UnknownAlignment(newId(),mess,myId,mess.getSender(),"unknown/Alignment/"+id,(Parameters)null);
+	}
+	// Retrieve the reference alignment
+	String rid = (String)params.getParameter("ref");
+	Alignment ref = null;
+	try {
+	    ref = alignmentCache.getAlignment( rid );
+	} catch (Exception e) {
+	    return new UnknownAlignment(newId(),mess,myId,mess.getSender(),"unknown/Alignment/"+rid,(Parameters)null);
+	}
+	// Set the comparison method
+	// Compare it
+	try { al = al.inverse(); }
+	catch (AlignmentException e) {
+	    return new ErrorMsg(newId(),mess,myId,mess.getSender(),"dummy//",(Parameters)null);
+	}
+	// Return it, not easy
+	return new AlignmentId(newId(),mess,myId,mess.getSender(),"dummy//",(Parameters)null);
+    }
+
     public boolean storedAlignment( Message mess ) {
 	// Retrieve the alignment
 	String id = (String)mess.getParameters().getParameter("id");
diff --git a/src/fr/inrialpes/exmo/align/service/CacheImpl.java b/src/fr/inrialpes/exmo/align/service/CacheImpl.java
index 1fad9412..ce3af168 100644
--- a/src/fr/inrialpes/exmo/align/service/CacheImpl.java
+++ b/src/fr/inrialpes/exmo/align/service/CacheImpl.java
@@ -37,6 +37,7 @@ import java.sql.ResultSet;
 import java.sql.Statement;
 import java.sql.SQLException;
 
+import fr.inrialpes.exmo.align.impl.BasicAlignment;
 import fr.inrialpes.exmo.align.impl.BasicRelation;
 import fr.inrialpes.exmo.align.impl.Annotations;
 import fr.inrialpes.exmo.align.impl.BasicParameters;
@@ -439,8 +440,25 @@ public class CacheImpl {
     public boolean isAlignmentStored( Alignment alignment ) {
 	if ( alignment.getExtension( SVCNS, STORED ) != null &&
 	     !alignment.getExtension( SVCNS, STORED ).equals("") )
-	    return false;
-	else return true;
+	    return true;
+	else return false;
+    }
+
+    /**
+     * Non publicised class
+     */
+    public void unStoreAlignment( String id ) throws Exception {
+	Alignment alignment = getAlignment( id );
+	if ( alignment != null ) {
+	    Statement st = (Statement) conn.createStatement();
+	    String query = "DELETE FROM extension WHERE id=''";
+	    st.executeUpdate(query);
+	    query = "DELETE FROM alignment WHERE id=''";
+	    st.executeUpdate(query);
+	    query = "DELETE FROM cell WHERE id=''";
+	    st.executeUpdate(query);
+	    alignment.setExtension( SVCNS, STORED, "");
+	}
     }
 
     public void storeAlignment( String id ) throws Exception {
@@ -501,8 +519,8 @@ public class CacheImpl {
 			cellid = c.getId();
 		    }
 		    else cellid = "";
-		    String uri1 = c.getObject1AsURI().toString();
-		    String uri2 = c.getObject2AsURI().toString();
+		    String uri1 = c.getObject1AsURI(alignment).toString();
+		    String uri2 = c.getObject2AsURI(alignment).toString();
 		    String strength = c.getStrength() + ""; // crazy Java
 		    String sem;
 		    if ( !c.getSemantics().equals("first-order") )
@@ -528,7 +546,11 @@ public class CacheImpl {
 		    }
 		}
 	    }
-	} catch (Exception e) { e.printStackTrace(); };
+	} catch (Exception e) { 
+	    // We should cancel this... IF NOT STORED!
+	    alignment.setExtension( SVCNS, STORED, "");
+	    e.printStackTrace(); 
+	};
 	// We reset cached date
 	resetCacheStamp(alignment);
     }
@@ -644,7 +666,7 @@ public class CacheImpl {
 		Statement st2 = (Statement) conn.createStatement();
 		while ( rse.next() ){
 		    String tag = rse.getString("tag");
-		    System.err.println(" Treating tag "+tag+" of "+rse.getString("id"));
+		    //System.err.println(" Treating tag "+tag+" of "+rse.getString("id"));
 		    if ( !tag.equals("") ){
 			int pos;
 			String ns;
@@ -662,7 +684,7 @@ public class CacheImpl {
 			    ns = Annotations.ALIGNNS;
 			    name = tag;
 			}
-			System.err.println("  >> "+ns+" : "+name);
+			//System.err.println("  >> "+ns+" : "+name);
 			st2.executeUpdate("UPDATE extension SET tag='"+name+"', uri='"+ns+"' WHERE id='"+rse.getString("id")+"' AND tag='"+tag+"'");
 		    }
 		}
diff --git a/src/fr/inrialpes/exmo/align/service/QueryMediator.java b/src/fr/inrialpes/exmo/align/service/QueryMediator.java
index 946bb810..572dbc35 100644
--- a/src/fr/inrialpes/exmo/align/service/QueryMediator.java
+++ b/src/fr/inrialpes/exmo/align/service/QueryMediator.java
@@ -30,6 +30,7 @@ package fr.inrialpes.exmo.align.service;
 import fr.inrialpes.exmo.queryprocessor.QueryProcessor;
 import fr.inrialpes.exmo.queryprocessor.Result;
 import fr.inrialpes.exmo.queryprocessor.Type;
+import fr.inrialpes.exmo.align.impl.BasicAlignment;
 
 import fr.inrialpes.exmo.align.parser.AlignmentParser;
 
@@ -188,8 +189,8 @@ public class QueryMediator implements QueryProcessor {
 	for( Enumeration e = align.getElements() ; e.hasMoreElements(); ){
 	    Cell cell = (Cell)e.nextElement();
 	    mainQuery = mainQuery.replaceAll(
-					     cell.getObject1AsURI().toString(),
-					     cell.getObject2AsURI().toString() );
+					     cell.getObject1AsURI(align).toString(),
+					     cell.getObject2AsURI(align).toString() );
 	}
         return mainQuery;
     }
diff --git a/src/org/semanticweb/owl/align/Cell.java b/src/org/semanticweb/owl/align/Cell.java
index 5a8b2efb..0e5d247e 100644
--- a/src/org/semanticweb/owl/align/Cell.java
+++ b/src/org/semanticweb/owl/align/Cell.java
@@ -51,8 +51,18 @@ public interface Cell extends Comparable<Cell> {
     public void setSemantics( String s );
     public Object getObject1();
     public Object getObject2();
+    /**
+     * //@deprecated use getObject1AsURI( null ) instead
+     */
+    //@Deprecated
     public URI getObject1AsURI() throws AlignmentException;
+    /**
+     * //@deprecated use getObject2AsURI( null ) instead
+     */
+    //@Deprecated
     public URI getObject2AsURI() throws AlignmentException;
+    public URI getObject1AsURI( Alignment al ) throws AlignmentException;
+    public URI getObject2AsURI( Alignment al ) throws AlignmentException;
     public void setObject1( Object ob ) throws AlignmentException;
     public void setObject2( Object ob ) throws AlignmentException;
     public Relation getRelation();
-- 
GitLab