package aminePlatform.test.util; /******************************************************************* Amine - Artificial Intelligence platform for the development of Intelligent Systems. Copyright (C) 2004 AmineGroup. GNU Lesser General Public License This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, version 2.1 of the License. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *********************************************************************/ import aminePlatform.kernel.lexicons.*; import aminePlatform.kernel.ontology.*; import aminePlatform.util.*; import aminePlatform.util.cg.*; import aminePlatform.guis.util.AmineFrame; import javax.swing.*; import java.util.*; import java.awt.event.*; /** *
Title : test.util.BindingContextTest Class
*Description : Test Binding Context, binding information, and treatment * of matching-based operations with variables (especially unification). * @author Adil KABBAJ * @version 2 */ public class BindingContextTest extends JDialog { public BindingContextTest() { this.setTitle("BindingContextTest"); StringBuffer output = new StringBuffer(""); JScrollPane scrol; JTextArea text; try { output.append("\n*********** Amine Structures (AmineList, AmineSet, Term) unification ************"); Term term1 = (Term) Term.parse("pere(Ahmad, Karim)"); output.append("\nA simple term term1 : " + term1); Term term2 = (Term) Term.parse("pere(x, Karim)"); output.append("\nA simple term term2 : " + term2); SimpleBindingContext bindContext = new SimpleBindingContext(); Boolean bindInf1 = new Boolean(true); Boolean bindInf2 = new Boolean(false); boolean unify = term1.unify(bindContext, bindInf1, term2, bindInf2); if (unify) bindContext.printResultSet(bindContext.getResultSet(bindInf2), output); output.append("\n*****************************************"); term1 = (Term) Term.parse("pere(Ahmad, x1, address(15, road_casa, rabat))"); output.append("\nA simple term term1 : \n" + term1); term2 = (Term) Term.parse("pere(y1, Karim, y2)"); output.append("\nA simple term term2 : " + term2); bindContext.clear(); // use the same binding context, but clear previous bindings unify = term1.unify(bindContext, bindInf1, term2, bindInf2); if (unify) { bindContext.printResultSet(bindContext.getResultSet(bindInf1), output); bindContext.printResultSet(bindContext.getResultSet(bindInf2), output); } output.append("\n*****************************************"); term1 = (Term) Term.parse("pere(Ahmad, x1, address(15, road_casa, rabat))"); output.append("\nA simple term term1 : \n" + term1); term2 = (Term) Term.parse("pere(y1, Karim, y1)"); output.append("\nA simple term term2 : " + term2); bindContext.clear(); // use the same binding context, but clear previous bindings unify = term1.unify(bindContext, bindInf1, term2, bindInf2); if (unify) { bindContext.printResultSet(bindContext.getResultSet(bindInf1), output); bindContext.printResultSet(bindContext.getResultSet(bindInf2), output); } else output.append("\nThe two terms can not be unified"); output.append("\n*****************************************"); term1 = (Term) Term.parse("term(Ahmad, [1, candy, hello|x], address({15, road_casa, rabat}))"); output.append("\nA simple term term1 : \n" + term1); term2 = (Term) Term.parse("term(y1, [1, candy, hello, 45, sos], address(y2))"); output.append("\nA simple term term2 : \n" + term2); bindContext.clear(); // use the same binding context, but clear previous bindings unify = term1.unify(bindContext, bindInf1, term2, bindInf2); if (unify) { bindContext.printResultSet(bindContext.getResultSet(bindInf1), output); bindContext.printResultSet(bindContext.getResultSet(bindInf2), output); } output.append("\n*****************************************"); term1 = (Term) Term.parse("term(Ahmad, [1, candy, hello|x], address({15, road_casa, rabat}))"); output.append("\nA simple term term1 : \n" + term1); term2 = (Term) Term.parse("term(y1, [1, y2, hello, 45, sos|y3], address({road_casa, 15, rabat, maroc}))"); output.append("\nA simple term term2 : \n" + term2); bindContext.clear(); // use the same binding context, but clear previous bindings unify = term1.unify(bindContext, bindInf1, term2, bindInf2); if (unify) { bindContext.printResultSet(bindContext.getResultSet(bindInf1), output); bindContext.printResultSet(bindContext.getResultSet(bindInf2), output); } output.append("\n*****************************************"); output.append("\n*********** Concept unification ************"); // Load the Lexicons/Ontology that is stored in the specified file. Ontology ontology = Ontology.load(AmineFrame.ontologyDirPath + "ManOntology.ont"); Lexicon mainLexicon = ontology.getMainLexicon(); CG.setCanonicity(false); term1 = (Term) Term.parse( "term([Man :x1 =descr(45, teacher)], [1, candy, hello|x2])", mainLexicon); output.append("\nA simple term term1 :\n" + term1.toString(mainLexicon)); term2 = (Term) Term.parse( "term([Person :imad = y1], [1, y2, hello, 45, sos|y3])", mainLexicon); output.append("\nA simple term term2 : \n" + term2.toString(mainLexicon)); output.append("\n"); bindContext.clear(); // use the same binding context, but clear previous bindings unify = term1.unify(bindContext, bindInf1, term2, bindInf2); if (unify) { bindContext.printResultSet(bindContext.getResultSet(bindInf1), mainLexicon, output); bindContext.printResultSet(bindContext.getResultSet(bindInf2), mainLexicon, output); } else output.append("\nThe two terms can not be unified"); output.append("\n*****************************************"); AmineList list1 = AmineList.parse("[[Man:x], [Man #0], [Woman:Sue], [Woman:Sue]]", mainLexicon); output.append("\nA list of concepts (list1) : \n" + list1.toString(mainLexicon)); AmineList list2 = AmineList.parse("[[Man:imad], [Man:imad], [Woman:y], [Woman #0]]", mainLexicon); output.append("\nA list of concepts (list2) : \n" + list2.toString(mainLexicon)); bindContext.clear(); // use the same binding context, but clear previous bindings unify = list1.unify(bindContext, bindInf1, list2, bindInf2); if (unify) { bindContext.printResultSet(bindContext.getResultSet(bindInf1), mainLexicon, output); bindContext.printResultSet(bindContext.getResultSet(bindInf2), mainLexicon, output); } else output.append("\nThe two lists can not be unified"); output.append("\n*****************************************"); Concept conc1 = Concept.parse("[Woman:Sue]", mainLexicon); output.append("\nA concept conc1 : " + conc1.toString(mainLexicon)); Concept conc2 = Concept.parse("[Woman :Mary]", mainLexicon); output.append("\nA concept conc2 : " + conc2.toString(mainLexicon)); bindContext.clear(); // use the same binding context, but clear previous bindings unify = conc1.unify(bindContext, bindInf1, conc2, bindInf2); if (unify) { bindContext.printResultSet(bindContext.getResultSet(bindInf1), mainLexicon, output); bindContext.printResultSet(bindContext.getResultSet(bindInf2), mainLexicon, output); } else output.append("\nThe two concepts can not be unified"); output.append("\n*****************************************"); output.append("\n****************** Relation unification ********************"); Relation rel1 = Relation.parse("(agnt [Work] [Man :karim])", mainLexicon); output.append("\nRelation rel1 : " + rel1.toString(mainLexicon)); Relation rel2 = Relation.parse("[y4 :y5]<-agnt-[Action]", mainLexicon); output.append("\nRelation rel2 : " + rel2.toString(mainLexicon)); bindContext.clear(); // use the same binding context, but clear previous bindings unify = rel1.unify(bindContext, bindInf1, rel2, bindInf2); if (unify) { bindContext.printResultSet(bindContext.getResultSet(bindInf1), mainLexicon, output); bindContext.printResultSet(bindContext.getResultSet(bindInf2), mainLexicon, output); } else output.append("\nThe two relations can not be unified"); output.append("\n*****************************************"); output.append("\n*********** CG unification ************"); CG cg1 = CG.parse("[Drive]-\n" + "-obj->[Car],\n" + "-agnt->[Person: x]", mainLexicon); output.append("\nA simple CG cg1 : \n" + cg1.toLF(mainLexicon)); CG cg2 = CG.parseLF("[Drive]-\n" + "-obj->[Vehicle],\n" + "-agnt->[Man : imad]-ageOf->[Age = 25],\n" + "-manr->[Fast]", mainLexicon); output.append("\nA simple CG cg2 : \n" + cg2.toLF(mainLexicon)); bindContext.clear(); // use the same binding context, but clear previous bindings unify = cg1.unify(bindContext, bindInf1, cg2, bindInf2); if (unify) { bindContext.printResultSet(bindContext.getResultSet(bindInf1), mainLexicon, output); bindContext.printResultSet(bindContext.getResultSet(bindInf2), mainLexicon, output); } else output.append("\nThe two CGs can not be unified"); output.append("\n*****************************************"); cg1 = CG.parse("[Drive]-\n" + "-obj->[Car],\n" + "-agnt->[Man: imad]-ageOf->[Age = 25]", mainLexicon); output.append("\nA simple CG cg1 : \n" + cg1.toLF(mainLexicon)); cg2 = CG.parseLF("[Drive]-\n" + "-obj->[Car],\n" + "-agnt->[Person : x],\n" + "-manr->[Fast]", mainLexicon); output.append("\nA simple CG cg2 : \n" + cg2.toLF(mainLexicon)); bindContext.clear(); // use the same binding context, but clear previous bindings unify = cg1.unify(bindContext, bindInf1, cg2, bindInf2); if (unify) { bindContext.printResultSet(bindContext.getResultSet(bindInf1), mainLexicon, output); bindContext.printResultSet(bindContext.getResultSet(bindInf2), mainLexicon, output); } else output.append("\nThe two CGs can not be unified"); output.append("\n*****************************************"); cg1 = CG.parse("[Drive]-\n" + "-obj->[Car],\n" + "-agnt->[Person: x]", mainLexicon); output.append("\nA simple CG cg1 : \n" + cg1.toLF(mainLexicon)); cg2 = CG.parseLF("[Drive]-\n" + "-obj->[Vehicle],\n" + "-agnt->[Man : imad]-ageOf->[Age = 25]", mainLexicon); output.append("\nA simple CG cg2 : \n" + cg2.toLF(mainLexicon)); bindContext.clear(); // use the same binding context, but clear previous bindings unify = cg1.unify(bindContext, bindInf1, cg2, bindInf2); if (unify) { bindContext.printResultSet(bindContext.getResultSet(bindInf1), mainLexicon, output); bindContext.printResultSet(bindContext.getResultSet(bindInf2), mainLexicon, output); } else output.append("\nThe two CGs can not be unified"); CG cg3 = CG.parseLF("[Action]-\n" + "-obj->[Car],\n" + "-agnt->[Person]-ageOf->[Age = 25],\n" + "-manr->[Fast]", mainLexicon); output.append("\nA simple CG cg3 : \n" + cg3.toLF(mainLexicon)); CG cgResult = (CG) cg1.maximalJoin(bindContext, bindInf1, cg3, bindInf2); output.append( "\nA result of maximal join with binding cg3 : \n" + cgResult.toLF(mainLexicon)); output.append("\n*****************************************"); text = new JTextArea(22, 30); text.setText(output.toString()); scrol = new JScrollPane(text); this.getContentPane().add(scrol); this.setLocation(100, 100); this.setSize(400, 450); this.show(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } public static void main(String[] args) { BindingContextTest bindingContextTest = new BindingContextTest(); bindingContextTest.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(-1); } }); } }