Difference between revisions of "ProgramaticLuceneQueryAccess"

From Protege Wiki
Jump to: navigation, search
 
(6 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
* 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.
+
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 code for this example can be downloaded from [[media:TestLuceneQuery.zip|here]].
 
The test code I used had the following structure:
 
The test code I used had the following structure:
 
<code><pre>
 
<code><pre>
Line 24: Line 24:
 
<code><pre>
 
<code><pre>
 
     private static void indexOntology(KnowledgeBase kb) {
 
     private static void indexOntology(KnowledgeBase kb) {
         new InstallNarrowFrameStore(kb).execute();
+
         QueryApi api = new QueryApi(kb);
         new IndexOntologies(kb).execute();
+
        QueryConfiguration config = new QueryConfiguration(kb);
 +
       
 +
        alterConfiguration(kb, config);
 +
       
 +
        api.install(config);
 +
        api.index();
 +
    }
 +
</pre></code>
 +
This code uses the QueryApi and the QueryConfiguration classes in the edu.stanford.smi.protege.query.api package.  Before installing the
 +
query capability into the knowledge base we perform the optional step of configuring the slots indexed, the location of the indicies and the
 +
types of queries supported:
 +
<code><pre>
 +
    private static void alterConfiguration(KnowledgeBase kb, QueryConfiguration config) {
 +
        config.setBaseIndexPath("/tmp/lucene");
 +
        Set<Indexer> indexers = new HashSet<Indexer>();
 +
         indexers.add(new PhoneticIndexer());
 +
        config.setIndexers(indexers);
 +
        Set<Slot> slots = new HashSet<Slot>();
 +
        slots.add(kb.getSystemFrames().getNameSlot());
 
     }
 
     }
 
</pre></code>
 
</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:
 
Now that we have indexed the ontology, we can do a programatic query.  This is done in the following code:
Line 68: Line 85:
 
</pre></code>
 
</pre></code>
  
<h3>Issues</h3>
+
[[Category:Protege developer documentation]]
 
 
The indicies are stored in a slightly funny place (protege-dir/lucene/project-name/*).  We may want to choose a better approach and implement that.
 
 
 
This should really support other types of lucene search than phonetic.  It looks like this is extremely easy to do but it has been a while since I touched this code.
 

Latest revision as of 06:59, October 2, 2009

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 code for this example can be downloaded from here. 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) {
        QueryApi api = new QueryApi(kb);
        QueryConfiguration config = new QueryConfiguration(kb);
        
        alterConfiguration(kb, config);
        
        api.install(config);
        api.index();
    }

This code uses the QueryApi and the QueryConfiguration classes in the edu.stanford.smi.protege.query.api package. Before installing the query capability into the knowledge base we perform the optional step of configuring the slots indexed, the location of the indicies and the types of queries supported:

    private static void alterConfiguration(KnowledgeBase kb, QueryConfiguration config) {
        config.setBaseIndexPath("/tmp/lucene");
        Set<Indexer> indexers = new HashSet<Indexer>();
        indexers.add(new PhoneticIndexer());
        config.setIndexers(indexers);
        Set<Slot> slots = new HashSet<Slot>();
        slots.add(kb.getSystemFrames().getNameSlot());
    }

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() + ")");
        }
    }

There are several types of queries (some of which are composed out of other queries) defined in the lucene query plugin including:

  • conjuctions and disjunctions of queries,
  • queries that can only return a limited set of results,
  • queries that find regular expression matches for a slot value,
  • queries that match slot values with the results of a nested query and
  • queries that look inside a restriction for matches to an inner query.

Finally we add the lucene plugin as a tab to the project. I was not quite able to use the code suggested in Adding and removing tabs programmatically because when I retrieved the project view it was null. The code is as follows:

   @SuppressWarnings("unchecked")
    private static void addTab(Project project) {
        List errors = new ArrayList();
        
        WidgetDescriptor tabWidgetDescriptor = project.getTabWidgetDescriptor("edu.stanford.smi.protege.query.LuceneQueryPlugin");
        tabWidgetDescriptor.setVisible(true);

        Collection widgets = project.getTabWidgetDescriptors();
        project.setTabWidgetDescriptorOrder(widgets);
        
        project.save(errors);
        displayErrors(errors);
    }