Search in sources :

Example 81 with DatasetGraph

use of org.apache.jena.sparql.core.DatasetGraph in project jena by apache.

the class TestStreamRDFThrift method dataset_02.

@Test
public void dataset_02() {
    DatasetGraph dsg1 = datasetGraph;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamRDFWriter.write(out, dsg1, Lang.RDFTHRIFT);
    byte[] bytes = out.toByteArray();
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    DatasetGraph dsg2 = DatasetGraphFactory.create();
    StreamRDF stream2 = StreamRDFLib.dataset(dsg2);
    BinRDF.inputStreamToStream(in, stream2);
    boolean b = IsoMatcher.isomorphic(dsg1, dsg2);
    assertTrue(b);
    // Stronger - same bNode and same as in original data.
    Node obj = Iter.first(dsg1.listGraphNodes(), Node::isBlank);
    termAsObject(dsg1, obj);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StreamRDF(org.apache.jena.riot.system.StreamRDF) Node(org.apache.jena.graph.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Example 82 with DatasetGraph

use of org.apache.jena.sparql.core.DatasetGraph in project jena by apache.

the class SPARQL_Upload method uploadWorker.

/** Process an HTTP file upload of RDF with additiona name field for the graph name.
     *  We can't stream straight into a dataset because the graph name can be after the data.
     *  @return graph name and count
     */
// ?? Combine with Upload.fileUploadWorker
// Difference is the handling of names for graphs.
private static UploadDetails uploadWorker(HttpAction action, String base) {
    DatasetGraph dsgTmp = DatasetGraphFactory.create();
    ServletFileUpload upload = new ServletFileUpload();
    String graphName = null;
    boolean isQuads = false;
    long count = -1;
    String name = null;
    ContentType ct = null;
    Lang lang = null;
    try {
        FileItemIterator iter = upload.getItemIterator(action.request);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            String fieldName = item.getFieldName();
            InputStream stream = item.openStream();
            if (item.isFormField()) {
                // Graph name.
                String value = Streams.asString(stream, "UTF-8");
                if (fieldName.equals(HttpNames.paramGraph)) {
                    graphName = value;
                    if (graphName != null && !graphName.equals("") && !graphName.equals(HttpNames.valueDefault)) {
                        IRI iri = IRIResolver.parseIRI(value);
                        if (iri.hasViolation(false))
                            ServletOps.errorBadRequest("Bad IRI: " + graphName);
                        if (iri.getScheme() == null)
                            ServletOps.errorBadRequest("Bad IRI: no IRI scheme name: " + graphName);
                        if (iri.getScheme().equalsIgnoreCase("http") || iri.getScheme().equalsIgnoreCase("https")) {
                            // Redundant??
                            if (iri.getRawHost() == null)
                                ServletOps.errorBadRequest("Bad IRI: no host name: " + graphName);
                            if (iri.getRawPath() == null || iri.getRawPath().length() == 0)
                                ServletOps.errorBadRequest("Bad IRI: no path: " + graphName);
                            if (iri.getRawPath().charAt(0) != '/')
                                ServletOps.errorBadRequest("Bad IRI: Path does not start '/': " + graphName);
                        }
                    }
                } else if (fieldName.equals(HttpNames.paramDefaultGraphURI))
                    graphName = null;
                else
                    // Add file type?
                    action.log.info(format("[%d] Upload: Field=%s ignored", action.id, fieldName));
            } else {
                // Process the input stream
                name = item.getName();
                if (name == null || name.equals("") || name.equals("UNSET FILE NAME"))
                    ServletOps.errorBadRequest("No name for content - can't determine RDF syntax");
                String contentTypeHeader = item.getContentType();
                ct = ContentType.create(contentTypeHeader);
                lang = RDFLanguages.contentTypeToLang(ct.getContentType());
                if (lang == null) {
                    lang = RDFLanguages.filenameToLang(name);
                    // present we wrap the stream accordingly
                    if (name.endsWith(".gz"))
                        stream = new GZIPInputStream(stream);
                }
                if (lang == null)
                    // Desperate.
                    lang = RDFLanguages.RDFXML;
                isQuads = RDFLanguages.isQuads(lang);
                action.log.info(format("[%d] Upload: Filename: %s, Content-Type=%s, Charset=%s => %s", action.id, name, ct.getContentType(), ct.getCharset(), lang.getName()));
                StreamRDF x = StreamRDFLib.dataset(dsgTmp);
                StreamRDFCounting dest = StreamRDFLib.count(x);
                ActionSPARQL.parse(action, dest, stream, lang, base);
                count = dest.count();
            }
        }
        if (graphName == null || graphName.equals(""))
            graphName = HttpNames.valueDefault;
        if (isQuads)
            graphName = null;
        return new UploadDetails(graphName, dsgTmp, count);
    } catch (ActionErrorException ex) {
        throw ex;
    } catch (Exception ex) {
        ServletOps.errorOccurred(ex);
        return null;
    }
}
Also used : IRI(org.apache.jena.iri.IRI) ContentType(org.apache.jena.atlas.web.ContentType) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) Lang(org.apache.jena.riot.Lang) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) GZIPInputStream(java.util.zip.GZIPInputStream) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) StreamRDF(org.apache.jena.riot.system.StreamRDF) StreamRDFCounting(org.apache.jena.riot.lang.StreamRDFCounting) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Example 83 with DatasetGraph

