From 48e5f26713d45f3bb766caf212eb99e6dc4aefc2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Euzenat?= <Jerome.Euzenat@inria.fr>
Date: Tue, 31 Jan 2012 16:43:38 +0000
Subject: [PATCH] - Improved error management of generic visitor

---
 .../renderer/GenericReflectiveVisitor.java    |  10 +-
 .../renderer/HTMLMetadataRendererVisitor.java |   4 +-
 .../impl/renderer/HTMLRendererVisitor.java    |   2 +-
 .../renderer/OWLAxiomsRendererVisitor.java    | 123 +++++++++++-------
 .../impl/renderer/RDFRendererVisitor.java     |   2 +-
 .../renderer/XMLMetadataRendererVisitor.java  |   4 +-
 6 files changed, 91 insertions(+), 54 deletions(-)

diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/GenericReflectiveVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/GenericReflectiveVisitor.java
index 5ad52ad9..ea9bd4fd 100644
--- a/src/fr/inrialpes/exmo/align/impl/renderer/GenericReflectiveVisitor.java
+++ b/src/fr/inrialpes/exmo/align/impl/renderer/GenericReflectiveVisitor.java
@@ -23,6 +23,8 @@ package fr.inrialpes.exmo.align.impl.renderer;
 import java.lang.reflect.Method;
 import java.lang.reflect.InvocationTargetException;
 
