Package com.clarkparsia.modularity

Examples of com.clarkparsia.modularity.IncrementalClassifier


  public void run() throws OWLOntologyCreationException {
    // Load the ontology file into an OWL ontology object
    OWLOntology ontology = OWL.manager.loadOntology( IRI.create( file ) );
   
    // Get an instance of the incremental classifier
    IncrementalClassifier classifier = new IncrementalClassifier( ontology );

    // trigger classification
    classifier.classify();
       
    // persist the current state of the classifier to a file
    try {
      System.out.print( "Saving the state of the classifier to the file ... " );
      System.out.flush();
     
      // open the stream to a file
      FileOutputStream outputStream = new FileOutputStream( persistenceFile );
     
      // write the contents to the stream
      IncrementalClassifierPersistence.save( classifier, outputStream );     
     
      // close stream
      outputStream.close();
     
      System.out.println( "done." );
    } catch( IOException e ) {
      System.out.println( "I/O Error occurred while saving the current state of the incremental classifier: " + e );
      System.exit(1);
    }
   
   
    // The following code introduces a few changes to the ontology, while the internal state of the classifier is stored in a file.
    // Later, the classifier read back from the file will automatically notice the changes, and incrementally apply them
   
    OWLClass headache = OWL.Class( NS + "Headache" );   
    OWLClass pain = OWL.Class( NS + "Pain" );
   
    // Now create a new OWL axiom, subClassOf(Headache, Pain)
    OWLAxiom axiom = OWL.subClassOf( headache, pain );

    // Add the axiom to the ontology
    // The copy of the classifier in memory, will receive the notification about this change.
    // However, the state of the classifier saved to the file will become out-of-sync at this point
    OWL.manager.applyChange( new AddAxiom( ontology, axiom ) );

   
    // Now let's restore the classifier from the saved file
    IncrementalClassifier restoredClassifier = null;
   
    try {
      System.out.print( "Reading the state of the classifier back from the file ... ");
      System.out.flush();
     
      // open the previously saved file
      FileInputStream inputStream = new FileInputStream( persistenceFile );
     
      // restore the classifier from the file
     
      // it is important to provide the ontology here, if we want the classifier to notice the changes that occurred while the
      // state was stored in the file, and incrementally update the classifier's state
      // (IncrementalClassifierPersistence has another "load" method without ontology parameter, which can be used
      // for cases when there is no ontology to compare).
      restoredClassifier = IncrementalClassifierPersistence.load( inputStream, ontology );
     
      // close stream
      inputStream.close();
      System.out.println( "done." );
    } catch( IOException e ) {
      System.out.println( "I/O Error occurred while reading the current state of the incremental classifier: " + e );
      System.exit(1);     
    }
   
    // Now query both of the classifiers for subclasses of "Pain" class. Both of the classifiers will incrementally update their state, and should print
    // the same information
   
    System.out.println( "[Original classifier] Subclasses of " + pain + ": " + classifier.getSubClasses( pain, true ).getFlattened() + "\n");
    System.out.println( "[Restored classifier] Subclasses of " + pain + ": " + restoredClassifier.getSubClasses( pain, true ).getFlattened() + "\n");
   
    // clean up by removing the file containing the persisted state
    File fileToDelete = new File( persistenceFile );
    fileToDelete.delete()
  }
View Full Code Here


    // Get some entities
    OWLClass headache = OWL.Class( NS + "Mesoscale" );
    OWLClass pain = OWL.Class( NS + "SynopticScale" );
   
    // Get an instance of the incremental classifier
    IncrementalClassifier classifier = new IncrementalClassifier( ontologyTBox );

    // We need some timing to show the performance of the classification
    Timers timers = new Timers();

    // We classify the ontology and use a specific timer to keep track of
    // the time required for the classification
    Timer t = timers.createTimer( "First classification" );
    t.start();
    classifier.classify();
    t.stop();
       
    System.out.println( "\nClassification time: " + t.getTotal() + "ms");
    System.out.println( "Subclasses of " + pain + ": " + classifier.getSubClasses( pain, true ).getFlattened() + "\n");
   
    // Now create a new OWL axiom, subClassOf(Headache, Pain)
    OWLAxiom axiom = OWL.subClassOf( headache, pain );

    // Add the axiom to the ontology, which creates a change event
    OWL.manager.applyChange( new AddAxiom( ontologyTBox, axiom ) );

    // Now we create a second timer to keep track of the performance of the
    // second classification
    t = timers.createTimer("Second classification");
    t.start();
    classifier.classify();
    t.stop();
   
    System.out.println( "\nClassification time: " + t.getTotal() + "ms");
    System.out.println( "Subclasses of " + pain + ": " + classifier.getSubClasses( pain, true ).getFlattened() + "\n");

    // Remove the axiom from the ontology, which creates a change event
    OWL.manager.applyChange( new RemoveAxiom( ontologyTBox, axiom ) );

    // Now we create a third timer to keep track of the performance of the
    // third classification
    timers.startTimer( "Third classification" );
    classifier.classify();
    timers.stopTimer( "Third classification" );
   
    System.out.println( "\nClassification time: " + t.getTotal() + "ms");
    System.out.println( "Subclasses of " + pain + ": " + classifier.getSubClasses( pain, true ).getFlattened() + "\n");
   
    // Finally, print the timing. As you can see, the second classification
    // takes significantly less time, which is the characteristic of the
    // incremental classifier.
    System.out.println( "Timers summary" );
