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 | jp1.add(new JLabel("Start Class:"),BorderLayout.WEST);
|
---|
123 | jp1.add(jtf_start_class,BorderLayout.CENTER);
|
---|
124 | JButton jb_start = new JButton("Select");
|
---|
125 | jp1.add(jb_start,BorderLayout.EAST);
|
---|
126 | jb_start.addActionListener(new ActionListener(){
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public void actionPerformed(ActionEvent arg0) {
|
---|
130 | // TODO Auto-generated method stub
|
---|
131 | System.out.println("jb_start action");
|
---|
132 | searchClass(jtf_start_class.getText());
|
---|
133 | String classURI = searchClass(jtf_start_class.getText());
|
---|
134 | jtf_start_class.setText(classURI);
|
---|
135 | }
|
---|
136 |
|
---|
137 | });
|
---|
138 |
|
---|
139 | JPanel jp2 = new JPanel();
|
---|
140 | jp2.setLayout(new BorderLayout());
|
---|
141 | jtf_end_class = new JTextField(40);
|
---|
142 | //jtf_end_class.setText("http://dbpedia.org/ontology/Award");
|
---|
143 | jtf_end_class.setText("http://purl.jp/bio/10/lsd/ontology/201209#JapaneseEntry");
|
---|
144 |
|
---|
145 | jp2.add(new JLabel("End Class:"),BorderLayout.WEST);
|
---|
146 | jp2.add(jtf_end_class,BorderLayout.CENTER);
|
---|
147 | JButton jb_end = new JButton("Select");
|
---|
148 | jb_end.addActionListener(new ActionListener(){
|
---|
149 |
|
---|
150 | @Override
|
---|
151 | public void actionPerformed(ActionEvent arg0) {
|
---|
152 | // TODO Auto-generated method stub
|
---|
153 | System.out.println("jb_end action");
|
---|
154 | String classURI = searchClass(jtf_end_class.getText());
|
---|
155 | jtf_end_class.setText(classURI);
|
---|
156 | }
|
---|
157 |
|
---|
158 | });
|
---|
159 |
|
---|
160 |
|
---|
161 | jp2.add(jb_end,BorderLayout.EAST);
|
---|
162 |
|
---|
163 |
|
---|
164 |
|
---|
165 | JPanel jp3 = new JPanel();
|
---|
166 | jtf_mode = new JTextField(3);
|
---|
167 | jtf_mode.setText("0");
|
---|
168 | jp3.add(jtf_mode);
|
---|
169 |
|
---|
170 | jcb_count_ins = new JCheckBox("Count Ins.",false);
|
---|
171 | jp3.add(jcb_count_ins);
|
---|
172 |
|
---|
173 | JButton jb_get_path = new JButton("Get Path");
|
---|
174 | jp3.add(jb_get_path);
|
---|
175 |
|
---|
176 | map = new MapFactory();
|
---|
177 | map.setSPARQLbuilder(this);
|
---|
178 |
|
---|
179 |
|
---|
180 | jb_get_path.addActionListener(new ActionListener() {
|
---|
181 |
|
---|
182 | @Override
|
---|
183 | public void actionPerformed(ActionEvent e) {
|
---|
184 | System.out.println("get path action");
|
---|
185 | //map.loadPathList(map.getDummyPathList());
|
---|
186 |
|
---|
187 | try {
|
---|
188 | System.out.println("getPaths:::>>>>"+jtf_start_class.getText()
|
---|
189 | +"<===>"+jtf_end_class.getText());
|
---|
190 | String mode_str = jtf_mode.getText();
|
---|
191 | int mode = Integer.parseInt(mode_str);
|
---|
192 | if(mode!=0 && mode!=1){
|
---|
193 | mode = 0;
|
---|
194 | }
|
---|
195 |
|
---|
196 | boolean count_ins = jcb_count_ins.isSelected();
|
---|
197 |
|
---|
198 | Path[] path = getOWLQueryBuilderImpl().getPaths(
|
---|
199 | jtf_start_class.getText(), jtf_end_class.getText(),
|
---|
200 | mode, count_ins);
|
---|
201 | System.out.println("RESULT:::>>>>"+path.length);
|
---|
202 |
|
---|
203 | map.loadPathList(path);
|
---|
204 | //jta_query.setText(createSPARQL(path[0]));
|
---|
205 |
|
---|
206 | } catch (Exception e1) {
|
---|
207 | // TODO Auto-generated catch block
|
---|
208 | e1.printStackTrace();
|
---|
209 | System.out.println(e.toString());
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 |
|
---|
214 | }
|
---|
215 | });
|
---|
216 |
|
---|
217 | this.add(jp1);
|
---|
218 | this.add(jp2);
|
---|
219 | this.add(jp3);
|
---|
220 |
|
---|
221 | // jta_query = new JTextArea();
|
---|
222 | // this.add(new JScrollPane(jta_query), BorderLayout.CENTER);
|
---|
223 | // JPanel jp_bottom = new JPanel();
|
---|
224 | // jp_bottom.add(new JButton("make query"));
|
---|
225 | // this.add(jp_bottom, BorderLayout.SOUTH);
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | String searchClass(String keyword){
|
---|
230 | System.out.println("SearcClass for..."+keyword);
|
---|
231 | String classURL ="";
|
---|
232 | try {
|
---|
233 | SClass[] classes = this.getOWLQueryBuilderImpl().getOWLClasses(null, keyword, false);
|
---|
234 | DefaultListModel model = new DefaultListModel();
|
---|
235 | for(int i=0;i<classes.length;i++){
|
---|
236 | model.addElement(classes[i].getClassURI());
|
---|
237 | }
|
---|
238 | JList list = new JList(model);
|
---|
239 |
|
---|
240 | JScrollPane listScrollPane = new JScrollPane(list);
|
---|
241 |
|
---|
242 | Object[] msg ={"Select a Class:",listScrollPane};
|
---|
243 |
|
---|
244 |
|
---|
245 | int isOK = JOptionPane.showConfirmDialog(null,msg,"Select Class",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
|
---|
246 |
|
---|
247 | if(isOK==JOptionPane.OK_OPTION){
|
---|
248 | int index = list.getSelectedIndex();
|
---|
249 | classURL = model.getElementAt(index).toString();
|
---|
250 | }
|
---|
251 |
|
---|
252 | } catch (Exception e) {
|
---|
253 | // TODO Auto-generated catch block
|
---|
254 | e.printStackTrace();
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | return classURL;
|
---|
259 | }
|
---|
260 |
|
---|
261 | public void setSPARQL(Path path,int num) throws Exception{
|
---|
262 | String text = createSPARQL(path,num);
|
---|
263 | //this.jta_query.setText(text);
|
---|
264 | this.search_panel.setQueryText(text);
|
---|
265 | }
|
---|
266 |
|
---|
267 | void makeOWLQueryBuilderImpl() {
|
---|
268 | // String sparqlEndpoint = "http://lsd.dbcls.jp/sparql";
|
---|
269 | String sparqlEndpoint = search_panel.getSparqlAccessorForm().getCurrentEndPoint();
|
---|
270 |
|
---|
271 | // String sparqlEndpoint = "http://dbpedia.org/sparql";
|
---|
272 |
|
---|
273 | System.out.println("Create OWLQueryBuilder for "+sparqlEndpoint);
|
---|
274 |
|
---|
275 | builder = new OWLQueryBuilderImpl(sparqlEndpoint);
|
---|
276 | /*
|
---|
277 |
|
---|
278 | String keyword = "\"artiste\"@fr";
|
---|
279 | String[] graphURIs = new String[0];
|
---|
280 | try {
|
---|
281 |
|
---|
282 | // OWLQueryBuilder builder = new OWLQueryBuilderImpl(sparqlEndpoint);
|
---|
283 | String[] clsURIs;
|
---|
284 | clsURIs = builder.getOWLClasses(graphURIs, keyword);
|
---|
285 | for(String cls: clsURIs){
|
---|
286 | System.out.println(cls);
|
---|
287 | }
|
---|
288 |
|
---|
289 | System.out.println("CLS");
|
---|
290 | ClassLink[] cls = builder.getNextClass(null, clsURIs[0], 0);
|
---|
291 | if( cls != null ){
|
---|
292 | for( ClassLink cl: cls){
|
---|
293 | System.out.println(cl.toString());
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | System.out.println("CLS-INS");
|
---|
298 | cls = builder.getNextClassViaInstanceLink(null, clsURIs[0], 100);
|
---|
299 | if( cls != null ){
|
---|
300 | for( ClassLink cl: cls){
|
---|
301 | System.out.println(cl.toString());
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | System.out.println("Instances");
|
---|
306 | Instance[] ins = builder.getInstances(null, "\"A.C. Reed\"@en");
|
---|
307 | if( ins != null ){
|
---|
308 | for( Instance in: ins){
|
---|
309 | System.out.println(in.toString());
|
---|
310 | }
|
---|
311 | }
|
---|
312 | } catch (Exception e) {
|
---|
313 | // TODO Auto-generated catch block
|
---|
314 | System.out.println(e.toString());
|
---|
315 | e.printStackTrace();
|
---|
316 | }*/
|
---|
317 |
|
---|
318 | //return (OWLQueryBuilderImpl) builder;
|
---|
319 | }
|
---|
320 |
|
---|
321 | public String createSPARQL(Path path) throws Exception {
|
---|
322 | return createSPARQL(path, 0);
|
---|
323 | }
|
---|
324 |
|
---|
325 | public String createSPARQL(Path path, int num) throws Exception {
|
---|
326 |
|
---|
327 |
|
---|
328 | String startClass = path.getStartClass();
|
---|
329 | List<ClassLink> classLinks = path.getClassLinks();
|
---|
330 |
|
---|
331 | StringBuffer queryStr = new StringBuffer();
|
---|
332 | StringBuffer selStr = new StringBuffer();
|
---|
333 | StringBuffer whereStr = new StringBuffer();
|
---|
334 | if(num==0){
|
---|
335 | num = classLinks.size();
|
---|
336 | }
|
---|
337 |
|
---|
338 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
339 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
340 |
|
---|
341 | selStr.append("SELECT ");
|
---|
342 | whereStr.append("WHERE { \n");
|
---|
343 |
|
---|
344 | String properties = null;
|
---|
345 | String objectClasses = null;
|
---|
346 | String subjectClasses = null;
|
---|
347 | Direction direction = null;
|
---|
348 | int i = 0;
|
---|
349 | int k = 0;
|
---|
350 | for (ClassLink link :classLinks )
|
---|
351 | {
|
---|
352 | properties = link.getPropertyURI();
|
---|
353 | objectClasses = link.getLinkedClassURI();
|
---|
354 | direction = link.getDirection();
|
---|
355 |
|
---|
356 | if (i==0)
|
---|
357 | subjectClasses = startClass;
|
---|
358 |
|
---|
359 | selStr.append("?c").append(i).append(" ");
|
---|
360 |
|
---|
361 | if(i == classLinks.size())
|
---|
362 | selStr.append("\n");
|
---|
363 |
|
---|
364 |
|
---|
365 | whereStr.append("?c").append(i).
|
---|
366 | append(" rdf:type ").
|
---|
367 | append("<").
|
---|
368 | append(subjectClasses).
|
---|
369 | append(">").
|
---|
370 | append(".\n");
|
---|
371 |
|
---|
372 | if(direction == Direction.forward)
|
---|
373 | {
|
---|
374 | whereStr.append("?c").append(i).append(" ");
|
---|
375 | whereStr.append("<").append(properties).append("> ");
|
---|
376 | whereStr.append("?c").append(i+1).append(".\n");
|
---|
377 | }
|
---|
378 | else
|
---|
379 | {
|
---|
380 | whereStr.append("?c").append(i+1).append(" ");
|
---|
381 | whereStr.append("<").append(properties).append("> ");
|
---|
382 | whereStr.append("?c").append(i).append(".\n");
|
---|
383 | }
|
---|
384 |
|
---|
385 | subjectClasses = objectClasses;
|
---|
386 | i++;
|
---|
387 | k++;
|
---|
388 | if(k>=num){
|
---|
389 | break;
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 | selStr.append("?c").append(i).append(" \n");
|
---|
394 | whereStr.append("?c").append(i).append(" rdf:type ").
|
---|
395 | append("<").
|
---|
396 | append(subjectClasses).
|
---|
397 | append(">").
|
---|
398 | append(".\n");
|
---|
399 |
|
---|
400 |
|
---|
401 | queryStr.append(selStr).append(whereStr).append("} LIMIT 100\n");;
|
---|
402 |
|
---|
403 | //System.out.println(queryStr);
|
---|
404 | return queryStr.toString();
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 |
|
---|
409 | }
|
---|