Casting CycList when using queryVariables

Help
Suzette
2008-11-10
2013-05-28
  • Suzette
    Suzette
    2008-11-10

    I want to query OpenCyc to get back all relationships between 2 elements; I consulted code shared online – in those examples, the programmer was able to take the result from the query and cast CycList elements into CycConstants.  When I try this with version 1.0.2, it gives me an exception, that I cannot cast CycList into CycConstant.  So my question is – what types can we cast CycList into with OpenCyc API v 1.0.2.  I finally used Object to cast the element; but I get back the original String variable, see below.  Also I proved that the query is correct and is in the knowlege base, it should bind.

    Thanks for any help,
    Suzette

    HERE IS HOW I AM INVOKING bindQuery:

    OpenCycInterface oc = new OpenCycInterface();
             ArrayList<String> result = new ArrayList<String>();
             try{
                String query = "(?X #$Abbreviation #$ObjectType)";
                String variable = "?X";
                result = oc.bindQuery(query, variable);
             }
             catch(Exception e){
                 System.out.println("Exception caught: "+e);
             }
             for(String r : result)
                 System.out.println(r);

    HERE IS bindQuery:

    public ArrayList<String> bindQuery (String query, String variable) throws IOException, UnknownHostException, CycApiException {

             ArrayList<String> result = new ArrayList<String>();

             CycList gaf = cycAccess.makeCycList(query);
             HashMap hm = new HashMap();
             CycFort mt = cycAccess.getConstantByName("UniversalVocabularyMt");
             CycList variables = CycList.makeCycList(variable);
             CycList response = cycAccess.queryVariables(variables, gaf, mt, hm);
            
             Iterator iterator=response.iterator();
             while (iterator.hasNext()){
                Object item=(Object)iterator.next();
                result.add(item.toString());
            }
           
            return result;

        }

    I GET BACK:

    ("?X")

     
    • Since CycAccess defines makeCycList as always returning a CycList

      public CycList makeCycList(String string)
                              throws CycApiException {
            return (new CycListParser(this)).read(string);
        }

      String variable = "?X";
      CycList variables = CycList.makeCycList(variable);

      Is not going to work out.

      So here is one way I'd define bindQuery:

      public ArrayList<String> bindQuery (String query, String variable) throws IOException, UnknownHostException, CycApiException {

      ArrayList<String> result = new ArrayList<String>();

        CycList gaf = cycAccess.makeCycList(query);
        HashMap hm = new HashMap();
        CycFort mt = cycAccess.getConstantByName("InferencePSC");
        // Sees more than Universal Vocab .. but EverythingPSC sees the most
        CycList variables = new CycList(new CycVariable(variable));
        CycList response = cycAccess.queryVariables(variables, gaf, mt, hm);

         Iterator iterator=response.iterator();
         while (iterator.hasNext()){
            Object item=(Object)iterator.next();
             result.add(item.toString());
         }

         return result;

      }

       
    • Suzette
      Suzette
      2008-11-11

      It works !  Thanks SO much for the help, I appreciate it more than you know !! 

      Suzette

       
  • Anand Nyam
    Anand Nyam
    2010-07-07

    Is it the only way to parse the result?
    Iterator iterator=response.iterator();
       while (iterator.hasNext()){
          Object item=(Object)iterator.next();
           result.add(item.toString());
       }

    or can I retrive the values of ?X directly from result so that its not dependent on positions of varibales?? Is there a way where I can do like

    Iterator iterator=response.iterator();
       while (iterator.hasNext()){
                Object item = (Object)iterator.next();
                String value = (String)item.getVaribale("?X");
            }

     
  • David Baxter
    David Baxter
    2010-07-07

    Anand,

    This capability will be in the next release of OpenCyc. We have a ResultSet class that implements java.sql.ResultSet, and some new inference methods that return it. I'm afraid we don't yet have a scheduled release date for that.

    David