Search in sources :

Example 31 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class RDFOutput method asRDF.

public Resource asRDF(Model model, ResultSet resultSet, boolean includeRowIndex) {
    Resource results = model.createResource();
    // This always goes in.
    results.addProperty(RDF.type, ResultSetGraphVocab.ResultSet);
    for (String vName : resultSet.getResultVars()) results.addProperty(ResultSetGraphVocab.resultVariable, vName);
    int count = 0;
    for (; resultSet.hasNext(); ) {
        count++;
        QuerySolution rBind = resultSet.nextSolution();
        Resource thisSolution = model.createResource();
        if (includeTypeProperties)
            thisSolution.addProperty(RDF.type, ResultSetGraphVocab.ResultSolution);
        results.addProperty(ResultSetGraphVocab.solution, thisSolution);
        if (includeRowIndex) {
            // This can lead to equivalent result sets having different graphs
            // Best used if and only if query was completely sorted. 
            Literal x = model.createTypedLiteral(count + "", XSDDatatype.XSDinteger);
            thisSolution.addLiteral(ResultSetGraphVocab.index, x);
        }
        Iterator<String> iter = getAllVars() ? rBind.varNames() : resultSet.getResultVars().iterator();
        for (; iter.hasNext(); ) {
            Resource thisBinding = model.createResource();
            String rVar = iter.next();
            RDFNode n = rBind.get(rVar);
            if (n == null)
                continue;
            //                }
            if (includeTypeProperties)
                thisBinding.addProperty(RDF.type, ResultSetGraphVocab.ResultBinding);
            thisBinding.addProperty(ResultSetGraphVocab.variable, rVar);
            thisBinding.addProperty(ResultSetGraphVocab.value, n);
            thisSolution.addProperty(ResultSetGraphVocab.binding, thisBinding);
        }
    }
    results.addProperty(ResultSetGraphVocab.size, model.createTypedLiteral(count));
    addPrefixes(model);
    return results;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Literal(org.apache.jena.rdf.model.Literal) Resource(org.apache.jena.rdf.model.Resource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 32 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class ResultSetApply method apply.

public void apply() {
    proc.start(rs);
    for (; rs.hasNext(); ) {
        QuerySolution qs = rs.next();
        proc.start(qs);
        for (String varName : rs.getResultVars()) {
            RDFNode node = qs.get(varName);
            // node may be null
            proc.binding(varName, node);
        }
        proc.finish(qs);
    }
    proc.finish(rs);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 33 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class TestSyntaxTransform method testUpdateModel.

private void testUpdateModel(String input, String output, String varStr, String valStr) {
    UpdateRequest req1 = UpdateFactory.create(PREFIX + input);
    UpdateRequest reqExpected = UpdateFactory.create(PREFIX + output);
    Map<String, RDFNode> map = new HashMap<>();
    Node n = SSE.parseNode(valStr);
    RDFNode x = ModelUtils.convertGraphNodeToRDFNode(n);
    map.put(varStr, x);
    UpdateRequest reqTrans = UpdateTransformOps.transformUpdate(req1, map);
    // Crude.
    String x1 = reqExpected.toString().replaceAll("[ \n\t]", "");
    String x2 = reqTrans.toString().replaceAll("[ \n\t]", "");
    //assertEquals(reqExpected, reqTrans) ;
    assertEquals(x1, x2);
}
Also used : UpdateRequest(org.apache.jena.update.UpdateRequest) HashMap(java.util.HashMap) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 34 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class ResultSetUtils method resultSetToStringList.

/**
     * Extracts a List filled with the binding of selectElement variable for each
     * query solution, turned into a string (URIs or lexical forms).  
     * Exhausts the result set.  Create a rewindable one to use multiple times. 
     * @see org.apache.jena.query.ResultSetFactory
     */
public static List<String> resultSetToStringList(ResultSet rs, String selectElement, String literalOrResource) {
    // feature suggested by James Howison
    List<String> items = new ArrayList<>();
    while (rs.hasNext()) {
        QuerySolution qs = rs.nextSolution();
        RDFNode rn = qs.get(selectElement);
        if (rn.isLiteral())
            items.add(((Literal) rn).getLexicalForm());
        else if (rn.isURIResource())
            items.add(((Resource) rn).getURI());
        else if (rn.isAnon()) {
            items.add(((Resource) rn).getId().getLabelString());
        } else
            throw new ARQException("Unknow thing in results : " + rn);
    }
    return items;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ARQException(org.apache.jena.sparql.ARQException) Literal(org.apache.jena.rdf.model.Literal) ArrayList(java.util.ArrayList) Resource(org.apache.jena.rdf.model.Resource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 35 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class ResultSetUtils method resultSetToList.

/**
     * Extracts a List filled with the binding of selectElement variable for each
     * query solution as RDFNodes (Resources or Literals).
     * Exhausts the result set.  Create a rewindable one to use multiple times. 
     *   
     * @see org.apache.jena.query.ResultSetFactory   
     */
public static List<RDFNode> resultSetToList(ResultSet rs, String selectElement) {
    // feature suggested by James Howison
    List<RDFNode> items = new ArrayList<>();
    while (rs.hasNext()) {
        QuerySolution qs = rs.nextSolution();
        RDFNode n = qs.get(selectElement);
        items.add(n);
    }
    return items;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ArrayList(java.util.ArrayList) RDFNode(org.apache.jena.rdf.model.RDFNode)

Aggregations

RDFNode (org.apache.jena.rdf.model.RDFNode)48 Resource (org.apache.jena.rdf.model.Resource)20 QuerySolution (org.apache.jena.query.QuerySolution)9 Model (org.apache.jena.rdf.model.Model)9 Node (org.apache.jena.graph.Node)8 Literal (org.apache.jena.rdf.model.Literal)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 Dataset (org.apache.jena.query.Dataset)5 Property (org.apache.jena.rdf.model.Property)5 Date (java.sql.Date)4 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)4 TextIndexException (org.apache.jena.query.text.TextIndexException)4 Extractor (infoeval.main.WikiData.Extractor)3 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)3 ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)3 Statement (org.apache.jena.rdf.model.Statement)3 ARQException (org.apache.jena.sparql.ARQException)3 UpdateRequest (org.apache.jena.update.UpdateRequest)3 File (java.io.File)2