00001 package instruction.converter;
00002
00003 import instruction.opencyc.OpenCyc;
00004 import instruction.semanticObjects.Action;
00005 import instruction.semanticObjects.Instruction;
00006 import instruction.semanticObjects.ObjectX;
00007 import instruction.semanticObjects.Preposition;
00008 import instruction.semanticObjects.Quantifier;
00009 import instruction.semanticObjects.Word;
00010 import instruction.wordnet.PrepositionalMap;
00011 import java.io.IOException;
00012 import java.net.UnknownHostException;
00013 import java.util.ArrayList;
00014 import java.util.HashMap;
00015 import java.util.List;
00016 import java.util.Map;
00017 import org.opencyc.api.CycApiException;
00018
00019 public class Instruction2CycLConverter {
00020
00022 List<String> assertions = new ArrayList<String>();
00023 String planName = "";
00024
00029 private HashMap<String, String> objectInstances = new HashMap<String, String>();
00030
00045 public String makeInstanceOfObject( String concept ) throws CycApiException, UnknownHostException,
00046 IOException {
00047
00048 if ( concept == null || concept.isEmpty() )
00049 return null;
00050
00051 if ( objectInstances.get( concept ) != null )
00052 return objectInstances.get( concept );
00053
00054
00055 int counter = 1;
00056 String instName;
00057 do {
00058 instName = concept.toLowerCase() + counter++;
00059 }
00060 while ( OpenCyc.getInstance().constantExists( instName ) );
00061
00062
00063 String assertion = "(#$isa #$" + instName + " #$" + concept + ")";
00064
00065
00066 OpenCyc.getInstance().getCycAcces().makeCycConstant( instName );
00067
00068
00069 OpenCyc.getInstance().makeAssertionInKitchenMt( assertion );
00070 assertions.add( assertion );
00071
00072 objectInstances.put( concept, instName );
00073
00074 return instName;
00075 }
00076
00090 public String makeInstanceOfAction( String concept, String nameProposal ) throws CycApiException,
00091 UnknownHostException, IOException {
00092
00093 if ( concept == null || concept.isEmpty() )
00094 return null;
00095
00096
00097 int counter = 1;
00098 String instName;
00099 do {
00100 instName = ( nameProposal == null ? concept.toLowerCase() : nameProposal ) + counter++;
00101 }
00102 while ( OpenCyc.getInstance().constantExists( instName ) );
00103
00104
00105 String assertion = "(#$isa #$" + instName + " #$" + concept + ")";
00106
00107
00108 OpenCyc.getInstance().getCycAcces().makeCycConstant( instName );
00109
00110
00111 OpenCyc.getInstance().makeAssertionInKitchenMt( assertion );
00112 assertions.add( assertion );
00113
00114 return instName;
00115 }
00116
00127 public String convertInstruction( Instruction inst ) throws CycApiException, UnknownHostException,
00128 IOException {
00129
00130 try {
00131
00132
00133
00134 Word action = inst.getAction().getAction();
00135 String actionInst = makeInstanceOfAction( action.getCycConcepts().get( 0 ), null );
00136
00137
00138
00139
00140 List<ObjectX> objects = inst.getObjects();
00141 List<String> objectInstances = convertObjects( objects, actionInst );
00142
00143 for ( int i = 0; i < objectInstances.size(); i++ ) {
00144
00145
00146 String assertion = "(#$objectActedOn #$" + actionInst + " #$" + objectInstances.get( i ) + ")";
00147 OpenCyc.getInstance().makeAssertionInKitchenMt( assertion );
00148 assertions.add( assertion );
00149 }
00150
00151
00152
00153
00154 for ( int i = 0; i < inst.getPrepositions().size(); i++ ) {
00155 Preposition pp = inst.getPrepositions().get( i );
00156 convertPP( pp, actionInst, objectInstances );
00157 }
00158
00159
00160
00161
00162 if ( inst.getTimeConstraint() != null ) {
00163 convertTimeConstraint( inst.getTimeConstraint(), actionInst );
00164 }
00165
00166
00167
00168
00169 String assertion = "(#$isa #$" + actionInst + " #$SimpleActionPredicate)";
00170 OpenCyc.getInstance().makeAssertionInKitchenPlanningMt( assertion );
00171 assertions.add( assertion );
00172
00173 return actionInst;
00174 }
00175 catch ( Exception e ) {
00176 e.printStackTrace();
00177 return null;
00178 }
00179 }
00180
00186 public void convertTimeConstraint( Quantifier tc, String actionInst ) {
00187
00188
00189
00190
00191
00192 List<String> range = new ArrayList<String>();
00193
00194 for ( int i = 0; i < tc.getAlternatives().size(); i++ ) {
00195 Word w = tc.getAlternatives().get( i );
00196
00197
00198 if ( w.getLabel().split( "\\\\/" ).length > 1 ) {
00199 String[] tokens = w.getLabel().split( "\\\\/" );
00200 range.add( "(#$QuotientFn " + tokens[0] + " " + tokens[1] + ")" );
00201 continue;
00202 }
00203
00204
00205 if ( w.getCycConcepts().size() > 0 ) {
00206 range.add( "#$" + w.getCycConcepts().get( 0 ) );
00207 continue;
00208 }
00209
00210
00211 try {
00212 Double.valueOf( w.getLabel() );
00213 range.add( w.getLabel() );
00214 }
00215 catch ( Exception e ) {
00216 e.printStackTrace();
00217 }
00218 }
00219
00220 if ( range.isEmpty() )
00221 return;
00222
00223 String measure;
00224
00225 if ( tc.getMeasure().getCycConcepts().size() > 0 )
00226 measure = tc.getMeasure().getCycConcepts().get( 0 );
00227 else
00228 return;
00229
00230 String assertion = "(#$timeSpan #$" + actionInst + " (#$" + measure + " " + range.get( 0 )
00231 + ( range.size() > 1 ? ( " " + range.get( range.size() - 1 ) ) : "" ) + "))";
00232
00233 try {
00234 OpenCyc.getInstance().makeAssertionInKitchenMt( assertion );
00235 assertions.add( assertion );
00236 }
00237 catch ( Exception e ) {
00238 e.printStackTrace();
00239 }
00240 }
00241
00253 public void convertHowTo( List<Instruction> instructionList, Instruction howto ) throws CycApiException,
00254 UnknownHostException, IOException {
00255
00256 resetConverter();
00257
00258 howto = new Instruction();
00259 Word set = new Word(Word.TYPE_VERB_INFINITIVE, "make");
00260 ArrayList<String> list = new ArrayList<String>();
00261 list.add("MakingSomething");
00262 set.setCycConcepts(list);
00263
00264 Word table = new Word(Word.TYPE_NOUN, "pancakes");
00265 ArrayList<String> list2 = new ArrayList<String>();
00266 list2.add("Pancake");
00267 table.setCycConcepts(list2);
00268
00269 howto.setAction(new Action(set));
00270 howto.addObject(new ObjectX(table));
00271
00272
00273
00274
00275 String howtoInst = makeInstanceOfAction( howto.getAction().getAction().getCycConcepts().get( 0 ), howto
00276 .getAction().getAction().getLabel()
00277 + "_" + howto.getObjects().get( 0 ).getName().get( 0 ).getLabel().replaceAll( " ", "_" ) );
00278
00279 String howtoObjInst = convertObjects( howto.getObjects(), howtoInst ).get( 0 );
00280
00281 String assertion = "(#$objectActedOn #$" + howtoInst + " #$" + howtoObjInst + ")";
00282 OpenCyc.getInstance().makeAssertionInKitchenMt( assertion );
00283 assertions.add( assertion );
00284
00285
00286
00287
00288 Map<String, Instruction> actionInstancesMap = new HashMap<String, Instruction>();
00289 ArrayList<String> actionInstances = new ArrayList<String>();
00290 for ( int i = 0; i < instructionList.size(); i++ ) {
00291 try {
00292 String actIn = convertInstruction( instructionList.get( i ) );
00293 if ( actIn != null ) {
00294 actionInstancesMap.put( actIn, instructionList.get( i ) );
00295 actionInstances.add( actIn );
00296 }
00297 }
00298 catch ( Exception e ) {
00299 e.printStackTrace();
00300 }
00301 }
00302
00303
00304
00305
00306 String actionSequence = "";
00307 boolean optionalSequence = false;
00308 for ( int i = 0; i < actionInstances.size(); i++ ) {
00309 if ( actionInstancesMap.get( actionInstances.get( i ) ).getAction().dontDoIt() )
00310 actionSequence += "(#$notDo #$" + actionInstances.get( i ) + ")";
00311 else if ( actionInstancesMap.get( actionInstances.get( i ) ).isOptional() && ! optionalSequence ) {
00312 actionSequence += "(#$orDo #$" + actionInstances.get( i );
00313 optionalSequence = true;
00314 }
00315 else {
00316 if ( optionalSequence && ! actionInstancesMap.get( actionInstances.get( i ) ).isOptional() ) {
00317 actionSequence += ") ";
00318 optionalSequence = false;
00319 }
00320 actionSequence += "#$" + actionInstances.get( i );
00321 }
00322
00323 if ( i < actionInstances.size() - 1 )
00324 actionSequence += " ";
00325 }
00326
00327 assertion = "(#$isa #$" + howtoInst + " #$ActionPredicate" + ")";
00328 OpenCyc.getInstance().makeAssertionInKitchenPlanningMt( assertion );
00329 assertions.add( assertion );
00330
00331 assertion = "(#$arity #$" + howtoInst + " 1)";
00332 OpenCyc.getInstance().makeAssertionInKitchenPlanningMt( assertion );
00333 assertions.add( assertion );
00334
00335 assertion = "(#$arg1Isa #$" + howtoInst + " #$"
00336 + howto.getObjects().get( 0 ).getName().get( 0 ).getCycConcepts().get( 0 ) + ")";
00337 OpenCyc.getInstance().makeAssertionInKitchenPlanningMt( assertion );
00338 assertions.add( assertion );
00339
00340 assertion = "(#$isa #$" + howtoObjInst + " #$"
00341 + howto.getObjects().get( 0 ).getName().get( 0 ).getCycConcepts().get( 0 ) + ")";
00342 OpenCyc.getInstance().makeAssertionInKitchenPlanningMt( assertion );
00343 assertions.add( assertion );
00344
00345 assertion = "(#$methodForAction (#$" + howtoInst + " #$" + howtoObjInst
00346 + ") (#$actionSequence (#$TheList " + actionSequence + ")))";
00347 OpenCyc.getInstance().makeAssertionInKitchenPlanningMt( assertion );
00348 planName = "(#$" + howtoInst + " #$" + howtoObjInst + ")";
00349 assertions.add( assertion );
00350 }
00351
00363 public void convertPP( Preposition pp, String actionInstance, List<String> parentObjects )
00364 throws CycApiException, UnknownHostException, IOException {
00365
00366 List<String> objectInstances = null;
00367
00368
00369
00370
00371 objectInstances = convertObjects( pp.getObjects(), actionInstance );
00372
00373
00374
00375
00376 for ( int i = 0; i < objectInstances.size(); i++ ) {
00377 String child = objectInstances.get( i );
00378
00379 for ( int k = 0; k < parentObjects.size(); k++ ) {
00380 String parent = parentObjects.get( k );
00381
00382 for ( int j = 0; j < pp.getPrepositions().size(); j++ )
00383 connectObjectWithPreposition( child, parent, pp.getPrepositions().get( j ), actionInstance );
00384
00385 }
00386
00387
00388
00389
00390 if ( parentObjects.size() == 0 ) {
00391 for ( int j = 0; j < pp.getPrepositions().size(); j++ )
00392 connectObjectWithPreposition( child, actionInstance, pp.getPrepositions().get( j ), actionInstance );
00393 }
00394 }
00395 }
00396
00410 public void connectObjectWithPreposition( String child, String parent, Word pp, String action ) {
00411
00412 if ( pp.getType() != Word.TYPE_PREPOSITION )
00413 return;
00414
00415 String p = PrepositionalMap.get( pp.getLabel() );
00416
00417 if ( p == null )
00418 return;
00419
00420
00421
00422
00423 String assertion;
00424
00425 if ( p.equals( PrepositionalMap.ON ) ) {
00426 assertion = "(#$" + PrepositionalMap.ON + " #$" + parent + " #$" + child + ")";
00427 }
00428 else if ( p.equals( PrepositionalMap.AT ) ) {
00429 assertion = "(#$" + PrepositionalMap.AT + " #$" + parent + " #$" + child + ")";
00430 }
00431 else if ( p.equals( PrepositionalMap.FROM ) ) {
00432 assertion = "(#$" + PrepositionalMap.FROM + " #$" + parent + " #$" + child + ")";
00433 }
00434 else if ( p.equals( PrepositionalMap.IN ) ) {
00435 assertion = "(#$" + PrepositionalMap.IN + " #$" + parent + " #$" + child + ")";
00436 }
00437 else if ( p.equals( PrepositionalMap.IN_FRONT_OF ) ) {
00438 assertion = "(#$" + PrepositionalMap.IN_FRONT_OF + " #$" + parent + " " + child + ")";
00439 }
00440 else if ( p.equals( PrepositionalMap.NEXT_TO ) ) {
00441 assertion = "(#$" + PrepositionalMap.NEXT_TO + " #$" + parent + " #$" + child + " #$SomethingExisting)";
00442 }
00443 else if ( p.equals( PrepositionalMap.OF ) ) {
00444 assertion = "(#$" + PrepositionalMap.OF + " #$" + child + " #$" + parent + ")";
00445 }
00446 else if ( p.equals( PrepositionalMap.TO ) ) {
00447 assertion = "(#$" + PrepositionalMap.TO + " #$" + parent + " #$" + child + ")";
00448 }
00449 else if ( p.equals( PrepositionalMap.FOR ) ) {
00450 assertion = "(#$" + PrepositionalMap.FOR + " #$" + parent + " #$" + child + ")";
00451 }
00452 else if ( p.equals( PrepositionalMap.THROUGH ) ) {
00453 assertion = "(#$" + PrepositionalMap.THROUGH + " #$" + parent + " #$" + child + ")";
00454 }
00455 else if ( p.equals( PrepositionalMap.AMONG ) ) {
00456 assertion = "(#$" + PrepositionalMap.AMONG + " #$" + parent + " #$" + child + ")";
00457 }
00458 else if ( p.equals( PrepositionalMap.INSIDE ) ) {
00459 assertion = "(#$" + PrepositionalMap.INSIDE + " #$" + parent + " #$" + child + ")";
00460 }
00461 else if ( p.equals( PrepositionalMap.ABOUT ) ) {
00462 assertion = "(#$" + PrepositionalMap.ABOUT + " #$" + parent + " #$" + child + ")";
00463 }
00464 else if ( p.equals( PrepositionalMap.WITHOUT ) ) {
00465 assertion = "(#$" + PrepositionalMap.WITHOUT + " #$" + parent + " #$" + child + ")";
00466 }
00467 else if ( p.equals( PrepositionalMap.ALONG ) ) {
00468 assertion = "(#$" + PrepositionalMap.ALONG + " #$" + parent + " #$" + child + ")";
00469 }
00470 else if ( p.equals( PrepositionalMap.AROUND ) ) {
00471 assertion = "(#$" + PrepositionalMap.AROUND + " #$" + parent + " #$" + child + ")";
00472 }
00473 else if ( p.equals( PrepositionalMap.BY ) ) {
00474 assertion = "(#$" + PrepositionalMap.BY + " #$" + parent + " #$" + child + ")";
00475 }
00476 else if ( p.equals( PrepositionalMap.UNDER ) ) {
00477 assertion = "(#$" + PrepositionalMap.UNDER + " #$" + parent + " #$" + child + ")";
00478 }
00479 else if ( p.equals( PrepositionalMap.OVER ) ) {
00480 assertion = "(#$" + PrepositionalMap.OVER + " #$" + parent + " #$" + child + ")";
00481 }
00482 else if ( p.equals( PrepositionalMap.WITH ) ) {
00483 assertion = "(#$" + PrepositionalMap.WITH + " #$" + parent + " #$" + child + ")";
00484 }
00485 else if ( p.equals( PrepositionalMap.INTO ) ) {
00486 assertion = "(#$" + PrepositionalMap.INTO + " #$" + parent + " #$" + child + ")";
00487 }
00488 else if ( p.equals( PrepositionalMap.ACROSS ) ) {
00489 assertion = "(#$" + PrepositionalMap.ACROSS + " #$" + parent + " #$" + child + ")";
00490 }
00491 else if ( p.equals( PrepositionalMap.FOR ) ) {
00492 assertion = "(#$" + PrepositionalMap.FOR + " #$" + parent + " #$" + child + ")";
00493 }
00494 else if ( p.equals( PrepositionalMap.AFTER ) ) {
00495 assertion = "(#$" + PrepositionalMap.AFTER + " #$" + parent + " #$" + child + ")";
00496 }
00497 else if ( p.equals( PrepositionalMap.BEFORE ) ) {
00498 assertion = "(#$" + PrepositionalMap.BEFORE + " #$" + parent + " #$" + child + ")";
00499 }
00500 else if ( p.equals( PrepositionalMap.AGAINST ) ) {
00501 assertion = "(#$" + PrepositionalMap.AGAINST + " #$" + parent + " #$" + child + ")";
00502 }
00503 else
00504 return;
00505
00506 if ( action != null )
00507 assertion = "(#$purposeOf-Generic #$" + action + " " + assertion + ")";
00508
00509
00510
00511
00512 try {
00513 OpenCyc.getInstance().makeAssertionInKitchenMt( assertion );
00514 assertions.add( assertion );
00515 }
00516 catch ( Exception e ) {
00517 e.printStackTrace();
00518 }
00519 }
00520
00531 public List<String> convertObjects( List<ObjectX> objects, String actionInstance ) throws CycApiException, UnknownHostException,
00532 IOException {
00533
00534 List<String> objectInstances = new ArrayList<String>();
00535
00536
00537
00538
00539 for ( int i = 0; i < objects.size(); i++ ) {
00540 if ( objects.get( i ).getName().size() == 0 )
00541 continue;
00542 String instance = makeInstanceOfObject( objects.get( i ).getName().get( 0 ).getCycConcepts().get( 0 ) );
00543 if ( instance != null && ! instance.isEmpty() ) {
00544 objectInstances.add( instance );
00545
00546
00547
00548
00549 convertAdjectives( objects.get( i ).getAdjectives(), instance, actionInstance );
00550
00551
00552
00553
00554 convertQuantifier( objects.get( i ).getQuantifier(), instance );
00555 }
00556 }
00557
00558
00559
00560
00561 for ( int i = 0; i < objects.size(); i++ ) {
00562 ObjectX o = objects.get( i );
00563
00564 for ( int j = 0; j < o.getPrepositions().size(); j++ )
00565 convertNestedPP( o.getPrepositions().get( j ), objectInstances, actionInstance );
00566 }
00567
00568 return objectInstances;
00569 }
00570
00578 public void convertAdjectives( List<Word> adjectives, String objectInstance, String actionInstance ) {
00579
00580 for ( int i = 0; i < adjectives.size(); i++ ) {
00581
00582 List<String> concepts = adjectives.get( i ).getCycConcepts();
00583 try {
00584 String assertion = null;
00585
00586
00587
00588
00589 if ( concepts.size() == 1 && OpenCyc.getInstance().genlsSomethingExisting( concepts.get( 0 ) ) ) {
00590 assertion = "(#$isa #$" + objectInstance + " #$" + concepts.get( 0 ) + ")";
00591 }
00592
00593
00594
00595
00596 else {
00597 String pred = null;
00598
00599
00600 for ( int j = 0; j < concepts.size(); j++ ) {
00601 if ( OpenCyc.getInstance().isaInUnivVocMt( concepts.get( j ), "Predicate" )
00602 && OpenCyc.getInstance().getArity( concepts.get( j ) ) == 2 ) {
00603 pred = concepts.get( j );
00604 }
00605 }
00606
00607 if ( pred != null ) {
00608
00609
00610 String typeOfArg2 = OpenCyc.getInstance().getTypeOfArg( pred, 2 );
00611 if ( typeOfArg2 != null ) {
00612
00613
00614 for ( int j = 0; j < concepts.size(); j++ ) {
00615 if ( ! OpenCyc.getInstance().isaInUnivVocMt( concepts.get( j ), typeOfArg2 )
00616 && ! OpenCyc.getInstance().genlsInUniversalVocabularyMt( concepts.get( j ), typeOfArg2 ) ) {
00617 concepts.remove( j );
00618 j--;
00619 continue;
00620 }
00621 }
00622
00623 assertion = "(#$" + pred + " #$" + objectInstance + " #$" + concepts.get( 0 ) + ")";
00624 }
00625 }
00626
00627 else {
00628
00629
00630
00631
00632
00633 String event = null;
00634 for ( int j = 0; j < concepts.size(); j++ ) {
00635 if ( OpenCyc.getInstance().isaInUnivVocMt( concepts.get( j ), "TemporalObjectType" )
00636 || OpenCyc.getInstance().isaInUnivVocMt( concepts.get( j ), "TemporalStuffType" )
00637 || OpenCyc.getInstance().isaInUnivVocMt( concepts.get( j ), "ProcessType" )) {
00638 event = concepts.get( j );
00639 }
00640 }
00641
00642 if ( event != null ) {
00643
00644 String precondAction = makeInstanceOfAction( event, null );
00645
00646 assertion = "(#$objectActedOn #$" + precondAction + " #$" + objectInstance + ")";
00647 OpenCyc.getInstance().makeAssertionInKitchenMt( assertion );
00648
00649 assertion = "(#$preconditionFor-Events #$" + precondAction + " #$" + actionInstance + ")";
00650 OpenCyc.getInstance().makeAssertionInKitchenMt( assertion );
00651 }
00652 }
00653 }
00654
00655 if ( assertion != null ) {
00656 OpenCyc.getInstance().makeAssertionInKitchenMt( assertion );
00657 assertions.add( assertion );
00658 }
00659
00660 }
00661 catch ( Exception e ) {
00662 e.printStackTrace();
00663 }
00664 }
00665 }
00666
00674 public void convertQuantifier( Quantifier q, String objectInstance ) {
00675
00676
00677
00678
00679
00680 List<String> range = new ArrayList<String>();
00681
00682 for ( int i = 0; i < q.getAlternatives().size(); i++ ) {
00683 Word w = q.getAlternatives().get( i );
00684
00685
00686 if ( w.getLabel().split( "\\\\/" ).length > 1 ) {
00687 String[] tokens = w.getLabel().split( "\\\\/" );
00688 range.add( "(#$QuotientFn " + tokens[0] + " " + tokens[1] + ")" );
00689 continue;
00690 }
00691
00692
00693 if ( w.getCycConcepts().size() > 0 ) {
00694 range.add( "#$" + w.getCycConcepts().get( 0 ) );
00695 continue;
00696 }
00697
00698
00699 try {
00700 Double.valueOf( w.getLabel() );
00701 range.add( w.getLabel() );
00702 }
00703 catch ( Exception e ) {
00704 e.printStackTrace();
00705 }
00706 }
00707
00708 if ( range.isEmpty() )
00709 return;
00710
00711 String measure;
00712
00713 if ( q.getMeasure().getCycConcepts().size() > 0 )
00714 measure = q.getMeasure().getCycConcepts().get( 0 );
00715 else
00716 measure = "Piece";
00717
00718 String assertion = "(#$amountOfObject #$" + objectInstance + " (#$" + measure + " " + range.get( 0 )
00719 + ( range.size() > 1 ? ( " " + range.get( range.size() - 1 ) ) : "" ) + "))";
00720
00721 try {
00722 OpenCyc.getInstance().makeAssertionInKitchenMt( assertion );
00723 assertions.add( assertion );
00724 }
00725 catch ( Exception e ) {
00726 e.printStackTrace();
00727 }
00728
00729 }
00730
00741 public void convertNestedPP( Preposition pp, List<String> parentObjects, String actionInstance ) throws CycApiException,
00742 UnknownHostException, IOException {
00743
00744 List<String> objectInstances = null;
00745
00746
00747
00748
00749 objectInstances = convertObjects( pp.getObjects(), actionInstance );
00750
00751
00752
00753
00754 for ( int i = 0; i < objectInstances.size(); i++ ) {
00755 String child = objectInstances.get( i );
00756
00757 for ( int k = 0; k < parentObjects.size(); k++ ) {
00758 String parent = parentObjects.get( k );
00759
00760 for ( int j = 0; j < pp.getPrepositions().size(); j++ )
00761 connectObjectWithPreposition( child, parent, pp.getPrepositions().get( j ), null );
00762
00763 }
00764 }
00765 }
00766
00770 public void resetConverter() {
00771
00772 objectInstances.clear();
00773 }
00774
00780 public List<String> getAssertions() {
00781
00782 return assertions;
00783 }
00784
00785 public String getPlanName() {
00786
00787 return planName;
00788 }
00789 }