1

There's a lisp function in ResearchCyc called random-assertion. I want to call that from some Java code. I'm using the Cyc Core API Suite v1.0.0-rc5 (from http://dev.cyc.com), but I don't see any way to call underlying Lisp code.

In the old OpenCyc API there was an object called CycAccess that you could use for this, but I can't figure out how to get one. If I could find it, I'd call this

access.converseObject("(random-assertion)");

At least in ResearchCyc, this would retrieve a pseudo-random assertion from the Cyc knowledge base. Not sure if it would work in OpenCyc, but it might also work there.

Can someone explain how to call lisp-code like this through Cyc's java API?

KnowsStuff
  • 100
  • 7

1 Answers1

0

(Disclaimer: I am one of the developers of the Cyc APIs...)

The reference implementation of the Core API Suite is the Core Client, which is based on the Base Client... which, in turn, is derived from the old OpenCyc API. So, it's absolutely possible to call arbitrary lisp (SubL) code on ResearchCyc, in several different ways...

First of all, there already is a method which wraps random-assertion:

try {
  CycAccess access = CycAccessManager.getCurrentAccess();
  CycAssertion cycAssertion = access.getLookupTool().getRandomAssertion();
} catch (SessionException ex) {
  // Do something with the exception...
} catch (CycConnectionException connEx) {
  // Do something else...
}

Speaking to the general case, though, you'll find that the syntax is fairly similar to the OpenCyc API:

try {
  CycAccess access = CycAccessManager.getCurrentAccess();
  Object cycAssertion = access.converse().converseObject("(random-assertion)");
} catch (SessionException ex) {
  // Do something with the exception...
} catch (CycConnectionException connEx) {
  // Do something else...
}

Or, if it's safe to assume that the result will be a CycObject:

  ...
  CycAccess access = CycAccessManager.getCurrentAccess();
  CycObject cycAssertion = access.converse().converseCycObject("(random-assertion)");
  ...

However, the Base Client adds a new way of encapsulating SubL functions, via the com.cyc.baseclient.subl.SublFunction interface. The SublFunction interface itself is pretty minimal, but there are a number of classes under com.cyc.baseclient.subl.subtypes which provide implementations for you to extend. For example, if you're calling a no-arg function and expect back a CycObject, you could extend SublCycObjectNoArgFunction like so:

public static final SublCycObjectNoArgFunction RANDOM_ASSERTION_FUNCTION = 
      new SublCycObjectNoArgFunction("random-assertion");

...

try {
  CycAccess access = CycAccessManager.getCurrentAccess();
  CycObject cycAssertion = RANDOM_ASSERTION_FUNCTION.eval(access);
} catch (SessionException ex) {
  // Do something with the exception...
} catch (CycConnectionException connEx) {
  // Do something else...
}

(For other examples of this, see com.cyc.baseclient.subl.functions.* in the Base Client.)

This approach makes it fairly simple to define SubL functions as static fields without writing (or re-writing) much code. We expect the Core Client to gradually migrate towards this approach.

Lastly, you can use the implementation classes in the KB Client to convert your results to KBObjects. For example:

try {
  CycAccess access = CycAccessManager.getCurrentAccess();
  CycObject cycAssertion = RANDOM_ASSERTION_FUNCTION.eval(access);

  // To convert to a com.cyc.kb.Assertion:
  Assertion assertion = AssertionImpl.get(cycAssertion);

  // Or, to convert to a more general KBObject:
  KbObject kbObj = KbObjectImpl.get(cycAssertion);
} catch (SessionException ex) {
  // Do something with the exception...
} catch (CycConnectionException connEx) {
  // Do something else...
} catch (KbTypeException ex) {
  // Potentially thrown by AssertionImpl#get() & KbObjectImpl#get()
} catch (CreateException ex) {
  // Also potentially thrown by AssertionImpl#get() & KbObjectImpl#get()
}
  • Great. After much digging around, I was able to find something just about like your "general case" as an example, and got it working well. I'll have to try out SublCycObjectNoArgFunction and its relatives soon. – KnowsStuff Feb 09 '16 at 19:31