Java Code Examples for org.apache.jena.tdb.TDBFactory#release()

The following examples show how to use org.apache.jena.tdb.TDBFactory#release() . 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: 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 3
Source File: ReportOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Given an input path to an ontology and a map of options, create a Report object and run (on TDB
 * dataset) the report queries specified in a profile (from options, or default). Return the
 * completed Report object. The labels option is not supported with TDB.
 *
 * @param inputPath path to load triples to TDB
 * @param options map of report options
 * @return Report object with violation details
 * @throws Exception on any loading, query, or reporting error
 */
public static Report getTDBReport(String inputPath, Map<String, String> options)
    throws Exception {
  String tdbDir = OptionsHelper.getOption(options, "tdb-directory", ".tdb");

  // Load dataset
  // Fail if the input path is not in RDF/XML or TTL
  Dataset dataset = IOHelper.loadToTDBDataset(inputPath, tdbDir);

  Report report;
  boolean keepMappings = OptionsHelper.optionIsTrue(options, "keep-tdb-mappings");
  try {
    report = getTDBReport(dataset, options);
  } finally {
    // Close and release
    dataset.close();
    try {
      TDBFactory.release(dataset);
    } catch (TDBTransactionException e) {
      // Do nothing - already released
    }

    if (!keepMappings) {
      // Maybe delete
      boolean success = IOHelper.cleanTDB(tdbDir);
      if (!success) {
        logger.error(String.format("Unable to remove directory '%s'", tdbDir));
      }
    }
  }

  return report;
}
 
Example 4
Source File: JenaTDBDataSourceTest.java    From Server.Java with MIT License 5 votes vote down vote up
/**
 *
 * @throws Exception
 */
@AfterClass
public static void tearDownClass() throws Exception {
    TDBFactory.release(dataset);
    File[] files = jena.listFiles();
    for (File f : files) {
        f.delete();
    }
    jena.delete();
 
}
 
Example 5
Source File: SparqlDataSourceTest.java    From Server.Java with MIT License 5 votes vote down vote up
/**
 *
 * @throws Exception
 */
@AfterClass
public static void tearDownClass() throws Exception {
    fuseki.stop();

    TDBFactory.release(dataset);
    File[] files = jena.listFiles();
    for (File f : files) {
        f.delete();
    }
    jena.delete();
 
}
 
Example 6
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;
}