Search code examples
javaswingsparqljenajoptionpane

Make an Object[] from a ResultSet (for a JOptionPane dialog)


I have a SPARQL query which returns some results. I need to let the user to choose which of these results he will use next. I have coded this with a Scanner, but I want to convert it now to a JOptionPane dialog so that it is more user friendly. I assume it is achieved via JOptionPane.showInputDialog. However, I don't know how to “translate” the Jena ResultSet in the object list of the JOptionPane in the right format, and then extract the selected elements. How could I achieve this?

Query query = QueryFactory.create(queryString);
QueryExecution qe= QueryExecutionFactory.create(query, model);
ResultSet resultset = qe.execSelect();
ResultSet results = ResultSetFactory.copyResults(resultset); 
final ResultSet results2 = ResultSetFactory.copyResults(results);

System.out.println( "== Available Options ==" );
ResultSetFormatter.out(System.out, results, query);

System.out.println( "== Select Option ==" );
System.out.println( "== Type 0,1,2,3.. to choose Option ==" );

Scanner input = new Scanner( System.in );
final String inputs ;
inputs = input.next();

final String[] indices = inputs.split("\\s*,\\s*");

final List<QuerySolution> selectedSolutions = new ArrayList<QuerySolution>( indices.length ) {{
        final List<QuerySolution> solutions = ResultSetFormatter.toList( results2 );
        for ( final String index : indices ) {
            add( solutions.get( Integer.valueOf( index )));
        }
    }};

String s = selectedSolutions.toString();

Pattern p = Pattern.compile("#([^>]+)>");
Matcher m = p.matcher(s);

Solution

  • I'm not much a Java GUI programmer, but based on the GUI tutorials, it looks like this isn't too hard. You just need to be able to pass an array of the things that the user should be able to select. This means you'll probably to get those values of the result set, but that's not too hard. An example follows. As I mentioned, I'm not much of a Java GUI programmer, and what I've learned for this comes from the tutorial How to Make Dialogs and the javadoc for JOptionPane. It ends up looking more or less like this:

    option pane screenshot

    In this code, I haven't done anything with the result of JOptionPane.showInputDialog, but its the resource that was selected. Just cast it back to a Jena Resource, and you're all set.

    import java.io.ByteArrayInputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JOptionPane;
    
    import com.hp.hpl.jena.query.QueryExecutionFactory;
    import com.hp.hpl.jena.query.ResultSet;
    import com.hp.hpl.jena.rdf.model.Model;
    import com.hp.hpl.jena.rdf.model.ModelFactory;
    import com.hp.hpl.jena.rdf.model.Resource;
    
    public class SPARQLJOptions {
    
        /**
         * Content for a model
         */
        final static String modelContent = "" +
                "@prefix : <http://example.org/>.\n" +
                ":a :p :b, :c, :d .\n" +
                ":b :p :c, :a .\n" +
                "";
    
        /** 
         * A query to run against the model (will return b, c, and d).
         */
        final static String query = "" +
                "prefix : <http://example.org/> \n" +
                "select ?value where { \n" +
                " :a :p ?value \n" +
                "}" +
                "";
    
        public static void main(String[] args) {
            // Create a model and read in the model contents
            final Model model = ModelFactory.createDefaultModel();
            model.read( new ByteArrayInputStream( modelContent.getBytes()), null, "TTL" );
    
            // Run the query and copy the values of ?value into an List<Object>
            final ResultSet results = QueryExecutionFactory.create( query, model ).execSelect();
            List<Object> values = new ArrayList<Object>();
            while ( results.hasNext() ) {
                values.add( results.next().get( "value" ));
            }
    
            // Show an input dialog whose options are the elements in the list and
            // whose default selection is the first element of the list.
            Resource choice = (Resource) JOptionPane.showInputDialog(
                    null,                           // no container window
                    "Select a resource",
                    "Selecting Resource...",
                    JOptionPane.QUESTION_MESSAGE,
                    null,                           // no Icon
                    values.toArray(),
                    values.get(0));
    
            System.out.println( choice );
        }
    }