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