Ejemplo n.º 1
0
 @Override
 public void removeDataProperty(String dataPropertyName) {
   OWLDataFactory factory = manager.getOWLDataFactory();
   OWLDataProperty dataProperty =
       factory.getOWLDataProperty(IRI.create(prefix + dataPropertyName));
   OWLAxiom axiom = factory.getOWLDeclarationAxiom(dataProperty);
   axiom = changeManager.getAnnotatedAxiom(axiom);
   List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
   for (OWLAxiom ax : localContext.getReferencingAxioms(dataProperty)) {
     ax = ax.getAxiomWithoutAnnotations();
     ax = changeManager.getAnnotatedAxiom(ax);
     changes.add(new RemoveAxiom(localContext, ax));
   }
   try {
     synchronized (this) {
       changeManager.validateRemovalChange(axiom);
       manager.applyChanges(changes);
     }
   } catch (RemovalException ex) {
     logger.severe(
         ex.getMessage()
             + "Data property not defined in Ontology or not defined by "
             + "this application. Change ( removeDataProperty"
             + dataPropertyName
             + ") will not be applied. ");
   }
 }
Ejemplo n.º 2
0
  public void mergeOntologies() {
    List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
    for (OWLOntology ont : ontologies) {
      if (!ont.equals(targetOntology)) {

        // move the axioms
        for (OWLAxiom ax : ont.getAxioms()) {
          changes.add(new AddAxiom(targetOntology, ax));
        }

        // move ontology annotations
        for (OWLAnnotation annot : ont.getAnnotations()) {
          changes.add(new AddOntologyAnnotation(targetOntology, annot));
        }

        if (!targetOntology.getOntologyID().isAnonymous()) {
          // move ontology imports
          for (OWLImportsDeclaration decl : ont.getImportsDeclarations()) {
            if (ontologies.contains(ont.getOWLOntologyManager().getImportedOntology(decl))) {
              continue;
            }
            Optional<IRI> defaultDocumentIRI =
                targetOntology.getOntologyID().getDefaultDocumentIRI();
            if (defaultDocumentIRI.isPresent() && !decl.getIRI().equals(defaultDocumentIRI.get())) {
              changes.add(new AddImport(targetOntology, decl));
            } else {
              logger.warn(
                  "Merge: ignoring import declaration for ontology "
                      + targetOntology.getOntologyID()
                      + " (would result in target ontology importing itself).");
            }
          }
        }
      }
    }
    try {
      owlOntologyManager.applyChanges(changes);
    } catch (OWLOntologyChangeException e) {
      ErrorLogPanel.showErrorDialog(e);
    }
  }
 public void executeActions(Collection<? extends OWLOntology> ontologies)
     throws LintActionException {
   List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
   OWLOntologyManager ontologyManager = LintManagerFactory.getInstance().getOntologyManager();
   OWLDataFactory dataFactory = ontologyManager.getOWLDataFactory();
   LintReport<OWLObjectProperty> report;
   try {
     report = this.detected(ontologies);
     for (OWLOntology owlOntology : ontologies) {
       for (OWLObjectProperty objectProperty : report.getAffectedOWLObjects(owlOntology)) {
         changes.add(
             new RemoveAxiom(
                 owlOntology, dataFactory.getOWLTransitiveObjectPropertyAxiom(objectProperty)));
       }
     }
     ontologyManager.applyChanges(changes);
   } catch (LintException e) {
     throw new LintActionException(e);
   } catch (OWLOntologyChangeException e) {
     throw new LintActionException(e);
   }
 }
