Algernon in Java Documentation
FAQ and How to...
How do I access Algernon query results from a Java program?
The Algernon.ask() and Algernon.tell() methods return an object
of class Result, or else null. If there was a serious error, null
is returned and the Error object contains relevant error messages.
Otherwise, you can pull result sets out of the Result object using
an iterator similar to the one below.
// Example that retrieves all bindings of the variable ?X.
// Note that the variable names are case-sensitive:
Result result = algy.ask("...");
if (result == null)
for (Iterator iterator = result.iterator();
// Result contains some BindingList objects.
BindingList bl = (BindingList) iterator.next();
Object value = algy.getBinding("?X", bl);
// 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().
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.
How to store rules in the KB
- Rules are instances of a class. Select or create a class for your rules.
You can store different sets of rules as instances of different classes.
- The default location is the class :ALGERNON-RULE, which is recommended to
to be a subclass of :CONSTRAINT.
- Recommended template slots for the class are:
- RuleClass, type=Class, cardinality=1, parents=:THING
- RuleSlot, type=Instance, cardinality=1, classes=:SLOT
- RuleBody, type=String, cardinality=1, required
- RuleTraceLevel, type=Symbol, cardinality=1, required
- Only RuleClass and RuleBody are required.
- The possible values for RuleTraceLevel should be :NORMAL, :SILENT,
:VERBOSE, :DETAIL and :DEBUG.
- A rule is assigned to a Class or a Slot, as stored in RuleClass and RuleSlot.
- The entire body of the rule is stored in the RuleBody slot.
How to use the Algernon Server
- In the Protege tab, select Server... from the Algernon menu.
- Enter a port number.
- From your client program, open a connection to the specified port.
socket = new Socket("localhost", 9888);
writer = new PrintWriter(socket.getOutputStream());
- Write paths to the socket, example:
writer.println("((:ADD-CLASS (?a :THING) (:NAME ?a \"Test-1\")))");
Why can't Algernon find my frames?
Algernon uses the :NAME slot in Protege to lookup frames. However,
many Protege KBs use a "browser key" to display the contents of another
frame as the "name". Algernon can't figure out which frame you want
if you use the "browser key" value.
Protege now prints the value of the :NAME slot in the title bar of an instance
display if the browser key is not :NAME. A programmatic way to find it is
the following Algernon code, which works in the (old) newspaper example project:
((:forc-instance (?x Author) (byname ?x "Michael Jordan")))
Note: FORC-INSTANCE appears to be broken in v3.3.0
Author: Micheal Hewett