use of org.apache.jena.sparql.core.DatasetGraph in project jena by apache.

the class ExQuadFilter method setup.

/** Example setup - in-memory dataset with two graphs, one triple in each */
private static Dataset setup() {
    Dataset ds = TDBFactory.createDataset();
    DatasetGraph dsg = ds.asDatasetGraph();
    Quad q1 = SSE.parseQuad("(<http://example/g1> <http://example/s> <http://example/p> <http://example/o1>)");
    Quad q2 = SSE.parseQuad("(<http://example/g2> <http://example/s> <http://example/p> <http://example/o2>)");
    dsg.add(q1);
    dsg.add(q2);
    return ds;
}
Also used : Quad(org.apache.jena.sparql.core.Quad) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 84 with DatasetGraph

use of org.apache.jena.sparql.core.DatasetGraph in project jena by apache.

the class TDB method sync.

/** Sync a TDB-backed DatasetGraph. Do nothing if not TDB-backed. */
public static void sync(DatasetGraph dataset) {
    if (dataset == null)
        return;
    // Should be: SystemARQ.sync(dataset) ;
    if (dataset instanceof DatasetGraphTDB) {
        syncObject(dataset);
        return;
    }
    if (dataset instanceof DatasetGraphTransaction) {
        DatasetGraphTransaction dsgt = (DatasetGraphTransaction) dataset;
        // This only sync if the dataset has not been used transactionally.
        // Can't sync transactional datasets (it's meaningless)
        dsgt.syncIfNotTransactional();
        return;
    }
    // May be a general purpose dataset with TDB objects in it.
    sync(dataset.getDefaultGraph());
    Iterator<Node> iter = dataset.listGraphNodes();
    // Avoid iterator concurrency.
    iter = Iter.toList(iter).iterator();
    for (; iter.hasNext(); ) {
        Node n = iter.next();
        Graph g = dataset.getGraph(n);
        sync(g);
    }
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) Node(org.apache.jena.graph.Node) DatasetGraphTransaction(org.apache.jena.tdb.transaction.DatasetGraphTransaction) DatasetGraphTDB(org.apache.jena.tdb.store.DatasetGraphTDB)

Example 85 with DatasetGraph

use of org.apache.jena.sparql.core.DatasetGraph in project jena by apache.

the class TestTransactionTDB method transaction_50.

@Test
public void transaction_50() {
    // This assumes you have two datasets on the same location.
    // That's not necessarily true for uncached memory datasets, 
    // where you get two separate datasets so changes to one are
    // not seen by the other at all.
    Dataset ds1 = create();
    Dataset ds2 = create();
    ds1.begin(WRITE);
    ds1.getDefaultModel().getGraph().add(triple1);
    ds2.begin(READ);
    assertTrue(ds2.getDefaultModel().isEmpty());
    ds2.commit();
    ds1.commit();
    ds2.begin(READ);
    // See ds1 updates
    Graph g = ds2.getDefaultModel().getGraph();
    DatasetGraph dsg = ds2.asDatasetGraph();
    g = dsg.getDefaultGraph();
    boolean b0 = g.isEmpty();
    boolean b1 = ds2.getDefaultModel().isEmpty();
    assertFalse(ds2.getDefaultModel().isEmpty());
    assertEquals(1, ds2.getDefaultModel().size());
    ds2.commit();
}
Also used : DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) Dataset(org.apache.jena.query.Dataset) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) ConfigTest(org.apache.jena.tdb.ConfigTest)

Aggregations

DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)222 Test (org.junit.Test)132 BaseTest (org.apache.jena.atlas.junit.BaseTest)59 Quad (org.apache.jena.sparql.core.Quad)47 Node (org.apache.jena.graph.Node)29 Graph (org.apache.jena.graph.Graph)18 StoreConnection (org.apache.jena.tdb.StoreConnection)17 DatasetGraphTxn (org.apache.jena.tdb.transaction.DatasetGraphTxn)15 Model (org.apache.jena.rdf.model.Model)10 IOException (java.io.IOException)7 Triple (org.apache.jena.graph.Triple)7 Dataset (org.apache.jena.query.Dataset)7 StreamRDF (org.apache.jena.riot.system.StreamRDF)7 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)5 ResultSet (org.apache.jena.query.ResultSet)5 JsonLDWriteContext (org.apache.jena.riot.JsonLDWriteContext)5 Element (org.apache.jena.sparql.syntax.Element)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 RiotException (org.apache.jena.riot.RiotException)4 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)4