View Full Code Here

    OWLAPILoader loader = (OWLAPILoader) getLoader( "OWLAPIv3" );
   
    loader.parse( getInputFiles() );
    OWLOntology ontology = loader.getOntology();
   
    IncrementalClassifier incrementalClassifier = createIncrementalClassifier( ontology );
   
    if ( !incrementalClassifier.isClassified() ) {
      startTask( "consistency check" );
      boolean isConsistent = incrementalClassifier.isConsistent();
      finishTask( "consistency check" );

      if( !isConsistent )
        throw new PelletCmdException( "Ontology is inconsistent, run \"pellet explain\" to get the reason" );

      startTask( "classification" );
      incrementalClassifier.classify();
      finishTask( "classification" );
    }

    TaxonomyPrinter<OWLClass> printer = new OWLClassTreePrinter();
    printer.print( incrementalClassifier.getTaxonomy() );
   
    if( !currentStateSaved ) {
      persistIncrementalClassifier( incrementalClassifier, ontology );
    }
  }
View Full Code Here

   * @param ontology the ontology (the current state of it)
   * @return the incremental classifier
   */
  private IncrementalClassifier createIncrementalClassifier( OWLOntology ontology ) {
    File saveFile = determineSaveFile( ontology );
    IncrementalClassifier result = null;
   
    // first try to restore the classifier from the file (if one exists)
    if( saveFile.exists() ) {
      result = loadIncrementalClassifier( ontology, saveFile );
    }
   
    // if it was not possible to restore the classifier, create one from scratch
    if( result == null ) {
      result = new IncrementalClassifier( ontology );
    }
   
    result.getReasoner().getKB().setTaxonomyBuilderProgressMonitor(
                                                                   PelletOptions.USE_CLASSIFICATION_MONITOR
                                                                                   .create());
   
    return result;
  }
