package jp.ac.osaka_u.sanken.sparql.gui; import hozo.maptool.MapFactory; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.*; import jp.ac.osaka_u.sanken.sparql.EndpointSettingsManager; import jp.ac.osaka_u.sanken.sparql.SparqlQueryListener; import org.biohackathon.SPARQLBuilder.OWL.ClassLink; import org.biohackathon.SPARQLBuilder.OWL.Direction; import org.biohackathon.SPARQLBuilder.OWL.Instance; import org.biohackathon.SPARQLBuilder.OWL.OWLQueryBuilder; import org.biohackathon.SPARQLBuilder.OWL.OWLQueryBuilderImpl; import org.biohackathon.SPARQLBuilder.OWL.Path; public class SparqlBuilderPanel extends JPanel{ JTextField jtf_start_class; JTextField jtf_end_class; JTextArea jta_query; MapFactory map; SparqlSearchPanel search_panel; // public SparqlBuilderPanel(SparqlAccessorForm parent) { // super(parent); // initialize(); // this.parent = parent; // } /* SparqlBuilderPanel(SparqlAccessorForm sparqlAccessorForm){ super(); this.setLayout(new BorderLayout()); JPanel jp_top = new JPanel(); jtf_start_class = new JTextField("http://dbpedia.org/ontology/Artist"); jp_top.add(new JLabel("Start Class:")); jp_top.add(jtf_start_class); JButton jb_start = new JButton("Select"); jp_top.add(jb_start); jtf_end_class = new JTextField("http://dbpedia.org/ontology/Award"); jp_top.add(new JLabel("End Class:")); jp_top.add(jtf_end_class); JButton jb_end = new JButton("Select"); jp_top.add(jb_end); JButton jb_get_path = new JButton("Get Path"); jp_top.add(jb_get_path); map = new MapFactory(); map.setSPARQLbuilder(this); jb_get_path.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("getPaths:::>>>>"+jtf_start_class.getText() +"<===>"+jtf_end_class.getText()); //map.loadPathList(map.getDummyPathList()); try { System.out.println("getPaths:::>>>>"+jtf_start_class.getText() +"<===>"+jtf_end_class.getText()); OWLQueryBuilderImpl builder = makeOWLQueryBuilderImpl(); Path[] path = builder.getPaths(jtf_start_class.getText(), jtf_end_class.getText()); System.out.println("RESULT:::>>>>"+path.length); map.loadPathList(path); //jta_query.setText(createSPARQL(path[0])); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); this.add(jp_top, BorderLayout.NORTH); jta_query = new JTextArea(); this.add(new JScrollPane(jta_query), BorderLayout.CENTER); JPanel jp_bottom = new JPanel(); jp_bottom.add(new JButton("make query")); this.add(jp_bottom, BorderLayout.SOUTH); }*/ SparqlBuilderPanel(SparqlSearchPanel panel){ super(); this.search_panel = panel; this.setLayout(new GridLayout(3,1)); JPanel jp1 = new JPanel(); jtf_start_class = new JTextField(40); //jtf_start_class.setText("http://dbpedia.org/ontology/Artist"); jtf_start_class = new JTextField("http://purl.jp/bio/10/lsd/ontology/201209#EnglishCode"); jp1.add(new JLabel("Start Class:")); jp1.add(jtf_start_class); JButton jb_start = new JButton("Select"); jp1.add(jb_start); jb_start.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub System.out.println("jb_start action"); } }); JPanel jp2 = new JPanel(); jtf_end_class = new JTextField(40); //jtf_end_class.setText("http://dbpedia.org/ontology/Award"); jtf_end_class = new JTextField("http://purl.jp/bio/10/lsd/ontology/201209#JapaneseEntry"); jp2.add(new JLabel("End Class:")); jp2.add(jtf_end_class); JButton jb_end = new JButton("Select"); jp2.add(jb_end); JPanel jp3 = new JPanel(); JButton jb_get_path = new JButton("Get Path"); jp3.add(jb_get_path); map = new MapFactory(); map.setSPARQLbuilder(this); jb_get_path.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("get path action"); //map.loadPathList(map.getDummyPathList()); try { OWLQueryBuilderImpl builder = makeOWLQueryBuilderImpl(); System.out.println("getPaths:::>>>>"+jtf_start_class.getText() +"<===>"+jtf_end_class.getText()); Path[] path = builder.getPaths(jtf_start_class.getText(), jtf_end_class.getText()); System.out.println("RESULT:::>>>>"+path.length); map.loadPathList(path); //jta_query.setText(createSPARQL(path[0])); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); System.out.println(e.toString()); } } }); this.add(jp1); this.add(jp2); this.add(jp3); // jta_query = new JTextArea(); // this.add(new JScrollPane(jta_query), BorderLayout.CENTER); // JPanel jp_bottom = new JPanel(); // jp_bottom.add(new JButton("make query")); // this.add(jp_bottom, BorderLayout.SOUTH); } public void setSPARQL(Path path,int num) throws Exception{ String text = createSPARQL(path,num); //this.jta_query.setText(text); this.search_panel.setQueryText(text); } OWLQueryBuilderImpl makeOWLQueryBuilderImpl() { // String sparqlEndpoint = "http://lsd.dbcls.jp/sparql"; String sparqlEndpoint = search_panel.getSparqlAccessorForm().getCurrentEndPoint(); // String sparqlEndpoint = "http://dbpedia.org/sparql"; System.out.println("Create OWLQueryBuilder for "+sparqlEndpoint); OWLQueryBuilder builder = new OWLQueryBuilderImpl(sparqlEndpoint); String keyword = "\"artiste\"@fr"; String[] graphURIs = new String[0]; try { // OWLQueryBuilder builder = new OWLQueryBuilderImpl(sparqlEndpoint); String[] clsURIs; clsURIs = builder.getOWLClasses(graphURIs, keyword); for(String cls: clsURIs){ System.out.println(cls); } System.out.println("CLS"); ClassLink[] cls = builder.getNextClass(null, clsURIs[0], 0); if( cls != null ){ for( ClassLink cl: cls){ System.out.println(cl.toString()); } } System.out.println("CLS-INS"); cls = builder.getNextClassViaInstanceLink(null, clsURIs[0], 100); if( cls != null ){ for( ClassLink cl: cls){ System.out.println(cl.toString()); } } System.out.println("Instances"); Instance[] ins = builder.getInstances(null, "\"A.C. Reed\"@en"); if( ins != null ){ for( Instance in: ins){ System.out.println(in.toString()); } } } catch (Exception e) { // TODO Auto-generated catch block System.out.println(e.toString()); e.printStackTrace(); } return (OWLQueryBuilderImpl) builder; } public String createSPARQL(Path path) throws Exception { return createSPARQL(path, 0); } public String createSPARQL(Path path, int num) throws Exception { String startClass = path.getStartClass(); List classLinks = path.getClassLinks(); StringBuffer queryStr = new StringBuffer(); StringBuffer selStr = new StringBuffer(); StringBuffer whereStr = new StringBuffer(); if(num==0){ num = classLinks.size(); } queryStr.append("PREFIX rdfs: \n"); queryStr.append("PREFIX rdf: \n"); selStr.append("SELECT "); whereStr.append("WHERE { \n"); String properties = null; String objectClasses = null; String subjectClasses = null; Direction direction = null; int i = 0; int k = 0; for (ClassLink link :classLinks ) { properties = link.getPropertyURI(); objectClasses = link.getLinkedClassURI(); direction = link.getDirection(); if (i==0) subjectClasses = startClass; selStr.append("?c").append(i).append(" "); if(i == classLinks.size()) selStr.append("\n"); whereStr.append("?c").append(i). append(" rdf:type "). append("<"). append(subjectClasses). append(">"). append(".\n"); if(direction == Direction.forward) { whereStr.append("?c").append(i).append(" "); whereStr.append("<").append(properties).append("> "); whereStr.append("?c").append(i+1).append(".\n"); } else { whereStr.append("?c").append(i+1).append(" "); whereStr.append("<").append(properties).append("> "); whereStr.append("?c").append(i).append(".\n"); } subjectClasses = objectClasses; i++; k++; if(k>=num){ break; } } selStr.append("?c").append(i).append(" \n"); whereStr.append("?c").append(i).append(" rdf:type "). append("<"). append(subjectClasses). append(">"). append(".\n"); queryStr.append(selStr).append(whereStr).append("} LIMIT 100\n");; //System.out.println(queryStr); return queryStr.toString(); } }