diff --git a/src/fr/inrialpes/exmo/align/service/Cache.java b/src/fr/inrialpes/exmo/align/service/Cache.java index 7b9fae83a98aa2788e176954169e06ad87c27b20..58851e038df7849c9b535cbc86f9af2340aced24 100644 --- a/src/fr/inrialpes/exmo/align/service/Cache.java +++ b/src/fr/inrialpes/exmo/align/service/Cache.java @@ -1,12 +1,32 @@ +/* + * $Id$ + * + * Copyright (C) XX, 2006 + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + package fr.inrialpes.exmo.align.service; import java.net.URI; -import java.util.Enumeration; +import java.util.Vector; import org.semanticweb.owl.align.Alignment; public interface Cache { - int put(long id, URI uri1, URI uri2, Alignment alignmnet); + int loading(); Alignment get(long id); - Enumeration get(URI uri); - Enumeration get(URI uri1, URI uri2); + Vector get(URI uri); + Vector get(URI uri1, URI uri2); } diff --git a/src/fr/inrialpes/exmo/align/service/CacheImpl.java b/src/fr/inrialpes/exmo/align/service/CacheImpl.java index 35633b9c36c76efa249a48bcf8032110d4fc68d2..8e18f7b42f6ca4e3dc6624095885534e39e8f82c 100644 --- a/src/fr/inrialpes/exmo/align/service/CacheImpl.java +++ b/src/fr/inrialpes/exmo/align/service/CacheImpl.java @@ -1,40 +1,240 @@ +/* + * $Id$ + * + * Copyright (C) XX, 2006 + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + package fr.inrialpes.exmo.align.service; import java.net.URI; +import java.sql.Connection; import java.sql.ResultSet; -import java.util.Enumeration; +import java.sql.Statement; import java.util.Hashtable; import java.util.Vector; import org.semanticweb.owl.align.Alignment; +import org.semanticweb.owl.align.Cell; +import org.semanticweb.owl.io.owl_rdf.OWLRDFParser; +import org.semanticweb.owl.model.OWLEntity; +import org.semanticweb.owl.model.OWLOntology; +import org.semanticweb.owl.util.OWLManager; + +import fr.inrialpes.exmo.align.impl.BasicAlignment; public class CacheImpl implements Cache { Hashtable alignmentTable = null; + Statement st = null; + ResultSet rs = null; + Connection conn = null; - public CacheImpl() { - + final int CONNECTION_ERROR = 1; + final int SUCCESS = 2; + final int INIT_ERROR = 3; + + public CacheImpl(Connection conn) { + try { + this.conn = conn; + st = (Statement) conn.createStatement(); + } catch(Exception e) {System.out.println(e.toString());} } - private Hashtable loadAll(boolean force){ - Hashtable result = null; - return result; + public int loading() { + alignmentTable = new Hashtable(); + if(loadAlignment(true)) return SUCCESS; + else return INIT_ERROR; + } + + private boolean loadAlignment(boolean force){ + String query = null; + String id = null; + Alignment alignment = null; + Vector idInfo = new Vector(); + + if (force) { + try { + query = "select id " + "from alignment"; + + rs = (ResultSet) st.executeQuery(query); + System.out.println("1234"); + while(rs.next()) { + id = rs.getString("id"); + idInfo.add(id); + System.out.println(id); + } + + for( int i = 0; i < idInfo.size(); i ++ ) { + id = (String) idInfo.get(i); + System.out.println("1"); + alignment = retrieve(Long.parseLong(id)); + System.out.println("2"); + alignmentTable.put(id, alignment); + System.out.println("3"); + } + } catch (Exception e) {System.out.println("Hashtable loading error1");} + return true; + } + else return false; } - public int put(long id, URI uri1, URI uri2, Alignment alignmnet) { + protected Alignment retrieve(long id){ + OWLOntology o1 = null; + OWLOntology o2 = null; + String query; + String tag; + String method; + OWLEntity ent1 = null, ent2 = null; + Cell cell = null; + + Alignment result = new BasicAlignment(); - return 0; + try { + query = "select * " + + "from alignment " + + "where id = " + id; + rs = (ResultSet) st.executeQuery(query); + + while(rs.next()) { + o1 = loadOntology(new URI(rs.getString("uri1"))); + o2 = loadOntology(new URI(rs.getString("uri2"))); + + result.setLevel(rs.getString("level")); + result.setType(rs.getString("type")); + result.setOntology1(o1); + result.setOntology2(o2); + } + + query = "select * " + + "from extension " + + "where id = " + id; + + rs = (ResultSet) st.executeQuery(query); + + while(rs.next()) { + tag = rs.getString("tag"); + method = rs.getString("method"); + result.setExtension(tag, method); + } + + query = "select * " + + "from cell " + + "where id = " + id; + rs = (ResultSet) st.executeQuery(query); + + while(rs.next()) { + ent1 = (OWLEntity) o1.getClass(new URI(rs.getString("uri1"))); + ent2 = (OWLEntity) o2.getClass(new URI(rs.getString("uri2"))); + + if(ent1 == null || ent2 == null) break; + cell = result.addAlignCell(ent1, ent2, rs.getString("relation"), Double.parseDouble(rs.getString("measure"))); + cell.setId(rs.getString("cell_id")); + cell.setSemantics(rs.getString("semantics")); + } + } catch (Exception e) { + System.out.println("No problem"); + System.out.println(e.toString()); + return null; + } + return result; } + + private boolean loadOne(long id, Alignment alignment, boolean force){ + if (force) { + try { + alignmentTable.put(Long.toString(id), alignment); + } catch (Exception e) {System.out.println("Hashtable loading error2");} + return true; + } + else return false; + } + public Alignment get(long id) { - Alignment result = null; + Alignment result = null; + String query = null; + + try { + result = (Alignment) alignmentTable.get(Long.toString(id)); + + if (result == null) { + query = "select id " + "from alignment " + "where id = " + id; + + rs = (ResultSet) st.executeQuery(query); + + if(rs == null) return null; + else { + while (rs.next()) { + result = retrieve(id); + loadOne(id, result, true); + } + } + } + } catch (Exception e) {System.out.println("Alignment Finding Exception1");} return result; } - public Enumeration get(URI uri) { - Enumeration result = null; + + public Vector get(URI uri) { + Vector result = new Vector(); + + Alignment temp = null; + String query = null; + + try { + query = "select id " + "from alignment " + "where uri1 = \'" + uri + "\' OR uri2 = \'" + uri + "\'"; + System.out.println(query); + rs = (ResultSet) st.executeQuery(query); + + if(rs == null) return null; + else { + while (rs.next()) { + System.out.println("1234"); + temp = get(Long.parseLong(rs.getString("id"))); + result.add(temp); + } + } + } catch (Exception e) {System.out.println("Alignment Finding Exception2");} return result; } - public Enumeration get(URI uri1, URI uri2) { - Enumeration result = null; + + public Vector get(URI uri1, URI uri2) { + Vector result = new Vector(); + + Alignment temp = null; + String query = null; + + try { + query = "select id " + "from alignment " + "where uri1 = \'" + uri1 + "\' AND uri2 = \'" + uri2 + "\'"; + + rs = (ResultSet) st.executeQuery(query); + + if(rs == null) return null; + else { + while (rs.next()) { + temp = get(Long.parseLong(rs.getString("id"))); + result.add(temp); + } + } + } catch (Exception e) {System.out.println("Alignment Finding Exception3");} return result; } + + public static OWLOntology loadOntology(URI uri) throws Exception { + OWLRDFParser parser = new OWLRDFParser(); + parser.setConnection(OWLManager.getOWLConnection()); + return parser.parseOntology(uri); + } } diff --git a/src/fr/inrialpes/exmo/align/service/DBService.java b/src/fr/inrialpes/exmo/align/service/DBService.java index 8740c299abda5468b309a8ed57cfc950733e9898..fd55ae476f20d836562e69055c659a35aaac9ef2 100644 --- a/src/fr/inrialpes/exmo/align/service/DBService.java +++ b/src/fr/inrialpes/exmo/align/service/DBService.java @@ -21,7 +21,7 @@ package fr.inrialpes.exmo.align.service; import java.net.URI; -import java.util.Enumeration; +import java.util.Vector; import org.semanticweb.owl.align.Alignment; @@ -30,7 +30,7 @@ public interface DBService { public int connect(String password); // password in database public int connect(String IPAdress, String id, String password); // with userID, password in database public Alignment find(long id); // find alignment with alignmentID - public Enumeration find(URI uri); // find alignment list with an ontology uri - public Enumeration find(URI uri1, URI uri2); // find alignment list with two ontology uri + public Vector find(URI uri); // find alignment list with an ontology uri + public Vector find(URI uri1, URI uri2); // find alignment list with two ontology uri public int close(); } diff --git a/src/fr/inrialpes/exmo/align/service/DBServiceImpl.java b/src/fr/inrialpes/exmo/align/service/DBServiceImpl.java index 12ec27f97cf5e4b0df35844985b698fa9353dbc1..a26763b55f9e8adc8d3a0cd94421ca088f112e91 100644 --- a/src/fr/inrialpes/exmo/align/service/DBServiceImpl.java +++ b/src/fr/inrialpes/exmo/align/service/DBServiceImpl.java @@ -37,9 +37,6 @@ import org.semanticweb.owl.model.OWLOntology; import org.semanticweb.owl.model.OWLException; import fr.inrialpes.exmo.align.impl.BasicRelation; -import fr.inrialpes.exmo.align.impl.BasicAlignment; - -import java.util.Hashtable; public class DBServiceImpl implements DBService{ int id = 0; @@ -70,7 +67,7 @@ public class DBServiceImpl implements DBService{ public DBServiceImpl() { try{ - Class.forName("com.mysql.jdbc.Driver").newInstance(); + Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch(Exception e){ System.out.println(e.toString()); } @@ -84,7 +81,6 @@ public class DBServiceImpl implements DBService{ if(init()) return SUCCESS; else return INIT_ERROR; - } public int connect(String IPAddress, String id, String password) { @@ -93,57 +89,24 @@ public class DBServiceImpl implements DBService{ st = (Statement) conn.createStatement(); } catch(Exception ex) {return CONNECTION_ERROR;} - if(init()) return SUCCESS; + if(init()) { + System.out.println("init success"); + return SUCCESS; + } else return INIT_ERROR; } private boolean init() { try { - cache = new CacheImpl(); - return true; + cache = new CacheImpl(conn); + if( cache.loading() == SUCCESS ) { + System.out.println("chche loading success"); + return true; + } + else return false; } catch (Exception e) {return false;} } - private Hashtable loadAll(boolean force){ - Hashtable result = new Hashtable(); - String query = null; - String id = null; - Alignment alignment = null; - Vector v = new Vector(); - - if (force) { - try { - query = "select id " + "from alignment"; - - rs = (ResultSet) st.executeQuery(query); - - while(rs.next()) { - id = rs.getString("id"); - v.add(id); - } - - for( int i = 0; i < v.size(); i ++ ) { - id = (String) v.get(i); - alignment = retrieve(Long.parseLong(id)); - result.put(id,alignment); - } - - } catch (Exception e) {System.out.println("Hashtable loading error");} - return result; - } - else return null; - } - - private boolean loadOne(long id, Alignment alignment, boolean force){ - if (force) { - try { - cache.put(Long.toString(id), alignment); - } catch (Exception e) {System.out.println("Hashtable loading error");} - return true; - } - else return false; - } - public long store(Alignment alignment){ long id = 0; String query = null; @@ -208,119 +171,27 @@ public class DBServiceImpl implements DBService{ public Alignment find(long id) { Alignment result = null; - String query = null; try { - result = (Alignment) cache.get(Long.toString(id)); - - if (result == null) { - query = "select id " + "from alignment " + "where id = " + id; - - rs = (ResultSet) st.executeQuery(query); - - if(rs == null) return null; - else { - while (rs.next()) { - result = retrieve(id); - loadOne(id, result, true); - } - } - } - + result = (Alignment) cache.get(id); } catch (Exception e) {System.out.println("Alignment Finding Exception");} return result; } - public Enumeration find(URI uri) { - Enumeration result = null; - Alignment temp = null; - String query = null; + public Vector find(URI uri) { // find alignment list with an ontology uri + Vector result = null; try { - result = (Enumeration) cache.get(uri); - - - if (result == null) { - query = "select id " + "from alignment " + "where id = " + id; - - rs = (ResultSet) st.executeQuery(query); - - if(rs == null) return null; - else { - while (rs.next()) { - temp = retrieve(id); - loadOne(id, temp, true); - } - } - } - + result = (Vector) cache.get(uri); } catch (Exception e) {System.out.println("Alignment Finding Exception");} return result; - } // find alignment list with an ontology uri - - public Enumeration find(URI uri1, URI uri2) { - Enumeration result = null; - return result; - } // find alignment list with two ontology uri + } - protected Alignment retrieve(long id){ - OWLOntology o1 = null; - OWLOntology o2 = null; - String query; - String tag; - String method; - OWLEntity ent1 = null, ent2 = null; - Cell cell = null; - - Alignment result = new BasicAlignment(); - + public Vector find(URI uri1, URI uri2) { // find alignment list with two ontology uri + Vector result = null; try { - query = "select * " + - "from alignment " + - "where id = " + id; - rs = (ResultSet) st.executeQuery(query); - - while(rs.next()) { - o1 = loadOntology(new URI(rs.getString("uri1"))); - o2 = loadOntology(new URI(rs.getString("uri2"))); - - result.setLevel(rs.getString("level")); - result.setType(rs.getString("type")); - result.setOntology1(o1); - result.setOntology2(o2); - } - - query = "select * " + - "from extension " + - "where id = " + id; - - rs = (ResultSet) st.executeQuery(query); - - while(rs.next()) { - tag = rs.getString("tag"); - method = rs.getString("method"); - result.setExtension(tag, method); - } - - query = "select * " + - "from cell " + - "where id = " + id; - rs = (ResultSet) st.executeQuery(query); - - while(rs.next()) { - ent1 = (OWLEntity) o1.getClass(new URI(rs.getString("uri1"))); - ent2 = (OWLEntity) o2.getClass(new URI(rs.getString("uri2"))); - - if(ent1 == null || ent2 == null) break; - cell = result.addAlignCell(ent1, ent2, rs.getString("relation"), Double.parseDouble(rs.getString("measure"))); - cell.setId(rs.getString("cell_id")); - cell.setSemantics(rs.getString("semantics")); - } - } catch (Exception e) { - System.out.println("No problem"); - System.out.println(e.toString()); - return null; - } - return result; - } + result = (Vector) cache.get(uri1, uri2); + } catch (Exception e) {System.out.println("Alignment Finding Exception");} + return result; + } public synchronized long nextID(){ long id = 0;