Search in sources :

Example 51 with DatasetGraph

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

the class BuilderGraph method buildDataset.

public static DatasetGraph buildDataset(DatasetGraph dsg, ItemList list) {
    BuilderLib.checkTag(list, Tags.tagDataset);
    list = list.cdr();
    for (Item item : list) {
        if (!item.isTagged(Tags.tagGraph)) {
            // Not (graph ...) so it's (quad), short form quad or an error.
            Quad q = BuilderGraph.buildQuad(item.getList(), "Expected (graph ...) or a quad () as elements of a dataset");
            dsg.add(q);
            continue;
        }
        Node name = null;
        ItemList graphContent = item.getList().cdr();
        if (!graphContent.isEmpty() && graphContent.car().isNode()) {
            name = graphContent.car().getNode();
            graphContent = graphContent.cdr();
        }
        Graph g;
        if (name == null) {
            g = dsg.getDefaultGraph();
            if (g == null) {
                g = GraphFactory.createDefaultGraph();
                dsg.setDefaultGraph(g);
            }
        } else {
            g = dsg.getGraph(name);
            if (g == null) {
                g = GraphFactory.createDefaultGraph();
                dsg.addGraph(name, g);
            }
        }
        BuilderGraph.buildGraph(g, graphContent);
    }
    return dsg;
}
Also used : Item(org.apache.jena.sparql.sse.Item) Quad(org.apache.jena.sparql.core.Quad) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) Node(org.apache.jena.graph.Node) ItemList(org.apache.jena.sparql.sse.ItemList)

Example 52 with DatasetGraph

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

the class WriterGraph method writeDataset.

// ---- Workers
private static void writeDataset(IndentedWriter out, DatasetGraph ds, SerializationContext naming) {
    WriterLib.start(out, Tags.tagDataset, NL);
    writeGraph(out, ds.getDefaultGraph(), naming);
    out.ensureStartOfLine();
    for (Iterator<Node> iter = ds.listGraphNodes(); iter.hasNext(); ) {
        Node node = iter.next();
        out.ensureStartOfLine();
        Graph g = ds.getGraph(node);
        writeGraph(out, g, node, naming);
    }
    WriterLib.finish(out, Tags.tagDataset);
    out.ensureStartOfLine();
}
Also used : DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Graph(org.apache.jena.graph.Graph) Node(org.apache.jena.graph.Node)

Example 53 with DatasetGraph

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

the class TestDatasetGraphInMemoryBasic method orderingOfNodesFromFindIsCorrect.

@Test
public void orderingOfNodesFromFindIsCorrect() {
    final DatasetGraph dsg = DatasetGraphFactory.createTxnMem();
    final Node p = parseNode(":p");
    final Triple triple = parseTriple("(:s :p :o)");
    dsg.getDefaultGraph().add(triple);
    final Iterator<Triple> iter = dsg.getDefaultGraph().find(null, p, null);
    assertTrue(some(iter, triple::equals));
    final Node p1 = parseNode(":p1");
    final Quad quad = parseQuad("(:g1 :s1 :p1 :o1)");
    dsg.add(quad);
    final Iterator<Quad> iter2 = dsg.find(null, null, p1, null);
    assertTrue(some(iter2, quad::equals));
    Iter.print(err, iter2);
}
Also used : Triple(org.apache.jena.graph.Triple) SSE.parseTriple(org.apache.jena.sparql.sse.SSE.parseTriple) SSE.parseQuad(org.apache.jena.sparql.sse.SSE.parseQuad) Quad(org.apache.jena.sparql.core.Quad) SSE.parseNode(org.apache.jena.sparql.sse.SSE.parseNode) NodeFactory.createBlankNode(org.apache.jena.graph.NodeFactory.createBlankNode) Node(org.apache.jena.graph.Node) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 54 with DatasetGraph

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

the class TestDatasetGraphInMemoryBasic method prefixesAreManaged.

@Test
public void prefixesAreManaged() {
    final Node graphName = createURI("http://example/g");
    final DatasetGraph dsg = emptyDataset();
    dsg.addGraph(graphName, createGraphMem());
    final Dataset dataset = DatasetFactory.wrap(dsg);
    Model model = dataset.getNamedModel(graphName.getURI());
    final String testPrefix = "example";
    final String testURI = "http://example/";
    model.setNsPrefix(testPrefix, testURI);
    assertEquals(testURI, model.getNsPrefixURI(testPrefix));
    model.close();
    model = dataset.getNamedModel(graphName.getURI());
    final String nsURI = dataset.getNamedModel(graphName.getURI()).getNsPrefixURI(testPrefix);
    assertNotNull(nsURI);
    assertEquals(testURI, nsURI);
}
Also used : Dataset(org.apache.jena.query.Dataset) SSE.parseNode(org.apache.jena.sparql.sse.SSE.parseNode) NodeFactory.createBlankNode(org.apache.jena.graph.NodeFactory.createBlankNode) Node(org.apache.jena.graph.Node) Model(org.apache.jena.rdf.model.Model) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 55 with DatasetGraph

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

the class AbstractDatasetGraphTests method quad_04.

@Test
public void quad_04() {
    DatasetGraph dsg = emptyDataset();
    assertNotNull(dsg);
    Quad quad1 = SSE.parseQuad("(quad <g> <s> <p> <o1>)");
    Quad quad2 = SSE.parseQuad("(quad <g> <s> <p> <o2>)");
    dsg.add(quad1);
    dsg.add(quad2);
    Iterator<Quad> iter = dsg.find(NodeFactory.createURI("g"), NodeFactory.createURI("s"), null, null);
    assertTrue(iter.hasNext());
    Set<Quad> x = Iter.iter(iter).toSet();
    assertEquals(2, x.size());
    assertTrue(x.contains(quad1));
    assertTrue(x.contains(quad2));
}
Also used : Quad(org.apache.jena.sparql.core.Quad) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

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