Difference between revisions of "ProgramaticLuceneQueryAccess"

From Protege Wiki
Jump to: navigation, search
(New page: This note will describe how to programatically * generate lucene indicies for an ontology * make a phonetic query that uses the lucene indicies and * save the project to include the lucen...)
 
Line 1: Line 1:
 
This note will describe how to programatically  
 
This note will describe how to programatically  
* generate lucene indicies for an ontology
+
* generate phonetic lucene indicies for an ontology
 
* make a phonetic query that uses the lucene indicies and
 
* make a phonetic query that uses the lucene indicies and
 
* save the project to include the lucene query tab.
 
* save the project to include the lucene query tab.
 +
I will assume that you have already obtained the binary or src (http://smi-protege/repos/protege/lucene-query/trunk) for the lucene query plugin.
 
The test code I used had the following structure:
 
The test code I used had the following structure:
 
<code><pre>
 
<code><pre>
Line 17: Line 18:
 
     }
 
     }
 
</pre></code>
 
</pre></code>
The first few lines just illustrate one of the standard ways of opening an ontology in Protege.  The last three lines show the code that we will explain here.
+
The first few lines just illustrate one of the standard ways of opening an ontology in Protege.  This test code was set up to work with the pizza project but it could easily be modified to fit other ontologies.
 +
The last three lines show the code that we will explain here.
 +
 
 +
First we index the ontology.  This is done by the indexOntology routine and the code looks like this:
 +
<code><pre>
 +
    private static void indexOntology(KnowledgeBase kb) {
 +
        new InstallNarrowFrameStore(kb).execute();
 +
        new IndexOntologies(kb).execute();
 +
    }
 +
</pre></code>
 +
In this code the InstallNarrowFrameStore and IndexOntologies classes are found in the package edu.stanford.smi.protege.query from the lucene query plugin.  Both classes are ProtegeJob classes and will  therefore work if the knowledge base is a client knowledge base accessing a remote server.
 +
 
 +
Now that we have indexed the ontology, we can do a programatic query.  This is done in the following code:
 +
<code><pre>
 +
    private static void testQuery(KnowledgeBase kb) {
 +
        Slot slot = kb.getSystemFrames().getNameSlot();
 +
        Set<Frame> results = null;
 +
        results = kb.executeQuery(new PhoneticQuery(slot, "sheazeypeasah"));
 +
        if (results == null || results.size() != 1) {
 +
            System.out.println("test of search failed");
 +
        }
 +
        else {
 +
            System.out.println("Search succeeded (" + results.iterator().next().getBrowserText() + ")");
 +
        }
 +
    }
 +
</pre></code>

Revision as of 14:11, January 2, 2008

This note will describe how to programatically

  • generate phonetic lucene indicies for an ontology
  • make a phonetic query that uses the lucene indicies and
  • save the project to include the lucene query tab.

I will assume that you have already obtained the binary or src (http://smi-protege/repos/protege/lucene-query/trunk) for the lucene query plugin. The test code I used had the following structure:

    @SuppressWarnings("unchecked")
    public static void main(String args[]) {
        List errors = new ArrayList();
        Project project = new Project(PROJECT_FILE, errors);
        displayErrors(errors);
        OWLModel om = (OWLModel) project.getKnowledgeBase();

        indexOntology(om);
        testQuery(om);
        addTab(project);
    }

The first few lines just illustrate one of the standard ways of opening an ontology in Protege. This test code was set up to work with the pizza project but it could easily be modified to fit other ontologies. The last three lines show the code that we will explain here.

First we index the ontology. This is done by the indexOntology routine and the code looks like this:

    private static void indexOntology(KnowledgeBase kb) {
        new InstallNarrowFrameStore(kb).execute();
        new IndexOntologies(kb).execute();
    }

In this code the InstallNarrowFrameStore and IndexOntologies classes are found in the package edu.stanford.smi.protege.query from the lucene query plugin. Both classes are ProtegeJob classes and will therefore work if the knowledge base is a client knowledge base accessing a remote server.

Now that we have indexed the ontology, we can do a programatic query. This is done in the following code:

    private static void testQuery(KnowledgeBase kb) {
        Slot slot = kb.getSystemFrames().getNameSlot();
        Set<Frame> results = null;
        results = kb.executeQuery(new PhoneticQuery(slot, "sheazeypeasah"));
        if (results == null || results.size() != 1) {
            System.out.println("test of search failed");
        }
        else {
            System.out.println("Search succeeded (" + results.iterator().next().getBrowserText() + ")");
        }
    }