00001 package org.opencyc.api;
00002
00003 import java.io.*;
00004 import java.net.*;
00005 import org.opencyc.cycobject.*;
00006 import org.opencyc.util.*;
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 public class ApiDemo {
00034
00035
00036
00037
00038 protected CycAccess cycAccess;
00039
00040 public ApiDemo() {
00041 Log.makeLog();
00042 Log.current.println("Initializing Cyc server connection, and caching frequently used terms.");
00043 try {
00044 cycAccess = new CycAccess();
00045 }
00046 catch (Exception e) {
00047 Log.current.errorPrintln(e.getMessage());
00048 Log.current.printStackTrace(e);
00049 }
00050 cycAccess.traceOn();
00051 Log.current.println("Now tracing Cyc server messages");
00052 }
00053
00054
00055
00056
00057 protected void demoInteraction() {
00058 Log.current.println("Ready. Enter demo number 1 ... 17, or exit");
00059 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
00060 try {
00061 while (true) {
00062 System.out.print("> ");
00063 String userDemoCommand = stdin.readLine();
00064 if (userDemoCommand.equals("exit"))
00065 return;
00066 int demoNbr = 0;
00067 try {
00068 demoNbr = Integer.parseInt(userDemoCommand);
00069 }
00070 catch (NumberFormatException e) {
00071 Log.current.println("Not a valid demo number");
00072 continue;
00073 }
00074 switch (demoNbr) {
00075 case 1:
00076 demo1();
00077 break;
00078 case 2:
00079 demo2();
00080 break;
00081 case 3:
00082 demo3();
00083 break;
00084 case 4:
00085 demo4();
00086 break;
00087 case 5:
00088 demo5();
00089 break;
00090 case 6:
00091 demo6();
00092 break;
00093 case 7:
00094 demo7();
00095 break;
00096 case 8:
00097 demo8();
00098 break;
00099 case 9:
00100 demo9();
00101 break;
00102 case 10:
00103 demo10();
00104 break;
00105 case 11:
00106 demo11();
00107 break;
00108 case 12:
00109 demo12();
00110 break;
00111 case 13:
00112 demo13();
00113 break;
00114 case 14:
00115 demo14();
00116 break;
00117 case 15:
00118 demo15();
00119 break;
00120 case 16:
00121 demo16();
00122 break;
00123 case 17:
00124 demo17();
00125 break;
00126 default:
00127 Log.current.println("Not a valid demo number");
00128 }
00129 }
00130 }
00131 catch (Exception e) {
00132 Log.current.errorPrintln(e.getMessage());
00133 Log.current.printStackTrace(e);
00134 }
00135 }
00136
00137
00138
00139
00140 public static void main(String[] args) {
00141 ApiDemo apiDemo = new ApiDemo();
00142 apiDemo.demoInteraction();
00143 }
00144
00145
00146
00147
00148 protected void demo1 () throws IOException, UnknownHostException, CycApiException {
00149 Log.current.println("Demonstrating getKnownConstantByName api function.\n");
00150 CycFort snowSkiing = cycAccess.getKnownConstantByName("SnowSkiing");
00151 Log.current.println("\nThe obtained constant is " + snowSkiing.cyclify());
00152 }
00153
00154
00155
00156
00157 protected void demo2 () throws IOException, UnknownHostException, CycApiException {
00158 Log.current.println("Demonstrating getConstantGuid api function.\n");
00159 Guid unitedStatesOfAmericaGuid = cycAccess.getConstantGuid("UnitedStatesOfAmerica");
00160 Log.current.println("\nThe obtained guid is " + unitedStatesOfAmericaGuid);
00161 }
00162
00163
00164
00165
00166 protected void demo3 () throws IOException, UnknownHostException, CycApiException {
00167 Log.current.println("Demonstrating getComment api function.\n");
00168 String comment = cycAccess.getComment(cycAccess.getKnownConstantByName("bordersOn"));
00169 Log.current.println("\nThe obtained comment is:\n" + comment);
00170 }
00171
00172
00173
00174
00175 protected void demo4 () throws IOException, UnknownHostException, CycApiException {
00176 Log.current.println("Demonstrating getIsas api function.\n");
00177 CycList isas = cycAccess.getIsas(cycAccess.getKnownConstantByName("BillClinton"));
00178 Log.current.println("\nThe obtained isas are:\n" + isas.cyclify());
00179 }
00180
00181
00182
00183
00184 protected void demo5 () throws IOException, UnknownHostException, CycApiException {
00185 Log.current.println("Demonstrating getGenls api function.\n");
00186 CycList genls = cycAccess.getGenls(cycAccess.getKnownConstantByName("Dog"));
00187 Log.current.println("\nThe obtained direct genls are:\n" + genls.cyclify());
00188 }
00189
00190
00191
00192
00193 protected void demo6 () throws IOException, UnknownHostException, CycApiException {
00194 Log.current.println("Demonstrating getArity api function.\n");
00195 int arity = cycAccess.getArity(cycAccess.getKnownConstantByName("likesAsFriend"));
00196 Log.current.println("\nThe obtained arity is " + arity);
00197 }
00198
00199
00200
00201
00202 protected void demo7 () throws IOException, UnknownHostException, CycApiException {
00203 Log.current.println("Demonstrating arg1Isas api function.\n");
00204 CycList arg1Isas = cycAccess.getArg1Isas(cycAccess.getKnownConstantByName("performedBy"));
00205 Log.current.println("\nThe obtained arg1Isas are:\n" + arg1Isas.cyclify());
00206 }
00207
00208
00209
00210
00211 protected void demo8 () throws IOException, UnknownHostException, CycApiException {
00212 Log.current.println("Demonstrating getArgNGenls api function.\n");
00213 CycList argNGenls = cycAccess.getArgNGenls(cycAccess.getKnownConstantByName("skillCapableOf"), 2);
00214 Log.current.println("\nThe obtained getArgNGenls are:\n" + argNGenls.cyclify());
00215 }
00216
00217
00218
00219
00220 protected void demo9 () throws IOException, UnknownHostException, CycApiException {
00221 Log.current.println("Demonstrating getParaphrase api function.\n");
00222 CycList formula = cycAccess.makeCycList("(#$forAll ?THING (#$isa ?Thing #$Thing))");
00223 String paraphrase = cycAccess.getParaphrase(formula);
00224 Log.current.println("\nThe obtained paraphrase for\n" + formula + "\nis:\n" + paraphrase);
00225 }
00226
00227
00228
00229
00230 protected void demo10 () throws IOException, UnknownHostException, CycApiException {
00231 Log.current.println("Demonstrating getParaphrase api function.\n");
00232 CycList formula = cycAccess.makeCycList(
00233 "(#$thereExists ?PLANET\n" +
00234 " (#$and\n" +
00235 " (#$isa ?PLANET #$Planet)\n" +
00236 " (#$orbits ?PLANET #$Sun)))");
00237 String paraphrase = cycAccess.getParaphrase(formula);
00238 Log.current.println("\nThe obtained paraphrase for\n" + formula + "\nis:\n" + paraphrase);
00239 }
00240
00241
00242
00243
00244 protected void demo11 () throws IOException, UnknownHostException, CycApiException {
00245 Log.current.println("Demonstrating getImpreciseParaphrase api function.\n");
00246 CycList formula = cycAccess.makeCycList(
00247 "(#$forAll ?PERSON1\n" +
00248 " (#$implies\n" +
00249 " (#$isa ?PERSON1 #$Person)\n" +
00250 " (#$thereExists ?PERSON\n" +
00251 " (#$and\n" +
00252 " (#$isa ?PERSON2 #$Person)\n" +
00253 " (#$loves ?PERSON1 ?PERSON2)))))");
00254 String paraphrase = cycAccess.getImpreciseParaphrase(formula);
00255 Log.current.println("\nThe obtained imprecise paraphrase for\n" + formula + "\nis:\n" + paraphrase);
00256 }
00257
00258
00259
00260
00261 protected void demo12 () throws IOException, UnknownHostException, CycApiException {
00262 Log.current.println("Demonstrating CycNart and getInstanceSiblings api function.\n");
00263 CycNart usGovernment = new CycNart(cycAccess.getKnownConstantByName("GovernmentFn"),
00264 cycAccess.getKnownConstantByName("UnitedStatesOfAmerica"));
00265 CycList siblings = cycAccess.getInstanceSiblings(usGovernment);
00266 Log.current.println("\nThe obtained instance sibling terms of " + usGovernment + "\nare:\n" + siblings.cyclify());
00267 }
00268
00269
00270
00271
00272 protected void demo13 () throws IOException, UnknownHostException, CycApiException {
00273 Log.current.println("Demonstrating isQueryTrue api function.\n");
00274 CycList gaf = cycAccess.makeCycList("(#$likesAsFriend #$BillClinton #$AlbertGore)");
00275 CycFort mt = cycAccess.getKnownConstantByName("PeopleDataMt");
00276 boolean isQueryTrue = cycAccess.isQueryTrue(gaf, mt);
00277 if (isQueryTrue)
00278 Log.current.println("\nThe assertion\n" + gaf + "\nis true in the " + mt.cyclify());
00279 else
00280 Log.current.println("\nThe assertion\n" + gaf + "\nis not true in the " + mt.cyclify());
00281 }
00282
00283
00284
00285
00286 protected void demo14 () throws IOException, UnknownHostException, CycApiException {
00287 Log.current.println("Demonstrating usage of the assertGaf api function.\n");
00288 CycFort mt = cycAccess.getKnownConstantByName("PeopleDataMt");
00289 CycList gaf = cycAccess.makeCycList("(#$likesAsFriend #$BillClinton #$AlbertGore)");
00290 cycAccess.assertGaf(gaf, mt);
00291 }
00292
00293
00294
00295
00296 protected void demo15 () throws IOException, UnknownHostException, CycApiException {
00297 Log.current.println("Demonstrating usage of the unassertGaf api function.\n");
00298 CycFort mt = cycAccess.getKnownConstantByName("PeopleDataMt");
00299 CycList gaf = cycAccess.makeCycList("(#$likesAsFriend #$BillClinton #$AlbertGore)");
00300 cycAccess.unassertGaf(gaf, mt);
00301 }
00302
00303
00304
00305
00306 protected void demo16 () throws IOException, UnknownHostException, CycApiException {
00307 Log.current.println("Demonstrating usage of the rkfPhraseReader api function.\n");
00308 String phrase = "penguins";
00309 CycFort inferencePsc =
00310 cycAccess.getKnownConstantByGuid("bd58915a-9c29-11b1-9dad-c379636f7270");
00311 CycFort rkfEnglishLexicalMicrotheoryPsc =
00312 cycAccess.getKnownConstantByGuid("bf6df6e3-9c29-11b1-9dad-c379636f7270");
00313 CycList parsingExpression = cycAccess.rkfPhraseReader(phrase,
00314 rkfEnglishLexicalMicrotheoryPsc,
00315 inferencePsc);
00316 Log.current.println("the result of parsing the phrase \"" + phrase + "\" is\n" + parsingExpression);
00317 }
00318
00319
00320
00321
00322 protected void demo17 () throws IOException, UnknownHostException, CycApiException {
00323 Log.current.println("Demonstrating usage of the generateDisambiguationPhraseAndTypes api function.\n");
00324 CycFort mt = cycAccess.getKnownConstantByName("PeopleDataMt");
00325 CycList objects = cycAccess.makeCycList("(#$Penguin #$PittsburghPenguins)");
00326 CycList disambiguationExpression = cycAccess.generateDisambiguationPhraseAndTypes(objects);
00327 Log.current.println("the result of disambiguating the objects \"" + objects.cyclify() + "\" is\n" +
00328 disambiguationExpression);
00329 }
00330
00331 }