View Full Code Here

  private IncrementalClassifier loadIncrementalClassifier( OWLOntology ontology, File file ) {
    try {
      FileInputStream inputStream = new FileInputStream( file );
   
      verbose( "Reading persisted classifier state from " + file );
      IncrementalClassifier result = IncrementalClassifierPersistence.load( inputStream, ontology );
     
      // check whether anything changed in the ontology in the time between the incremental classifier
      // was persisted and the current time
      OntologyDiff ontologyDiff = OntologyDiff.diffAxioms( result.getAxioms(), ontology.getAxioms() );
     
      if( ontologyDiff.getDiffCount() > 0 ) {
        verbose( "There were changes to the underlying ontology since the classifier was persisted. Incrementally updating the classifier" );
        result.ontologiesChanged( new LinkedList<OWLOntologyChange>( ontologyDiff.getChanges( ontology ) ) );
      } else {
        currentStateSaved = true;
      }
     
      return result;
View Full Code Here

    // Get some entities
    OWLClass headache = OWL.Class( NS + "Headache" );
    OWLClass pain = OWL.Class( NS + "Pain" );
   
    // Get an instance of the incremental classifier
    IncrementalClassifier classifier = new IncrementalClassifier( ontology );

    // We need some timing to show the performance of the classification
    Timers timers = new Timers();

    // We classify the ontology and use a specific timer to keep track of
    // the time required for the classification
    Timer t = timers.createTimer( "First classification" );
    t.start();
    classifier.classify();
    t.stop();
       
    System.out.println( "\nClassification time: " + t.getTotal() + "ms");
    System.out.println( "Subclasses of " + pain + ": " + classifier.getSubClasses( pain, true ).getFlattened() + "\n");
   
    // Now create a new OWL axiom, subClassOf(Headache, Pain)
    OWLAxiom axiom = OWL.subClassOf( headache, pain );

    // Add the axiom to the ontology, which creates a change event
    OWL.manager.applyChange( new AddAxiom( ontology, axiom ) );

    // Now we create a second timer to keep track of the performance of the
    // second classification
    t = timers.createTimer( "Second classification" );
    t.start();
    classifier.classify();
    t.stop();
   
    System.out.println( "\nClassification time: " + t.getTotal() + "ms");
    System.out.println( "Subclasses of " + pain + ": " + classifier.getSubClasses( pain, true ).getFlattened() + "\n");

    // Remove the axiom from the ontology, which creates a change event
    OWL.manager.applyChange( new RemoveAxiom( ontology, axiom ) );

    // Now we create a third timer to keep track of the performance of the
    // third classification
    timers.startTimer( "Third classification" );
    classifier.classify();
    timers.stopTimer( "Third classification" );
   
    System.out.println( "\nClassification time: " + t.getTotal() + "ms");
    System.out.println( "Subclasses of " + pain + ": " + classifier.getSubClasses( pain, true ).getFlattened() + "\n");
   
    // Finally, print the timing. As you can see, the second classification
    // takes significantly less time, which is the characteristic of the
    // incremental classifier.
    System.out.println( "Timers summary" );
View Full Code Here

  public void run() throws OWLOntologyCreationException {
    // Load the ontology file into an OWL ontology object
    OWLOntology ontology = OWL.manager.loadOntology( IRI.create( file ) );
   
    // Get an instance of the incremental classifier
    IncrementalClassifier classifier = new IncrementalClassifier( ontology );

    // trigger classification
    classifier.classify();
       
    // persist the current state of the classifier to a file
    try {
      System.out.print( "Saving the state of the classifier to the file ... " );
      System.out.flush();
     
      // open the stream to a file
      FileOutputStream outputStream = new FileOutputStream( persistenceFile );
     
      // write the contents to the stream
      IncrementalClassifierPersistence.save( classifier, outputStream );     
     
      // close stream
      outputStream.close();
     
      System.out.println( "done." );
    } catch( IOException e ) {
      System.out.println( "I/O Error occurred while saving the current state of the incremental classifier: " + e );
      System.exit(1);
    }
   
   
    // The following code introduces a few changes to the ontology, while the internal state of the classifier is stored in a file.
    // Later, the classifier read back from the file will automatically notice the changes, and incrementally apply them
   
    OWLClass headache = OWL.Class( NS + "Headache" );   
    OWLClass pain = OWL.Class( NS + "Pain" );
   
    // Now create a new OWL axiom, subClassOf(Headache, Pain)
    OWLAxiom axiom = OWL.subClassOf( headache, pain );

    // Add the axiom to the ontology
    // The copy of the classifier in memory, will receive the notification about this change.
    // However, the state of the classifier saved to the file will become out-of-sync at this point
    OWL.manager.applyChange( new AddAxiom( ontology, axiom ) );

   
    // Now let's restore the classifier from the saved file
    IncrementalClassifier restoredClassifier = null;
   
    try {
      System.out.print( "Reading the state of the classifier back from the file ... ");
      System.out.flush();
     
      // open the previously saved file
      FileInputStream inputStream = new FileInputStream( persistenceFile );
     
      // restore the classifier from the file
     
      // it is important to provide the ontology here, if we want the classifier to notice the changes that occurred while the
      // state was stored in the file, and incrementally update the classifier's state
      // (IncrementalClassifierPersistence has another "load" method without ontology parameter, which can be used
      // for cases when there is no ontology to compare).
      restoredClassifier = IncrementalClassifierPersistence.load( inputStream, ontology );
     
      // close stream
      inputStream.close();
      System.out.println( "done." );
    } catch( IOException e ) {
      System.out.println( "I/O Error occurred while reading the current state of the incremental classifier: " + e );
      System.exit(1);     
    }
   
    // Now query both of the classifiers for subclasses of "Pain" class. Both of the classifiers will incrementally update their state, and should print
    // the same information
   
    System.out.println( "[Original classifier] Subclasses of " + pain + ": " + classifier.getSubClasses( pain, true ).getFlattened() + "\n");
    System.out.println( "[Restored classifier] Subclasses of " + pain + ": " + restoredClassifier.getSubClasses( pain, true ).getFlattened() + "\n");
   
    // clean up by removing the file containing the persisted state
    File fileToDelete = new File( persistenceFile );
    fileToDelete.delete()
  }
View Full Code Here

  @Test
  public void addNonLocal() throws OWLException {
    OWLAxiom[] axioms = { subClassOf( A, B ), subClassOf( C, D ) };
    createOntology( axioms );

    IncrementalClassifier modular = PelletIncremantalReasonerFactory.getInstance().createReasoner( ontology );
    modular.classify();
   
    assertTrue( modular.isEntailed( subClassOf( A, B ) ) );
    assertFalse( modular.isEntailed( subClassOf( B, C ) ) );
    assertTrue( modular.isEntailed( subClassOf( C, D ) ) );

    OntologyUtils.addAxioms( ontology, Arrays.asList( equivalentClasses( D, all( p, D ) ),
        subClassOf( B, C ) ) );
    modular.classify();
    assertTrue( modular.isEntailed( subClassOf( A, B ) ) );
    assertTrue( modular.isEntailed( subClassOf( B, C ) ) );
    assertTrue( modular.isEntailed( subClassOf( C, D ) ) );

    OntologyUtils.removeAxioms( ontology, Arrays.asList( subClassOf( A, B ) ) );
    modular.classify();
    assertFalse( modular.isEntailed( subClassOf( A, B ) ) );
    assertTrue( modular.isEntailed( subClassOf( B, C ) ) );
    assertTrue( modular.isEntailed( subClassOf( D, D ) ) );

    modular.dispose();
  }
View Full Code Here

  @Test
  public void deleteNonLocal() throws OWLException {
    OWLAxiom[] axioms = { subClassOf( A, B ), subClassOf( C, D ), equivalentClasses( D, all( p, D ) ) };
    createOntology( axioms );

    IncrementalClassifier modular = PelletIncremantalReasonerFactory.getInstance().createReasoner( ontology );
    modular.classify();
   
    assertTrue( modular.isEntailed( subClassOf( A, B ) ) );
    assertFalse( modular.isEntailed( subClassOf( B, C ) ) );
    assertTrue( modular.isEntailed( subClassOf( C, D ) ) );

    OntologyUtils.removeAxioms( ontology, Arrays.asList( equivalentClasses( D, all( p, D ) ) ) );
    OntologyUtils.addAxioms( ontology, Arrays.asList( subClassOf( B, C ) ) );
    modular.classify();
    assertTrue( modular.isEntailed( subClassOf( A, B ) ) );
    assertTrue( modular.isEntailed( subClassOf( B, C ) ) );
    assertTrue( modular.isEntailed( subClassOf( C, D ) ) );

    OntologyUtils.removeAxioms( ontology, Arrays.asList( subClassOf( A, B ) ) );
    modular.classify();
    assertFalse( modular.isEntailed( subClassOf( A, B ) ) );
    assertTrue( modular.isEntailed( subClassOf( B, C ) ) );
    assertTrue( modular.isEntailed( subClassOf( D, D ) ) );

    modular.dispose();
  }
View Full Code Here

  @Test
  public void testDeferredClassification() {
    OWLAxiom[] axioms = { subClassOf( A, B ), subClassOf( C, D ) };
    createOntology( axioms );

    IncrementalClassifier modular = PelletIncremantalReasonerFactory.getInstance().createReasoner( ontology );
    modular.classify();
   
    assertTrue(modular.isClassified());
   
    assertEquals(Collections.emptySet(), modular.getTypes(a, false).getFlattened());
   
    assertTrue(modular.isRealized());
   
    OntologyUtils.addAxioms( ontology, Arrays.asList( classAssertion( a, A ) ) );
   
    // despite of having added a new fact, the classifier should still be in classified state (the axiom was an A-Box axiom)
    assertTrue(modular.isClassified());
    assertFalse(modular.isRealized());
   
    assertEquals(SetUtils.create(A, B, OWL.Thing), modular.getTypes(a, false).getFlattened());   
    assertTrue(modular.isEntailed(subClassOf( A, B )));
    assertFalse(modular.isEntailed(subClassOf( A, C )));
   
    assertTrue(modular.isRealized());
   
    // now try to add a T-Box axiom
    OntologyUtils.addAxioms( ontology, Arrays.asList( subClassOf( A, C ) ) );
   
    // the classifier should no longer be in classified state
    assertFalse(modular.isClassified());
    assertFalse(modular.isRealized());
   
    // force classification
    modular.classify();
   
    // check whether the classifier returned to the classified state
    assertTrue(modular.isClassified());
   
    assertEquals(SetUtils.create(A, B, C, D, OWL.Thing), modular.getTypes(a, false).getFlattened());
    assertTrue(modular.isEntailed(subClassOf( A, B )));
    assertTrue(modular.isEntailed(subClassOf( A, C )));
  }
View Full Code Here

TOP

Related Classes of com.clarkparsia.modularity.IncrementalClassifier

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.