/** * Create an access to Cyc. Must be call from all methods who converse with Cyc. * * @throw CycSupportError if some Exception occurs */ private void createAccess() throws CycSupportError { if (myAccess == null) { try { if (hostName == null) { myAccess = new CycAccess(); } else { int portNumber = CycConnection.DEFAULT_BASE_PORT; int index = hostName.indexOf(":"); if (index != -1 && (hostName.length() > (index + 1))) { try { portNumber = Integer.parseInt(hostName.substring(index + 1)); } catch (NumberFormatException e) { portNumber = CycConnection.DEFAULT_BASE_PORT; } hostName = hostName.substring(0, index); } String localName; String localIP; try { localName = InetAddress.getLocalHost().getHostName(); localIP = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { localName = "unknown"; localIP = "unknown"; } if (localName.equalsIgnoreCase(hostName)) hostName = "localhost"; else if (localIP.equals(hostName)) hostName = "localhost"; myAccess = new CycAccess( hostName, portNumber, CycConnection.DEFAULT_COMMUNICATION_MODE, CycAccess.DEFAULT_CONNECTION); } } catch (CycApiException ce) { ce.printStackTrace(); throw new CycSupportError("Socket error!"); } catch (UnknownHostException ue) { ue.printStackTrace(); throw new CycSupportError("Unable to find OpenCyc!"); } catch (IOException ioe) { ioe.printStackTrace(); throw new CycSupportError(ioe.getMessage() + LINE_SEPARATOR + "I/O error!"); } catch (Exception e) { e.printStackTrace(); throw new CycSupportError(e.getMessage() + LINE_SEPARATOR + "Unknow error!"); } } // javax.swing.JOptionPane.showMessageDialog(null, "Ok!"); }
@SuppressWarnings("unchecked") public String executeRandomCycL(String formula) throws CycSupportError { createAccess(); try { Object res = myAccess.converseObject(formula); // This will store the result String result = res.toString(); if (result.equals(CYC_FALSE)) { result = FALSE; checkAndStore(formula, result); } else if (trimBracketAndQuote(result).equals(CYC_FALSE)) { result = TRUE; checkAndStore(formula, result); } else if (res instanceof CycList) { checkAndStore(formula, result); CycList cl = ((CycList) res).randomPermutation(); ArrayList results = getVariables(cl); if (results.size() > 0) { result = getContentVariable(results.get(0).toString()); } else result = UNKNOWN; } return result; } catch (CycApiException ce) { ce.printStackTrace(); checkAndStore(formula, "Cyc communication error!"); throw new CycSupportError("Cyc communication error!"); } catch (UnknownHostException oe) { checkAndStore(formula, "Unable to find OpenCyc!"); throw new CycSupportError("Unable to find OpenCyc!"); } catch (IOException ioe) { checkAndStore(formula, "I/O error!"); throw new CycSupportError("I/O error!"); } catch (Exception e) { checkAndStore(formula, "Unknow error!"); throw new CycSupportError("Unknow error!"); } }
// Opencyc // algrism***************************************************************************************************************************************************// private void sample() { // TODO Auto-generated method stub System.out.println("Starting Cyc NART examples."); try { access = new CycAccess(); // Log.current.println("Demonstrating getConstantGuid api function.\n"); // Guid a = access.getConstantGuid("apple"); CycConstant cycAdministrator = access.getKnownConstantByName("CycAdministrator"); System.out.println("Unique id ?" + cycAdministrator.guid); System.out.println("Unique id ?" + DefaultCycObject.toCompactExternalId("AppleInc")); CycList isas = access.getIsas(access.getKnownConstantByName("AppleInc")); Log.current.println("\nThe obtained isas are:\n" + isas.cyclify()); CycConstant generalCycKE = access.getKnownConstantByName("GeneralCycKE"); access.setCyclist(cycAdministrator); // needed to maintain // bookeeping information access.setKePurpose(generalCycKE); // needed to maintain bookeeping // information // find nart by external id (preferred lookup mechanism) CycNart apple = (CycNart) DefaultCycObject.fromCompactExternalId( "Mx8Ngh4rvVipdpwpEbGdrcN5Y29ycB4rvVjBnZwpEbGdrcN5Y29ycA", access); // access.getConstantId(""); // find nart by name (dispreferred because names in the KB can // change) CycNart apple2 = access.getCycNartFromCons( (CycList) CycLParserUtil.parseCycLDenotationalTerm("(FruitFn AppleTree)", true, access)); assert apple.equals(apple2) : "Lookup failed to produce equal results."; // getting the external id DefaultCycObject.tofor a NART String appleEID = DefaultCycObject.toCompactExternalId(apple, access); // creating a nart // There is no direct way of creating NARTs, they are an // implementation detail // of the inference engine. However, if you make an assertion using // arguments // to a reifiable function that the inference engine hasn't seen // before, then it will be create // automatically. CycNart elmFruit = new CycNart( access.getKnownConstantByName("FruitFn"), access.getKnownConstantByName("ElmTree")); // NOTE: the previous call only makes the NART locally and not in // the KB // Asserting the isa and genls relations // every new term should have at least 1 isa assertion made on it access.assertIsa(elmFruit, CycAccess.collection, CycAccess.baseKB); // Note: the previous line causes the new NART to be created in the // KB! // Every new collection should have at least 1 genls assertion made // on it, // however, in this case, the inference engine has made it for you // since // any new terms involving FruitFn's must be types of fruits. // access.assertGenls(elmFruit, // access.getKnownConstantByName("Fruit"), CycAccess.baseKB); // verify genls relation assert access.isSpecOf(elmFruit, access.getKnownConstantByName("Fruit"), CycAccess.baseKB) : "Good grief! Elm fruit isn't known to be a type of fruit."; // find everything that is an apple System.out.println("Got instances of Apple: " + access.getInstances(apple)); System.out.println( "Got instances of Apple: " + access.getAllInstances(apple, CycAccess.baseKB)); // find everything that a apple is a type of System.out.println( "Got generalizations of Apple: " + access.getAllGenls(apple, CycAccess.baseKB)); // find everything that is a type of apple System.out.println( "Got specializations of Apple: " + access.getAllSpecs(apple, CycAccess.baseKB)); // generating NL // System.out.println("The concept " + apple.cyclify() // + " can be referred to in English as '" + // access.getGeneratedPhrase(apple) + "'."); // Killing a NART -- removing a NART and all assertions involving // that NART from the KB // Warning: you can potentially do serious harm to the KB if you // remove critical information access.kill(elmFruit); // CycConstant dog2 = access.getKnownConstantByName("Dog"); // CycFort snowSkiing = access.getKnownConstantByName("SnowSkiing"); } catch (UnknownHostException nohost) { nohost.printStackTrace(); } catch (IOException io) { io.printStackTrace(); } catch (CycApiException cyc_e) { cyc_e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } System.out.println("Finished."); }