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