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);
}
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();
}
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;
}
}
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());
}
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));
}
Aggregations