Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

ApiDemo.java

Go to the documentation of this file.
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  * Provides a simple demo of the OpenCyc API.<p>
00010  *
00011  * @version $Id: ApiDemo.java,v 1.1 2002/03/27 19:51:19 stephenreed Exp $
00012  * @author Stephen L. Reed
00013  *
00014  * <p>Copyright 2001 Cycorp, Inc., license is open source GNU LGPL.
00015  * <p><a href="http://www.opencyc.org/license.txt">the license</a>
00016  * <p><a href="http://www.opencyc.org">www.opencyc.org</a>
00017  * <p><a href="http://www.sourceforge.net/projects/opencyc">OpenCyc at SourceForge</a>
00018  * <p>
00019  * THIS SOFTWARE AND KNOWLEDGE BASE CONTENT ARE PROVIDED ``AS IS'' AND
00020  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00021  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00022  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENCYC
00023  * ORGANIZATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00024  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00026  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00027  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00028  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE AND KNOWLEDGE
00030  * BASE CONTENT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 public class ApiDemo {
00034 
00035     /**
00036      * the CycAccess object
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      * Interacts with the user to perform specified demos.
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      * Provides the main method for the api demo application
00139      */
00140     public static void main(String[] args) {
00141         ApiDemo apiDemo = new ApiDemo();
00142         apiDemo.demoInteraction();
00143     }
00144 
00145     /**
00146      * Demonstrates getKnownConstantByName api function.
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      * Demonstrates getConstantGuid api function.
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      * Demonstrates getComment api function.
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      * Demonstrates getIsas api function.
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      * Demonstrates getGenls api function.
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      * Demonstrates getArity api function.
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      * Demonstrates arg1Isas api function.
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      * Demonstrates getArgNGenls api function.
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      * Demonstrates getParaphrase (with quantified formula) api function.
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      * Demonstrates getParaphrase (with quantified formula) api function.
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      * Demonstrates getImpreciseParaphrase (with quantified formula) api function.
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      * Demonstrates usage of CycNart and getInstanceSiblings api function.
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      * Demonstrates usage of isQueryTrue api function.
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      * Demonstrates usage of the assertGaf api function.
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      * Demonstrates usage of the unassertGaf api function.
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      * Demonstrates usage of the rkfPhraseReader api function.
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      * Demonstrates usage of the generateDisambiguationPhraseAndTypes api function.
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 }

Generated on Sat Sep 3 23:03:25 2005 for JCalculator 0.1b by  doxygen 1.4.4