Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package fr.inrialpes.exmo.align.service;
import java.util.*;
import java.net.URI;
import java.sql.Connection;
import java.sql.DriverManager;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import org.semanticweb.owl.align.Alignment;
import org.semanticweb.owl.align.Cell;
import org.semanticweb.owl.io.owl_rdf.OWLRDFParser;
import org.semanticweb.owl.util.OWLManager;
import org.semanticweb.owl.model.OWLEntity;
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;
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/DBService";
Statement st = null;
ResultSet rs = null;
OWLOntology O1 = null;
OWLOntology O2 = null;
String type = null;
String level = null;
String s_O1 = "";
String s_O2 = "";
String s_File1 = "";
String s_File2 = "";
String s_uri1 = "";
String s_uri2 = "";
String s_method = "";
String s_cell = "";
static Hashtable cache = null;
public DBServiceImpl() {
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch(Exception e){
System.out.println(e.toString());
}
}
public int connect(String password){
try {
conn = DriverManager.getConnection(url, "root", password);
st = (Statement) conn.createStatement();
cache = loadAll(true);
} catch(Exception ex) {return -1;}
return 1;
}
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;
id = nextID();
try {
O1 = (OWLOntology) alignment.getOntology1();
O2 = (OWLOntology) alignment.getOntology2();
s_O1 = O1.getLogicalURI().toString();
s_O2 = O2.getLogicalURI().toString();
if (alignment.getFile1() != null) s_File1 = alignment.getFile1().toString();
if (alignment.getFile2() != null) s_File2 = alignment.getFile2().toString();
s_uri1 = O1.getPhysicalURI().toString();
s_uri2 = O2.getPhysicalURI().toString();
type = alignment.getType();
level = alignment.getLevel();
query = "insert into alignment " +
"(id, owlontology1, owlontology2, type, level, file1, file2, uri1, uri2) " +
"values (" + id + ",'" + s_O1 + "','" + s_O2 + "','" + type + "','" + level + "','" + s_File1 + "','" + s_File2 + "','" + s_uri1 + "','" + s_uri2 + "')";
st.executeUpdate(query);
for( Enumeration e = alignment.getExtensions().getNames() ; e.hasMoreElements() ; ){
String tag = (String)e.nextElement();
s_method = alignment.getExtension(tag);
query = "insert into extension " +
"(id, tag, method) " +
"values (" + id + ",'" + tag + "','" + s_method + "')";
st.executeUpdate(query);
}
for( Enumeration e = alignment.getElements() ; e.hasMoreElements(); ){
Cell c = (Cell)e.nextElement();
String temp[] = new String[10];
try {
if ( ((OWLEntity)c.getObject1()).getURI() != null && ((OWLEntity)c.getObject2()).getURI() != null ){
if ( c.getId() != null ){
temp[0] = c.getId();
}
else temp[0] = "";
temp[1] = ((OWLEntity)c.getObject1()).getURI().toString();
temp[2] = ((OWLEntity)c.getObject2()).getURI().toString();
temp[3] = c.getStrength() + "";
if ( !c.getSemantics().equals("first-order") )
temp[4] = c.getSemantics();
else temp[4] = "";
temp[5] = ((BasicRelation)c.getRelation()).getRelation();
query = "insert into cell " +
"(id, cell_id, uri1, uri2, measure, semantics, relation) " +
"values (" + id + ",'" + temp[0] + "','" + temp[1] + "','" + temp[2] + "','" + temp[3] + "','" + temp[4] + "','" + temp[5] + "')";
st.executeUpdate(query);
}
} catch ( OWLException ex) { System.out.println( "getURI problem" + ex.toString() ); }
}
}
catch (Exception e) {System.out.println(e.toString());}
return id;
}
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);
}
}
}
} catch (Exception e) {System.out.println("Alignment Finding Exception");}
return result;
}
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();
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;
}
public synchronized long nextID(){
long id = 0;
try {
st.executeUpdate("insert into id_seq (aa) values ('a')");
rs = (ResultSet) st.executeQuery("select max(id) from id_seq");
//System.out.println(rs.toString());
while(rs.next()) {
id = rs.getInt(1);
}
//id = rs.getBigDecimal(1).longValue();
} catch(Exception ex){
System.out.println(ex.toString());
return -1;
}
return id;
}
public int open(String id, String password){
try{
String userid = null;;
String pwd;
userid = id;
pwd = password;
conn = DriverManager.getConnection(url, userid, pwd);
} catch(Exception ex){System.out.println(ex.toString());}
return 1;
}
public int close(){
try {
conn.close();
st.close();
rs.close();
return 1;
} catch (Exception ex) {
return -1;
}
}
public static OWLOntology loadOntology(URI uri) throws Exception {
OWLRDFParser parser = new OWLRDFParser();
parser.setConnection(OWLManager.getOWLConnection());
return parser.parseOntology(uri);
}
}