コード例 #1
0
  private void runWithSeparateFiles() {
    if (owlFile == null) {
      throw new NullPointerException("You have to specify an ontology file!");
    }

    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLOntology ontology = null;
    OBDADataFactory obdaDataFactory = OBDADataFactoryImpl.getInstance();
    try {
      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);
      }

      Collection<Predicate> predicates = new ArrayList<>();

      for (OWLClass owlClass : ontology.getClassesInSignature()) {
        Predicate predicate = obdaDataFactory.getClassPredicate(owlClass.getIRI().toString());
        predicates.add(predicate);
      }
      for (OWLDataProperty owlDataProperty : ontology.getDataPropertiesInSignature()) {
        Predicate predicate =
            obdaDataFactory.getDataPropertyPredicate(owlDataProperty.getIRI().toString());
        predicates.add(predicate);
      }
      for (OWLObjectProperty owlObjectProperty : ontology.getObjectPropertiesInSignature()) {
        Predicate predicate =
            obdaDataFactory.getObjectPropertyPredicate(owlObjectProperty.getIRI().toString());
        predicates.add(predicate);
      }

      OBDAModel obdaModel = loadMappingFile(mappingFile);

      Ontology inputOntology = OWLAPI3TranslatorUtility.translate(ontology);

      obdaModel.declareAll(inputOntology.getVocabulary());

      int numPredicates = predicates.size();

      int i = 1;
      for (Predicate predicate : predicates) {
        System.err.println(String.format("Materializing %s (%d/%d)", predicate, i, numPredicates));
        serializePredicate(ontology, inputOntology, obdaModel, predicate, outputFile, format);
        i++;
      }

    } catch (OWLOntologyCreationException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
コード例 #2
0
 public boolean isOntologyDefined() {
   try {
     Concept concept = getConceptByIRI(OntologyRepository.ROOT_CONCEPT_IRI);
     return concept != null; // todo should check for more
   } catch (Exception e) {
     if (e.getMessage() != null
         && e.getMessage().contains(OntologyLumifyProperties.ONTOLOGY_TITLE.getPropertyName())) {
       return false;
     }
     throw new RuntimeException(e);
   }
 }
コード例 #3
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();
    }
  }