All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.fortytwo.ripple.LinkedDataSailUsageExample Maven / Gradle / Ivy

There is a newer version: 1.5
Show newest version
package net.fortytwo.ripple;

import info.aduna.iteration.CloseableIteration;
import net.fortytwo.linkeddata.sail.LinkedDataSail;
import org.openrdf.query.BindingSet;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.impl.EmptyBindingSet;
import org.openrdf.query.parser.ParsedQuery;
import org.openrdf.query.parser.sparql.SPARQLParser;
import org.openrdf.sail.Sail;
import org.openrdf.sail.SailConnection;
import org.openrdf.sail.memory.MemoryStore;

/**
 * @author Joshua Shinavier (http://fortytwo.net)
 */
public class LinkedDataSailUsageExample {
    public static void main(final String[] args) throws Exception {
        Sail baseSail = new MemoryStore();
        baseSail.initialize();

        LinkedDataSail sail = new LinkedDataSail(baseSail);
        sail.initialize();

        SailConnection sc = sail.getConnection();
        try {
            SPARQLParser parser = new SPARQLParser();
            String queryStr = "PREFIX foaf: \n" +
                    "SELECT ?friend ?name WHERE {\n" +
                    "     foaf:knows ?friend .\n" +
                    "    ?friend foaf:name ?name .\n" +
                    "}";
            String baseURI = "http://example.org/";
            ParsedQuery query = parser.parseQuery(queryStr, baseURI);
            CloseableIteration results
                    = sc.evaluate(query.getTupleExpr(), query.getDataset(), new EmptyBindingSet(), false);

            while (results.hasNext()) {
                System.out.println(results.next().getBinding("name"));
            }
        } finally {
            sc.close();
        }

        sail.shutDown();
        baseSail.shutDown();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy