コード例 #1
0
 public OWLOntology getMergedOntology() {
   final IRI mergedOntologyIRI =
       IRI.create(queryOntology.getOntologyID().getDefaultDocumentIRI() + "-merged");
   final OWLOntologyManager mm = controller.getOWLOntologyManager();
   if (mm.contains(mergedOntologyIRI)) {
     return mm.getOntology(mergedOntologyIRI);
   } else {
     try {
       final OWLOntology mergedOntology = mm.createOntology(mergedOntologyIRI);
       mm.setOntologyFormat(mergedOntology, new RDFXMLOntologyFormat());
       final String mergedOntologyFileName =
           mergedOntologyIRI
                   .toURI()
                   .toString()
                   .substring(mergedOntologyIRI.toURI().toString().lastIndexOf("/") + 1)
               + ".owl";
       mm.setOntologyDocumentIRI(
           mergedOntology,
           IRI.create(
               controller.getRuleSpec().getOutputDir().toURI() + "/" + mergedOntologyFileName));
       mm.applyChange(
           new AddImport(
               mergedOntology,
               mm.getOWLDataFactory()
                   .getOWLImportsDeclaration(
                       queryOntology.getOntologyID().getDefaultDocumentIRI())));
       return mergedOntology;
     } catch (OWLOntologyCreationException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       return null;
     }
   }
 }
コード例 #2
0
  /** This method gets an OWL ontology used as an input for the supplied rule. */
  private OWLOntology getInputOntologyForRule(IRI iri) throws OWLOntologyCreationException {
    if (iri.getScheme().equals(PREFIX)) {
      iri = getOntologyIRIForRuleName(iri.toString().substring(PREFIX.length()));
    }

    final OWLOntologyManager m = controller.getOWLOntologyManager();
    OWLOntology o;
    if (m.contains(iri)) {
      o = m.getOntology(iri);
    } else {
      o = m.loadOntology(iri);
    }
    return o;
  }
コード例 #3
0
    @Override
    protected boolean exec() {
      Timer t = new Timer();
      if (ontology == null) {
        extractor = null;
        return true;
      }
      IRI iri =
          IRI.create("http://www.cs.ox.ac.uk/isg/tools/MORe/ontologies/inverseRewritingModule.owl");
      extractor.findLsignature(ontology, LogicFragment.SHOIQ);
      if (containsNonInternalClasses(
          extractor
              .getCompSignature())) { // then the ontology goes beyond SHOIQ and we need to work
        // with a SHOIQ module rather than the whole ontology
        Set<OWLEntity> aux = getNonInternalClasses(extractor.getLsignature());
        if (aux.isEmpty()) {
          extractor = null;
          Logger_MORe.logDebug(
              t.duration()
                  + "s to find Lsignature with inverseRewriting (failed - empty SHOIQ-signature)");
          return true;
        }
        SyntacticLocalityModuleExtractor moduleExtractor =
            new SyntacticLocalityModuleExtractor(manager, ontology, ModuleType.BOT);
        try {
          //					ontology = manager.createOntology(iri);
          //					manager.addAxioms(ontology, moduleExtractor.extract(aux));
          ontology = moduleExtractor.extractAsOntology(aux, iri);
        } catch (OWLOntologyCreationException e1) {
          extractor = null;
          e1.printStackTrace();
          Logger_MORe.logDebug(
              t.duration()
                  + "s to find Lsignature with inverseRewriting (failed - exception creating a SHOIQ module)");
          return true;
        }
      }

      // if we get this far then we have a nonempty ontology (maybe module) that we need to
      // normalize and then rewrite
      OWLNormalization4MORe normalization = new OWLNormalization4MORe(ontology, true, true, true);
      Rewriter rewriter =
          new Rewriter(normalization.getNormalizedOntology(), normalization.getSortedGCIs());
      if (manager.contains(iri)) manager.removeOntology(ontology);
      Set<OWLAxiom> rewrittenAxioms = rewriter.getRewrittenOntology();
      if (!rewriter.anyRewrittenRoles()) {
        extractor = null;
        Logger_MORe.logDebug(
            t.duration()
                + "s to find Lsignature with inverseRewriting (failed - could not rewrite any roles)");
        return true;
      }
      try {
        ontology = manager.createOntology();
        manager.addAxioms(ontology, rewrittenAxioms);
        extractor = new LsignatureExtractor_reducedGreedyness();
        extractor.findLsignature(ontology, fragment);
      } catch (OWLOntologyCreationException e1) {
        extractor = null;
        e1.printStackTrace();
        Logger_MORe.logDebug(
            t.duration()
                + "s to find Lsignature with inverseRewriting (failed - exception creating ontology for rewritten axioms)");
        return true;
      }
      Logger_MORe.logDebug(t.duration() + "s to find Lsignature with inverseRewriting");
      return true;
    }
