Java Code Examples for org.apache.jena.query.Dataset#close()

The following examples show how to use org.apache.jena.query.Dataset#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryCommand.java    From robot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Given a command line and a list of queries, execute 'query' using TDB and writing mappings to
 * disk.
 *
 * @param line CommandLine with options
 * @param queries List of queries
 * @throws IOException on problem running queries
 */
private static void executeOnDisk(CommandLine line, List<List<String>> queries)
    throws IOException {
  Dataset dataset = createTDBDataset(line);
  boolean keepMappings = CommandLineHelper.getBooleanValue(line, "keep-tdb-mappings", false);
  String tdbDir = CommandLineHelper.getDefaultValue(line, "tdb-directory", ".tdb");
  try {
    runQueries(line, dataset, queries);
  } finally {
    dataset.close();
    TDBFactory.release(dataset);
    if (!keepMappings) {
      boolean success = IOHelper.cleanTDB(tdbDir);
      if (!success) {
        logger.error(String.format("Unable to remove directory '%s'", tdbDir));
      }
    }
  }
}
 
Example 2
Source File: ExTDB5.java    From xcurator with Apache License 2.0 6 votes vote down vote up
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();
    
}
 
Example 3
Source File: ExTDB4.java    From xcurator with Apache License 2.0 6 votes vote down vote up
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) ;
    ResultSet results = qexec.execSelect() ;
    ResultSetFormatter.out(results) ;
    qexec.close() ;

    dataset.close();
}
 
Example 4
Source File: QueryCommand.java    From robot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Given a command line, an ontology, and a list of queries, run the queries over the ontology
 * with any options.
 *
 * @param line CommandLine with options
 * @param inputOntology OWLOntology to query
 * @param queries List of queries
 * @throws Exception on issue loading ontology or running queries
 */
private static void executeInMemory(
    CommandLine line, OWLOntology inputOntology, List<List<String>> queries) throws Exception {
  boolean useGraphs = CommandLineHelper.getBooleanValue(line, "use-graphs", false);
  Dataset dataset = QueryOperation.loadOntologyAsDataset(inputOntology, useGraphs);
  try {
    runQueries(line, dataset, queries);
  } finally {
    dataset.close();
    TDBFactory.release(dataset);
  }
}
 
Example 5
Source File: IOHelper.java    From robot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Given a path to an RDF/XML or TTL file and a RDF language, load the file as the default model
 * of a TDB dataset backed by a directory to improve processing time. Return the new dataset.
 *
 * <p>WARNING - this creates a directory at given tdbDir location!
 *
 * @param inputPath input path of RDF/XML or TTL file
 * @param tdbDir location to put TDB mappings
 * @return Dataset instantiated with triples
 * @throws JenaException if TDB directory can't be written to
 */
public static Dataset loadToTDBDataset(String inputPath, String tdbDir) throws JenaException {
  Dataset dataset;
  if (new File(tdbDir).isDirectory()) {
    dataset = TDBFactory.createDataset(tdbDir);
    if (!dataset.isEmpty()) {
      return dataset;
    }
  }
  dataset = TDBFactory.createDataset(tdbDir);
  logger.debug(String.format("Parsing input '%s' to dataset", inputPath));
  // Track parsing time
  long start = System.nanoTime();
  Model m;
  dataset.begin(ReadWrite.WRITE);
  try {
    m = dataset.getDefaultModel();
    FileManager.get().readModel(m, inputPath);
    dataset.commit();
  } catch (JenaException e) {
    dataset.abort();
    dataset.end();
    dataset.close();
    throw new JenaException(String.format(syntaxError, inputPath));
  } finally {
    dataset.end();
  }
  long time = (System.nanoTime() - start) / 1000000000;
  logger.debug(String.format("Parsing complete - took %s seconds", String.valueOf(time)));
  return dataset;
}
 
Example 6
Source File: ExTDB1.java    From xcurator with Apache License 2.0 5 votes vote down vote up
public static void main(String... argv)
{
    // Direct way: Make a TDB-back Jena model in the named directory.
    String directory = "MyDatabases/DB1" ;
    Dataset ds = TDBFactory.createDataset(directory) ;
    Model model = ds.getDefaultModel() ;
    
    // ... do work ...
    
    // Close the dataset.
    ds.close();
    
}
 
Example 7
Source File: QueryCommand.java    From robot with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Given an input state and command line arguments, query the ontolgy. The input ontology is not
 * changed.
 *
 * @param state the state from the previous command, or null
 * @param args the command-line arguments
 * @return the unchanged state
 * @throws Exception on any problem
 */
public CommandState execute(CommandState state, String[] args) throws Exception {
  CommandLine line = CommandLineHelper.getCommandLine(getUsage(), getOptions(), args);
  if (line == null) {
    return null;
  }

  IOHelper ioHelper = CommandLineHelper.getIOHelper(line);

  // If an update(s) are provided, run then return the OWLOntology
  // This is different than the rest of the Query operations because it returns an ontology
  // Whereas the others return query results
  List<String> updatePaths = CommandLineHelper.getOptionalValues(line, "update");
  if (!updatePaths.isEmpty()) {
    state = CommandLineHelper.updateInputOntology(ioHelper, state, line);
    OWLOntology inputOntology = state.getOntology();

    OWLOntology outputOntology = executeUpdate(state, inputOntology, ioHelper, updatePaths);
    CommandLineHelper.maybeSaveOutput(line, outputOntology);
    state.setOntology(outputOntology);
    return state;
  }

  boolean createTDB = CommandLineHelper.getBooleanValue(line, "create-tdb", false);
  if (createTDB) {
    // Create and close without deleting TDB directory
    Dataset dataset = createTDBDataset(line);
    dataset.close();
    TDBFactory.release(dataset);
    return state;
  }

  List<List<String>> queries = getQueries(line);

  boolean useTDB = CommandLineHelper.getBooleanValue(line, "tdb", false);
  if (useTDB) {
    // DOES NOT UPDATE STATE
    // This will not work with chained commands as it uses the `--input` option
    // Updating the state results in loading the ontology to memory
    executeOnDisk(line, queries);
  } else {
    state = CommandLineHelper.updateInputOntology(ioHelper, state, line);
    executeInMemory(line, state.getOntology(), queries);
  }

  return state;
}
 
Example 8
Source File: ExTDB2.java    From xcurator with Apache License 2.0 3 votes vote down vote up
public static void main(String... argv)
{
    String assemblerFile = "Store/tdb-assembler.ttl" ;

    Dataset ds = TDBFactory.assembleDataset(assemblerFile) ;
    
    // ... do work ...
    
    ds.close() ;
}