From 9cc84d160fa47d88e7d3f8a267957ced8ad261c6 Mon Sep 17 00:00:00 2001
From: Luz-Maria Priego <lpriego2003@hotmail.com>
Date: Mon, 19 May 2014 16:20:48 +0000
Subject: [PATCH] implemented Available Networks functionality

---
 .../align/service/AServProtocolManager.java   | 131 +++++++++++++++---
 .../exmo/align/service/HTMLAServProfile.java  | 118 +++++++++++-----
 .../exmo/align/service/HTTPTransport.java     |  25 ++++
 .../align/service/msg/OntologyNetworkId.java  |   2 +-
 4 files changed, 214 insertions(+), 62 deletions(-)

diff --git a/src/fr/inrialpes/exmo/align/service/AServProtocolManager.java b/src/fr/inrialpes/exmo/align/service/AServProtocolManager.java
index f43ba22e..36239172 100644
--- a/src/fr/inrialpes/exmo/align/service/AServProtocolManager.java
+++ b/src/fr/inrialpes/exmo/align/service/AServProtocolManager.java
@@ -91,7 +91,9 @@ import java.net.URI;
 import java.net.URL;
 import java.net.JarURLConnection;
 import java.net.URISyntaxException;
+import java.util.ArrayList;
 import java.util.Hashtable;
+import java.util.List;
 import java.util.Set;
 import java.util.HashSet;
 import java.util.Collection;