Ejemplo n.º 4
0
  public KuabaRepository copy(KuabaRepository kr, String url, File destination) {
    IRI iri;
    if (url == null) iri = IRI.generateDocumentIRI();
    else iri = IRI.create(url);

    try {
      OWLOntology model = (OWLOntology) kr.getModel();
      OWLOntology inst = manager.createOntology(iri);

      OntologyMigrator migrator = new OntologyMigrator(manager, model, inst);
      migrator.performMigration();

      EntityFindAndReplaceURIRenamer renamer =
          new EntityFindAndReplaceURIRenamer(
              manager,
              inst.getSignature(false),
              Collections.singleton(inst),
              model.getOntologyID().getOntologyIRI().toString(),
              iri.toString());

      if (renamer.hasErrors()) System.err.println("ERRO durante a cópia - rename");

      manager.applyChanges(renamer.getChanges());

      KuabaRepository repo = new OwlApiKuabaRepository(inst, manager.getOWLDataFactory());
      repoMap.put(inst.getOntologyID().getOntologyIRI(), repo);
      fileMap.put(repo, destination);

      return repo;

    } catch (OWLOntologyCreationException ex) {
      System.err.println("ERRO em copy");
      Logger.getLogger(OwlApiFileGateway.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
  }
  /**
   * This method has no conversion calls, to it can be invoked by subclasses that wish to modify it
   * afterwards.
   *
   * <p>FIXME not merging yet FIXME not including imported ontologies unless they are merged
   * *before* storage.
   *
   * @param merge
   * @return
   */
  protected OWLOntology exportToOWLOntology(boolean merge, IRI prefix) {

    long before = System.currentTimeMillis();

    // Create a new ontology
    OWLOntology root;
    OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
    IRI iri = IRI.create(prefix + _id);
    try {
      root = ontologyManager.createOntology(iri);
    } catch (OWLOntologyAlreadyExistsException e) {
      // It should be impossible, but just in case.
      ontologyManager.removeOntology(ontologyManager.getOntology(iri));
      try {
        root = ontologyManager.createOntology(iri);
      } catch (OWLOntologyAlreadyExistsException e1) {
        root = ontologyManager.getOntology(iri);
      } catch (OWLOntologyCreationException e1) {
        log.error("Failed to assemble root ontology for scope " + iri, e);
        root = null;
      }
    } catch (OWLOntologyCreationException e) {
      log.error("Failed to assemble root ontology for scope " + _id, e);
      root = null;
    }

    // Add the import declarations for directly managed ontologies.
    if (root != null) {

      if (merge) {

        final Set<OWLOntology> set = new HashSet<OWLOntology>();
        log.debug("Merging {} with its imports.", root);
        set.add(root);

        for (OWLOntologyID ontologyId : managedOntologies) {
          log.debug("Merging {} with {}.", ontologyId, root);
          set.add(getOntology(ontologyId, OWLOntology.class, true));
        }

        OWLOntologySetProvider provider =
            new OWLOntologySetProvider() {
              @Override
              public Set<OWLOntology> getOntologies() {
                return set;
              }
            };
        OWLOntologyMerger merger = new OWLOntologyMerger(provider);
        try {
          root = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), iri);

        } catch (OWLOntologyCreationException e) {
          log.error("Failed to merge imports for ontology " + iri, e);
          root = null;
        }

      } else {
        // Add the import declarations for directly managed ontologies.
        List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
        OWLDataFactory df = ontologyManager.getOWLDataFactory();

        String base = prefix + getID();
        for (int i = 0; i < backwardPathLength; i++)
          base = URIUtils.upOne(URI.create(base)).toString();
        base += "/";

        // The key set of managedOntologies contains the ontology IRIs, not their storage keys.
        for (OWLOntologyID ontologyId : managedOntologies) {
          // XXX some day the versionIRI will be the only physical reference for the ontology
          IRI physIRI = IRI.create(base + OntologyUtils.encode(ontologyId));
          changes.add(new AddImport(root, df.getOWLImportsDeclaration(physIRI)));
        }
        ontologyManager.applyChanges(changes);
      }
    }
    log.debug("OWL export of {} completed in {} ms.", getID(), System.currentTimeMillis() - before);

    return root;
  }