+import org.semanticweb.owl.align.AlignmentException;
+
 /**
  * This class offers the tools for implementing Reflective visitors,
  * i.e., visitors in which the visit method will depend on the actual
@@ -74,7 +76,7 @@ public class GenericReflectiveVisitor {
 	return m;
     }
 
-    public boolean subsumedInvocableMethod( Object visitor, Object o, Class cl ) {
+    public boolean subsumedInvocableMethod( Object visitor, Object o, Class cl ) throws AlignmentException {
 	Method method = getMethod( o.getClass(), cl );
 	if ( method != null ) {
 	    try {
@@ -83,7 +85,11 @@ public class GenericReflectiveVisitor {
 	    } catch ( IllegalAccessException iaex ) {
 		iaex.printStackTrace();
 	    } catch ( InvocationTargetException itex ) { 
-		itex.printStackTrace();
+		if ( itex.getCause() instanceof AlignmentException ) {
+		    throw (AlignmentException)itex.getCause();
+		} else {
+		    itex.printStackTrace();
+		}
 	    }
 	}
 	return false;
diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/HTMLMetadataRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/HTMLMetadataRendererVisitor.java
index b450c9d8..d237631e 100644
--- a/src/fr/inrialpes/exmo/align/impl/renderer/HTMLMetadataRendererVisitor.java
+++ b/src/fr/inrialpes/exmo/align/impl/renderer/HTMLMetadataRendererVisitor.java
@@ -128,12 +128,12 @@ public class HTMLMetadataRendererVisitor extends GenericReflectiveVisitor implem
 	writer.print("</body>\n</html>\n");
     }
 
-    public void visit( Cell c ) {
+    public void visit( Cell c ) throws AlignmentException {
 	if ( subsumedInvocableMethod( this, c, Cell.class ) ) return;
 	// default behaviour
     };
 
-    public void visit( Relation r ) {
+    public void visit( Relation r ) throws AlignmentException {
 	if ( subsumedInvocableMethod( this, r, Relation.class ) ) return;
 	// default behaviour
     };
diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/HTMLRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/HTMLRendererVisitor.java
index 67c19f5c..ad3a8dc1 100644
--- a/src/fr/inrialpes/exmo/align/impl/renderer/HTMLRendererVisitor.java
+++ b/src/fr/inrialpes/exmo/align/impl/renderer/HTMLRendererVisitor.java
@@ -175,7 +175,7 @@ public class HTMLRendererVisitor extends GenericReflectiveVisitor implements Ali
 	//	writer.print("      <semantics>"+cell.getSemantics()+"</semantics>\n");
 	writer.println("</tr>");
     }
-    public void visit( Relation rel ) {
+    public void visit( Relation rel ) throws AlignmentException {
 	if ( subsumedInvocableMethod( this, rel, Relation.class ) ) return;
 	// default behaviour
 	rel.write( writer );
diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/OWLAxiomsRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/OWLAxiomsRendererVisitor.java
index e197bea7..fb5ef254 100644
--- a/src/fr/inrialpes/exmo/align/impl/renderer/OWLAxiomsRendererVisitor.java
+++ b/src/fr/inrialpes/exmo/align/impl/renderer/OWLAxiomsRendererVisitor.java
@@ -166,26 +166,27 @@ public class OWLAxiomsRendererVisitor extends IndentedRendererVisitor implements
 	    Object ob2 = cell.getObject2();
 	    URI u1;
 	    try {
-		if ( cell.getRelation() instanceof SubsumedRelation ){
-		    u1 = onto2.getEntityURI( cell.getObject2() );
+		Relation rel = cell.getRelation();
+		if ( rel instanceof SubsumedRelation || rel instanceof HasInstanceRelation ){
+		    u1 = onto2.getEntityURI( ob2 );
 		} else {
 		    u1 = onto1.getEntityURI( ob1 );
 		}
 		if ( ob1 instanceof ClassExpression || onto1.isClass( ob1 ) ) {
 		    writer.print("  <owl:Class rdf:about=\""+u1+"\">"+NL);
-		    cell.getRelation().accept( this );
+		    rel.accept( this );
 		    writer.print("  </owl:Class>"+NL);
 		} else if ( ob1 instanceof PropertyExpression || onto1.isDataProperty( ob1 ) ) {
 		    writer.print("  <owl:DatatypeProperty rdf:about=\""+u1+"\">"+NL);
-		    cell.getRelation().accept( this );
+		    rel.accept( this );
 		    writer.print("  </owl:DatatypeProperty>"+NL);
 		} else if ( ob1 instanceof RelationExpression || onto1.isObjectProperty( ob1 ) ) {
 		    writer.print("  <owl:ObjectProperty rdf:about=\""+u1+"\">"+NL);
-		    cell.getRelation().accept( this );
+		    rel.accept( this );
 		    writer.print("  </owl:ObjectProperty>"+NL);
 		} else if ( ob1 instanceof InstanceExpression || onto1.isIndividual( ob1 ) ) {
 		    writer.print("  <owl:Thing rdf:about=\""+u1+"\">"+NL);
-		    cell.getRelation().accept( this );
+		    rel.accept( this );
 		    writer.print("  </owl:Thing>"+NL);
 		}
 	    } catch ( OntowrapException owex ) {
@@ -197,11 +198,13 @@ public class OWLAxiomsRendererVisitor extends IndentedRendererVisitor implements
     public void visit( EDOALCell cell ) throws AlignmentException {
 	this.cell = cell;
 	toProcess = cell.getRelation();
+	increaseIndent();
 	if ( toProcess instanceof SubsumeRelation || toProcess instanceof HasInstanceRelation ) {
 	    ((Expression)cell.getObject2()).accept( this );
 	} else {
 	    ((Expression)cell.getObject1()).accept( this );
 	}
+	decreaseIndent();
 	writer.print(NL);
     }
 
@@ -233,14 +236,6 @@ public class OWLAxiomsRendererVisitor extends IndentedRendererVisitor implements
 		throw new AlignmentException( "Error accessing ontology", owex );
 	    }
 	}
-	/* This dispatch may be used for more customization
-	if ( rel instanceof EquivRelation ) visit( (EquivRelation)rel );
-	else if ( rel instanceof SubsumeRelation ) visit( (SubsumeRelation)rel );
-	else if ( rel instanceof SubsumedRelation ) visit( (SubsumedRelation)rel );
-	else if ( rel instanceof IncompatRelation ) visit( (IncompatRelation)rel );
-	else if ( rel instanceof InstanceOfRelation ) visit( (InstanceOfRelation)rel );
-	else if ( rel instanceof HasInstanceRelation ) visit( (HasInstanceRelation)rel );
-	*/
     }
 
 
