Search in sources :

Example 86 with Dataset

use of org.apache.jena.query.Dataset in project jena by apache.

the class ExTDB3 method main.

public static void main(String... argv) {
    String assemblerFile = "Store/tdb-assembler.ttl";
    // Find a particular description in the file where there are several: 
    Model spec = RDFDataMgr.loadModel(assemblerFile);
    // Find the right starting point for the description in some way.
    Resource root = null;
    if (false)
        // If you know the Resource URI:
        root = spec.createResource("http://example/myChoiceOfURI");
    else {
        // Alternatively, look for the a single resource of the right type. 
        try {
            // Find the required description - the file can contain descriptions of many different types.
            root = GraphUtils.findRootByType(spec, VocabTDB.tDatasetTDB);
            if (root == null)
                throw new JenaException("Failed to find a suitable root");
        } catch (TypeNotUniqueException ex) {
            throw new JenaException("Multiple types for: " + DatasetAssemblerVocab.tDataset);
        }
    }
    Dataset ds = (Dataset) Assembler.general.open(root);
}
Also used : JenaException(org.apache.jena.shared.JenaException) TypeNotUniqueException(org.apache.jena.sparql.util.TypeNotUniqueException) Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource)

Example 87 with Dataset

use of org.apache.jena.query.Dataset in project jena by apache.

the class ExTDB5 method main.

public static void main(String... argv) {
    // Direct way: Make a TDB-back Jena model in the named directory.
    String directory = "MyDatabases/DB1";
    Dataset dataset = TDBFactory.createDataset(directory);
    // Potentially expensive query.
    String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }";
    // See http://incubator.apache.org/jena/documentation/query/app_api.html
    Query query = QueryFactory.create(sparqlQueryString);
    QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
    try {
        ResultSet results = qexec.execSelect();
        for (; results.hasNext(); ) {
            QuerySolution soln = results.nextSolution();
            int count = soln.getLiteral("count").getInt();
            System.out.println("count = " + count);
        }
    } finally {
        qexec.close();
    }
    // Close the dataset.
    dataset.close();
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 88 with Dataset

use of org.apache.jena.query.Dataset in project jena by apache.

the class TDBGraphAssembler method open.

@Override
public Model open(Assembler a, Resource root, Mode mode) {
    // Make a model - the default model of the TDB dataset
    // [] rdf:type tdb:GraphTDB ;
    //    tdb:location "dir" ;
    // Make a named model.
    // [] rdf:type tdb:GraphTDB ;
    //    tdb:location "dir" ;
    //    tdb:graphName <http://example/name> ;
    // Location or dataset reference.
    String locationDir = getStringValue(root, pLocation);
    Resource dataset = getResourceValue(root, pDataset);
    if (locationDir != null && dataset != null)
        throw new AssemblerException(root, "Both location and dataset given: exactly one required");
    if (locationDir == null && dataset == null)
        throw new AssemblerException(root, "Must give location or refer to a dataset description");
    String graphName = null;
    if (root.hasProperty(pGraphName1))
        graphName = getAsStringValue(root, pGraphName1);
    if (root.hasProperty(pGraphName2))
        graphName = getAsStringValue(root, pGraphName2);
    if (root.hasProperty(pIndex))
        Log.warn(this, "Custom indexes not implemented yet - ignored");
    final Dataset ds;
    if (locationDir != null) {
        Location location = Location.create(locationDir);
        ds = TDBFactory.createDataset(location);
    } else
        ds = DatasetAssemblerTDB.make(dataset);
    try {
        if (graphName != null)
            return ds.getNamedModel(graphName);
        else
            return ds.getDefaultModel();
    } catch (RuntimeException ex) {
        ex.printStackTrace(System.err);
        throw ex;
    }
}
Also used : AssemblerException(org.apache.jena.assembler.exceptions.AssemblerException) VocabTDB.pDataset(org.apache.jena.tdb.assembler.VocabTDB.pDataset) Dataset(org.apache.jena.query.Dataset) VocabTDB.pLocation(org.apache.jena.tdb.assembler.VocabTDB.pLocation) Location(org.apache.jena.tdb.base.file.Location)

Example 89 with Dataset

use of org.apache.jena.query.Dataset in project jena by apache.

the class TestDatasetTDBPersist method dataset5.

@Test
public void dataset5() {
    String graphName = "http://example/";
    Triple triple = SSE.parseTriple("(<x> <y> <z>)");
    Dataset ds = graphLocation.getDataset();
    Graph g2 = ds.asDatasetGraph().getGraph(org.apache.jena.graph.NodeFactory.createURI(graphName));
    // Graphs only exists if they have a triple in them
    g2.add(triple);
    assertTrue(ds.containsNamedModel(graphName));
    List<String> x = Iter.toList(ds.listNames());
    List<String> y = Arrays.asList(graphName);
    assertEquals(x, y);
    assertEquals(1, ds.asDatasetGraph().size());
}
Also used : Triple(org.apache.jena.graph.Triple) Graph(org.apache.jena.graph.Graph) Dataset(org.apache.jena.query.Dataset) Test(org.junit.Test) ConfigTest(org.apache.jena.tdb.ConfigTest) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Example 90 with Dataset

use of org.apache.jena.query.Dataset in project jena by apache.

the class TestDatasetTDBPersist method dataset3.

@Test
public void dataset3() {
    Dataset ds = graphLocation.getDataset();
    Graph g1 = ds.getDefaultModel().getGraph();
    // Sometimes, under windows, deleting the files by 
    // graphLocation.clearDirectory does not work.  
    // Needed for safe tests on windows.
    g1.clear();
    Graph g2 = ds.getNamedModel("http://example/").getGraph();
    g2.add(new Triple(n0, n1, n2));
    assertTrue(g2.contains(n0, n1, n2));
    assertFalse(g1.contains(n0, n1, n2));
}
Also used : Triple(org.apache.jena.graph.Triple) Graph(org.apache.jena.graph.Graph) Dataset(org.apache.jena.query.Dataset) Test(org.junit.Test) ConfigTest(org.apache.jena.tdb.ConfigTest) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Aggregations

Dataset (org.apache.jena.query.Dataset)138 Test (org.junit.Test)63 BaseTest (org.apache.jena.atlas.junit.BaseTest)39 Model (org.apache.jena.rdf.model.Model)31 Resource (org.apache.jena.rdf.model.Resource)20 Node (org.apache.jena.graph.Node)13 RDFNode (org.apache.jena.rdf.model.RDFNode)8 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)7 RDFConnection (org.apache.jena.rdfconnection.RDFConnection)7 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)7 ConfigTest (org.apache.jena.tdb.ConfigTest)7 CmdException (jena.cmd.CmdException)6 Graph (org.apache.jena.graph.Graph)6 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)5 JenaTransactionException (org.apache.jena.sparql.JenaTransactionException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Path (java.nio.file.Path)4 Property (org.apache.jena.rdf.model.Property)4 OutputStream (java.io.OutputStream)3 Triple (org.apache.jena.graph.Triple)3