From 2a1c871b63299dfc79fc3505a3b72d8eccaa75dd Mon Sep 17 00:00:00 2001 From: Chan Le Duc <chan.leduc@univ-paris13.fr> Date: Tue, 5 May 2009 08:27:33 +0000 Subject: [PATCH] --Improved data transfer from server --- .../exmo/align/plugin/neontk/OnlineAlign.java | 280 ++++++++++++++++-- .../align/plugin/neontk/SWTInterface.java | 91 ++++-- 2 files changed, 319 insertions(+), 52 deletions(-) diff --git a/plugins/neon/src/fr/inrialpes/exmo/align/plugin/neontk/OnlineAlign.java b/plugins/neon/src/fr/inrialpes/exmo/align/plugin/neontk/OnlineAlign.java index 3f59907f..6eee68d6 100755 --- a/plugins/neon/src/fr/inrialpes/exmo/align/plugin/neontk/OnlineAlign.java +++ b/plugins/neon/src/fr/inrialpes/exmo/align/plugin/neontk/OnlineAlign.java @@ -28,8 +28,13 @@ import java.io.FileInputStream; //import java.io.FileWriter; import java.io.IOException; //import java.io.InputStream; +import java.io.FileReader; +import java.io.FileWriter; import java.io.InputStreamReader; +import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; +import java.io.PrintWriter; +import java.io.StringWriter; //mport javax.swing.JOptionPane; import javax.swing.ProgressMonitorInputStream; @@ -46,6 +51,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; +import org.semanticweb.owl.align.AlignmentVisitor; import org.semanticweb.owl.align.Parameters; import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -65,7 +71,11 @@ import javax.xml.transform.OutputKeys; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import fr.inrialpes.exmo.align.impl.BasicAlignment; import fr.inrialpes.exmo.align.impl.BasicParameters; +import fr.inrialpes.exmo.align.impl.renderer.RDFRendererVisitor; +import fr.inrialpes.exmo.align.parser.AlignmentParser; +import org.semanticweb.owl.align.AlignmentException; public class OnlineAlign { @@ -127,7 +137,7 @@ public class OnlineAlign { //System.out.println("SOAP loaded align=" + answer ); } catch ( Exception ex ) { ex.printStackTrace(); }; - if(! connected ) return null; + //if(! connected ) return null; Document domMessage = null; try { @@ -168,12 +178,13 @@ public class OnlineAlign { //System.out.println("Message :"+ message); // Send message + System.out.println("Trim Message="+ message); answer = sendMessage( message, params ); - + System.out.println("Trim Align="+ answer); } catch ( Exception ex ) { ex.printStackTrace(); }; - if(! connected ) return null; + //if(! connected ) return null; Document domMessage = null; try { @@ -216,7 +227,7 @@ public class OnlineAlign { answer = sendMessage( message, params ); } catch ( Exception ex ) { ex.printStackTrace(); }; - if(! connected ) return null; + //if(! connected ) return null; Document domMessage = null; try { @@ -259,7 +270,7 @@ public class OnlineAlign { //System.out.println("answer find=" + answer); } catch ( Exception ex ) { ex.printStackTrace(); }; - if(!connected ) return null; + //if(!connected ) return null; Document domMessage = null; try { @@ -304,7 +315,7 @@ public class OnlineAlign { } catch ( Exception ex ) { ex.printStackTrace(); }; - if(! connected ) return null; + //if(! connected ) return null; // Cut SOAP header //answer = "<?xml version='1.0' encoding='utf-8' standalone='no'?>" + answer ; @@ -324,6 +335,57 @@ public class OnlineAlign { } + //Used without ProgressBar + public String getAlignIdMonoThread(String method, String wserver, String wsmethod, String onto1, String onto2) { + + String answer = null ; + + Parameters params = new BasicParameters(); + params.setParameter( "host", HOST ); + params.setParameter( "command","match"); + params.setParameter( "arg1", method); + params.setParameter( "arg2", onto1); + params.setParameter( "arg3", onto2); + params.setParameter( "arg4", wserver); + if(wsmethod != null ) + params.setParameter( "arg5", wsmethod); + + try { + // Read parameters + // Create the SOAP message + String message = createMessage( params ); + + // Send message + answer = sendMessageMonoThread( message, params ); + + //System.out.println("SOAP Match align=" + answer ); + + } + + catch ( Exception ex ) { ex.printStackTrace(); }; + //if(! connected ) return null; + + // Cut SOAP header + //answer = "<?xml version='1.0' encoding='utf-8' standalone='no'?>" + answer ; + Document domMessage = null; + try { + domMessage = BUILDER.parse( new ByteArrayInputStream( answer.getBytes()) ); + + } catch ( IOException ioex ) { + ioex.printStackTrace(); + } catch ( SAXException saxex ) { + saxex.printStackTrace(); + } + + + String result[] = getTagFromSOAP( domMessage, "matchResponse" ); + + //System.out.println("Match align Id=" + result[0]); + + return result[0]; + + } + public void getAlignId(String method, String wserver, String wsmethod, String onto1, String onto2 ) { @@ -420,7 +482,7 @@ public class OnlineAlign { // Send message answer = sendMessage( message, params ); - if(! connected ) return null; + //if(! connected ) return null; } catch ( Exception ex ) { ex.printStackTrace(); }; @@ -497,7 +559,57 @@ public class OnlineAlign { } catch ( Exception ex ) { ex.printStackTrace(); }; } - + public void getRDFAlignmentMonoThread(String alignId) { + + //retrieve alignment for storing in OWL file + + + Parameters params = new BasicParameters(); + params.setParameter( "host", HOST ); + //params.setParameter( "http", PORT ); + //params.setParameter( "wsdl", WSDL ); + params.setParameter( "command","retrieve"); + params.setParameter( "arg1", alignId); + params.setParameter( "arg2", "fr.inrialpes.exmo.align.impl.renderer.RDFRendererVisitor" ); + + String answer=null; + + try { + // Read parameters + //Parameters params = ws.readParameters( aservArgRetrieve ); + + // Create the SOAP message + String message = createMessage( params ); + + //System.out.println("URL SOAP :"+ SOAPUrl + ", Action:"+ SOAPAction); + //System.out.println("Message :" + message); + + // Send message + answer = sendMessageMonoThread( message, params ); + + + } catch ( Exception ex ) { ex.printStackTrace(); }; + + /* + Document domMessage = null; + try { + domMessage = BUILDER.parse( new ByteArrayInputStream( answer.getBytes()) ); + + } catch ( IOException ioex ) { + ioex.printStackTrace(); + } catch ( SAXException saxex ) { + saxex.printStackTrace(); + } + + + String result[] = getTagFromSOAP( domMessage, "retrieveResponse/result/RDF" ); + + globalAnswer = result[0]; + */ + globalAnswer = answer; + + //return result[0]; + } public String storeAlign(String alignId) { @@ -530,7 +642,7 @@ public class OnlineAlign { } catch ( Exception ex ) { ex.printStackTrace() ;}; - if(! connected ) return null; + //if(! connected ) return null; Document domMessage = null; try { @@ -759,6 +871,96 @@ public class OnlineAlign { return message; } + public String sendMessageMonoThread( String message, Parameters param ) { + // Create the connection + + byte[] b = message.getBytes(); + + String answer = ""; + // Create HTTP Request + try { + + URLConnection connection = SOAPUrl.openConnection(); + HttpURLConnection httpConn = (HttpURLConnection) connection; + + httpConn.setRequestProperty( "Content-Length", + String.valueOf( b.length ) ); + httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8"); + + + httpConn.setRequestProperty("SOAPAction",SOAPAction); + httpConn.setRequestMethod( "POST" ); + httpConn.setDoOutput(true); + httpConn.setDoInput(true); + + // Send the request through the connection + OutputStream out = httpConn.getOutputStream(); + + out.write( b ); + out.close( ); + + if( param.getParameter("command").equals("retrieve") ) { + /* + try { + + AlignmentParser parser = new AlignmentParser( 0 ); + parser.initAlignment( null ); + parser.setEmbedded( true ); + BasicAlignment clonedA1 = (BasicAlignment) parser.parse( httpConn.getInputStream() ); + + StringWriter sw = new StringWriter(); + AlignmentVisitor rdfV = new RDFRendererVisitor( new PrintWriter ( sw ) ); + clonedA1.render( rdfV ); + answer = sw.toString(); + + + } catch (SAXException saxex) { + throw new AlignmentException( "Malformed XML/SOAP result ("+saxex.getMessage()+")", saxex ); + } catch (IOException ioex) { + throw new AlignmentException( "XML/SOAP parsing error", ioex ); + } catch (javax.xml.parsers.ParserConfigurationException pcex) { + throw new AlignmentException( "XML/SOAP parsing error", pcex ); + } + + System.out.println("RDF=" + answer ); + */ + InputStreamReader isr = new InputStreamReader(httpConn.getInputStream()); + BufferedReader in = new BufferedReader(isr); + + StringBuffer lineBuff = new StringBuffer(); + String line; + while ((line = in.readLine()) != null) { + lineBuff.append(line + "\n"); + } + if (in != null) + in.close(); + answer = lineBuff.toString(); + + //System.out.println("RDF=" + answer ); + } + else { + // Read the response and write it to standard output + InputStreamReader isr = new InputStreamReader(httpConn.getInputStream()); + BufferedReader in = new BufferedReader(isr); + + String line; + while ((line = in.readLine()) != null) { + answer += line + "\n"; + } + if (in != null) + in.close(); + + if(httpConn.HTTP_REQ_TOO_LONG == httpConn.getResponseCode()) System.err.println("Request too long"); + } + + + } catch (Exception ex) { + connected= false; ex.printStackTrace() ; return null; + } + + return answer; + } + public String sendMessage( String message, Parameters param ) { // Create the connection @@ -790,6 +992,8 @@ public class OnlineAlign { if( param.getParameter("command").equals("match") ) { globalAnswer = ""; + + Thread th = new Thread() { public void run() { @@ -803,14 +1007,15 @@ public class OnlineAlign { BufferedReader in = new BufferedReader(isr); String line; - String alignRes = ""; - + //String alignRes = ""; + StringBuffer strBuff = new StringBuffer(); while ((line = in.readLine()) != null) { - alignRes += line + "\n"; + //alignRes += line + "\n"; + strBuff.append(line + "\n"); } - String alignId = getAlignIdParsed( alignRes ); - + //String alignId = getAlignIdParsed( alignRes ); + String alignId = getAlignIdParsed( strBuff.toString() ); if(alignId == null || alignId.equals("")) { JOptionPane.showMessageDialog(null, "Alignment is not produced.","Warning",2); SWTInterface.set3Buttons(); @@ -863,9 +1068,10 @@ public class OnlineAlign { //String line; int co = 0; - + StringBuffer strBuff2 = new StringBuffer(); while ((line = in2.readLine()) != null) { - globalAnswer += line + "\n"; + strBuff2.append( line + "\n"); + //globalAnswer += line + "\n"; co += line.length(); pm.setNote(co + " bytes read."); if( co >= maxSize -10000 ) { @@ -874,6 +1080,7 @@ public class OnlineAlign { pm.setProgress( co ); } } + globalAnswer = strBuff2.toString(); SWTInterface.resetActionButtons( true ); @@ -885,8 +1092,7 @@ public class OnlineAlign { SWTInterface.set3Buttons(); } e.printStackTrace(); - } - + } } }; @@ -916,9 +1122,11 @@ public class OnlineAlign { String line; int co = 0; - //int coL = 0; + StringBuffer strBuff3 = new StringBuffer(); + while ((line = in.readLine()) != null) { - globalAnswer += line + "\n"; + //globalAnswer += line + "\n"; + strBuff3.append( line + "\n" ); co += line.length(); pm.setNote(co + " bytes read."); if( co >= maxSize -10000 ) { @@ -927,7 +1135,7 @@ public class OnlineAlign { pm.setProgress( co ); } } - + globalAnswer = strBuff3.toString(); SWTInterface.resetActionButtons( true ); pm.setProgress( maxSize - 1 ); @@ -949,10 +1157,12 @@ public class OnlineAlign { InputStreamReader isr = new InputStreamReader( httpConn.getInputStream() ); BufferedReader in = new BufferedReader( isr ); String line; + StringBuffer strBuff4 = new StringBuffer(); while ((line = in.readLine()) != null) { - answer += line + "\n"; + strBuff4.append( line + "\n" ); + //answer += line + "\n"; } - + answer = strBuff4.toString(); in.close(); } //System.out.println("answer= " + answer); @@ -1047,4 +1257,28 @@ public class OnlineAlign { return answer; } + public static String fileToString(File f){ + String texto = ""; + StringBuffer toto = new StringBuffer(); + int i=0; + try{ + + FileReader rd = new FileReader(f); + i = rd.read(); + + while(i!=-1){ + //texto = texto+(char)i; + toto.append((char)i); + i = rd.read(); + } + + } catch(IOException e){ + System.err.println(e.getMessage()); + } + + return toto.toString(); + } + + + } diff --git a/plugins/neon/src/fr/inrialpes/exmo/align/plugin/neontk/SWTInterface.java b/plugins/neon/src/fr/inrialpes/exmo/align/plugin/neontk/SWTInterface.java index 0118924c..455f1a0b 100644 --- a/plugins/neon/src/fr/inrialpes/exmo/align/plugin/neontk/SWTInterface.java +++ b/plugins/neon/src/fr/inrialpes/exmo/align/plugin/neontk/SWTInterface.java @@ -155,7 +155,7 @@ public class SWTInterface extends JPanel { public static Hashtable<String,Alignment> alignmentTable = new Hashtable<String,Alignment>(); static String [] forUniqueness = new String[0]; static int alignId = 0; - + JComponent phrases; JTextField fileName1, fileName2, hostField, portField, serverField, methodField; @@ -163,11 +163,11 @@ public class SWTInterface extends JPanel { JEditorPane htmlView; - JButton cancelButton, discardButton, fetchButton, resButton, ontoRefresh, + JButton cancelButton, discardButton, resButton, ontoRefresh, localAlignImportButton, alignUploadButton, localAlignTrimButton, connButton, goButton, offlineButton, onlineButton; - static JButton alignStoreButton, alignImportButton, serverAlignTrimButton, mapButton, alignFindButton, allAlignButton; + static JButton alignStoreButton, alignImportButton, serverAlignTrimButton, mapButton, alignFindButton, allAlignButton, fetchButton; JDialog connDialog, matchDialog; //JPanel pane2; @@ -212,11 +212,8 @@ public class SWTInterface extends JPanel { public File ontoFolder = null; public File alignFolder = null; public static File basicFolder = null; - public Frame rootFrame; - //public SWTInterface(Frame f) { - //It is needed - // rootFrame = f; - //} + + public void offlineInit(boolean init){ //OntologyFactory.setDefaultFactory("fr.inrialpes.exmo.align.onto.owlapi2.OWLAPI2OntologyFactory"); online = false; @@ -349,9 +346,9 @@ public class SWTInterface extends JPanel { alignImportButton.setEnabled( flag ); alignStoreButton.setEnabled( flag ); serverAlignTrimButton.setEnabled( flag ); - mapButton.setEnabled( flag ); - alignFindButton.setEnabled( flag ); - allAlignButton.setEnabled( flag ); + //mapButton.setEnabled( flag ); + //alignFindButton.setEnabled( flag ); + //allAlignButton.setEnabled( flag ); } @@ -361,6 +358,17 @@ public class SWTInterface extends JPanel { allAlignButton.setEnabled( true ); } + public static void setFetchButton( boolean flag) { + fetchButton.setEnabled( flag ); + //alignFindButton.setEnabled( flag ); + //allAlignButton.setEnabled( flag ); + alignImportButton.setEnabled( !flag ); + alignStoreButton.setEnabled( !flag ); + serverAlignTrimButton.setEnabled( !flag ); + } + + + public void initialize() { //selectedHost = "aserv.inrialpes.fr"; @@ -688,12 +696,22 @@ public class SWTInterface extends JPanel { JOptionPane.showMessageDialog(null, "Choose an alignment ID from list!","Warning",2); return; } - + /* alignIdList = new String[1]; alignIdList[0] = selectedAlign; alignBox.removeAllItems(); alignBox.addItem(selectedAlign); onAlign.getRDFAlignment( selectedAlign ); + */ + + JOptionPane.showMessageDialog(null, "This operation may take a while.","Warning",2); + alignIdList = new String[1]; + alignIdList[0] = selectedAlign; + alignBox.removeAllItems(); + alignBox.addItem(selectedAlign); + onAlign.getRDFAlignmentMonoThread( selectedAlign ); + setFetchButton( false ); + } }; }); @@ -726,7 +744,7 @@ public class SWTInterface extends JPanel { alignIdList = new String[1]; alignIdList[0] = selectedAlign; alignBox.removeAllItems(); - alignBox.addItem(alignIdList[0]); + alignBox.addItem( alignIdList[0] ); String rdfPath = alignKey + ".rdf"; @@ -736,8 +754,7 @@ public class SWTInterface extends JPanel { try { File rdfFile = new File( rdfPath ); - //if (rdfFile.exists()) rdfFile.delete(); - + out = new FileWriter( rdfFile ); out.write( rdfalignStr ); out.flush(); @@ -753,7 +770,7 @@ public class SWTInterface extends JPanel { String[] list = offAlign.getAllAlign( ); - if(list!=null) { + if(list != null) { localAlignIdList = new String[1]; localAlignBox.removeAllItems(); @@ -882,9 +899,7 @@ public class SWTInterface extends JPanel { if(alignIdList.length > 0) { selectedAlign = alignIdList[0]; } - - - + String[] list1 = offAlign.getAllAlign( ); if(list1!=null) { localAlignIdList = new String[list1.length]; @@ -940,7 +955,7 @@ public class SWTInterface extends JPanel { String[] list = onAlign.findAlignForOntos(selectedOnto1, selectedOnto2); if(list == null || list.length==0) - JOptionPane.showMessageDialog(null, "Impossible connection!","Warning",2); + JOptionPane.showMessageDialog(null, "No alignment is found!","Warning",2); else { //String[] list = getResultsFromAnswer(ao, "alid", null ); @@ -985,7 +1000,7 @@ public class SWTInterface extends JPanel { String at = onAlign.trimAlign(selectedAlign, thres); if(at == null || at.equals("")) - JOptionPane.showMessageDialog(null, "Impossible connection!","Warning",2); + JOptionPane.showMessageDialog(null, "No alignment is obtained!","Warning",2); else { alignIdList = new String[1]; @@ -1084,17 +1099,27 @@ public class SWTInterface extends JPanel { public void actionPerformed(ActionEvent e) { if (e.getSource() == goButton) { wserver = serverField.getText(); - if(wserver.equals("")) wserver = null; + if(wserver.equals("")) + wserver = null; wsmethod = methodField.getText(); - if(wsmethod.equals("")) wsmethod = null; + if(wsmethod.equals("")) + wsmethod = null; matchDialog.dispose(); resetActionButtons( false ); - onAlign.getAlignId( matchMethod, wserver, wsmethod, selectedOnto1, selectedOnto2 ); - + //onAlign.getAlignId( matchMethod, wserver, wsmethod, selectedOnto1, selectedOnto2 ); + + String alignId = onAlign.getAlignIdMonoThread( matchMethod, wserver, wsmethod, selectedOnto1, selectedOnto2 ); + //System.out.println("match from sji finished "); + alignIdList = new String[1]; + alignIdList[0] = alignId; + selectedAlign = alignIdList[0]; + alignBox.removeAllItems(); + alignBox.addItem(selectedAlign); + setFetchButton( true ); + } // getsource - }; }); @@ -1137,8 +1162,16 @@ public class SWTInterface extends JPanel { matchDialog.setVisible(true); } else { resetActionButtons( false ); - onAlign.getAlignId( matchMethod, wserver, wsmethod, selectedOnto1, selectedOnto2 ); + String alignId = onAlign.getAlignIdMonoThread( matchMethod, wserver, wsmethod, selectedOnto1, selectedOnto2 ); + //onAlign.getAlignId( matchMethod, wserver, wsmethod, selectedOnto1, selectedOnto2 ); + alignIdList = new String[1]; + alignIdList[0] = alignId; + selectedAlign = alignIdList[0]; + alignBox.removeAllItems(); + alignBox.addItem(selectedAlign); + setFetchButton( true ); } + //String answer = onAlign.getAlignId( matchMethod, selectedOnto1, selectedOnto2 ); //if(answer==null || answer.equals("")) { // JOptionPane.showMessageDialog(null, "Alignment is not produced.","Warning",2); @@ -1222,7 +1255,7 @@ public class SWTInterface extends JPanel { this.setLayout (new BorderLayout()); this.add (new JLabel ("Computing and managing ontology alignments"), BorderLayout.NORTH); - phrases = createPhraseList(rootFrame); + phrases = createPhraseList( ); phrases.setBorder(BorderFactory.createEmptyBorder (10, 5, 10, 5) ); JScrollPane top = new JScrollPane( phrases ); @@ -1242,7 +1275,7 @@ public class SWTInterface extends JPanel { } -private JPanel createPhraseList (Frame root) { +private JPanel createPhraseList ( ) { JPanel phrasePane = new JPanel (new GridLayout (0, 1, 0, 10)); -- GitLab