1 | package hozo.sparql.gui;
|
---|
2 |
|
---|
3 | import hozo.maptool.MapFactory;
|
---|
4 |
|
---|
5 | import java.awt.BorderLayout;
|
---|
6 | import java.awt.GridLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.*;
|
---|
12 |
|
---|
13 | import hozo.sparql.EndpointSettingsManager;
|
---|
14 | import hozo.sparql.SparqlQueryListener;
|
---|
15 |
|
---|
16 | import org.biohackathon.SPARQLBuilder.OWL.*;
|
---|
17 |
|
---|
18 | /*
|
---|
19 | import org.biohackathon.SPARQLBuilder.OWL.ClassLink;
|
---|
20 | import org.biohackathon.SPARQLBuilder.OWL.Direction;
|
---|
21 | import org.biohackathon.SPARQLBuilder.OWL.Instance;
|
---|
22 | import org.biohackathon.SPARQLBuilder.OWL.OWLQueryBuilder;
|
---|
23 | import org.biohackathon.SPARQLBuilder.OWL.OWLQueryBuilderImpl;
|
---|
24 | import org.biohackathon.SPARQLBuilder.OWL.Path;
|
---|
25 | import org.biohackathon.SPARQLBuilder.OWL.SClass;
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | public class SparqlBuilderPanel extends JPanel{
|
---|
30 |
|
---|
31 | JTextField jtf_start_class;
|
---|
32 | JTextField jtf_end_class;
|
---|
33 | JTextField jtf_mode;
|
---|
34 | JCheckBox jcb_count_ins;
|
---|
35 |
|
---|
36 | JTextArea jta_query;
|
---|
37 | MapFactory map;
|
---|
38 | SparqlSearchPanel search_panel;
|
---|
39 | //OWLQueryBuilderImpl builder;
|
---|
40 | QueryPathGenerator generator;
|
---|
41 |
|
---|
42 | // public SparqlBuilderPanel(SparqlAccessorForm parent) {
|
---|
43 | // super(parent);
|
---|
44 | // initialize();
|
---|
45 | // this.parent = parent;
|
---|
46 | // }
|
---|
47 | /*
|
---|
48 | public OWLQueryBuilderImpl getOWLQueryBuilderImpl(){
|
---|
49 | makeOWLQueryBuilderImpl();
|
---|
50 |
|
---|
51 | return builder;
|
---|
52 | }*/
|
---|
53 |
|
---|
54 | public QueryPathGenerator getQueryPathGenerator(){
|
---|
55 | if ( generator == null ){
|
---|
56 | makeQueryPathGenerator();
|
---|
57 | }
|
---|
58 | return generator;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /* SparqlBuilderPanel(SparqlAccessorForm sparqlAccessorForm){
|
---|
62 | super();
|
---|
63 |
|
---|
64 | this.setLayout(new BorderLayout());
|
---|
65 | JPanel jp_top = new JPanel();
|
---|
66 | jtf_start_class = new JTextField("http://dbpedia.org/ontology/Artist");
|
---|
67 | jp_top.add(new JLabel("Start Class:"));
|
---|
68 | jp_top.add(jtf_start_class);
|
---|
69 | JButton jb_start = new JButton("Select");
|
---|
70 | jp_top.add(jb_start);
|
---|
71 |
|
---|
72 | jtf_end_class = new JTextField("http://dbpedia.org/ontology/Award");
|
---|
73 | jp_top.add(new JLabel("End Class:"));
|
---|
74 | jp_top.add(jtf_end_class);
|
---|
75 | JButton jb_end = new JButton("Select");
|
---|
76 | jp_top.add(jb_end);
|
---|
77 |
|
---|
78 | JButton jb_get_path = new JButton("Get Path");
|
---|
79 | jp_top.add(jb_get_path);
|
---|
80 |
|
---|
81 | map = new MapFactory();
|
---|
82 | map.setSPARQLbuilder(this);
|
---|
83 |
|
---|
84 |
|
---|
85 | jb_get_path.addActionListener(new ActionListener() {
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public void actionPerformed(ActionEvent e) {
|
---|
89 | System.out.println("getPaths:::>>>>"+jtf_start_class.getText()
|
---|
90 | +"<===>"+jtf_end_class.getText());
|
---|
91 | //map.loadPathList(map.getDummyPathList());
|
---|
92 |
|
---|
93 | try {
|
---|
94 | System.out.println("getPaths:::>>>>"+jtf_start_class.getText()
|
---|
95 | +"<===>"+jtf_end_class.getText());
|
---|
96 | OWLQueryBuilderImpl builder = makeOWLQueryBuilderImpl();
|
---|
97 | Path[] path = builder.getPaths(jtf_start_class.getText(), jtf_end_class.getText());
|
---|
98 | System.out.println("RESULT:::>>>>"+path.length);
|
---|
99 |
|
---|
100 | map.loadPathList(path);
|
---|
101 | //jta_query.setText(createSPARQL(path[0]));
|
---|
102 |
|
---|
103 | } catch (Exception e1) {
|
---|
104 | // TODO Auto-generated catch block
|
---|
105 | e1.printStackTrace();
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 |
|
---|
110 | }
|
---|
111 | });
|
---|
112 |
|
---|
113 | this.add(jp_top, BorderLayout.NORTH);
|
---|
114 | jta_query = new JTextArea();
|
---|
115 | this.add(new JScrollPane(jta_query), BorderLayout.CENTER);
|
---|
116 | JPanel jp_bottom = new JPanel();
|
---|
117 | jp_bottom.add(new JButton("make query"));
|
---|
118 | this.add(jp_bottom, BorderLayout.SOUTH);
|
---|
119 | }*/
|
---|
120 |
|
---|
121 |
|
---|
122 | SparqlBuilderPanel(SparqlSearchPanel panel){
|
---|
123 | super();
|
---|
124 | this.search_panel = panel;
|
---|
125 |
|
---|
126 | this.setLayout(new GridLayout(3,1));
|
---|
127 | JPanel jp1 = new JPanel();
|
---|
128 | jp1.setLayout(new BorderLayout());
|
---|
129 |
|
---|
130 | jtf_start_class = new JTextField(40);
|
---|
131 | jtf_start_class.setText("http://dbpedia.org/ontology/Artist");
|
---|
132 | //jtf_start_class.setText("http://purl.jp/bio/10/lsd/ontology/201209#EnglishCode");
|
---|
133 | //jtf_start_class.setText("http://purl.org/allie/ontology/201108#ShortForm");
|
---|
134 | jp1.add(new JLabel("Start Class:"),BorderLayout.WEST);
|
---|
135 | jp1.add(jtf_start_class,BorderLayout.CENTER);
|
---|
136 | JButton jb_start = new JButton("Select");
|
---|
137 | jp1.add(jb_start,BorderLayout.EAST);
|
---|
138 | jb_start.addActionListener(new ActionListener(){
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | public void actionPerformed(ActionEvent arg0) {
|
---|
142 | // TODO Auto-generated method stub
|
---|
143 | System.out.println("jb_start action");
|
---|
144 | searchClass(jtf_start_class.getText());
|
---|
145 | String classURI = searchClass(jtf_start_class.getText());
|
---|
146 | jtf_start_class.setText(classURI);
|
---|
147 | }
|
---|
148 |
|
---|
149 | });
|
---|
150 |
|
---|
151 | JPanel jp2 = new JPanel();
|
---|
152 | jp2.setLayout(new BorderLayout());
|
---|
153 | jtf_end_class = new JTextField(40);
|
---|
154 | jtf_end_class.setText("http://dbpedia.org/ontology/Award");
|
---|
155 | //jtf_end_class.setText("http://purl.jp/bio/10/lsd/ontology/201209#JapaneseEntry");
|
---|
156 | //jtf_end_class.setText("http://purl.org/allie/ontology/201108#LongForm");
|
---|
157 |
|
---|
158 | jp2.add(new JLabel("End Class:"),BorderLayout.WEST);
|
---|
159 | jp2.add(jtf_end_class,BorderLayout.CENTER);
|
---|
160 | JButton jb_end = new JButton("Select");
|
---|
161 | jb_end.addActionListener(new ActionListener(){
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | public void actionPerformed(ActionEvent arg0) {
|
---|
165 | // TODO Auto-generated method stub
|
---|
166 | System.out.println("jb_end action");
|
---|
167 | String classURI = searchClass(jtf_end_class.getText());
|
---|
168 | jtf_end_class.setText(classURI);
|
---|
169 | }
|
---|
170 |
|
---|
171 | });
|
---|
172 |
|
---|
173 |
|
---|
174 | jp2.add(jb_end,BorderLayout.EAST);
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 | JPanel jp3 = new JPanel();
|
---|
179 | jtf_mode = new JTextField(3);
|
---|
180 | jtf_mode.setText("0");
|
---|
181 | jp3.add(jtf_mode);
|
---|
182 |
|
---|
183 | jcb_count_ins = new JCheckBox("Count Ins.",false);
|
---|
184 | jp3.add(jcb_count_ins);
|
---|
185 |
|
---|
186 | JButton jb_get_path = new JButton("Get Path");
|
---|
187 | jp3.add(jb_get_path);
|
---|
188 |
|
---|
189 | map = new MapFactory();
|
---|
190 | map.setSPARQLbuilder(this);
|
---|
191 |
|
---|
192 |
|
---|
193 | jb_get_path.addActionListener(new ActionListener() {
|
---|
194 |
|
---|
195 | @Override
|
---|
196 | public void actionPerformed(ActionEvent e) {
|
---|
197 | System.out.println("get path action");
|
---|
198 | //map.loadPathList(map.getDummyPathList());
|
---|
199 |
|
---|
200 | try {
|
---|
201 | System.out.println("getPaths:::>>>>"+jtf_start_class.getText()
|
---|
202 | +"<===>"+jtf_end_class.getText());
|
---|
203 | String mode_str = jtf_mode.getText();
|
---|
204 | int mode = Integer.parseInt(mode_str);
|
---|
205 | if(mode!=0 && mode!=1){
|
---|
206 | mode = 0;
|
---|
207 | }
|
---|
208 |
|
---|
209 | boolean count_ins = jcb_count_ins.isSelected();
|
---|
210 | /*
|
---|
211 | Path[] path = getOWLQueryBuilderImpl().getPaths(
|
---|
212 | jtf_start_class.getText(), jtf_end_class.getText(),
|
---|
213 | mode, !count_ins);
|
---|
214 | */
|
---|
215 | Path[] path = getQueryPathGenerator().getPaths(jtf_start_class.getText(), jtf_end_class.getText(), !count_ins);
|
---|
216 | System.out.println("RESULT:::>>>>"+path.length);
|
---|
217 |
|
---|
218 | map.loadPathList(path);
|
---|
219 | //jta_query.setText(createSPARQL(path[0]));
|
---|
220 |
|
---|
221 | } catch (Exception e1) {
|
---|
222 | // TODO Auto-generated catch block
|
---|
223 | e1.printStackTrace();
|
---|
224 | System.out.println(e.toString());
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 |
|
---|
229 | }
|
---|
230 | });
|
---|
231 |
|
---|
232 | this.add(jp1);
|
---|
233 | this.add(jp2);
|
---|
234 | this.add(jp3);
|
---|
235 |
|
---|
236 | // jta_query = new JTextArea();
|
---|
237 | // this.add(new JScrollPane(jta_query), BorderLayout.CENTER);
|
---|
238 | // JPanel jp_bottom = new JPanel();
|
---|
239 | // jp_bottom.add(new JButton("make query"));
|
---|
240 | // this.add(jp_bottom, BorderLayout.SOUTH);
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | String searchClass(String keyword){
|
---|
245 | System.out.println("SearcClass for..."+keyword);
|
---|
246 | String classURL ="";
|
---|
247 | try {
|
---|
248 | //SClass[] classes = this.getOWLQueryBuilderImpl().getOWLClasses(null, null, keyword, false);
|
---|
249 | SClass[] classes = this.getQueryPathGenerator().getClasses(keyword);
|
---|
250 | DefaultListModel model = new DefaultListModel();
|
---|
251 | for(int i=0;i<classes.length;i++){
|
---|
252 | model.addElement(classes[i].getClassURI());
|
---|
253 | System.out.println("..."+classes[i].getClassURI());
|
---|
254 | }
|
---|
255 | JList list = new JList(model);
|
---|
256 |
|
---|
257 | JScrollPane listScrollPane = new JScrollPane(list);
|
---|
258 |
|
---|
259 | Object[] msg ={
|
---|
260 | "Select a Class:",listScrollPane};
|
---|
261 |
|
---|
262 |
|
---|
263 | int isOK = JOptionPane.showConfirmDialog(null,msg,"Select Class",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
|
---|
264 |
|
---|
265 | if(isOK==JOptionPane.OK_OPTION){
|
---|
266 | int index = list.getSelectedIndex();
|
---|
267 | classURL = model.getElementAt(index).toString();
|
---|
268 | }
|
---|
269 |
|
---|
270 | } catch (Exception e) {
|
---|
271 | // TODO Auto-generated catch block
|
---|
272 | e.printStackTrace();
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | return classURL;
|
---|
277 | }
|
---|
278 |
|
---|
279 | public void setSPARQL(Path path,int num) throws Exception{
|
---|
280 | String text = createSPARQL(path,num);
|
---|
281 | //this.jta_query.setText(text);
|
---|
282 | this.search_panel.setQueryText(text);
|
---|
283 | }
|
---|
284 |
|
---|
285 | /*
|
---|
286 | void makeOWLQueryBuilderImpl() {
|
---|
287 | // String sparqlEndpoint = "http://lsd.dbcls.jp/sparql";
|
---|
288 | String sparqlEndpoint = search_panel.getSparqlAccessorForm().getCurrentEndPoint();
|
---|
289 |
|
---|
290 | // String sparqlEndpoint = "http://dbpedia.org/sparql";
|
---|
291 |
|
---|
292 | System.out.println("Create OWLQueryBuilder for "+sparqlEndpoint);
|
---|
293 |
|
---|
294 | builder = new OWLQueryBuilderImpl(sparqlEndpoint);
|
---|
295 |
|
---|
296 | String keyword = "\"artiste\"@fr";
|
---|
297 | String[] graphURIs = new String[0];
|
---|
298 | try {
|
---|
299 |
|
---|
300 | // OWLQueryBuilder builder = new OWLQueryBuilderImpl(sparqlEndpoint);
|
---|
301 | String[] clsURIs;
|
---|
302 | clsURIs = builder.getOWLClasses(graphURIs, keyword);
|
---|
303 | for(String cls: clsURIs){
|
---|
304 | System.out.println(cls);
|
---|
305 | }
|
---|
306 |
|
---|
307 | System.out.println("CLS");
|
---|
308 | ClassLink[] cls = builder.getNextClass(null, clsURIs[0], 0);
|
---|
309 | if( cls != null ){
|
---|
310 | for( ClassLink cl: cls){
|
---|
311 | System.out.println(cl.toString());
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | System.out.println("CLS-INS");
|
---|
316 | cls = builder.getNextClassViaInstanceLink(null, clsURIs[0], 100);
|
---|
317 | if( cls != null ){
|
---|
318 | for( ClassLink cl: cls){
|
---|
319 | System.out.println(cl.toString());
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | System.out.println("Instances");
|
---|
324 | Instance[] ins = builder.getInstances(null, "\"A.C. Reed\"@en");
|
---|
325 | if( ins != null ){
|
---|
326 | for( Instance in: ins){
|
---|
327 | System.out.println(in.toString());
|
---|
328 | }
|
---|
329 | }
|
---|
330 | } catch (Exception e) {
|
---|
331 | // TODO Auto-generated catch block
|
---|
332 | System.out.println(e.toString());
|
---|
333 | e.printStackTrace();
|
---|
334 | }
|
---|
335 |
|
---|
336 | //return (OWLQueryBuilderImpl) builder;
|
---|
337 | }
|
---|
338 | */
|
---|
339 |
|
---|
340 | void makeQueryPathGenerator() {
|
---|
341 | // String sparqlEndpoint = "http://lsd.dbcls.jp/sparql";
|
---|
342 | String sparqlEndpoint = search_panel.getSparqlAccessorForm().getCurrentEndPoint();
|
---|
343 |
|
---|
344 | // String sparqlEndpoint = "http://dbpedia.org/sparql";
|
---|
345 |
|
---|
346 | System.out.println("Create OWLQueryBuilder for "+sparqlEndpoint);
|
---|
347 |
|
---|
348 | generator = new QueryPathGenerator(sparqlEndpoint);
|
---|
349 |
|
---|
350 |
|
---|
351 | //String keyword = "\"artiste\"@fr";
|
---|
352 | //String[] graphURIs = new String[0];
|
---|
353 | /*
|
---|
354 | try {
|
---|
355 |
|
---|
356 | // OWLQueryBuilder builder = new OWLQueryBuilderImpl(sparqlEndpoint);
|
---|
357 |
|
---|
358 | String[] clsURIs;
|
---|
359 | clsURIs = builder.getOWLClasses(graphURIs, keyword);
|
---|
360 | for(String cls: clsURIs){
|
---|
361 | System.out.println(cls);
|
---|
362 | }
|
---|
363 |
|
---|
364 | System.out.println("CLS");
|
---|
365 | ClassLink[] cls = builder.getNextClass(null, clsURIs[0], 0);
|
---|
366 | if( cls != null ){
|
---|
367 | for( ClassLink cl: cls){
|
---|
368 | System.out.println(cl.toString());
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | System.out.println("CLS-INS");
|
---|
373 | cls = builder.getNextClassViaInstanceLink(null, clsURIs[0], 100);
|
---|
374 | if( cls != null ){
|
---|
375 | for( ClassLink cl: cls){
|
---|
376 | System.out.println(cl.toString());
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | System.out.println("Instances");
|
---|
381 | Instance[] ins = builder.getInstances(null, "\"A.C. Reed\"@en");
|
---|
382 | if( ins != null ){
|
---|
383 | for( Instance in: ins){
|
---|
384 | System.out.println(in.toString());
|
---|
385 | }
|
---|
386 | }
|
---|
387 | } catch (Exception e) {
|
---|
388 | // TODO Auto-generated catch block
|
---|
389 | System.out.println(e.toString());
|
---|
390 | e.printStackTrace();
|
---|
391 | }*/
|
---|
392 |
|
---|
393 | //return (OWLQueryBuilderImpl) builder;
|
---|
394 | }
|
---|
395 |
|
---|
396 |
|
---|
397 | public String createSPARQL(Path path) throws Exception {
|
---|
398 | return createSPARQL(path, 0);
|
---|
399 | }
|
---|
400 |
|
---|
401 | public String createSPARQL(Path path, int num) throws Exception {
|
---|
402 |
|
---|
403 | if( path == null ){
|
---|
404 | throw new Exception("Path is null.");
|
---|
405 | }
|
---|
406 | String startClass = path.getStartClass();
|
---|
407 | List<ClassLink> classLinks = path.getClassLinks();
|
---|
408 |
|
---|
409 | StringBuffer queryStr = new StringBuffer();
|
---|
410 | StringBuffer selStr = new StringBuffer();
|
---|
411 | StringBuffer whereStr = new StringBuffer();
|
---|
412 | if(num==0){
|
---|
413 | num = classLinks.size();
|
---|
414 | }
|
---|
415 |
|
---|
416 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
417 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
418 |
|
---|
419 | selStr.append("SELECT ");
|
---|
420 | whereStr.append("WHERE { \n");
|
---|
421 |
|
---|
422 | String properties = null;
|
---|
423 | String objectClasses = null;
|
---|
424 | String subjectClasses = null;
|
---|
425 | Direction direction = null;
|
---|
426 | int i = 0;
|
---|
427 | int k = 0;
|
---|
428 | for (ClassLink link :classLinks )
|
---|
429 | {
|
---|
430 | properties = link.getPropertyURI();
|
---|
431 | objectClasses = link.getLinkedClassURI();
|
---|
432 | direction = link.getDirection();
|
---|
433 |
|
---|
434 | if (i==0)
|
---|
435 | subjectClasses = startClass;
|
---|
436 |
|
---|
437 | selStr.append("?c").append(i).append(" ");
|
---|
438 | selStr.append("?l").append(i).append(" ");
|
---|
439 |
|
---|
440 | if(i == classLinks.size())
|
---|
441 | selStr.append("\n");
|
---|
442 |
|
---|
443 |
|
---|
444 | whereStr.append("?c").append(i).
|
---|
445 | append(" rdf:type ").
|
---|
446 | append("<").
|
---|
447 | append(subjectClasses).
|
---|
448 | append(">").
|
---|
449 | append(".\n");
|
---|
450 |
|
---|
451 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n");
|
---|
452 |
|
---|
453 |
|
---|
454 | if(direction == Direction.forward)
|
---|
455 | {
|
---|
456 | whereStr.append("?c").append(i).append(" ");
|
---|
457 | whereStr.append("<").append(properties).append("> ");
|
---|
458 | whereStr.append("?c").append(i+1).append(".\n");
|
---|
459 | }
|
---|
460 | else
|
---|
461 | {
|
---|
462 | whereStr.append("?c").append(i+1).append(" ");
|
---|
463 | whereStr.append("<").append(properties).append("> ");
|
---|
464 | whereStr.append("?c").append(i).append(".\n");
|
---|
465 | }
|
---|
466 |
|
---|
467 | subjectClasses = objectClasses;
|
---|
468 | i++;
|
---|
469 | k++;
|
---|
470 | if(k>=num){
|
---|
471 | break;
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | selStr.append("?c").append(i).append(" \n");
|
---|
476 | selStr.append("?l").append(i).append(" \n");
|
---|
477 | whereStr.append("?c").append(i).append(" rdf:type ").
|
---|
478 | append("<").
|
---|
479 | append(subjectClasses).
|
---|
480 | append(">").
|
---|
481 | append(".\n");
|
---|
482 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n");
|
---|
483 |
|
---|
484 |
|
---|
485 | queryStr.append(selStr).append(whereStr);
|
---|
486 |
|
---|
487 | queryStr.append("}");
|
---|
488 | //OPTIONAL
|
---|
489 | queryStr.append("LIMIT 100\n");;
|
---|
490 |
|
---|
491 | //System.out.println(queryStr);
|
---|
492 | return queryStr.toString();
|
---|
493 | }
|
---|
494 |
|
---|
495 |
|
---|
496 |
|
---|
497 | }
|
---|
498 |
|
---|
499 | class classSelectDialog extends JDialog{
|
---|
500 |
|
---|
501 | classSelectDialog(){
|
---|
502 | super();
|
---|
503 | this.setVisible(true);
|
---|
504 |
|
---|
505 | }
|
---|
506 |
|
---|
507 |
|
---|
508 |
|
---|
509 | }
|
---|