@@ -341,107 +336,143 @@ public class OWLAxiomsRendererVisitor extends IndentedRendererVisitor implements
 
     public void visit( EquivRelation rel ) throws AlignmentException {
 	Object ob2 = cell.getObject2();
-	String owlrel = getRelationName( onto2, rel, ob2 );
-	if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
 	if ( !edoal ) {
+	    String owlrel = getRelationName( onto2, rel, ob2 );
+	    if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
 	    try {
 		writer.print("    <"+owlrel+" rdf:resource=\""+onto2.getEntityURI( ob2 )+"\"/>"+NL);
 	    } catch ( OntowrapException owex ) {
 		throw new AlignmentException( "Error accessing ontology", owex );
 	    }
 	} else {
+	    String owlrel = getRelationName( rel, ob2 );
+	    if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
 	    if ( ob2 instanceof InstanceId ) {
-		writer.print("    <"+owlrel+" rdf:resource=\""+((InstanceId)ob2).getURI()+"\"/>"+NL);
+		indentedOutput("<"+owlrel+" rdf:resource=\""+((InstanceId)ob2).getURI()+"\"/>");
 	    } else {
-		writer.println("    <"+owlrel+">");
+		indentedOutput("<"+owlrel+">");
+		writer.print(NL);
+		increaseIndent();
 		((Expression)ob2).accept( this );
-		writer.println("    </"+owlrel+">");
+		decreaseIndent();
+		writer.print(NL);
+		indentedOutput("</"+owlrel+">");
 	    }
 	}
     }
 
     public void visit( SubsumeRelation rel ) throws AlignmentException {
-	Object ob2 = cell.getObject2();
-	String owlrel = getRelationName( onto2, rel, ob2 );
-	if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
+	Object ob1 = cell.getObject1();
 	if ( !edoal ) {
+	    String owlrel = getRelationName( onto1, rel, ob1 );
+	    if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
 	    try {
-		writer.print("    <"+owlrel+" rdf:resource=\""+onto2.getEntityURI( ob2 )+"\"/>"+NL);
+		writer.print("    <"+owlrel+" rdf:resource=\""+onto1.getEntityURI( ob1 )+"\"/>"+NL);
 	    } catch ( OntowrapException owex ) {
 		throw new AlignmentException( "Error accessing ontology", owex );
 	    }
 	} else {
-	    writer.println("    <"+owlrel+">");
-	    ((Expression)ob2).accept( this );
-	    writer.println("    </"+owlrel+">");
+	    String owlrel = getRelationName( rel, ob1 );
+	    if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
+	    indentedOutput("<"+owlrel+">");
+	    writer.print(NL);
+	    increaseIndent();
+	    ((Expression)ob1).accept( this );
+	    decreaseIndent();
+	    writer.print(NL);
+	    indentedOutput("</"+owlrel+">");
 	}
     }
 
     public void visit( SubsumedRelation rel ) throws AlignmentException {
-	Object ob1 = cell.getObject1();
-	String owlrel = getRelationName( onto1, rel, ob1 );
-	if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
+	Object ob2 = cell.getObject2();
 	if ( !edoal ) {
+	    String owlrel = getRelationName( onto2, rel, ob2 );
+	    if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
 	    try {
-		writer.print("    <"+owlrel+" rdf:resource=\""+onto1.getEntityURI( ob1 )+"\"/>"+NL);
+		writer.print("    <"+owlrel+" rdf:resource=\""+onto2.getEntityURI( ob2 )+"\"/>"+NL);
 	    } catch ( OntowrapException owex ) {
 		throw new AlignmentException( "Error accessing ontology", owex );
 	    }
 	} else {
-	    writer.println("    <"+owlrel+">");
-	    ((Expression)ob1).accept( this );
-	    writer.println("    </"+owlrel+">");
+	    String owlrel = getRelationName( rel, ob2 );
+	    if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
+	    indentedOutput("<"+owlrel+">");
+	    writer.print(NL);
+	    increaseIndent();
+	    ((Expression)ob2).accept( this );
+	    decreaseIndent();
+	    writer.print(NL);
+	    indentedOutput("</"+owlrel+">");
 	}
     }
 
     public void visit( IncompatRelation rel ) throws AlignmentException {
 	Object ob2 = cell.getObject2();
-	String owlrel = getRelationName( onto2, rel, ob2 );
-	if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
 	if ( !edoal ) {
+	    String owlrel = getRelationName( onto2, rel, ob2 );
+	    if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
 	    try {
 		writer.print("    <"+owlrel+" rdf:resource=\""+onto2.getEntityURI( ob2 )+"\"/>"+NL);
 	    } catch ( OntowrapException owex ) {
 		throw new AlignmentException( "Cannot find entity URI", owex );
 	    }
 	} else {
-	    writer.println("    <"+owlrel+">");
+	    String owlrel = getRelationName( rel, ob2 );
+	    if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
+	    indentedOutput("<"+owlrel+">");
+	    writer.print(NL);
+	    increaseIndent();
 	    ((Expression)ob2).accept( this );
-	    writer.println("    </"+owlrel+">");
+	    writer.print(NL);
+	    decreaseIndent();
+	    indentedOutput("</"+owlrel+">");
 	}
     }
 
     public void visit( InstanceOfRelation rel ) throws AlignmentException {
 	Object ob2 = cell.getObject2();
-	String owlrel = getRelationName( onto2, rel, ob2 );
-	if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
 	if ( !edoal ) {
+	    String owlrel = getRelationName( onto2, rel, ob2 );
+	    if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
 	    try {
 		writer.print("    <"+owlrel+" rdf:resource=\""+onto2.getEntityURI( ob2 )+"\"/>"+NL);
 	    } catch ( OntowrapException owex ) {
 		throw new AlignmentException( "Cannot find entity URI", owex );
 	    }
 	} else {
-	    writer.println("    <"+owlrel+">");
+	    String owlrel = getRelationName( rel, ob2 );
+	    if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
+	    indentedOutput("<"+owlrel+">");
+	    writer.print(NL);
+	    increaseIndent();
 	    ((Expression)ob2).accept( this );
-	    writer.println("    </"+owlrel+">");
+	    writer.print(NL);
+	    decreaseIndent();
+	    indentedOutput("</"+owlrel+">");
 	}
     }
 
     public void visit( HasInstanceRelation rel ) throws AlignmentException {
 	Object ob1 = cell.getObject1();
-	String owlrel = getRelationName( onto1, rel, ob1 );
-	if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
 	if ( !edoal ) {
+	    String owlrel = getRelationName( onto1, rel, ob1 );
+	    if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
 	    try {
 		writer.print("    <"+owlrel+" rdf:resource=\""+onto1.getEntityURI( ob1 )+"\"/>"+NL);
 	    } catch ( OntowrapException owex ) {
 		throw new AlignmentException( "Error accessing ontology", owex );
 	    }
 	} else {
-	    writer.println("    <"+owlrel+">");
+	    String owlrel = getRelationName( rel, ob1 );
+	    if ( owlrel == null ) throw new AlignmentException( "Cannot express relation "+rel );
+	    indentedOutput("<"+owlrel+">");
+	    writer.print(NL);
+	    increaseIndent();
 	    ((Expression)ob1).accept( this );
-	    writer.println("    </"+owlrel+">");
+	    writer.print(NL);
+	    decreaseIndent();
+	    indentedOutput("</"+owlrel+">");
 	}
     }
 
