0

我有一个返回一些结果的 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);
4

1 回答 1

2

我不是一个 Java GUI 程序员,但根据 GUI 教程,看起来这并不太难。您只需要能够传递用户应该能够选择的东西的数组。这意味着您可能会获得结果集的那些值,但这并不难。下面是一个例子。正如我所提到的,我不是一个 Java GUI 程序员,我从中学到的知识来自教程How to Make DialogsJOptionPane 的 javadoc。它最终看起来或多或少像这样:

选项窗格屏幕截图

在这段代码中,我没有对 的结果JOptionPane.showInputDialog任何事情,但它是被选中的资源。只需将其投射回 Jena 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 );
    }
}
于 2013-10-02T23:46:31.850 回答