Search in sources :

Example 51 with SailConnection

use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.

the class SailGraph method loadRDF.

/**
 * Load RDF data into the SailGraph. Supported formats include rdf-xml, n-triples, turtle, n3, trix, or trig.
 * Before loading data, the current transaction is successfully committed.
 *
 * @param input       The InputStream of RDF data.
 * @param baseURI     The baseURI for RDF data.
 * @param rdfParser   The {@link RDFParser} to use. It's {@link RDFHandler} will be
 *                    changed to an internal one. The main purpose of this is to use
 *                    a custom {@link ValueFactory}. It is recommended to use
 *                    <code>Rio.createParser(getFormat(format))</code> and set the
 *                    {@link ValueFactory} of the parser to something that
 *                    <code>extends</code> the {@link ValueFactory} of the
 *                    {@link Sail} used to initialize this class.
 *                    <p>
 *                    For example, <code>extend {@link ValueFactoryImpl}</code> if
 *                    you used a <code>GraphSail</code> to initialize this class.
 * @param baseGraph   The baseGraph to insert the data into.  May be null, in which case data is added to the default graph.
 * @param rdfHandlers Any number of {@link RDFHandler}s into which to pass parsed RDF statements <b>after</b>
 *                    they have been added to the graph.
 *                    These may be used, for example, for implementing your own logging.
 *                    Can be <code>null</code> if you only want to use the default
 *                    {@link RDFHandler} created internally.
 */
public void loadRDF(final InputStream input, final String baseURI, final RDFParser rdfParser, final String baseGraph, final RDFHandler... rdfHandlers) {
    try {
        this.commit();
        final SailConnection c = this.rawGraph.getConnection();
        try {
            c.begin();
            RDFHandler h = null == baseGraph ? new SailAdder(c) : new SailAdder(c, new URIImpl(baseGraph));
            if (rdfHandlers != null) {
                RDFHandler[] handlers = new RDFHandler[rdfHandlers.length + 1];
                handlers[0] = h;
                System.arraycopy(rdfHandlers, 0, handlers, 1, rdfHandlers.length);
                h = new RDFHandlerWrapper(handlers);
            }
            rdfParser.setRDFHandler(h);
            rdfParser.parse(input, baseURI);
            c.commit();
        } finally {
            c.rollback();
            c.close();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SailConnection(org.openrdf.sail.SailConnection) RDFHandlerWrapper(org.openrdf.rio.helpers.RDFHandlerWrapper) URIImpl(org.openrdf.model.impl.URIImpl) SailException(org.openrdf.sail.SailException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) RDFHandler(org.openrdf.rio.RDFHandler)

Example 52 with SailConnection

use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.

the class SailGraph method commit.

public void commit() {
    try {
        SailConnection sc = this.sailConnection.get();
        sc.commit();
        sc.begin();
    } catch (SailException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : SailConnection(org.openrdf.sail.SailConnection) SailException(org.openrdf.sail.SailException)

Example 53 with SailConnection

use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.

the class SailGraph method saveRDF.

/**
 * Save RDF data from the SailGraph.
 * Supported formats include rdf-xml, n-triples, turtle, n3, trix, or trig.
 *
 * @param output the OutputStream to which to write RDF data
 * @param format supported formats include rdf-xml, n-triples, turtle, n3, trix, or trig
 */
public void saveRDF(final OutputStream output, final String format) {
    try {
        this.commit();
        final SailConnection c = this.rawGraph.getConnection();
        try {
            c.begin();
            RDFWriter w = Rio.createWriter(getFormat(format), output);
            w.startRDF();
            CloseableIteration<? extends Statement, SailException> iter = c.getStatements(null, null, null, false);
            try {
                while (iter.hasNext()) {
                    w.handleStatement(iter.next());
                }
            } finally {
                iter.close();
            }
            w.endRDF();
        } finally {
            c.rollback();
            c.close();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SailConnection(org.openrdf.sail.SailConnection) RDFWriter(org.openrdf.rio.RDFWriter) SailException(org.openrdf.sail.SailException) SailException(org.openrdf.sail.SailException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RDFHandlerException(org.openrdf.rio.RDFHandlerException)

Example 54 with SailConnection

use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.

the class SailGraph method rollback.

public void rollback() {
    try {
        SailConnection sc = this.sailConnection.get();
        sc.rollback();
        sc.begin();
    } catch (SailException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : SailConnection(org.openrdf.sail.SailConnection) SailException(org.openrdf.sail.SailException)

Example 55 with SailConnection

use of org.openrdf.sail.SailConnection in project frames by tinkerpop.

the class SailFramesTest method testAll.

@Test
public void testAll() throws Exception {
    URI planet = new URIImpl("http://example.org/terms/planet");
    URI gasGiant = new URIImpl("http://example.org/terms/gasGiant");
    URI narrower = new URIImpl("http://www.w3.org/2004/02/skos/core#narrower");
    SailConnection sc = sail.getConnection();
    try {
        sc.begin();
        sc.addStatement(planet, RDFS.LABEL, new LiteralImpl("planet", "en"));
        sc.addStatement(gasGiant, RDFS.LABEL, new LiteralImpl("gas giant", "en"));
        sc.addStatement(planet, narrower, gasGiant);
        sc.commit();
    } finally {
        sc.close();
    }
    Vertex p = sailGraph.getVertex(planet.stringValue());
    FramedGraph<SailGraph> framedGraph = new FramedGraphFactory().create(sailGraph);
    Concept planetFrame = framedGraph.frame(p, Concept.class);
    assertNotNull(planetFrame);
    assertEquals("uri", planetFrame.getKind());
    // assertEquals("...", planetFrame.getValue());
    RDFFrame label = planetFrame.getLabel();
    assertNotNull(label);
    assertEquals("literal", label.getKind());
    assertEquals("en", label.getLang());
    assertEquals("planet", label.getValue());
    Iterable<Concept> narrowerConcepts = planetFrame.getNarrower();
    int counter = 0;
    for (Concept c : narrowerConcepts) {
        counter++;
    }
    assertEquals(counter, 1);
    Concept gasGiantFrame = narrowerConcepts.iterator().next();
    label = gasGiantFrame.getLabel();
    assertEquals("literal", label.getKind());
    assertEquals("en", label.getLang());
    assertEquals("gas giant", label.getValue());
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) Vertex(com.tinkerpop.blueprints.Vertex) SailConnection(org.openrdf.sail.SailConnection) SailGraph(com.tinkerpop.blueprints.impls.sail.SailGraph) URIImpl(org.openrdf.model.impl.URIImpl) URI(org.openrdf.model.URI) Test(org.junit.Test)

Aggregations

SailConnection (org.openrdf.sail.SailConnection)55 Test (org.junit.Test)34 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)28 URI (org.openrdf.model.URI)25 SailException (org.openrdf.sail.SailException)14 Sail (org.openrdf.sail.Sail)10 ValueFactory (org.openrdf.model.ValueFactory)9 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)8 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)5 RyaClient (org.apache.rya.api.client.RyaClient)5 Literal (org.openrdf.model.Literal)5 Resource (org.openrdf.model.Resource)5 Statement (org.openrdf.model.Statement)5 ParsedQuery (org.openrdf.query.parser.ParsedQuery)5 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)5 URIImpl (org.openrdf.model.impl.URIImpl)4 BindingSet (org.openrdf.query.BindingSet)4 Vertex (com.tinkerpop.blueprints.Vertex)3 EmptyBindingSet (org.openrdf.query.impl.EmptyBindingSet)3 RDFHandlerException (org.openrdf.rio.RDFHandlerException)3