Simple Algernon Example Java Program

Below is an example Java program that accesses a Protege KB (the standard Newspaper example) and performs Algernon queries on it, then prints the results in two different ways.

To run it, first copy the program and edit the pathname in the NEWSPAPER_PROJECT variable to match the pathname on your computer.

You may also want to change the package of the program. In most cases, simply remove the package statement.

Then compile the program:

  javac -classpath algernon.jar:protege.jar SimpleAlgernonExample.java
and run it:
  java -classpath algernon.jar:protege.jar:. SimpleAlgernonExample


SimpleAlgernonExample.java

/*
 * Algernon - a rule-based inference engine in Java.
 * http://algernon-j.sourceforge.net/
 *
 * This example shows how to open a Protege knowledge base
 * in Java and use Algernon to query the KB.
 *
 * To run it, be sure to change the path in NEWSPAPER_PROJECT
 * to match the correct project on your system.
 *
 * Micheal Hewett
 * 05 May 2004
 * hewett@cs.stanford.edu
 */

package org.algernon.test;

import java.util.*;

import org.algernon.Algernon;
import org.algernon.util.ErrorSet;
import org.algernon.datatype.Result;
import org.algernon.datatype.BindingList;
import org.algernon.kb.okbc.protege.AlgernonProtegeKB;
import org.algernon.kb.AlgernonKB;


// date created: Wed Aug 14 11:02:44 2002
/**
 * Opens the Protege newspaper KB and makes some simple queries on it.
 *
 * Example:
 * 
 * java  -classpath algernon.jar:protege.jar org.algernon.test.SimpleAlgernonExample
 * 
* @see org.algernon.Algernon * @author Micheal S. Hewett hewett@smi.stanford.edu * */ public class SimpleAlgernonExample extends Object { public static String NEWSPAPER_PROJECT = "/Applications/Protege_2.0/examples/newspaper/newspaper.pprj"; protected Algernon f_algy = null; // Algernon instance protected AlgernonKB f_kb = null; protected String f_projectFile = ""; // date created: Wed Aug 14 11:02:44 2002 // created by: Micheal S. Hewett hewett@smi.stanford.edu public SimpleAlgernonExample() { f_projectFile = NEWSPAPER_PROJECT; } public SimpleAlgernonExample(String project) { f_projectFile = project; } /** * Opens the KB and initializes Algernon. */ public void init() { try { f_algy = new Algernon(); f_kb = new AlgernonProtegeKB(f_algy, f_projectFile); f_algy.addKB(f_kb); } catch (Exception e) { System.err.println(e + "\nUnable to open the '" + f_projectFile + "' KB."); stop(); } } /** * Halts the program nicely. */ public void stop() { if (f_kb != null) f_kb.close(); } public static void main(String[] args) { SimpleAlgernonExample example = new SimpleAlgernonExample(NEWSPAPER_PROJECT); example.init(); example.start(); } public void start() { try { // Example that retrieves all bindings of the variable ?X. // Note that the variable names are case-sensitive: ErrorSet errors = new ErrorSet(); String query = "((:instance Author ?a)(name ?a ?name))"; Result result = (Result)f_algy.ask(query, errors); if (result == null) { System.err.println("Errors during ask of '" + query + "'"); System.err.println(errors.toString()); } else { // One way to access the results is to iterate on the results // using result.iterator(). Each element of the iterator // is of type BindingList. for (Iterator iterator = result.iterator(); iterator.hasNext();) { // Result contains some BindingList objects. BindingList bl = (BindingList) iterator.next(); // The bound object will either be a Java Object (Integer, Float, etc.) // or a KB object (typically a Protege Instance). // If you want Algernon objects, use algy.getAlgernonBinding(). Object author = f_algy.getBinding("?a", bl); Object name = f_algy.getBinding("?name", bl); System.out.println("Author: " + author + ", name: " + name); // If for some reason you don't know the names of the variables // in the Binding Lists, you can iterate on the BindingList to // retrieve variable names and values. See the JavaDoc documentation // for the BindingList api. } // A simpler way to show the results is to let Algernon print the results: System.out.println(f_algy.printResultToString("ask", result)); } } catch (Exception e) { System.err.println("Error running Algernon: " + e); e.printStackTrace(); } finally { stop(); } } }

Author: Micheal Hewett
Last Updated: Thursday, June 06, 2005