@@ -673,7 +704,7 @@ public class OWLAxiomsRendererVisitor extends IndentedRendererVisitor implements
      * and Property (DataProperty) constructor than owl:inverseOf
      * It is thus imposible to transcribe our and, or and not constructors.
      */
-    public void visit(final PropertyConstruction e) throws AlignmentException {
+    public void visit( final PropertyConstruction e ) throws AlignmentException {
 	Relation toProcessNext = toProcess;
 	toProcess = null;
 	indentedOutput("<owl:DatatypePropety>"+NL);
diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/RDFRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/RDFRendererVisitor.java
index 6bc42f65..0a3b9198 100644
--- a/src/fr/inrialpes/exmo/align/impl/renderer/RDFRendererVisitor.java
+++ b/src/fr/inrialpes/exmo/align/impl/renderer/RDFRendererVisitor.java
@@ -306,7 +306,7 @@ public class RDFRendererVisitor extends IndentedRendererVisitor implements Align
 	}
     }
 
-    public void visit( Relation rel ) {
+    public void visit( Relation rel ) throws AlignmentException {
 	if ( subsumedInvocableMethod( this, rel, Relation.class ) ) return;
 	// default behaviour
 	rel.write( writer );
diff --git a/src/fr/inrialpes/exmo/align/impl/renderer/XMLMetadataRendererVisitor.java b/src/fr/inrialpes/exmo/align/impl/renderer/XMLMetadataRendererVisitor.java
index f69c70c1..0b529812 100644
--- a/src/fr/inrialpes/exmo/align/impl/renderer/XMLMetadataRendererVisitor.java
+++ b/src/fr/inrialpes/exmo/align/impl/renderer/XMLMetadataRendererVisitor.java
@@ -149,12 +149,12 @@ public class XMLMetadataRendererVisitor extends GenericReflectiveVisitor impleme
 	writer.print("</rdf:RDF>\n");
     }
 
-    public void visit( Cell c ) {
+    public void visit( Cell c ) throws AlignmentException {
 	if ( subsumedInvocableMethod( this, c, Cell.class ) ) return;
 	// default behaviour
     };
 
-    public void visit( Relation r ) {
+    public void visit( Relation r ) throws AlignmentException {
 	if ( subsumedInvocableMethod( this, r, Relation.class ) ) return;
 	// default behaviour
     };
-- 
GitLab