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