예제 #1
0
  /**
   * Serializes a batch of triples corresponding to a predicate into one file. Upper bound:
   * TRIPLE_LIMIT_PER_FILE.
   */
  private static int serializeTripleBatch(
      OWLOntology ontology,
      QuestOWLIndividualAxiomIterator iterator,
      String filePrefix,
      String predicateName,
      int fileCount,
      String format)
      throws Exception {
    String fileName = filePrefix + fileCount + ".owl";

    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

    // Main buffer
    OWLOntology aBox = manager.createOntology(IRI.create(predicateName));

    // Add the signatures
    for (OWLDeclarationAxiom axiom : ontology.getAxioms(AxiomType.DECLARATION)) {
      manager.addAxiom(aBox, axiom);
    }

    int tripleCount = 0;
    while (iterator.hasNext() && (tripleCount < TRIPLE_LIMIT_PER_FILE)) {
      manager.addAxiom(aBox, iterator.next());
      tripleCount++;
    }

    // BufferedOutputStream output = new BufferedOutputStream(new
    // FileOutputStream(outputPath.toFile()));
    // BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));
    BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
    manager.saveOntology(aBox, getOntologyFormat(format), new WriterDocumentTarget(writer));

    return tripleCount;
  }
예제 #2
0
  public void runWithSingleFile() {
    BufferedOutputStream output = null;
    BufferedWriter writer = null;

    try {
      final long startTime = System.currentTimeMillis();

      if (outputFile != null) {
        output = new BufferedOutputStream(new FileOutputStream(outputFile));
      } else {
        output = new BufferedOutputStream(System.out);
      }
      writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));

      OBDAModel obdaModel = loadMappingFile(mappingFile);

      OWLOntology ontology = null;
      OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
      OWLAPI3Materializer materializer = null;

      if (owlFile != null) {
        // Loading the OWL ontology from the file as with normal OWLReasoners
        ontology = manager.loadOntologyFromOntologyDocument((new File(owlFile)));

        if (disableReasoning) {
          /*
           * when reasoning is disabled, we extract only the declaration assertions for the vocabulary
           */
          ontology = extractDeclarations(manager, ontology);
        }

        Ontology onto = OWLAPI3TranslatorUtility.translate(ontology);
        obdaModel.declareAll(onto.getVocabulary());
        materializer = new OWLAPI3Materializer(obdaModel, onto, DO_STREAM_RESULTS);
      } else {
        ontology = manager.createOntology();
        materializer = new OWLAPI3Materializer(obdaModel, DO_STREAM_RESULTS);
      }

      // OBDAModelSynchronizer.declarePredicates(ontology, obdaModel);

      QuestOWLIndividualAxiomIterator iterator = materializer.getIterator();

      while (iterator.hasNext()) manager.addAxiom(ontology, iterator.next());

      OWLOntologyFormat ontologyFormat = getOntologyFormat(format);

      manager.saveOntology(ontology, ontologyFormat, new WriterDocumentTarget(writer));

      System.err.println("NR of TRIPLES: " + materializer.getTriplesCount());
      System.err.println("VOCABULARY SIZE (NR of QUERIES): " + materializer.getVocabularySize());

      materializer.disconnect();
      if (outputFile != null) output.close();

      final long endTime = System.currentTimeMillis();
      final long time = endTime - startTime;
      System.out.println("Elapsed time to materialize: " + time + " {ms}");

    } catch (Exception e) {
      System.out.println("Error materializing ontology:");
      e.printStackTrace();
    }
  }