从ResultSet创建一个Object [](用于JOptionPane对话框)

时间:2013-10-02 21:32:53

标签: java swing sparql jena joptionpane

我有一个返回一些结果的SPARQL查询。我需要让用户选择接下来将使用哪些这些结果。我用扫描仪对此进行了编码,但我想将其转换为JOptionPane对话框,以便更加用户友好。我认为它是通过JOptionPane.showInputDialog实现的。但是,我不知道如何以正确的格式“翻译”JOptionPane的对象列表中的Jena ResultSet,然后提取所选元素。我怎么能做到这一点?

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);

1 个答案:

答案 0 :(得分:2)

我不是一个Java GUI程序员,但基于GUI教程,看起来这并不太难。您只需要能够传递用户应该能够选择的事物的数组。这意味着你可能会得到结果集的那些值,但这并不太难。一个例子如下。正如我所提到的,我不是一个Java GUI程序员,我为此学到的东西来自教程How to Make Dialogsjavadoc for JOptionPane。它最终看起来或多或少像这样:

option pane screenshot

在这段代码中,我没有完成任何结果为JOptionPane.showInputDialog的东西,但它是所选择的资源。只需将它投回耶拿Resource,你就已经完成了。

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 );
    }
}