$search
00001 package instruction.opencyc; 00002 00003 import java.io.FileNotFoundException; 00004 import java.io.IOException; 00005 import java.net.UnknownHostException; 00006 import java.util.ArrayList; 00007 import java.util.Iterator; 00008 import org.opencyc.api.CycAccess; 00009 import org.opencyc.api.CycApiException; 00010 import org.opencyc.api.CycObjectFactory; 00011 import org.opencyc.cycobject.CycConstant; 00012 import org.opencyc.cycobject.CycFort; 00013 import org.opencyc.cycobject.CycList; 00014 import org.opencyc.cycobject.CycObject; 00015 import org.opencyc.cycobject.CycVariable; 00016 00017 public class OpenCyc { 00018 00019 public static final String MT_UNIVERSAL_VOCABULARY = "#$UniversalVocabularyMt"; 00020 public static final String MT_TUM_KITCHEN_ENVIRONMENT = "#$TUMKitchenEnvironmentMt"; 00021 public static final String MT_TUM_KITCHEN_ENVIRONMENT_PLANNING = "#$TUMKitchenEnvironmentPlanningMt"; 00022 public static final String MT_TOPIC = "#$TopicMt"; 00023 public static final String MT_WORDNET_MAPPING = "#$WordNetMappingMt"; 00024 00025 public static final String PRED_IS_EXECUTABLE_BY_ROBOT = "#$isExecutableByRobot"; 00026 public static final String PRED_AMOUNT_OF_OBJECT = "#$amountOfObject"; 00027 public static final String COLL_TYPICAL_OBJECT_IN_KITCHEN = "#$TypicalObjectInKitchen"; 00028 00029 public static final String MT_TUM_KITCHEN_ENVIRONMENT_COMMENT = "Microtheory containing specific Assertions for the TUM Kitchen Environment"; 00030 public static final String MT_TUM_KITCHEN_ENVIRONMENT_PLANNING_COMMENT = "Microtheory containing Plans for the TUM Kitchen Environment"; 00031 00033 protected static CycAccess cyc = null; 00034 00036 protected static OpenCyc me = null; 00037 00039 protected CycObject universalVocMt = null; 00040 00042 protected CycObject kitchenEnvMt = null; 00043 00045 protected CycObject kitchenPlanningMt = null; 00046 00048 protected CycObject topicMt = null; 00049 00051 protected CycObject wordNetMappingMt = null; 00052 00054 protected CycCache cache = null; 00055 00056 protected OpenCyc () throws UnknownHostException, CycApiException, IOException { 00057 00058 cyc = new CycAccess(); 00059 makeCycAssertions(); 00060 } 00061 00067 public CycObject getKitchenEnvMt() { 00068 00069 return kitchenEnvMt; 00070 } 00071 00077 public CycObject getKitchenEnvPlanningMt() { 00078 00079 return kitchenPlanningMt; 00080 } 00081 00087 public CycObject getUniversalVocMt() { 00088 00089 return universalVocMt; 00090 } 00091 00092 00098 public CycObject getWordNetMappingMt() { 00099 00100 return wordNetMappingMt; 00101 } 00102 00110 protected void initializeCyc() throws UnknownHostException, CycApiException, IOException, FileNotFoundException { 00111 00112 universalVocMt = cyc.getConstantByName( MT_UNIVERSAL_VOCABULARY ); 00113 kitchenEnvMt = cyc.getConstantByName( MT_TUM_KITCHEN_ENVIRONMENT ); 00114 kitchenPlanningMt = cyc.getConstantByName( MT_TUM_KITCHEN_ENVIRONMENT_PLANNING ); 00115 topicMt = cyc.getConstantByName( MT_TOPIC ); 00116 wordNetMappingMt = cyc.getConstantByName( MT_WORDNET_MAPPING ); 00117 00118 // Initialize the Cyc concept cache 00119 cache = new CycCache(); 00120 cache.load(); 00121 } 00122 00123 public void addMapping(String synset, String concept) throws UnknownHostException, CycApiException, IOException { 00124 CycList assertion = cyc.makeCycList( "(#$ist " + MT_WORDNET_MAPPING + " " 00125 + "(#$synonymousExternalConcept #$" + concept + " #$WordNet-Version2_0 \"" + synset 00126 + "\"))" ); 00127 cyc.assertGaf( assertion, wordNetMappingMt ); 00128 } 00129 00138 public boolean constantExists( String constant ) throws UnknownHostException, IOException { 00139 00140 try { 00141 if ( cyc.getConstantByName( constant ) != null ) { 00142 return true; 00143 } 00144 else { 00145 return false; 00146 } 00147 } 00148 catch ( CycApiException e ) { 00149 return false; 00150 } 00151 } 00152 00164 @SuppressWarnings("deprecation") 00165 public boolean genlsInUniversalVocabularyMt( String subConcept, String upperConcept ) 00166 throws UnknownHostException, CycApiException, IOException { 00167 00168 CycList query = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt " + "(#$genls #$" + subConcept + " #$" 00169 + upperConcept + "))" ); 00170 00171 return cyc.isQueryTrue( query, universalVocMt ); 00172 } 00173 00185 @SuppressWarnings("deprecation") 00186 public boolean typeGenlsInUniversalVocabularyMt( String subConcept, String upperConcept ) 00187 throws UnknownHostException, CycApiException, IOException { 00188 00189 CycList query = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt " + "(#$typeGenls #$" + subConcept 00190 + " #$" + upperConcept + "))" ); 00191 00192 return cyc.isQueryTrue( query, universalVocMt ); 00193 } 00194 00206 @SuppressWarnings("deprecation") 00207 public boolean genlsInTopicMt( String subConcept, String upperConcept ) throws UnknownHostException, 00208 CycApiException, IOException { 00209 00210 CycList query = cyc.makeCycList( "(#$ist #$TopicMt (#$genls #$" + subConcept + " #$" + upperConcept 00211 + "))" ); 00212 00213 return cyc.isQueryTrue( query, topicMt ); 00214 } 00215 00225 public void makeAssertionInKitchenMt( String assertion ) throws CycApiException, UnknownHostException, 00226 IOException { 00227 00228 CycList list = OpenCyc.getInstance().getCycAcces().makeCycList( 00229 "(#$ist " + MT_TUM_KITCHEN_ENVIRONMENT + " " + assertion + ")" ); 00230 System.out.println( assertion ); 00231 OpenCyc.getInstance().getCycAcces().assertGaf( list, OpenCyc.getInstance().getKitchenEnvMt() ); 00232 } 00233 00243 public void makeAssertionInKitchenPlanningMt( String assertion ) throws CycApiException, 00244 UnknownHostException, IOException { 00245 00246 CycList list = OpenCyc.getInstance().getCycAcces().makeCycList( 00247 "(#$ist " + MT_TUM_KITCHEN_ENVIRONMENT_PLANNING + " " + assertion + ")" ); 00248 System.out.println( assertion ); 00249 OpenCyc.getInstance().getCycAcces().assertGaf( list, OpenCyc.getInstance().getKitchenEnvPlanningMt() ); 00250 } 00251 00263 @SuppressWarnings("deprecation") 00264 public String getTypeOfArg( String pred, int arg ) throws UnknownHostException, CycApiException, 00265 IOException { 00266 00267 CycList query = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt " + "(#$argIsa #$" + pred + " " + arg 00268 + " ?TYPE))" ); 00269 00270 CycVariable var = CycObjectFactory.makeCycVariable( "?TYPE" ); 00271 00272 CycList ret = cyc.askWithVariable( query, var, universalVocMt ); 00273 00274 if ( ret.size() > 0 ) 00275 return ret.get( 0 ).toString(); 00276 00277 return null; 00278 } 00279 00291 @SuppressWarnings("deprecation") 00292 public boolean isaInTopicMt( String subConcept, String upperConcept ) throws UnknownHostException, 00293 CycApiException, IOException { 00294 00295 CycList query = cyc.makeCycList( "(#$ist #$TopicMt (#$isa #$" + subConcept + " #$" + upperConcept + "))" ); 00296 00297 return cyc.isQueryTrue( query, topicMt ); 00298 } 00299 00309 @SuppressWarnings({ "deprecation", "unchecked" }) 00310 public ArrayList<String> getCycConceptFromWordNetID( String synsetID ) throws Exception { 00311 00312 // Return the cached concepts, if existing 00313 ArrayList<String> concepts = (ArrayList) cache.ask( synsetID ); 00314 00315 if (concepts != null) { 00316 return concepts; 00317 } 00318 00319 // Ask Cyc for the concepts 00320 CycList query = cyc.makeCycList( "(#$ist #$WordNetMappingMt " 00321 + "(#$synonymousExternalConcept ?CONCEPT #$WordNet-Version2_0 \"" + synsetID + "\"))" ); 00322 00323 CycVariable var = CycObjectFactory.makeCycVariable( "?CONCEPT" ); 00324 00325 CycFort mt = cyc.getConstantByName( "#$WordNetMappingMt" ); 00326 00327 CycList ret = cyc.askWithVariable( query, var, mt ); 00328 00329 ArrayList<String> list = new ArrayList<String>(); 00330 for ( Iterator i = ret.iterator(); i.hasNext(); ) { 00331 String str = i.next().toString(); 00332 if ( str.indexOf( "(" ) == - 1 && str.indexOf( ")" ) == - 1 && str.indexOf( " " ) == - 1 ) 00333 list.add( str ); 00334 } 00335 00336 // Cache the retrieved Cyc concepts 00337 cache.tell( synsetID, list ); 00338 cache.save(); 00339 00340 return list; 00341 } 00342 00343 public CycAccess getCycAcces() { 00344 00345 return cyc; 00346 } 00347 00357 public static OpenCyc getInstance() throws UnknownHostException, CycApiException, IOException { 00358 00359 if ( me == null ) { 00360 me = new OpenCyc20(); 00361 me.initializeCyc(); 00362 } 00363 return me; 00364 } 00365 00375 public int getArity( String predicate ) throws CycApiException, UnknownHostException, IOException { 00376 00377 int arity = cyc.getArity( cyc.getConstantByName( predicate ) ); 00378 return arity; 00379 } 00380 00391 @SuppressWarnings("deprecation") 00392 public ArrayList<String> getRequiredActorSlots( String concept ) throws UnknownHostException, 00393 CycApiException, IOException { 00394 00395 CycList query = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$requiredActorSlots #$" + concept 00396 + " ?ACTORSLOT))" ); 00397 CycVariable var = CycObjectFactory.makeCycVariable( "?ACTORSLOT" ); 00398 CycFort mt = cyc.getConstantByName( "#$UniversalVocabularyMt" ); 00399 00400 CycList ret = cyc.askWithVariable( query, var, mt ); 00401 00402 // System.out.println("Size: " + ret.size()); 00403 ArrayList<String> list = new ArrayList<String>(); 00404 for ( Iterator i = ret.iterator(); i.hasNext(); ) { 00405 String str = i.next().toString(); 00406 list.add( str ); 00407 } 00408 00409 return list; 00410 } 00411 00423 @SuppressWarnings("deprecation") 00424 public boolean isExecutableByRobot( String action ) throws UnknownHostException, CycApiException, 00425 IOException { 00426 00427 CycList query = cyc.makeCycList( "(#$ist " + MT_TUM_KITCHEN_ENVIRONMENT + " (" 00428 + PRED_IS_EXECUTABLE_BY_ROBOT + " #$" + action + "))" ); 00429 00430 return cyc.isQueryTrue( query, kitchenEnvMt ); 00431 00432 } 00433 00445 @SuppressWarnings("deprecation") 00446 public boolean isaInUnivVocMt( String concept, String upperConcept ) throws UnknownHostException, 00447 CycApiException, IOException { 00448 00449 CycList query = cyc.makeCycList( "(#$ist " + MT_UNIVERSAL_VOCABULARY + " (" + "#$isa" + " #$" + concept 00450 + " " + "#$" + upperConcept + "))" ); 00451 00452 return cyc.isQueryTrue( query, universalVocMt ); 00453 00454 } 00455 00467 @SuppressWarnings("deprecation") 00468 public boolean isTypicalObjectInKitchen( String concept ) throws UnknownHostException, CycApiException, 00469 IOException { 00470 00471 CycList query = cyc.makeCycList( "(#$ist " + MT_TUM_KITCHEN_ENVIRONMENT + " (" + "#$isa" + " #$" 00472 + concept + " " + COLL_TYPICAL_OBJECT_IN_KITCHEN + "))" ); 00473 00474 return cyc.isQueryTrue( query, universalVocMt ); 00475 00476 } 00477 00488 @SuppressWarnings("deprecation") 00489 public boolean genlsSomethingExisting( String concept ) throws UnknownHostException, CycApiException, 00490 IOException { 00491 00492 CycList query = cyc.makeCycList( "(#$ist " + MT_UNIVERSAL_VOCABULARY + " (" + "#$genls" + " #$" + concept 00493 + " #$SomethingExisting))" ); 00494 00495 return cyc.isQueryTrue( query, universalVocMt ); 00496 00497 } 00498 00510 public void addExecutableByRobot( String action ) throws UnknownHostException, CycApiException, IOException { 00511 00512 CycList assertion = cyc.makeCycList( "(#$ist " + MT_TUM_KITCHEN_ENVIRONMENT + "(" 00513 + PRED_IS_EXECUTABLE_BY_ROBOT + " #$" + action + "))" ); 00514 cyc.assertGaf( assertion, kitchenEnvMt ); 00515 } 00516 00524 public void addTypicalObjectInKitchen( String object ) throws UnknownHostException, CycApiException, 00525 IOException { 00526 00527 CycList assertion; 00528 00529 assertion = cyc.makeCycList( "(#$ist " + MT_TUM_KITCHEN_ENVIRONMENT + "(#$isa #$" + object + " " 00530 + COLL_TYPICAL_OBJECT_IN_KITCHEN + "))" ); 00531 cyc.assertGaf( assertion, kitchenEnvMt ); 00532 } 00533 00540 private void makeCycAssertions() { 00541 00545 try { 00546 CycFort baseKB = cyc.getKnownConstantByName( "#$BaseKB" ); 00547 00548 System.out.println( "Creating " + MT_TUM_KITCHEN_ENVIRONMENT + " Microtheory..." ); 00549 cyc.makeCycConstant( MT_TUM_KITCHEN_ENVIRONMENT ); 00550 CycList assertion = cyc.makeCycList( "(#$ist #$BaseKB (#$isa " + MT_TUM_KITCHEN_ENVIRONMENT 00551 + " #$Microtheory))" ); 00552 cyc.assertGaf( assertion, baseKB ); 00553 00554 System.out.println( "Creating " + MT_TUM_KITCHEN_ENVIRONMENT_PLANNING + "..." ); 00555 cyc.makeCycConstant( MT_TUM_KITCHEN_ENVIRONMENT_PLANNING ); 00556 assertion = cyc.makeCycList( "(#$ist #$BaseKB (#$isa " + MT_TUM_KITCHEN_ENVIRONMENT_PLANNING 00557 + " #$Microtheory))" ); 00558 cyc.assertGaf( assertion, baseKB ); 00559 00560 kitchenEnvMt = cyc.getConstantByName( MT_TUM_KITCHEN_ENVIRONMENT ); 00561 kitchenPlanningMt = cyc.getConstantByName( MT_TUM_KITCHEN_ENVIRONMENT_PLANNING ); 00562 00563 CycConstant univoc = cyc.getConstantByName( "#$UniversalVocabularyMt" ); 00564 00565 // ****************************************************************************** 00566 // Create amountOfObject 00567 // ****************************************************************************** 00568 cyc.makeCycConstant( PRED_AMOUNT_OF_OBJECT ); 00569 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$isa " + PRED_AMOUNT_OF_OBJECT 00570 + " #$UnderspecifiedPredicate))" ); 00571 cyc.assertGaf( assertion, univoc ); 00572 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$arity " + PRED_AMOUNT_OF_OBJECT 00573 + " 2))" ); 00574 cyc.assertGaf( assertion, univoc ); 00575 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$argIsa " + PRED_AMOUNT_OF_OBJECT 00576 + " 1 #$Thing))" ); 00577 cyc.assertGaf( assertion, univoc ); 00578 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$argIsa " + PRED_AMOUNT_OF_OBJECT 00579 + " 2 #$UnitOfMeasureConcept))" ); 00580 cyc.assertGaf( assertion, univoc ); 00581 00582 // ****************************************************************************** 00583 // Create Piece 00584 // ****************************************************************************** 00585 cyc.makeCycConstant( "#$Piece" ); 00586 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$isa #$Piece #$UnitOfMeasureConcept))" ); 00587 cyc.assertGaf( assertion, univoc ); 00588 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$isa #$Piece #$UnitOfMeasure))" ); 00589 cyc.assertGaf( assertion, univoc ); 00590 assertion = cyc 00591 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$resultIsa #$Piece #$UnitOfMeasureConcept))" ); 00592 cyc.assertGaf( assertion, univoc ); 00593 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$argsIsa #$Piece #$NumericInterval))" ); 00594 cyc.assertGaf( assertion, univoc ); 00595 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$arityMax #$Piece 2))" ); 00596 cyc.assertGaf( assertion, univoc ); 00597 00598 // ****************************************************************************** 00599 // Create Drop-UnitOfVolume 00600 // ****************************************************************************** 00601 cyc.makeCycConstant( "#$Drop-UnitOfVolume" ); 00602 assertion = cyc 00603 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$isa #$Drop-UnitOfVolume #$UnitOfMeasureConcept))" ); 00604 cyc.assertGaf( assertion, univoc ); 00605 assertion = cyc 00606 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$isa #$Drop-UnitOfVolume #$UnitOfVolume))" ); 00607 cyc.assertGaf( assertion, univoc ); 00608 assertion = cyc 00609 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$resultIsa #$Drop-UnitOfVolume #$UnitOfVolume))" ); 00610 cyc.assertGaf( assertion, univoc ); 00611 assertion = cyc 00612 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$argsIsa #$Drop-UnitOfVolume #$NumericInterval))" ); 00613 cyc.assertGaf( assertion, univoc ); 00614 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$arityMax #$Drop-UnitOfVolume 2))" ); 00615 cyc.assertGaf( assertion, univoc ); 00616 00617 // ****************************************************************************** 00618 // Create Splash-UnitOfVolume 00619 // ****************************************************************************** 00620 cyc.makeCycConstant( "#$Splash-UnitOfVolume" ); 00621 assertion = cyc 00622 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$isa #$Splash-UnitOfVolume #$UnitOfMeasureConcept))" ); 00623 cyc.assertGaf( assertion, univoc ); 00624 assertion = cyc 00625 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$isa #$Splash-UnitOfVolume #$UnitOfVolume))" ); 00626 cyc.assertGaf( assertion, univoc ); 00627 assertion = cyc 00628 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$resultIsa #$Splash-UnitOfVolume #$UnitOfVolume))" ); 00629 cyc.assertGaf( assertion, univoc ); 00630 assertion = cyc 00631 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$argsIsa #$Splash-UnitOfVolume #$NumericInterval))" ); 00632 cyc.assertGaf( assertion, univoc ); 00633 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$arityMax #$Splash-UnitOfVolume 2))" ); 00634 cyc.assertGaf( assertion, univoc ); 00635 00636 // ****************************************************************************** 00637 // Create Pinch-UnitOfVolume 00638 // ****************************************************************************** 00639 cyc.makeCycConstant( "#$Pinch-UnitOfVolume" ); 00640 assertion = cyc 00641 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$isa #$Pinch-UnitOfVolume #$UnitOfMeasureConcept))" ); 00642 cyc.assertGaf( assertion, univoc ); 00643 assertion = cyc 00644 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$isa #$Pinch-UnitOfVolume #$UnitOfVolume))" ); 00645 cyc.assertGaf( assertion, univoc ); 00646 assertion = cyc 00647 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$resultIsa #$Pinch-UnitOfVolume #$UnitOfVolume))" ); 00648 cyc.assertGaf( assertion, univoc ); 00649 assertion = cyc 00650 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$argsIsa #$Pinch-UnitOfVolume #$NumericInterval))" ); 00651 cyc.assertGaf( assertion, univoc ); 00652 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$arityMax #$Pinch-UnitOfVolume 2))" ); 00653 cyc.assertGaf( assertion, univoc ); 00654 00655 // ****************************************************************************** 00656 // Create TeaBag 00657 // ****************************************************************************** 00658 cyc.makeCycConstant( "#$TeaBag" ); 00659 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$isa #$TeaBag #$ExistingObjectType))" ); 00660 cyc.assertGaf( assertion, univoc ); 00661 assertion = cyc 00662 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$isa #$TeaBag #$ArtifactTypeByGenericCategory))" ); 00663 cyc.assertGaf( assertion, univoc ); 00664 assertion = cyc.makeCycList( "(#$ist #$UniversalVocabularyMt (#$genls #$TeaBag #$ContainerArtifact))" ); 00665 cyc.assertGaf( assertion, univoc ); 00666 00667 // ****************************************************************************** 00668 // Create BoilingPoint 00669 // ****************************************************************************** 00670 00671 assertion = cyc 00672 .makeCycList( "(#$ist #$UniversalVocabularyMt (#$genls #$KeepingInACertainState #$Event))" ); 00673 cyc.assertGaf( assertion, univoc ); 00674 00675 // ****************************************************************************** 00676 // Create donot 00677 // ****************************************************************************** 00678 cyc.makeCycConstant( "#$notDo" ); 00679 assertion = cyc.makeCycList( "(#$isa #$notDo #$UnaryPredicate)" ); 00680 cyc.assertGaf( assertion, kitchenPlanningMt ); 00681 assertion = cyc.makeCycList( "(#$arg1Isa #$notDo #$SimpleActionPredicate)" ); 00682 cyc.assertGaf( assertion, kitchenPlanningMt ); 00683 00684 cyc.makeCycConstant( "#$orDo" ); 00685 assertion = cyc.makeCycList( "(#$isa #$orDo #$VariableArityPredicate)" ); 00686 cyc.assertGaf( assertion, kitchenPlanningMt ); 00687 assertion = cyc.makeCycList( "(#$argsIsa #$orDo #$SimpleActionPredicate)" ); 00688 cyc.assertGaf( assertion, kitchenPlanningMt ); 00689 00690 universalVocMt = cyc.getConstantByName( MT_UNIVERSAL_VOCABULARY ); 00691 00692 } 00693 catch ( UnknownHostException e ) { 00694 e.printStackTrace(); 00695 } 00696 catch ( CycApiException e ) { 00697 e.printStackTrace(); 00698 } 00699 catch ( IOException e ) { 00700 e.printStackTrace(); 00701 } 00702 00703 } 00704 00705 }