Esempio n. 1
0
  /**
   * Initialise les composants du conteneur sémantique une fois les URI des ontologies dispatchées
   * par OwlDistributionInitializer.
   */
  public void semInit() throws ExceptionInInitializerError {
    if (isInitialized()) {
      /** Initialisation de l'ontologie locale. Cette ontologie n'est plus modifiée ensuite. */
      // Initialisation du manager de l'ontologie locale. N'est utilisé qu'ici.
      OWLOntologyManager localOntoManager = OWLManager.createOWLOntologyManager();
      OWLOntologyIRIMapper localOntoMapper =
          new SimpleIRIMapper(getOntologyIri(), getPhysicalIri());
      localOntoManager.addIRIMapper(localOntoMapper);
      // Initialisation de l'ontologie
      try {
        // On crée l'ontologie locale.
        this.localOntology = localOntoManager.loadOntology(getPhysicalIri());
      } catch (OWLOntologyCreationException e) {
        e.printStackTrace();
      }
      // System.out.println("localOntology : " + localOntology + " ; pyhsicalIri : " +
      // getPhysicalIri().toString());

      /**
       * Initialisation de l'ontologie du reasoner, qui contiendra l'ontologie locale plus d'autres
       * axiomes éventuels. On crée l'instance, qu'on rattache à un manager et un reasoner
       * accessibles par des getters. L'instance reste la même, mais le contenu change.
       */
      // Initialisation du manager de l'ontologie du reasoner
      this.manager = OWLManager.createOWLOntologyManager();
      // On crée une deuxième ontologie.
      try {
        IRI ontoName = IRI.create(this.nameSpace);
        OWLOntologyID ontoId = new OWLOntologyID(ontoName);
        this.reasoningOntology = this.manager.createOntology(ontoId);
      } catch (OWLOntologyCreationException e) {
        e.printStackTrace();
      }
      // Initialisation du reasoner
      PelletReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance();

      PelletOptions.USE_INCREMENTAL_CONSISTENCY = true;
      PelletOptions.USE_COMPLETION_QUEUE = true;

      // PelletReasoner reasoner = reasonerFactory.createReasoner(reasoningOntology);
      PelletReasoner reasoner = reasonerFactory.createNonBufferingReasoner(reasoningOntology);

      // add the reasoner as an ontology change listener
      this.manager.addOntologyChangeListener(reasoner);

      reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);
      /*			reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS);
      			reasoner.precomputeInferences(InferenceType.DATA_PROPERTY_HIERARCHY);
      			reasoner.precomputeInferences(InferenceType.DISJOINT_CLASSES);
      			reasoner.precomputeInferences(InferenceType.OBJECT_PROPERTY_HIERARCHY);
      			reasoner.precomputeInferences(InferenceType.SAME_INDIVIDUAL);

      */ this.reasoner = reasoner;
    } else
      throw new ExceptionInInitializerError(
          "Paramètres de l'ontologie non initialisés ; ontoParamsInit = "
              + Reflection.getCallerClass(2));

    // initOntoReasoner();
  }
  public static void main(String[] args) {
    try {
      SimpleRenderer renderer = new SimpleRenderer();
      renderer.setShortFormProvider(
          new DefaultPrefixManager("http://www.mindswap.org/ontologies/tambis-full.owl#"));
      ToStringRenderer.getInstance().setRenderer(renderer);
      OWLOntologyManager man = OWLManager.createOWLOntologyManager();
      OWLOntology ont =
          man.loadOntology(
              IRI.create(
                  "http://owl.cs.manchester.ac.uk/repository/download?ontology=http://www.cs.manchester.ac.uk/owl/ontologies/tambis-patched.owl"));

      System.out.println("Loaded!");
      OWLReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance();
      OWLReasoner reasoner = reasonerFactory.createNonBufferingReasoner(ont);
      reasoner.getUnsatisfiableClasses();
      ExplanationBasedRootClassFinder rdr =
          new ExplanationBasedRootClassFinder(man, reasoner, reasonerFactory);
      for (OWLClass cls : rdr.getRootUnsatisfiableClasses()) System.out.println("ROOT! " + cls);
    } catch (TimeOutException e) {
      e.printStackTrace();
    } catch (ReasonerInterruptedException e) {
      e.printStackTrace();
    } catch (OWLOntologyCreationException e) {
      e.printStackTrace();
    }
  }
  private IRI executeRule(final Rule r, final IRI inputIRI) {
    try {
      PelletOptions.USE_ANNOTATION_SUPPORT = true;

      PelletOptions.TREAT_ALL_VARS_DISTINGUISHED = controller.isTreatAllVariablesDistinguished();

      QueryEngineType type = (QueryEngineType) controller.getQueryEngineType();

      final QueryExecution qe;
      final ByteArrayOutputStream w = new ByteArrayOutputStream();

      final Query qSelect = getSelectExampleQuery(r.getQuery());

      if (type.toPellet() != null) {
        final OWLOntology queryOntology = getInputOntologyForRule(inputIRI);

        final PelletReasoner reasoner =
            PelletReasonerFactory.getInstance().createReasoner(queryOntology);

        log.info("Ontology size: " + reasoner.getKB().getInfo());

        final Dataset ds = kb2ds(reasoner.getKB());

        final QueryExecution qeSelect =
            SparqlDLExecutionFactory.create(qSelect, ds, null, type.toPellet());

        final ResultSet rs = qeSelect.execSelect();
        controller.setSelect(r, rs.getResultVars(), ResultSetFormatter.toList(rs));

        qe =
            SparqlDLExecutionFactory.create(
                r.getQuery(), kb2ds(reasoner.getKB()), null, type.toPellet());
        qe.execConstruct().write(w);
      } else {
        final ByteArrayOutputStream w2 = new ByteArrayOutputStream();
        final Model model = ModelFactory.createDefaultModel();
        try {
          controller
              .getOWLOntologyManager()
              .saveOntology(queryOntology, new TurtleOntologyFormat(), w2);
          model.read(new ByteArrayInputStream(w2.toByteArray()), "", "TURTLE");

          final QueryExecution qeSelect = QueryExecutionFactory.create(qSelect, model);

          final ResultSet rs = qeSelect.execSelect();
          controller.setSelect(r, rs.getResultVars(), ResultSetFormatter.toList(rs));

          qe = QueryExecutionFactory.create(r.getQuery(), model);
          qe.execConstruct().write(w);
        } catch (OWLOntologyStorageException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

      final IRI outputIRI = getOntologyIRIForRuleName(r.getName());

      // loaded generated ontology
      final OWLOntology generatedOntology =
          controller
              .getOWLOntologyManager()
              .loadOntologyFromOntologyDocument(new ByteArrayInputStream(w.toByteArray()));
      controller.updateOntology(
          generatedOntology,
          outputIRI,
          inputIRI,
          controller.getRuleSpec().getResultFile(r).toURI());
      controller.setStatus("Rule " + r.getName() + " successfully executed");
      return outputIRI;
    } catch (OWLOntologyCreationException e1) {
      controller.setStatus(e1.getMessage());
      return null;
    }
  }
Esempio n. 4
0
  private OWLOntology parseWithReasoner(OWLOntologyManager manager, OWLOntology ontology) {
    try {
      PelletOptions.load(new URL("http://" + cssLocation + "pellet.properties"));
      PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner(ontology);
      reasoner.getKB().prepare();
      List<InferredAxiomGenerator<? extends OWLAxiom>> generators =
          new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
      generators.add(new InferredSubClassAxiomGenerator());
      generators.add(new InferredClassAssertionAxiomGenerator());
      generators.add(new InferredDisjointClassesAxiomGenerator());
      generators.add(new InferredEquivalentClassAxiomGenerator());
      generators.add(new InferredEquivalentDataPropertiesAxiomGenerator());
      generators.add(new InferredEquivalentObjectPropertyAxiomGenerator());
      generators.add(new InferredInverseObjectPropertiesAxiomGenerator());
      generators.add(new InferredPropertyAssertionGenerator());
      generators.add(new InferredSubDataPropertyAxiomGenerator());
      generators.add(new InferredSubObjectPropertyAxiomGenerator());

      InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, generators);

      OWLOntologyID id = ontology.getOntologyID();
      Set<OWLImportsDeclaration> declarations = ontology.getImportsDeclarations();
      Set<OWLAnnotation> annotations = ontology.getAnnotations();

      Map<OWLEntity, Set<OWLAnnotationAssertionAxiom>> entityAnnotations =
          new HashMap<OWLEntity, Set<OWLAnnotationAssertionAxiom>>();
      for (OWLClass aEntity : ontology.getClassesInSignature()) {
        entityAnnotations.put(aEntity, aEntity.getAnnotationAssertionAxioms(ontology));
      }
      for (OWLObjectProperty aEntity : ontology.getObjectPropertiesInSignature()) {
        entityAnnotations.put(aEntity, aEntity.getAnnotationAssertionAxioms(ontology));
      }
      for (OWLDataProperty aEntity : ontology.getDataPropertiesInSignature()) {
        entityAnnotations.put(aEntity, aEntity.getAnnotationAssertionAxioms(ontology));
      }
      for (OWLNamedIndividual aEntity : ontology.getIndividualsInSignature()) {
        entityAnnotations.put(aEntity, aEntity.getAnnotationAssertionAxioms(ontology));
      }
      for (OWLAnnotationProperty aEntity : ontology.getAnnotationPropertiesInSignature()) {
        entityAnnotations.put(aEntity, aEntity.getAnnotationAssertionAxioms(ontology));
      }
      for (OWLDatatype aEntity : ontology.getDatatypesInSignature()) {
        entityAnnotations.put(aEntity, aEntity.getAnnotationAssertionAxioms(ontology));
      }

      manager.removeOntology(ontology);
      OWLOntology inferred = manager.createOntology(id);
      iog.fillOntology(manager, inferred);

      for (OWLImportsDeclaration decl : declarations) {
        manager.applyChange(new AddImport(inferred, decl));
      }
      for (OWLAnnotation ann : annotations) {
        manager.applyChange(new AddOntologyAnnotation(inferred, ann));
      }
      for (OWLClass aEntity : inferred.getClassesInSignature()) {
        applyAnnotations(aEntity, entityAnnotations, manager, inferred);
      }
      for (OWLObjectProperty aEntity : inferred.getObjectPropertiesInSignature()) {
        applyAnnotations(aEntity, entityAnnotations, manager, inferred);
      }
      for (OWLDataProperty aEntity : inferred.getDataPropertiesInSignature()) {
        applyAnnotations(aEntity, entityAnnotations, manager, inferred);
      }
      for (OWLNamedIndividual aEntity : inferred.getIndividualsInSignature()) {
        applyAnnotations(aEntity, entityAnnotations, manager, inferred);
      }
      for (OWLAnnotationProperty aEntity : inferred.getAnnotationPropertiesInSignature()) {
        applyAnnotations(aEntity, entityAnnotations, manager, inferred);
      }
      for (OWLDatatype aEntity : inferred.getDatatypesInSignature()) {
        applyAnnotations(aEntity, entityAnnotations, manager, inferred);
      }

      return inferred;
    } catch (FileNotFoundException e1) {
      return ontology;
    } catch (MalformedURLException e1) {
      return ontology;
    } catch (IOException e1) {
      return ontology;
    } catch (OWLOntologyCreationException e) {
      return ontology;
    }
  }
Esempio n. 5
0
  public Set<NamingIssue> detectNonMatchingChildIssues(OWLOntology ontology, boolean directChild) {
    long start = System.currentTimeMillis();

    // get SubClass - SuperClass pairs
    Set<NamingIssue> nonMatchingChildren = new HashSet<NamingIssue>();
    if (directChild) {
      Set<OWLSubClassOfAxiom> axioms = ontology.getAxioms(AxiomType.SUBCLASS_OF);
      for (OWLSubClassOfAxiom ax : axioms) {

        OWLClassExpression subClass = ax.getSubClass();
        OWLClassExpression superClass = ax.getSuperClass();

        if (!subClass.isAnonymous() && !superClass.isAnonymous()) {
          String subClassURI = subClass.asOWLClass().toStringID();
          String superClassURI = superClass.asOWLClass().toStringID();

          if (ignoreSingleTokenSubClasses && !singleToken(subClassURI)) {
            String subClassHead = getHeadNoun(subClassURI);
            String superClassHead = getHeadNoun(superClassURI);

            boolean matching =
                subClassHead.equals(superClassHead) || isHypernymOf(superClassHead, subClassHead);

            if (!matching) {
              String newClassURI = buildNewURI(subClassURI, superClassHead);
              nonMatchingChildren.add(
                  new NamingIssue(
                      subClassURI,
                      superClassURI,
                      new RenamingInstruction(subClassURI, newClassURI)));
            }
          }
        }
      }
    } else {
      OWLReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance();
      OWLReasoner reasoner = reasonerFactory.createNonBufferingReasoner(ontology);

      Set<OWLClass> classes = ontology.getClassesInSignature();

      for (OWLClass cls : classes) {
        Set<OWLClass> superClasses = reasoner.getSuperClasses(cls, false).getFlattened();
        superClasses.remove(OWL_THING);
        for (OWLClass superClass : superClasses) {
          String subClassURI = cls.asOWLClass().toStringID();
          String superClassURI = superClass.asOWLClass().toStringID();

          if (ignoreSingleTokenSubClasses && !singleToken(subClassURI)) {
            String subClassHead = getHeadNoun(subClassURI);
            String superClassHead = getHeadNoun(superClassURI);

            boolean matching =
                subClassHead.equals(superClassHead) || isHypernymOf(superClassHead, subClassHead);

            if (!matching) {
              String newClassURI = buildNewURI(subClassURI, superClassHead);
              nonMatchingChildren.add(
                  new NamingIssue(
                      subClassURI,
                      superClassURI,
                      new RenamingInstruction(subClassURI, newClassURI)));
            }
          }
        }
      }
    }

    long end = System.currentTimeMillis();
    logger.info("Operation took " + (end - start) + "ms");

    return nonMatchingChildren;
  }