Difference between revisions of "ChAO API"

From Protege Wiki
Jump to: navigation, search
Line 14: Line 14:
  
 
to get access to the ChAO KB associated to kb.
 
to get access to the ChAO KB associated to kb.
 +
 +
=== Create an RDF(S) files ChAO KB project ===
  
 
You may also create the ChAO KB. To create a ChAO KB stored in RDF(S) files (default option), call:
 
You may also create the ChAO KB. To create a ChAO KB stored in RDF(S) files (default option), call:
Line 34: Line 36:
 
  }
 
  }
  
 +
 +
=== Create a ChAO KB stored in a database ===
 
 
 
To create the ChAO KB stored in a database, call:
 
To create the ChAO KB stored in a database, call:

Revision as of 16:12, October 24, 2008

Accessing the collaboration features programatically (The Changes and Annotations API)

Protege provides support for collaboration through the Collaborative Protege extension: e.g., tracking changes, annotating ontology components (classes, properties, etc.) or changes (creation of a class). This wiki page describes how to access the change tracking and the annotation information programatically.

Other relevant documentation: the Changes Tab, the Collaborative Protege paper, the paper describing the Changes and Annotation ontology.

The change tracking information and annotation of ontology entities and changes is stored as instances of the Changes and Annotation Ontology (ChAO), called the ChAO KB. This wiki page shows how to access this information programmatically.

The ChAO KB Manager

One of the most important classes is the edu.stanford.bmir.protegex.chao.ChAOKbManager found in the change-model.jar from the edu.stanford.smi.protegex.changes plugin folder. It contains methods for creating and retrieving the ChAO KB for different modes: stand-alone, multi-user client and multi-user server. All code should use the method:

public static KnowledgeBase getChAOKb(KnowledgeBase kb)

to get access to the ChAO KB associated to kb.

Create an RDF(S) files ChAO KB project

You may also create the ChAO KB. To create a ChAO KB stored in RDF(S) files (default option), call:

public static KnowledgeBase createRDFFileChAOKb(KnowledgeBase kb, URI chaoURI) 

Code example:

private KnowledgeBase void createCHAOFileProject() {
   //load the project
   Project prj = Project.loadProjectFromFile("/tmp/pizza.pprj", new ArrayList());
   OWLModel owlModel = (OWLModel) prj.getKnowledgeBase();
   //create the ChAO KB
   KnowledgeBase changesKb = ChAOKbManager.createRDFFileChAOKb(owlModel, ChAOKbManager.getChAOProjectURI(owlModel));
   //save the ChAO
   ArrayList errors = new ArrayList();
   changesKb.getProject().save(errors);
   //optional to save also the main project. It helps if the ChAO project does not have the default name
   prj.save(errors);
   return changesKb;
}


Create a ChAO KB stored in a database

To create the ChAO KB stored in a database, call:

public static KnowledgeBase createDbChAOKb(KnowledgeBase kb, URI chaoURI, String dbDriver, String dbUrl, String dbTable, String dbUser, String dbPassword)

Italic text Code example:

private KnowledgeBase void createCHAOFileProject() {
   //load the project
   Project prj = Project.loadProjectFromFile("/tmp/pizza.pprj", new ArrayList());
   OWLModel owlModel = (OWLModel) prj.getKnowledgeBase();
   //create the ChAO KB
    KnowledgeBase changesKb = ChAOKbManager.createDbChAOKb(owlModel, ChAOKbManager.getChAOProjectURI(owlModel), "com.mysql.jdbc.Driver", "jdbc:mysql://localhost/protege", "myTableName", "myUser", "myPassword");
   //save the ChAO
   ArrayList errors = new ArrayList();
   changesKb.getProject().save(errors);
   //optional to save also the main project. It helps if the ChAO project does not have the default name
   prj.save(errors);
   return changesKb;
}


Accessing the annotation information

The access to the annotation information is done through the edu.stanford.bmir.protegex.chao.annotation.api.AnnotationFactory class found in the change-model.jar from the edu.stanford.smi.protegex.changes plugin folder. The name of the methods are quite self evident. The code for the AnnotationFactory has been generated from the ChAO ontology. For example, if in the ontology there is a class Annotation that has a slot attached to it, author, then the generated Java interface Annotation will have a method getAuthor().

Get all annotations

Example of how to print all annotations of a pizza.pprj file (stored in a ChAOKB project, for example annotation_pizza.pprj).

private void printAllAnnotations() {
   Project prj = Project.loadProjectFromFile("/tmp/pizza.pprj", new ArrayList());
   KnowledgeBase kb = prj.getKnowledgeBase();
   AnnotationFactory factory = new AnnotationFactory(ChAOKbManager.getChAOKb(kb));
   Collection<Annotation> annotations = factory.getAllAnnotationObjects(true);
   for (Annotation annotation : annotations) {
       System.out.println("\nAuthor: " + annotation.getAuthor());
       System.out.println("Date: " + annotation.getCreated().getDate());
       System.out.println("Body: " + annotation.getBody());
    }
}

Get annotations associated to an ontology entity

Below is an example of how to get the annotations associated to a class "Pizza".



Accessing the changes information