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 |
|
---|
23 |
|
---|
24 |
|
---|
25 | public class SparqlBuilderPanel extends JPanel{
|
---|
26 |
|
---|
27 | JTextField jtf_start_class;
|
---|
28 | JTextField jtf_end_class;
|
---|
29 | JTextArea jta_query;
|
---|
30 | MapFactory map;
|
---|
31 | SparqlSearchPanel search_panel;
|
---|
32 |
|
---|
33 | // public SparqlBuilderPanel(SparqlAccessorForm parent) {
|
---|
34 | // super(parent);
|
---|
35 | // initialize();
|
---|
36 | // this.parent = parent;
|
---|
37 | // }
|
---|
38 |
|
---|
39 |
|
---|
40 | /* SparqlBuilderPanel(SparqlAccessorForm sparqlAccessorForm){
|
---|
41 | super();
|
---|
42 |
|
---|
43 | this.setLayout(new BorderLayout());
|
---|
44 | JPanel jp_top = new JPanel();
|
---|
45 | jtf_start_class = new JTextField("http://dbpedia.org/ontology/Artist");
|
---|
46 | jp_top.add(new JLabel("Start Class:"));
|
---|
47 | jp_top.add(jtf_start_class);
|
---|
48 | JButton jb_start = new JButton("Select");
|
---|
49 | jp_top.add(jb_start);
|
---|
50 |
|
---|
51 | jtf_end_class = new JTextField("http://dbpedia.org/ontology/Award");
|
---|
52 | jp_top.add(new JLabel("End Class:"));
|
---|
53 | jp_top.add(jtf_end_class);
|
---|
54 | JButton jb_end = new JButton("Select");
|
---|
55 | jp_top.add(jb_end);
|
---|
56 |
|
---|
57 | JButton jb_get_path = new JButton("Get Path");
|
---|
58 | jp_top.add(jb_get_path);
|
---|
59 |
|
---|
60 | map = new MapFactory();
|
---|
61 | map.setSPARQLbuilder(this);
|
---|
62 |
|
---|
63 |
|
---|
64 | jb_get_path.addActionListener(new ActionListener() {
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public void actionPerformed(ActionEvent e) {
|
---|
68 | System.out.println("getPaths:::>>>>"+jtf_start_class.getText()
|
---|
69 | +"<===>"+jtf_end_class.getText());
|
---|
70 | //map.loadPathList(map.getDummyPathList());
|
---|
71 |
|
---|
72 | try {
|
---|
73 | System.out.println("getPaths:::>>>>"+jtf_start_class.getText()
|
---|
74 | +"<===>"+jtf_end_class.getText());
|
---|
75 | OWLQueryBuilderImpl builder = makeOWLQueryBuilderImpl();
|
---|
76 | Path[] path = builder.getPaths(jtf_start_class.getText(), jtf_end_class.getText());
|
---|
77 | System.out.println("RESULT:::>>>>"+path.length);
|
---|
78 |
|
---|
79 | map.loadPathList(path);
|
---|
80 | //jta_query.setText(createSPARQL(path[0]));
|
---|
81 |
|
---|
82 | } catch (Exception e1) {
|
---|
83 | // TODO Auto-generated catch block
|
---|
84 | e1.printStackTrace();
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 | }
|
---|
90 | });
|
---|
91 |
|
---|
92 | this.add(jp_top, BorderLayout.NORTH);
|
---|
93 | jta_query = new JTextArea();
|
---|
94 | this.add(new JScrollPane(jta_query), BorderLayout.CENTER);
|
---|
95 | JPanel jp_bottom = new JPanel();
|
---|
96 | jp_bottom.add(new JButton("make query"));
|
---|
97 | this.add(jp_bottom, BorderLayout.SOUTH);
|
---|
98 | }*/
|
---|
99 |
|
---|
100 |
|
---|
101 | SparqlBuilderPanel(SparqlSearchPanel panel){
|
---|
102 | super();
|
---|
103 | this.search_panel = panel;
|
---|
104 |
|
---|
105 | this.setLayout(new GridLayout(3,1));
|
---|
106 | JPanel jp1 = new JPanel();
|
---|
107 |
|
---|
108 | jtf_start_class = new JTextField(40);
|
---|
109 | //jtf_start_class.setText("http://dbpedia.org/ontology/Artist");
|
---|
110 | jtf_start_class = new JTextField("http://purl.jp/bio/10/lsd/ontology/201209#EnglishCode");
|
---|
111 | jp1.add(new JLabel("Start Class:"));
|
---|
112 | jp1.add(jtf_start_class);
|
---|
113 | JButton jb_start = new JButton("Select");
|
---|
114 | jp1.add(jb_start);
|
---|
115 | jb_start.addActionListener(new ActionListener(){
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public void actionPerformed(ActionEvent arg0) {
|
---|
119 | // TODO Auto-generated method stub
|
---|
120 | System.out.println("jb_start action");
|
---|
121 |
|
---|
122 | }
|
---|
123 |
|
---|
124 | });
|
---|
125 |
|
---|
126 | JPanel jp2 = new JPanel();
|
---|
127 | jtf_end_class = new JTextField(40);
|
---|
128 | //jtf_end_class.setText("http://dbpedia.org/ontology/Award");
|
---|
129 | jtf_end_class = new JTextField("http://purl.jp/bio/10/lsd/ontology/201209#JapaneseEntry");
|
---|
130 | jp2.add(new JLabel("End Class:"));
|
---|
131 | jp2.add(jtf_end_class);
|
---|
132 | JButton jb_end = new JButton("Select");
|
---|
133 | jp2.add(jb_end);
|
---|
134 |
|
---|
135 |
|
---|
136 |
|
---|
137 | JPanel jp3 = new JPanel();
|
---|
138 | JButton jb_get_path = new JButton("Get Path");
|
---|
139 | jp3.add(jb_get_path);
|
---|
140 |
|
---|
141 | map = new MapFactory();
|
---|
142 | map.setSPARQLbuilder(this);
|
---|
143 |
|
---|
144 |
|
---|
145 | jb_get_path.addActionListener(new ActionListener() {
|
---|
146 |
|
---|
147 | @Override
|
---|
148 | public void actionPerformed(ActionEvent e) {
|
---|
149 | System.out.println("get path action");
|
---|
150 | //map.loadPathList(map.getDummyPathList());
|
---|
151 |
|
---|
152 | try {
|
---|
153 | OWLQueryBuilderImpl builder = makeOWLQueryBuilderImpl();
|
---|
154 | System.out.println("getPaths:::>>>>"+jtf_start_class.getText()
|
---|
155 | +"<===>"+jtf_end_class.getText());
|
---|
156 | Path[] path = builder.getPaths(jtf_start_class.getText(), jtf_end_class.getText());
|
---|
157 | System.out.println("RESULT:::>>>>"+path.length);
|
---|
158 |
|
---|
159 | map.loadPathList(path);
|
---|
160 | //jta_query.setText(createSPARQL(path[0]));
|
---|
161 |
|
---|
162 | } catch (Exception e1) {
|
---|
163 | // TODO Auto-generated catch block
|
---|
164 | e1.printStackTrace();
|
---|
165 | System.out.println(e.toString());
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 |
|
---|
170 | }
|
---|
171 | });
|
---|
172 |
|
---|
173 | this.add(jp1);
|
---|
174 | this.add(jp2);
|
---|
175 | this.add(jp3);
|
---|
176 |
|
---|
177 | // jta_query = new JTextArea();
|
---|
178 | // this.add(new JScrollPane(jta_query), BorderLayout.CENTER);
|
---|
179 | // JPanel jp_bottom = new JPanel();
|
---|
180 | // jp_bottom.add(new JButton("make query"));
|
---|
181 | // this.add(jp_bottom, BorderLayout.SOUTH);
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 |
|
---|
186 | public void setSPARQL(Path path,int num) throws Exception{
|
---|
187 | String text = createSPARQL(path,num);
|
---|
188 | //this.jta_query.setText(text);
|
---|
189 | this.search_panel.setQueryText(text);
|
---|
190 | }
|
---|
191 |
|
---|
192 | OWLQueryBuilderImpl makeOWLQueryBuilderImpl() {
|
---|
193 | // String sparqlEndpoint = "http://lsd.dbcls.jp/sparql";
|
---|
194 | String sparqlEndpoint = search_panel.getSparqlAccessorForm().getCurrentEndPoint();
|
---|
195 |
|
---|
196 |
|
---|
197 | // String sparqlEndpoint = "http://dbpedia.org/sparql";
|
---|
198 |
|
---|
199 | System.out.println("Create OWLQueryBuilder for "+sparqlEndpoint);
|
---|
200 |
|
---|
201 | OWLQueryBuilder builder = new OWLQueryBuilderImpl(sparqlEndpoint);
|
---|
202 | /*
|
---|
203 |
|
---|
204 | String keyword = "\"artiste\"@fr";
|
---|
205 | String[] graphURIs = new String[0];
|
---|
206 | try {
|
---|
207 |
|
---|
208 | // OWLQueryBuilder builder = new OWLQueryBuilderImpl(sparqlEndpoint);
|
---|
209 | String[] clsURIs;
|
---|
210 | clsURIs = builder.getOWLClasses(graphURIs, keyword);
|
---|
211 | for(String cls: clsURIs){
|
---|
212 | System.out.println(cls);
|
---|
213 | }
|
---|
214 |
|
---|
215 | System.out.println("CLS");
|
---|
216 | ClassLink[] cls = builder.getNextClass(null, clsURIs[0], 0);
|
---|
217 | if( cls != null ){
|
---|
218 | for( ClassLink cl: cls){
|
---|
219 | System.out.println(cl.toString());
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | System.out.println("CLS-INS");
|
---|
224 | cls = builder.getNextClassViaInstanceLink(null, clsURIs[0], 100);
|
---|
225 | if( cls != null ){
|
---|
226 | for( ClassLink cl: cls){
|
---|
227 | System.out.println(cl.toString());
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | System.out.println("Instances");
|
---|
232 | Instance[] ins = builder.getInstances(null, "\"A.C. Reed\"@en");
|
---|
233 | if( ins != null ){
|
---|
234 | for( Instance in: ins){
|
---|
235 | System.out.println(in.toString());
|
---|
236 | }
|
---|
237 | }
|
---|
238 | } catch (Exception e) {
|
---|
239 | // TODO Auto-generated catch block
|
---|
240 | System.out.println(e.toString());
|
---|
241 | e.printStackTrace();
|
---|
242 | }*/
|
---|
243 |
|
---|
244 | return (OWLQueryBuilderImpl) builder;
|
---|
245 | }
|
---|
246 |
|
---|
247 | public String createSPARQL(Path path) throws Exception {
|
---|
248 | return createSPARQL(path, 0);
|
---|
249 | }
|
---|
250 |
|
---|
251 | public String createSPARQL(Path path, int num) throws Exception {
|
---|
252 |
|
---|
253 |
|
---|
254 | String startClass = path.getStartClass();
|
---|
255 | List<ClassLink> classLinks = path.getClassLinks();
|
---|
256 |
|
---|
257 | StringBuffer queryStr = new StringBuffer();
|
---|
258 | StringBuffer selStr = new StringBuffer();
|
---|
259 | StringBuffer whereStr = new StringBuffer();
|
---|
260 | if(num==0){
|
---|
261 | num = classLinks.size();
|
---|
262 | }
|
---|
263 |
|
---|
264 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
265 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
266 |
|
---|
267 | selStr.append("SELECT ");
|
---|
268 | whereStr.append("WHERE { \n");
|
---|
269 |
|
---|
270 | String properties = null;
|
---|
271 | String objectClasses = null;
|
---|
272 | String subjectClasses = null;
|
---|
273 | Direction direction = null;
|
---|
274 | int i = 0;
|
---|
275 | int k = 0;
|
---|
276 | for (ClassLink link :classLinks )
|
---|
277 | {
|
---|
278 | properties = link.getPropertyURI();
|
---|
279 | objectClasses = link.getLinkedClassURI();
|
---|
280 | direction = link.getDirection();
|
---|
281 |
|
---|
282 | if (i==0)
|
---|
283 | subjectClasses = startClass;
|
---|
284 |
|
---|
285 | selStr.append("?c").append(i).append(" ");
|
---|
286 |
|
---|
287 | if(i == classLinks.size())
|
---|
288 | selStr.append("\n");
|
---|
289 |
|
---|
290 |
|
---|
291 | whereStr.append("?c").append(i).
|
---|
292 | append(" rdf:type ").
|
---|
293 | append("<").
|
---|
294 | append(subjectClasses).
|
---|
295 | append(">").
|
---|
296 | append(".\n");
|
---|
297 |
|
---|
298 | if(direction == Direction.forward)
|
---|
299 | {
|
---|
300 | whereStr.append("?c").append(i).append(" ");
|
---|
301 | whereStr.append("<").append(properties).append("> ");
|
---|
302 | whereStr.append("?c").append(i+1).append(".\n");
|
---|
303 | }
|
---|
304 | else
|
---|
305 | {
|
---|
306 | whereStr.append("?c").append(i+1).append(" ");
|
---|
307 | whereStr.append("<").append(properties).append("> ");
|
---|
308 | whereStr.append("?c").append(i).append(".\n");
|
---|
309 | }
|
---|
310 |
|
---|
311 | subjectClasses = objectClasses;
|
---|
312 | i++;
|
---|
313 | k++;
|
---|
314 | if(k>=num){
|
---|
315 | break;
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | selStr.append("?c").append(i).append(" \n");
|
---|
320 | whereStr.append("?c").append(i).append(" rdf:type ").
|
---|
321 | append("<").
|
---|
322 | append(subjectClasses).
|
---|
323 | append(">").
|
---|
324 | append(".\n");
|
---|
325 |
|
---|
326 |
|
---|
327 | queryStr.append(selStr).append(whereStr).append("} LIMIT 100\n");;
|
---|
328 |
|
---|
329 | //System.out.println(queryStr);
|
---|
330 | return queryStr.toString();
|
---|
331 | }
|
---|
332 |
|
---|
333 |
|
---|
334 |
|
---|
335 | }
|
---|