@@ -226,12 +228,12 @@ public class AServProtocolManager implements Service {
     
     public Set<Alignment> networkAlignmentUri(String uri) {
     	OntologyNetwork noo = null;
-	try {
-	    noo = alignmentCache.getOntologyNetwork(uri);
-	} catch (AlignmentException e) {
-	    e.printStackTrace();
-	}
-	return ((BasicOntologyNetwork) noo).getAlignments();
+		try {
+			noo = alignmentCache.getOntologyNetwork(uri);
+		} catch (AlignmentException e) {
+			e.printStackTrace();
+		}
+		return ((BasicOntologyNetwork) noo).getAlignments();
     }
     
     public Collection<Alignment> alignments( URI uri1, URI uri2 ) {
@@ -1221,29 +1223,31 @@ public class AServProtocolManager implements Service {
 	try {
 	    noo = (BasicOntologyNetwork) BasicOntologyNetwork.read( name );
 	    logger.trace(" Ontology network parsed");
-	} catch (Exception e) {
-	    return new UnreachableOntologyNetwork( params, newId(), serverId, name );
-	}
+	    } catch (Exception e) {
+		  return new UnreachableOntologyNetwork( params, newId(), serverId, name );
+		  }
 	// We preserve the pretty tag within the loaded ontology network
 	String pretty = noo.getExtension( Namespace.ALIGNMENT.uri, Annotations.PRETTY ); 
 	if ( pretty == null ) pretty = params.getProperty("pretty");
 	if ( pretty != null && !pretty.equals("") ) {
-	    noo.setExtension( Namespace.ALIGNMENT.uri, Annotations.PRETTY, pretty );
-	}
+		noo.setExtension( Namespace.ALIGNMENT.uri, Annotations.PRETTY, pretty );
+		}
 	// register it
 	String id = alignmentCache.recordNewNetwork( noo, true );
 	logger.debug(" Ontology network loaded, id: {} total ontologies: {} total alignments: {}",id, noo.getOntologies().size(),noo.getAlignments().size());
 
-	//=== The alignment has the id of the source file (e.g. file:///path/FileName.rdf)
-	for ( Alignment al : noo.getAlignments() ) {
-	    alignmentCache.recordNewAlignment( al, true );
+    //=== The alignment has the id of the source file (e.g. file:///path/FileName.rdf)
+	Set<Alignment> networkAlignments = networkAlignmentUri(id);
+	for (Alignment al : networkAlignments) {
+		String idAl = alignmentCache.recordNewAlignment( al, true );
 	}
+	
 	return new OntologyNetworkId( params, newId(), serverId, id ,pretty );
     }
 
     
     public Message renderonet(Properties params) {
-   
+   //rdf render
     	OntologyNetwork noo = null;
    	    noo = new BasicOntologyNetwork();
     	String id = params.getProperty( "id" );
@@ -1281,7 +1285,43 @@ public class AServProtocolManager implements Service {
 
     }
 
-
+    public Message renderHTMLNetwork( Properties params ){
+    	// Retrieve the alignment
+    	String result = new String();
+    	String id = params.getProperty( "id" );
+    	//String id = idON;
+    	BasicOntologyNetwork noo = null;
+    	try {
+    	    logger.trace("Network sought for {}", id);
+    	    noo = (BasicOntologyNetwork) alignmentCache.getOntologyNetwork(id);
+    	    logger.trace("Network found");
+    	} catch (Exception e) {
+    	    return new UnknownOntologyNetwork( params, newId(), serverId,id );
+    	    }
+	    result = "<h1>" + id + "</h1>";
+	    
+	    result += "<table border=\"0\">\n";
+	    result += "<h2>Ontologies of the Network</h2>\n";
+    	Collection<URI> networkOntology = networkOntologyUri(id);
+	    result += "<p><tr><th><b>Total ontologies: </b>" + networkOntology.size() + "</th></tr></p>";
+	    result += "<table>\n";
+	    for ( URI onto : networkOntology ) {
+	    	result += "<tr><td>" + "<a href=\"" + onto.toString() +"\">"+ onto.toString() + "</a>" + "</td></tr>\n";
+	    }
+	    result += "</table>\n";
+	    
+    	result += "<h2>Alignments of the Network</h2>\n"; 
+	    Set<Alignment> networkAlignments = networkAlignmentUri(id);
+	    result += "<table>\n";
+	    result += "<p><tr><th><b>Total alignments: </b>" + networkAlignments.size() + "</th></tr></p>";
+	    for (Alignment al : networkAlignments) {	
+	    	String idAl = al.getExtension( Namespace.ALIGNMENT.uri, Annotations.ID );
+			String pidAl = al.getExtension( Namespace.ALIGNMENT.uri, Annotations.PRETTY );
+			if ( pidAl == null ) pidAl = idAl; else pidAl = idAl+" ("+pidAl+")";
+	    	result += "<tr><td>" + "<a href=\""+idAl+"\">"+pidAl+"</a>" + "</td></tr>\n";
+	    	}
+    	return new RenderedAlignment( params, newId(), serverId, result );
+        }
     
     public boolean storedOntologyNetwork( Properties params ) {
     	// Retrieve the ontology network
@@ -1335,9 +1375,12 @@ public class AServProtocolManager implements Service {
     	}
     	return (Message)null;
         }
+  
     
-    public Message alignonet( Properties params ) {
-    	Message result = null;
+public List<Message> alignonet( Properties params ) {
+    	
+    	
+    	List<Message> result = new ArrayList<>();
     	//not finished
     	//parameters: onID, method, reflexive, symmetric
     	for ( Enumeration<String> e = (Enumeration<String>)commandLineParams.propertyNames(); e.hasMoreElements();) {
@@ -1355,16 +1398,58 @@ public class AServProtocolManager implements Service {
     	
 	    Collection<URI> networkOntologyA = networkOntologyUri(id);
 	    Collection<URI> networkOntologyB = networkOntologyUri(id);
-	    for ( URI ontoA : networkOntologyA ) {
-	    	for ( URI ontoB : networkOntologyB ) {
-	    		result = retrieveAlignmentON( params ,ontoA, ontoB);
-	        	if ( result != null ) return result;
+	    
+	    // Reflexive and Symmetric
+	    if ( reflexive && symmetric ) {
+		    for ( URI ontoA : networkOntologyA ) {
+		    	for ( URI ontoB : networkOntologyB ) {
+		    		params.setProperty("onto1", ontoA.toString());
+		    		params.setProperty("onto2", ontoB.toString());
+		    		Message answer = align( params );
+		        	if ( answer != null || answer.toString().contains("AlignmentId")) {
+		        		result.add(answer);
+		        	};
+			    }
 		    }
 	    }
-	    return (Message)null;
+	    
+	    return result;
  
     }
 
+    
+    /* TO VERIFY
+    public Message alignonet( Properties params ) {
+    	  	
+      	//parameters: onID, method, reflexive, symmetric 	
+       	OntologyNetwork noo = null;
+    	Boolean reflexive = false;
+    	Boolean symmetric = false;
+    	String id = params.getProperty("id");
+    	String method = params.getProperty("method");
+    	if (params.getProperty("reflexive") != null) reflexive = true;
+    	if (params.getProperty("symmetric") != null) symmetric = true;
+    	
+    	try {
+			noo = alignmentCache.getOntologyNetwork( id );
+			} catch (AlignmentException e1) {
+				return new UnknownOntologyNetwork( params, newId(), serverId,id );
+				}
+    	
+    	try { 
+    		((BasicOntologyNetwork) noo).match(method, reflexive, symmetric);
+    		} catch (AlignmentException e) {
+    	    return new ErrorMsg( params, newId(), serverId,"Network alignment error" );
+    	}
+    	
+    	logger.debug(" Network alignments results, id: {} total ontologies: {} total alignments: {}",id, noo.getOntologies().size(),noo.getAlignments().size());
+    	return new OntologyNetworkId( params, newId(), serverId, id,
+				   ((BasicOntologyNetwork) noo).getExtension( Namespace.ALIGNMENT.uri, Annotations.PRETTY ));
+    	
+    }
+    
+    */
+
     public Message listNetworkOntology( Properties params ) { //not UsED??
     	String result = "";   	
     	
diff --git a/src/fr/inrialpes/exmo/align/service/HTMLAServProfile.java b/src/fr/inrialpes/exmo/align/service/HTMLAServProfile.java
index 8cf64653..fddd19dd 100644
--- a/src/fr/inrialpes/exmo/align/service/HTMLAServProfile.java
+++ b/src/fr/inrialpes/exmo/align/service/HTMLAServProfile.java
@@ -46,6 +46,7 @@ import java.io.ByteArrayInputStream;
 import java.io.OutputStream;
 import java.io.BufferedInputStream;
 import java.io.FileOutputStream;
+import java.util.List;
 import java.util.Locale;
 import java.util.Set;
 import java.util.TimeZone;
@@ -195,35 +196,38 @@ public class HTMLAServProfile implements AlignmentServiceProfile {
 	logger.trace( "ONTONET[{}]", perf);
 	String msg = "";
 	String eSource = "on";
-        if ( perf.equals("prmlistonet") ){
-        	URI uriON = null;	
-    	    String uON = params.getProperty("uri");
-    	    try {
-    		if ( uON != null && !uON.equals("all") ) uriON = new URI( uON );
-    	    } catch ( URISyntaxException usex ) {
-    		logger.debug( "IGNORED Invalid URI parameter", usex );
-    	    };
-
-        	Collection<URI> ontologyNetworksURI = manager.ontologyNetworkUris();
+        if ( perf.equals("listnetwork") ){
+        	
+        	Collection<OntologyNetwork> ontologyNetworks = manager.ontologyNetworks();
+        	BasicOntologyNetwork noo = null;
+        	String id = "";
+        	String pid = "";
     	    msg = "<h1>Available networks</h1>";
-    	    msg += "<form action=\"listNetOnto\">";
-    	    msg += "Network:  <select name=\"uri\"><option value=\"all\">all</option>";
-		    if ( uriON == null ) msg += " selected=\"1\"";
-		    for ( URI onet : ontologyNetworksURI ) { 
-				msg += "<option";
-				if ( onet.equals( uriON ) ) msg += " selected =\"1\"";
-				msg += " value=\""+onet+"\">"+onet+"</option>";
+    	    //msg += "<form action=\"listNetOnto\"><ul compact=\"1\">";
+    	    msg += "<form action=\"listnetwork\"><ul compact=\"1\">";
+    	    for ( OntologyNetwork oNetwork : ontologyNetworks ) {
+    	    	noo = (BasicOntologyNetwork) oNetwork;
+    	    	id = noo.getExtension( Namespace.ALIGNMENT.uri, Annotations.ID );
+    			pid = noo.getExtension( Namespace.ALIGNMENT.uri, Annotations.PRETTY );
+    			if ( pid == null ) pid = id; else pid = id+" ("+pid+")";
+     			msg += "<li><a href=\"" + id +"\">" + pid + "</a></li>";
+    		    }
+		   msg += "</ul></form>";
+
+        } else if ( perf.equals("listNetOnto") ){   //TODO eliminate this
+		   //Message answer = manager.renderHTMLON( params );
+		   Message answer = manager.renderHTMLNetwork( params ); // "http://localhost:8089/onid/1400170347760/525" );
+		    if ( answer instanceof ErrorMsg ) {
+			msg = testErrorMessages( answer, params, eSource );
+		    } else {
+		    	return answer.getContent();
 		    }
-		    msg += "</select>";
-		    msg += "&nbsp;<input type=\"submit\" value=\"List ontologies\"/></form><ul compact=\"1\">";
-		    //msg += "<table><tr><td><input type=\"submit\" value=\"List ontologies\"name=\"listNetOnto\"/></form>";
-		    //msg += "<input type=\"submit\" value=\"List alignments\"/ name=\"listNetAlig\"></form></td></tr></table/>";
-		    
-        } else if ( perf.equals("listNetOnto") ){
+		      
+        } else if ( perf.equals("listNetOnto2") ){
         	
         	msg = "<h1>Ontologies of the Network</h1>";
         	msg += "<form action=\"listNetAlig\">";
-		    String uriON = params.getProperty("uri");  //send as parameter uriON for listNetAlig
+		    String uriON = params.getProperty("uriON");  //send as parameter uriON for listNetAlig
 		    int numOnto = 0;
 		    Collection<URI> networkOntology = manager.networkOntologyUri(uriON);
 		    msg += "<p>" + uriON + "   ";
@@ -358,32 +362,68 @@ public class HTMLAServProfile implements AlignmentServiceProfile {
 		    		msg += "<option selected=\"1\" value=\""+id+"\">"+pid+"</option>";
 		    		} else { msg += "<option value=\""+id+"\">"+pid+"</option>";}
 		    	}
-		    msg += "<!--input type=\"submit\" name=\"action\" value=\"Find\"/>";
-		    msg += "<br /-->Methods: <select name=\"method\">";
+		    msg += "</select><br />";
 
+//		    msg += "<!--input type=\"submit\" name=\"action\" value=\"Find\"/>";
+//		    msg += "<br /-->Methods: <select name=\"method\">";
+
+		    msg += "<br />Methods: <select name=\"method\">";
 		    for( String idMethod : manager.listmethods() ) {
 				msg += "<option value=\""+idMethod+"\">"+idMethod+"</option>"; 
 			    }
+		    msg += "</select><br /><br />";
 
-		    msg += "</select><br />";
-		    msg += "<input type=\"checkbox\" name=\"reflexive\" /> Reflexive ";
+		    msg += "Pretty name: <input type=\"text\" name=\"pretty\" size=\"80\"/><br />";
+		    msg += "  <input type=\"checkbox\" name=\"force\" /> Force <input type=\"checkbox\" name=\"async\" /> Asynchronous<br />";
+		    msg += "Additional parameters:<br /><input type=\"text\" name=\"paramn1\" size=\"15\"/> = <input type=\"text\" name=\"paramv1\" size=\"65\"/><br /><input type=\"text\" name=\"paramn2\" size=\"15\"/> = <input type=\"text\" name=\"paramv2\" size=\"65\"/><br /><input type=\"text\" name=\"paramn3\" size=\"15\"/> = <input type=\"text\" name=\"paramv3\" size=\"65\"/><br /><input type=\"text\" name=\"paramn4\" size=\"15\"/> = <input type=\"text\" name=\"paramv4\" size=\"65\"/>";
+
+		    msg += "<br /><input type=\"checkbox\" name=\"reflexive\" /> Reflexive ";
 		    msg += "<input type=\"checkbox\" name=\"symmetric\" /> Symmetric ";
 		    msg += "<input type=\"checkbox\" name=\"new\" /> New ";
-		    msg += "<br /><input type=\"submit\" name=\"action\" value=\"Match\"/> ";
+		    msg += "<br /><br /><input type=\"submit\" name=\"action\" value=\"Match\"/> ";
+		    msg += "</form>";
 		    	    
 	} else if ( perf.equals("matchonet") ) {
+		
 		// DO MATCHING
-	    Message answer = manager.alignonet( params );
-	    if ( answer instanceof ErrorMsg ) {
-		msg = testErrorMessages( answer, params, eSource );
-	    } else {
+	    List<Message> answer = manager.alignonet( params );
 		msg = "<h1>Network Alignments results</h1>";
-		msg += displayAnswer( answer, params );
-	    }
+		Iterator<Message> it = answer.iterator();
+		msg += "<ul>";
+		while (it.hasNext()) {
+			final Message alignment = it.next();
+ 			msg += "<li><a href=\"" + alignment.getContent() +"\">" + alignment.getContent() + "</a></li>";
+		}
+		msg += "</ul>";
+		
+		/*
+		// DO MATCHING
+		String idON = params.getProperty("id");
+		logger.debug("Matching network {}", idON);
+		Message answer = manager.alignonet( params );
+		if ( answer instanceof ErrorMsg ) {
+			msg = testErrorMessages( answer, params, eSource );
+		    } else {
+		    	//return answer.getContent();
+				msg = "<h1>Network alignments results</h1>";
+			    
+			    //String idON = "http://localhost:8089/onid/1400144735513/788";
+			    Set<Alignment> networkAlignments = manager.networkAlignmentUri(idON);
+			    msg += "<p> Network ID: <a href=\""+idON+"\">"+idON+"</a></p>";
+			    msg += "<p><tr><th><b>Total alignments: </b>" + networkAlignments.size() + "</th></tr></p>";
+			    msg += "<ul>";
+			    for ( Alignment al : networkAlignments ) {
+					String id = al.getExtension( Namespace.ALIGNMENT.uri, Annotations.ID );
+					String pid = al.getExtension( Namespace.ALIGNMENT.uri, Annotations.PRETTY );
+					if ( pid == null ) pid = id; else pid = id+" ("+pid+")";
+					msg += "<li><a href=\""+id+"\">"+pid+"</a></li>";
+				    }
+			    msg += "</ul>";
+		    }
+		    */
 	    
 	} else if ( perf.equals("prmretreiveonet") ){
 		
-		
 		String sel = params.getProperty("id");
 	    msg = "<h1>Retrieve ontology network</h1><form action=\"retrieveonet\">";
 	    msg += "Network:  <select name=\"id\">";
@@ -436,7 +476,8 @@ public class HTMLAServProfile implements AlignmentServiceProfile {
 
 	} else if ( perf.equals("") ) {
 		msg = "<h1>Ontology Network commands</h1>";
-		msg += "<form action=\"prmlistonet\"><button title=\"List networks stored in the server\" type=\"submit\">Available networks</button></form>";
+		msg += "<form action=\"../ontonet/listnetwork\"><button title=\"List networks stored in the server\" type=\"submit\">Available networks</button></form>";
+	//	msg += "<form action=\"prmlistonet\"><button title=\"List networks stored in the server\" type=\"submit\">Available networks</button></form>";
 		msg += "<form action=\"prmloadonet\"><button title=\"Load a network from a valid source\" type=\"submit\">Load a network</button></form>";
 		msg += "<form action=\"prmstoreonet\"><button title=\"Store a network in the server\" type=\"submit\">Store network</button></form>";
 		msg += "<form action=\"prmmatchonet\"><button title=\"Match an ontology network\" type=\"submit\">Match network </button></form>";
@@ -455,7 +496,7 @@ public class HTMLAServProfile implements AlignmentServiceProfile {
      * uses the protocol but offers user-targeted interaction
      */
     public String htmlAnswer( String uri, String perf, Properties header, Properties params ) {
-	//logger.trace("HTML[{}]", perf );
+	logger.trace("HTML[{}]", perf );
 	// REST get
 	String msg = "";
 	String eSource = "al";
@@ -512,6 +553,7 @@ public class HTMLAServProfile implements AlignmentServiceProfile {
 		msg += "<li><a href=\""+id+"\">"+pid+"</a></li>";
 	    }
 	    msg += "</ul>";
+	    
 	} else if ( perf.equals("manalignments") ){ // Manage alignments
 	    msg = "<h1>Available alignments</h1><ul compact=\"1\">";
 	    for ( Alignment al : manager.alignments() ) {
diff --git a/src/fr/inrialpes/exmo/align/service/HTTPTransport.java b/src/fr/inrialpes/exmo/align/service/HTTPTransport.java
index fba20953..c6204208 100644
--- a/src/fr/inrialpes/exmo/align/service/HTTPTransport.java
+++ b/src/fr/inrialpes/exmo/align/service/HTTPTransport.java
@@ -319,6 +319,8 @@ public class HTTPTransport {
 
 	if ( oper.equals( "alid" ) ){ // Asks for an alignment by URI
 	    return returnAlignment( uri, returnType );
+	} else if ( oper.equals( "onid" ) ){ // Asks for a network by URI
+		return returnNetwork( uri, returnType );		
 	} else if ( oper.equals( "" ) ) {
 	    // SHOULD BE ASSIGNED TO CONTENT NEGOCIATION AS WELL... (DEFAULT IN SERVERS)
 	    //return serveFile( uri, header, new File("."), true );
@@ -366,6 +368,29 @@ public class HTTPTransport {
 	}
     }
 
+    
+    /**
+     * Returns the network in HTML format
+     */
+    
+    public HTTPResponse returnNetwork( String uri, String mimeType ) {
+    	Properties params = new Properties();
+    	params.setProperty( "id", manager.serverURL()+uri );
+    	if ( returnType == HTTPResponse.MIME_JSON ) { // YES string compared by ==.
+    	    params.setProperty( "method", "fr.inrialpes.exmo.align.impl.renderer.JSONRendererVisitor" );
+    	} else if ( returnType == HTTPResponse.MIME_RDFXML ) {
+    	    params.setProperty( "method", "fr.inrialpes.exmo.align.impl.renderer.RDFRendererVisitor" );
+    	} else {
+    	    params.setProperty( "method", "fr.inrialpes.exmo.align.impl.renderer.HTMLRendererVisitor" );
+    	}
+    	logger.trace( "Bloody Network URI : {}", manager.serverURL()+uri);
+    	Message answer = manager.renderHTMLNetwork(params);
+    	if ( answer instanceof ErrorMsg ) {
+    	    return new HTTPResponse( HTTPResponse.HTTP_NOTFOUND, HTTPResponse.MIME_PLAINTEXT, "Alignment server: unknown network : "+answer.getContent() );
+    	} else {
+    	    return new HTTPResponse( HTTPResponse.HTTP_OK, mimeType, answer.getContent() );
+    	}
+        }
     // ===============================================
     // Util
 
diff --git a/src/fr/inrialpes/exmo/align/service/msg/OntologyNetworkId.java b/src/fr/inrialpes/exmo/align/service/msg/OntologyNetworkId.java
index d4c80c16..c44c7438 100644
--- a/src/fr/inrialpes/exmo/align/service/msg/OntologyNetworkId.java
+++ b/src/fr/inrialpes/exmo/align/service/msg/OntologyNetworkId.java
@@ -56,7 +56,7 @@ public class OntologyNetworkId extends Success {
 	}                                                                                                                                                                                
     };                                                                                                                                                                               
     public String HTMLString(){                                                                                                                                                      
-	//return "Alignment ID: <a href=\"../html/retrieve?method=fr.inrialpes.exmo.align.impl.renderer.HTMLRendererVisitor&id="+getContent()+"\">"+getPretty(getContent())+"</a>&nbsp;";
+	//return "Ontology Network ID: <a href=\"../ontonet/retrieve?method=fr.inrialpes.exmo.align.impl.renderer.HTMLRendererVisitor&id="+getContent()+"\">"+getPretty(getContent())+"</a>&nbsp;";
 	return "Ontology Network ID: <a href=\""+getContent()+"\">"+getPretty(getContent())+"</a>&nbsp;";                                                                                       
     }                                                                                                                                                                                
     public String HTMLRESTString(){                                                                                                                                                  
-- 
GitLab