Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

java API how to find a constant?

Help
Claudio
2009-10-14
2013-05-28
  • Claudio
    Claudio
    2009-10-14

    hi, I'm new to cyc and I want to use it in a java application. I need to find a constant without knowing exactly its name (hence I want a list of possible constants)
    I mean.. something like the autocompletion implemented here http://sw.opencyc.org/
    I cannot figure out how to do this using the java API, any kind of help would be appreciated :P

     
  • David Baxter
    David Baxter
    2009-10-14

    One way is to use one of these SubL functions:

    <pre>
    (define-api constant-complete-exact (string &optional (start 0) end)
      "Return a valid constant whose name exactly matches STRING.
    Optionally the START and END character positions can be
    specified, such that the STRING matches characters between the START and
    END range.  If no constant exists, return NIL."
      ((:argument-types
        (string stringp)
        (start fixnump)))
      (ret (kb-constant-complete-exact string start end)))

    (define-api constant-complete (prefix &optional case-sensitive? exact-length? (start 0) end)
      "Return all valid constants with PREFIX as a prefix of their name
    When CASE-SENSITIVE? is non-nil, the comparison is done in a case-sensitive fashion.
    When EXACT-LENGTH? is non-nil, the prefix must be the entire string
    Optionally the START and END character positions can be
    specified, such that the PREFIX matches characters between the START and
    END range.  If no constant exists, return NIL."
      ((:argument-types
        (prefix stringp)
        (case-sensitive? booleanp)
        (exact-length? booleanp)
        (start fixnump)))
      (ret (kb-constant-complete prefix case-sensitive? exact-length? start end)))

    (define-api constant-apropos (substring &optional case-sensitive? (start 0) end)
      "Return all valid constants with SUBSTRING somewhere in their name
    When CASE-SENSITIVE? is non-nil, the comparison is done in a case-sensitive fashion.
    Optionally the START and END character positions can be
    specified, such that the SUBSTRING matches characters between the START and
    END range.  If no constant exists, return NIL."
      ((:argument-types
        (substring stringp)
        (case-sensitive? booleanp)
        (start fixnump)))
      (ret (kb-constant-apropos substring case-sensitive? start end)))

    (define-api constant-postfix (postfix &optional case-sensitive? (start 0) end)
      "Return all valid constants with POSTFIX as a postfix of their name
    When CASE-SENSITIVE? is non-nil, the comparison is done in a case-sensitive fashion.
    Optionally the START and END character positions can be
    specified, such that the SUBSTRING matches characters between the START and
    END range.  If no constant exists, return NIL."
      ((:argument-types
        (postfix stringp)
        (case-sensitive? booleanp)
        (start fixnump)))
      (ret (kb-constant-postfix postfix case-sensitive? start end)))</pre>

    To use them in a java application, you'll need a CycAccess object, call it "cycAccess". Then you can do this:

    <pre>
        final CycSymbol CONSTANT_COMPLETE = CycObjectFactory.makeCycSymbol("CONSTANT-COMPLETE");
        final CycList constants = cycAccess.converseList(CycList.makeCycList(CONSTANT_COMPLETE, "Michael"));
        </pre>

    You should get a list like this:

    (MichaelABaldwin MichaelAherne MichaelAnthonyJewelers-Corporation MichaelBarnes MichaelBenedikt MichaelBiehn MichaelBishop MichaelBlakley MichaelBloomberg MichaelBolton MichaelBruce MichaelCafarella-ComputerScientist MichaelCaine MichaelCaloz MichaelCasey MichaelChang-TennisPlayer MichaelClarke-Musician MichaelCollins MichaelCollins-TheMovie MichaelCrawford MichaelCrichton MichaelDEisner MichaelDell MichaelDelorenzo MichaelDeluise MichaelDesBarres MichaelDiamond-Musician MichaelDorn MichaelDouglas MichaelDrayton MichaelDudikoff MichaelEaston MichaelFinley-BasketballPlayer MichaelFoodsInc MichaelGaleota MichaelGambon MichaelGreyeyes MichaelGross MichaelHorse MichaelHoward MichaelHurst MichaelHutchence-Musician MichaelImperioli MichaelIronside MichaelJFox MichaelJPollard MichaelJackson MichaelJohnson MichaelJohnson-Singer MichaelJordan MichaelKeaton MichaelLandon MichaelLaudrup-SoccerPlayer MichaelLearned MichaelMadsen MichaelMahonen MichaelMcClure MichaelMilken MichaelMoore MichaelMoriarty MichaelMukasey MichaelNesmith MichaelNorton MichaelOHare MichaelOLeavitt MichaelOndaatje MichaelOwen-SoccerPlayer MichaelOxley MichaelPKubeMcDowell MichaelPare MichaelPark MichaelPeterson MichaelPhelps MichaelPraed MichaelRapaport MichaelRichards MichaelRodrick MichaelSeewald-Photographer MichaelShanks MichaelSomare MichaelStewart-FootballPlayer-American MichaelStone MichaelSutton MichaelTheodoulou MichaelTracy-TheArtist MichaelVGazzo MichaelWalsh MichaelWaltrip-RaceCarDriver MichaelWeaver MichaelWincott MichaelYork MichaelsComputer-9 MichaelsStores-Corporation MichaelsStoresInc)

     
  • Claudio
    Claudio
    2009-10-15

    thank you very much! that's exactly what I was searching for :)
    now I have another question.. I hope the last one
    I'm using the web interface to do some practice with the query tool. say that I want to retrieve the list of "prettyString" relative to the concept "Cat"
    why a query like this:
    (#$prettyString #$Cat ?X)
    returns nothing?
    isn't "prettyString" a predicate just like "isa"?

     

  • Anonymous
    2009-10-15

    Try changing the query mt (context) to #$EnglishMt. The default query mt doesn't see everything.

     
  • Claudio
    Claudio
    2009-10-15

    now it works ;)
    for the sake of completeness I post the java code:
    <pre>
    CycConstant conceptConst = cycAccess.find(constName);
    CycVariable xVariable = CycObjectFactory.makeCycVariable("?X");
    CycConstant microTheory = cycAccess.getConstantByName("EnglishMt");
    CycList query = cycAccess.makeCycList("(#$prettyString " + conceptConst.cyclify() + " ?X)");
    CycList result = cycAccess.queryVariable(xVariable, query, microTheory, null);
    for (Object object : result) {
    System.out.println(object.toString());
    }
    </pre>