Search in sources :

Example 1 with MemoryModel

use of org.apache.jena.assembler.JA.MemoryModel in project jena by apache.

the class TestInMemDatasetAssembler method directDataLinkForDefaultAndNamedGraphs.

@Test
public void directDataLinkForDefaultAndNamedGraphs() throws IOException {
    // first make a file of triples to load later
    final Model model = createDefaultModel();
    final Path triples = createTempFile("simpleExample", ".nt");
    final Resource triplesURI = model.createResource(triples.toFile().toURI().toString());
    final Resource simpleExample = model.createResource("test:simpleExample");
    simpleExample.addProperty(type, DatasetAssemblerVocab.tDatasetTxnMem);
    // add a default graph
    simpleExample.addProperty(data, triplesURI);
    // add a named graph
    final Resource namedGraphDef = model.createResource("test:namedGraphDef");
    simpleExample.addProperty(pNamedGraph, namedGraphDef);
    final Resource namedGraphName = model.createResource("test:namedGraphExample");
    namedGraphDef.addProperty(type, MemoryModel);
    namedGraphDef.addProperty(pGraphName, namedGraphName);
    namedGraphDef.addProperty(data, triplesURI);
    try (OutputStream out = new FileOutputStream(triples.toFile())) {
        write(out, model, NTRIPLES);
    }
    final Dataset dataset = assemble(simpleExample);
    final Model assembledDefaultModel = dataset.getDefaultModel();
    final Model assembledNamedModel = dataset.getNamedModel(namedGraphName.getURI());
    // we put the same triples in each model, so we check for the same triples in each model
    for (final Model m : new Model[] { assembledDefaultModel, assembledNamedModel }) {
        assertTrue(m.contains(simpleExample, pNamedGraph, namedGraphDef));
        assertTrue(m.contains(namedGraphDef, pGraphName, namedGraphName));
        assertTrue(m.contains(simpleExample, data, triplesURI));
    }
    final Iterator<Node> graphNodes = dataset.asDatasetGraph().listGraphNodes();
    assertTrue(graphNodes.hasNext());
    assertEquals(namedGraphName.asNode(), graphNodes.next());
    assertFalse(graphNodes.hasNext());
}
Also used : Path(java.nio.file.Path) Dataset(org.apache.jena.query.Dataset) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Node(org.apache.jena.graph.Node) Model(org.apache.jena.rdf.model.Model) MemoryModel(org.apache.jena.assembler.JA.MemoryModel) ModelFactory.createDefaultModel(org.apache.jena.rdf.model.ModelFactory.createDefaultModel) Resource(org.apache.jena.rdf.model.Resource) Test(org.junit.Test)

Aggregations

FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 MemoryModel (org.apache.jena.assembler.JA.MemoryModel)1 Node (org.apache.jena.graph.Node)1 Dataset (org.apache.jena.query.Dataset)1 Model (org.apache.jena.rdf.model.Model)1 ModelFactory.createDefaultModel (org.apache.jena.rdf.model.ModelFactory.createDefaultModel)1 Resource (org.apache.jena.rdf.model.Resource)1 Test (org.junit.Test)1