javardfsparqljenatdb

Jena: Getting an empty result set


I am getting an empty result set when I try to retrieve the data stored in a jena model.

This is the code to load the data (I have removed the imports for brevity)

package basic;    

//imports here

public class DataLoaderQn {
    public static void main(String[] args) throws FileNotFoundException {

        String resourceURI = "http://www.abc123.com/riskmodelling/static/risks";
        String directory = "D:/mywork/dp/projs/static-risks/data/test";
        Dataset dataset = TDBFactory.createDataset(directory);
        Model model = null;

        try {
            dataset.begin(ReadWrite.WRITE);
            model = dataset.getDefaultModel();
            model.enterCriticalSection(Lock.WRITE);

            model = model.removeAll();

            Resource projectNatureRes = model.createResource(resourceURI+":PROJECT_NATURE");

            Property projectNatureRiskProps = model.createProperty(resourceURI + ":COMPLEX_FUNCTIONALITY");         
            Bag projectNatureRisks = model.createBag();         
            projectNatureRisks.add("More defects");
            projectNatureRisks.add("Effort estimation inaccurate");         
            projectNatureRes.addProperty(projectNatureRiskProps, projectNatureRisks);

            Property migrationRiskProps = model.createProperty(resourceURI + ":MIGRATION");         
            Bag migrationRisks = model.createBag();         
            migrationRisks.add("Lack of knowledge of exsting application");
            migrationRisks.add("Documentation not available");          
            projectNatureRes.addProperty(migrationRiskProps, migrationRisks);

            model.write(System.out);
            model.write(new FileOutputStream(new File(directory + "/Project_risk.ttl")), "N-TRIPLES");
            model.commit();

            dataset.commit();

            TDB.sync(model);

        } finally {
            dataset.end();
            model.leaveCriticalSection();
        }

    }

}

And this is how I read in the data from a java program

public class ReadBasicQn {
public static void main(String[] args) {
    String directory = "D:/mywork/dp/projs/static-risks/data/test";
    Dataset dataset = TDBFactory.createDataset(directory);

    Model model = null;
    ResultSet rs;
    QueryExecution qexeExecution = null;
    try{
        /*model = ModelFactory.createDefaultModel();
        TDBLoader.loadModel(model, directory + "/Project_risk.ttl");*/

        model = dataset.getDefaultModel();

        String queryString = "PREFIX proj: <http://www.abc123.com/riskmodelling/static/risks#> ";
        queryString += "select ?risks where ";
        queryString += "{proj:PROJECT_NATURE proj:COMPLEX_FUNCTIONALITY ?risks}";


        String queryString2 = "SELECT * WHERE { ?s ?p ?o }";

        Query q = QueryFactory.create(queryString);
        qexeExecution = QueryExecutionFactory.create(q, model);
        rs = qexeExecution.execSelect();
        ResultSetFormatter.out(System.out, rs);
        qexeExecution.close();

        q = QueryFactory.create(queryString2);
        qexeExecution = QueryExecutionFactory.create(q, model);
        rs = qexeExecution.execSelect();
        ResultSetFormatter.out(System.out, rs);

        /*while(rs.hasNext()){
            QuerySolution qSol = rs.nextSolution();
            RDFNode n = qSol.get("risks");
            System.out.println(n);
        }*/

    }finally{
        qexeExecution.close();
    }
}

}

The output of the second query (select *) from the ResultSetFormatter shows

------------------------------------------------------------------------------------------------------------------------------------------------
| s                                                                 | p                                                                        | o                                                |
===================================================================================================================================================================================================
| <http://www.abc123.com/riskmodelling/static/risks:PROJECT_NATURE> | <http://www.abc123.com/riskmodelling/static/risks:COMPLEX_FUNCTIONALITY> | _:b0                                             |
| <http://www.abc123.com/riskmodelling/static/risks:PROJECT_NATURE> | <http://www.abc123.com/riskmodelling/static/risks:MIGRATION>             | _:b1                                             |
| _:b0                                                              | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>                        | <http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag> |
| _:b0                                                              | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1>                          | "More defects"                                   |
| _:b0                                                              | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_2>                          | "Effort estimation inaccurate"                   |
| _:b1                                                              | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>                        | <http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag> |
| _:b1                                                              | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1>                          | "Lack of knowledge of exsting application"       |
| _:b1                                                              | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_2>                          | "Documentation not available"                    |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

which means data is available and correctly loaded (correct ?). The custom query however returns the following output.

---------
| risks |
=========
---------

Any help is appreciated. I have just about started with Jena, so maybe I am doing something really foolish.


Solution

  • There's a typo. Your prefix ends in #, but in your URIs, there's a :. Here's your prefix declaration and URI, lined up:

    prefix proj: http://www.abc123.com/riskmodelling/static/risks#>
                 http://www.abc123.com/riskmodelling/static/risks:PROJECT_NATURE
                                                                 ^
                                                                 |
    

    In your URI, it's risks[COLON]PROJECT_NATURE, not risks[HASH]PROJECT_NATURE. You need to change your prefix to:

    prefix proj: http://www.abc123.com/riskmodelling/static/risks:>
    

    or change your data to

    Resource projectNatureRes = model.createResource(resourceURI+"#PROJECT_NATURE");
    // ...and in a few other places, too