コード例 #4
0
  /** @param args */
  public static void main(String[] args) {
    String external = "http://purl.obolibrary.org/obo/iao/dev/external.owl";
    String externalDerived = "http://purl.obolibrary.org/obo/iao/dev/externalDerived.owl";
    String externalByHand = "http://purl.obolibrary.org/obo/iao/dev/externalByHand.owl";
    String metaData = "http://purl.obolibrary.org/obo/iao/dev/ontology-metadata.owl";

    String reasonerName = "hermit";

    String path = "C:/Documents and Settings/Jie/My Documents/Ontology/iao/releases/2012-01-05/";
    String saveIaoFilename = path + "merged/iao-main-infer.owl";
    String inferOntURIStr = "http://purl.obolibrary.org/obo/iao/dev/iao_inferredSuperClasses.owl";
    String saveInferFilename = path + "merged/iao-inferredSuperClasses.owl";

    // Get hold of an ontology manager
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    AutoIRIMapper mapper = new AutoIRIMapper(new File(path + "ontology/"), false);
    manager.addIRIMapper(mapper);

    OWLOntology iaoOnt = OntologyManipulator.load(path + "ontology/iao-main.owl", manager);
    // OntologyManipulator.printPrefixNSs(manager, iaoOnt);

    OWLDataFactory df = manager.getOWLDataFactory();

    // get all imported ontologies
    Set<OWLOntology> importOnts = iaoOnt.getImports();

    // remove imports statements from the loaded ontology
    for (OWLOntology importOnt : importOnts) {
      IRI importOntIRI = importOnt.getOntologyID().getOntologyIRI();
      if (importOntIRI.equals(IRI.create(metaData))
          || importOntIRI.equals(IRI.create(external))
          || importOntIRI.equals(IRI.create(externalByHand))
          || importOntIRI.equals(IRI.create(externalDerived))) {
        RemoveImport ri = new RemoveImport(iaoOnt, df.getOWLImportsDeclaration(importOntIRI));
        manager.applyChange(ri);
      }
    }

    // merge the removed imported ontologies to the loaded one
    for (OWLOntology importOnt : importOnts) {
      IRI importOntIRI = importOnt.getOntologyID().getOntologyIRI();
      if (importOntIRI.equals(IRI.create(metaData))
          || importOntIRI.equals(IRI.create(external))
          || importOntIRI.equals(IRI.create(externalByHand))
          || importOntIRI.equals(IRI.create(externalDerived))) {
        // OntologyManipulator.printPrefixNSs(manager, importOnt);
        iaoOnt = OntologyManipulator.mergeToTargetOnt(manager, iaoOnt, importOnt);
      }
    }

    // generate the inferred hierarchy and clean the super classes
    OWLReasoner reasoner = OWLReasonerRunner.runReasoner(manager, iaoOnt, reasonerName);

    iaoOnt =
        OWLReasonerRunner.getCleanedOntologyWithInferredSuperClasses(
            manager, iaoOnt, inferOntURIStr, reasoner);
    OntologyManipulator.saveToFile(manager, iaoOnt, saveIaoFilename);

    if (manager.contains(IRI.create(inferOntURIStr))) {
      OWLOntology inferOnt = manager.getOntology(IRI.create(inferOntURIStr));
      OntologyManipulator.saveToFile(manager, inferOnt, saveInferFilename);
    }
  }